Add tests for fseek problem
authorP. F. Chimento <philip.chimento@gmail.com>
Sat, 18 Jun 2011 19:19:02 +0000 (21:19 +0200)
committerP. F. Chimento <philip.chimento@gmail.com>
Sat, 18 Jun 2011 20:09:56 +0000 (22:09 +0200)
tests/fileio.c

index ba5d80b3df5adac9f812ecc4b3e7b5a145d0cf97..d96a73487ee137c1500ca1aa52754e66704cf194 100644 (file)
@@ -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");
 }