Removed the notion of a "large" context. For simplicity, all contexts
[panda.git] / tests / test-input.c
blob993a22e777a4826b0c919018bd1ebd13956e78a3
2 #include <st-input.h>
3 #include <glib.h>
5 #include <stdio.h>
7 static const char test_string_1[] = "abcdef\nghijklmno\npqrstuvwx\nz";
8 static const char test_string_2[] = "\nabcdef\n\nghijklmno\npqrstuvwxz\n";
9 static const char test_string_3[] = "\342\272\206\342\273\204\351\252\260\n\351\253\200";
10 static const char test_string_4[] = "\n";
13 static void
14 test_lookahead_consume (void)
16 st_input_t *stream;
18 char utf8char[6] = { 0, };
20 gunichar uchar;
21 int i = 0;
23 printf ("\nTEST - lookahead/consume\n");
25 // on test_data_1
27 stream = st_input_new (test_string_1);
29 printf ("\n\"%s\"\n", g_strescape (test_string_1, NULL));
31 while ((uchar = st_input_look_ahead (stream, 1)) != ST_INPUT_EOF) {
33 g_unichar_to_utf8 (uchar, utf8char);
35 if (uchar != 0x000a)
36 printf ("%s %i,%i\n", utf8char,
37 st_input_get_line (stream), st_input_get_column (stream));
39 else
40 printf ("%s %i,%i\n", "\\n",
41 st_input_get_line (stream), st_input_get_column (stream));
44 st_input_consume (stream);
47 // on test_data_2
48 st_input_destroy (stream);
49 stream = st_input_new (test_string_2);
52 printf ("\n\"%s\"\n", g_strescape (test_string_2, NULL));
54 while ((uchar = st_input_look_ahead (stream, 1)) != ST_INPUT_EOF) {
56 g_unichar_to_utf8 (uchar, utf8char);
58 if (uchar != 0x000a)
59 printf ("%s %i,%i\n", utf8char,
60 st_input_get_line (stream), st_input_get_column (stream));
62 else
63 printf ("%s %i,%i\n", "\\n",
64 st_input_get_line (stream), st_input_get_column (stream));
66 st_input_consume (stream);
70 // on test_data_3
71 st_input_destroy (stream);
72 stream = st_input_new (test_string_3);
75 // lookahead up to 4 chars this time, without consuming - line,col numbers not calculated though
76 printf ("\n\"%s\"\n", g_strescape (test_string_3, NULL));
78 i = 1;
80 while ((uchar = st_input_look_ahead (stream, i)) != ST_INPUT_EOF) {
82 g_unichar_to_utf8 (uchar, utf8char);
84 if (uchar != 0x000a)
85 printf ("%s\n", utf8char);
86 else
87 printf ("%s\n", "\\n");
89 i++;
93 // on test_data_4
94 st_input_destroy (stream);
95 stream = st_input_new (test_string_4);
98 printf ("\n\"%s\"\n", g_strescape (test_string_4, NULL));
100 while ((uchar = st_input_look_ahead (stream, 1)) != ST_INPUT_EOF) {
102 g_unichar_to_utf8 (uchar, utf8char);
104 if (uchar != 0x000a)
105 printf ("%s %i,%i\n", utf8char,
106 st_input_get_line (stream), st_input_get_column (stream));
108 else
109 printf ("%s %i,%i\n", "\\n",
110 st_input_get_line (stream), st_input_get_column (stream));
112 st_input_consume (stream);
119 static void
120 test_mark_rewind (void)
122 char buf[6] = { 0, };
124 st_input_t *stream = st_input_new (test_string_1);
126 printf ("\nTEST - mark/rewind\n");
128 gunichar uchar;
129 while ((uchar = st_input_look_ahead (stream, 1)) != ST_INPUT_EOF) {
131 st_input_consume (stream);
133 // add markers at each newline
134 if (uchar == 0x000a)
135 st_input_mark (stream);
139 st_input_rewind_to_marker (stream, 1);
141 uchar = st_input_look_ahead (stream, 1);
142 g_unichar_to_utf8 (uchar, buf);
143 printf ("%s %i,%i\n", buf,
144 st_input_get_line (stream), st_input_get_column (stream));
146 st_input_rewind_to_marker (stream, 2);
148 uchar = st_input_look_ahead (stream, 1);
149 g_unichar_to_utf8 (uchar, buf);
150 printf ("%s %i,%i\n", buf,
151 st_input_get_line (stream), st_input_get_column (stream));
156 static void
157 test_seek (void)
159 char buf[6] = { 0, };
160 gunichar uchar;
162 st_input_t *stream = st_input_new (test_string_2);
164 printf ("\nTEST - seek\n");
166 st_input_seek (stream, 6);
168 uchar = st_input_look_ahead (stream, 1);
169 g_unichar_to_utf8 (uchar, buf);
170 printf ("%s %i,%i\n", buf,
171 st_input_get_line (stream), st_input_get_column (stream));
174 st_input_seek (stream, 3);
176 uchar = st_input_look_ahead (stream, 1);
177 g_unichar_to_utf8 (uchar, buf);
178 printf ("%s %i,%i\n", buf,
179 st_input_get_line (stream), st_input_get_column (stream));
181 st_input_seek (stream, 9);
183 uchar = st_input_look_ahead (stream, 1);
184 g_unichar_to_utf8 (uchar, buf);
185 printf ("%s %i,%i\n", buf,
186 st_input_get_line (stream), st_input_get_column (stream));
190 static void
191 test_substring (void)
196 st_input_t *stream = st_input_new (test_string_1);
198 printf ("\nTEST - seek\n");
200 gunichar *uchars = st_input_substring (stream, 3, 27);
202 char *str = g_ucs4_to_utf8 (uchars, -1, NULL, NULL, NULL);
204 printf ("%s\n", str);
208 main (int argc, char *argv[])
211 test_lookahead_consume ();
213 test_mark_rewind ();
215 test_seek ();
217 test_substring ();
219 return 0;