C stdio trickery for read/write mode
[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 void
11 glk_main(void)
12 {
13         char buffer[BUFLEN + 1];
14         
15         /* Open a temporary file */
16         frefid_t ref = glk_fileref_create_temp(fileusage_Data | fileusage_BinaryMode, 0);
17         strid_t file = glk_stream_open_file_uni(ref, filemode_Write, 0);
18         
19         /* Write the string to the file */
20         glk_put_string_stream(file, MAGIC_STRING);
21         
22         /* Close and check result counts */
23         stream_result_t counts;
24         glk_stream_close(file, &counts);
25         g_assert_cmpint(counts.readcount, ==, 0);
26         g_assert_cmpint(counts.writecount, ==, 14);
27         
28         file = glk_stream_open_file_uni(ref, filemode_Read, 0);
29         glui32 readcount = glk_get_line_stream(file, buffer, BUFLEN);
30         g_printerr("String: %s\n", buffer);
31         g_assert_cmpint(readcount, ==, strlen(buffer));
32         
33         glk_stream_close(file, &counts);
34         glk_fileref_destroy(ref);
35 }