From: P. F. Chimento Date: Sat, 18 Jun 2011 19:19:02 +0000 (+0200) Subject: Add tests for fseek problem X-Git-Tag: v0.9~73^2~1^2~2 X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=1f1c29341f7da406b8cbbe72793eceefcb69d058;p=projects%2Fchimara%2Fchimara.git Add tests for fseek problem --- diff --git a/tests/fileio.c b/tests/fileio.c index ba5d80b..d96a734 100644 --- a/tests/fileio.c +++ b/tests/fileio.c @@ -7,11 +7,22 @@ #define MAGIC_STRING "Zapp\xF6licious.\n" #define BUFLEN 80 +static void +delete_if_exists(frefid_t ref) +{ + if(glk_fileref_does_file_exist(ref) == 1) { + g_print("(Deleting existing file) "); + glk_fileref_delete_file(ref); + } +} + void glk_main(void) { char buffer[BUFLEN + 1]; + g_print("Test getline... "); + /* Open a temporary file */ frefid_t ref = glk_fileref_create_temp(fileusage_Data | fileusage_BinaryMode, 0); strid_t file = glk_stream_open_file_uni(ref, filemode_Write, 0); @@ -27,9 +38,64 @@ glk_main(void) file = glk_stream_open_file_uni(ref, filemode_Read, 0); glui32 readcount = glk_get_line_stream(file, buffer, BUFLEN); - g_printerr("String: %s\n", buffer); + g_print("(String: %s) ", buffer); g_assert_cmpint(readcount, ==, strlen(buffer)); + g_print("PASS\n"); + glk_stream_close(file, &counts); glk_fileref_destroy(ref); + + /* testfile7 - append, seek, write, close, read. */ + g_print("Test append-seek-write-close-read... "); + + ref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, "testfile7", 0); + delete_if_exists(ref); + strid_t str = glk_stream_open_file(ref, filemode_WriteAppend, 0); + glk_put_string_stream(str, "Purple monkey chef.\n"); + glk_stream_set_position(str, 14, seekmode_Start); + g_assert_cmpuint(glk_stream_get_position(str), ==, 14); + glk_put_string_stream(str, "dishwasher.\n"); + glk_stream_close(str, &counts); + g_assert_cmpuint(counts.readcount, ==, 0); + g_assert_cmpuint(counts.writecount, ==, 32); + + str = glk_stream_open_file(ref, filemode_Read, 0); + readcount = glk_get_buffer_stream(str, buffer, BUFLEN); + buffer[readcount] = '\0'; + g_assert_cmpstr(buffer, ==, "Purple monkey dishwasher.\n"); + + glk_stream_close(str, &counts); + g_assert_cmpuint(counts.readcount, ==, 26); + g_assert_cmpuint(counts.writecount, ==, 0); + + g_print("PASS\n"); + + /* testfile10 - Write, close, read, write, close, read. */ + g_print("Test write-close-read-write-close-read... "); + + ref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, "testfile10", 0); + delete_if_exists(ref); + str = glk_stream_open_file(ref, filemode_ReadWrite, 0); + glk_put_string_stream(str, "Purple synchroscopes.\n"); + glk_stream_close(str, &counts); + g_assert_cmpuint(counts.readcount, ==, 0); + g_assert_cmpuint(counts.writecount, ==, 22); + + str = glk_stream_open_file(ref, filemode_ReadWrite, 0); + readcount = glk_get_buffer_stream(str, buffer, 7); + buffer[readcount] = '\0'; + g_assert_cmpstr(buffer, ==, "Purple "); + g_assert_cmpuint(glk_stream_get_position(str), ==, 7); + glk_put_string_stream(str, "monkey dishwasher.\n"); + glk_stream_set_position(str, 0, seekmode_Start); + g_assert_cmpuint(glk_stream_get_position(str), ==, 0); + readcount = glk_get_buffer_stream(str, buffer, BUFLEN); + buffer[readcount] = '\0'; + g_assert_cmpstr(buffer, ==, "Purple monkey dishwasher.\n"); + glk_stream_close(str, &counts); + g_assert_cmpuint(counts.readcount, ==, 33); + g_assert_cmpuint(counts.writecount, ==, 19); + + g_print("PASS\n"); }