git: Line endings of README.txt
[projects/chimara/chimara.git] / tests / fileio.c
1 /* Test for file I/O bug */
2
3 #include <libchimara/glk.h>
4 #include <glib.h>
5 #include <string.h>
6
7 #define MAGIC_STRING "Zapp\xF6licious.\n"
8 #define BUFLEN 80
9
10 static void
11 delete_if_exists(frefid_t ref)
12 {
13     if(glk_fileref_does_file_exist(ref) == 1) {
14         g_print("(Deleting existing file) ");
15                 glk_fileref_delete_file(ref);
16     }
17 }
18
19 void
20 glk_main(void)
21 {
22         char buffer[BUFLEN + 1];
23         
24         g_print("Test getline... ");
25
26         /* Open a temporary file */
27         frefid_t ref = glk_fileref_create_temp(fileusage_Data | fileusage_BinaryMode, 0);
28         strid_t file = glk_stream_open_file_uni(ref, filemode_Write, 0);
29         
30         /* Write the string to the file */
31         glk_put_string_stream(file, MAGIC_STRING);
32         
33         /* Close and check result counts */
34         stream_result_t counts;
35         glk_stream_close(file, &counts);
36         g_assert_cmpint(counts.readcount, ==, 0);
37         g_assert_cmpint(counts.writecount, ==, 14);
38         
39         file = glk_stream_open_file_uni(ref, filemode_Read, 0);
40         glui32 readcount = glk_get_line_stream(file, buffer, BUFLEN);
41         g_print("(String: %s) ", buffer);
42         g_assert_cmpint(readcount, ==, strlen(buffer));
43         
44         g_print("PASS\n");
45
46         glk_stream_close(file, &counts);
47         glk_fileref_destroy(ref);
48
49         /* testfile7 - append, seek, write, close, read. */
50         g_print("Test append-seek-write-close-read... ");
51
52         ref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, "testfile7", 0);
53         delete_if_exists(ref);
54         strid_t str = glk_stream_open_file(ref, filemode_WriteAppend, 0);
55         glk_put_string_stream(str, "Purple monkey chef.\n");
56         glk_stream_set_position(str, 14, seekmode_Start);
57         g_assert_cmpuint(glk_stream_get_position(str), ==, 14);
58         glk_put_string_stream(str, "dishwasher.\n");
59         glk_stream_close(str, &counts);
60         g_assert_cmpuint(counts.readcount, ==, 0);
61         g_assert_cmpuint(counts.writecount, ==, 32);
62
63         str = glk_stream_open_file(ref, filemode_Read, 0);
64         readcount = glk_get_buffer_stream(str, buffer, BUFLEN);
65         buffer[readcount] = '\0';
66         g_assert_cmpstr(buffer, ==, "Purple monkey dishwasher.\n");
67
68         glk_stream_close(str, &counts);
69         g_assert_cmpuint(counts.readcount, ==, 26);
70         g_assert_cmpuint(counts.writecount, ==, 0);
71
72         g_print("PASS\n");
73
74         /* testfile10 - Write, close, read, write, close, read. */
75         g_print("Test write-close-read-write-close-read... ");
76
77         ref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode, "testfile10", 0);
78         delete_if_exists(ref);
79         str = glk_stream_open_file(ref, filemode_ReadWrite, 0);
80         glk_put_string_stream(str, "Purple synchroscopes.\n");
81         glk_stream_close(str, &counts);
82         g_assert_cmpuint(counts.readcount, ==, 0);
83         g_assert_cmpuint(counts.writecount, ==, 22);
84
85         str = glk_stream_open_file(ref, filemode_ReadWrite, 0);
86         readcount = glk_get_buffer_stream(str, buffer, 7);
87         buffer[readcount] = '\0';
88         g_assert_cmpstr(buffer, ==, "Purple ");
89         g_assert_cmpuint(glk_stream_get_position(str), ==, 7);
90         glk_put_string_stream(str, "monkey dishwasher.\n");
91         glk_stream_set_position(str, 0, seekmode_Start);
92         g_assert_cmpuint(glk_stream_get_position(str), ==, 0);
93         readcount = glk_get_buffer_stream(str, buffer, BUFLEN);
94         buffer[readcount] = '\0';
95         g_assert_cmpstr(buffer, ==, "Purple monkey dishwasher.\n");
96         glk_stream_close(str, &counts);
97         g_assert_cmpuint(counts.readcount, ==, 33);
98         g_assert_cmpuint(counts.writecount, ==, 19);
99
100         g_print("PASS\n");
101 }