-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-oblig3.c
304 lines (246 loc) · 6.64 KB
/
test-oblig3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#define FALSE 0
#define TRUE 1
typedef unsigned char byte;
typedef unsigned long unicode;
extern long readbyte (FILE *f);
extern long readutf8char (FILE *f);
extern void writebyte (FILE *f, byte b);
extern void writeutf8char (FILE *f, unicode u);
void error (char *message)
{
printf("\nERROR: %s\n", message);
exit(1);
}
void dump_byte_seq (byte b[], int n_b)
{
int i;
printf("%d bytes {", n_b);
for (i = 0; i < n_b; i++) {
if (i > 0) printf(", ");
printf("0x%02x", b[i]);
}
printf("}");
}
void dump_unicode_seq (unicode u[], int n_u)
{
int i;
printf("%d chars {", n_u);
for (i = 0; i < n_u; i++) {
if (i > 0) printf(", ");
printf("0x%lx", u[i]);
}
printf("}");
}
void compare_byte_seqs (byte a[], int n_a, byte b[], int n_b)
{
int ok = TRUE;
if (n_a != n_b) {
ok = FALSE;
} else {
int i;
for (i = 0; i < n_a; i++)
if (a[i] != b[i]) ok = FALSE;
}
if (ok) {
printf("OK\n");
} else {
printf("\n Error: Result is "); dump_byte_seq(a, n_a);
printf("\n but should be "); dump_byte_seq(b, n_b); printf("\n");
}
}
void compare_unicode_seqs (unicode a[], int n_a, unicode b[], int n_b)
{
int ok = TRUE;
if (n_a != n_b) {
ok = FALSE;
} else {
int i;
for (i = 0; i < n_a; i++)
if (a[i] != b[i]) ok = FALSE;
}
if (ok) {
printf("OK\n");
} else {
printf("\n Error: Result is "); dump_unicode_seq(a, n_a);
printf("\n but should be "); dump_unicode_seq(b, n_b); printf("\n");
}
}
int read_test_byte (FILE *f)
{
int status;
byte c;
status = fread(&c, 1, 1, f);
if (status <= 0) return -1;
return (int)c;
}
void test_byte_file (char *f_name, byte data[], int n_data)
{
byte file_bytes[200];
int n_file_bytes, i;
FILE *f = fopen(f_name, "rb");
if (f == NULL) error("Could not open file!");
for (n_file_bytes = 0; n_file_bytes < 200; n_file_bytes++) {
int b = read_test_byte(f);
if (b < 0) break;
file_bytes[n_file_bytes] = b;
}
fclose(f);
compare_byte_seqs(file_bytes, n_file_bytes, data, n_data);
}
void create_byte_file (char *f_name, byte b_seq[], int n_b_seq)
{
FILE *f = fopen(f_name, "wb");
if (f == NULL) error("Could not create file!");
fwrite(b_seq, n_b_seq, 1, f);
fclose(f);
}
/* Test #1 */
byte b_seq_1[] = { 4, 0, 255, 17, 200 };
void test_1 (void)
{
int n_bytes = sizeof(b_seq_1)/sizeof(b_seq_1[0]);
int i;
FILE *f = fopen("test1.txt", "wb");
if (f == NULL) error("Could not create test1.txt!");
for (i = 0; i < n_bytes; i++)
writebyte(f, b_seq_1[i]);
fclose(f);
test_byte_file("test1.txt", b_seq_1, n_bytes);
}
/* Test #2 */
unicode u_seq_2[] = { 0x24, 0x20, 0x41, 0x3d, 0x32, 0x78 }; /* "$ A=2x" */
byte b_seq_2[] = { '$', ' ', 'A', '=', '2', 'x' };
void test_2 (void)
{
int n_u = sizeof(u_seq_2)/sizeof(u_seq_2[0]);
int n_b = sizeof(b_seq_2)/sizeof(b_seq_2[0]);
int i;
FILE *f = fopen("test2.txt", "wb");
if (f == NULL) error("Could not create test2.txt!");
for (i = 0; i < n_u; i++)
writeutf8char(f, u_seq_2[i]);
fclose(f);
test_byte_file("test2.txt", b_seq_2, n_b);
}
/* Test #3 */
unicode u_seq_3[] = { 0x35, 0xa2, 0x20, 0x429, 0x3c9 }; /* "5¢ Щω" */
byte b_seq_3[] = { '5', 0xc2, 0xa2, ' ', 0xd0, 0xa9, 0xcf, 0x89 };
void test_3 (void)
{
int n_u = sizeof(u_seq_3)/sizeof(u_seq_3[0]);
int n_b = sizeof(b_seq_3)/sizeof(b_seq_3[0]);
int i;
FILE *f = fopen("test3.txt", "wb");
if (f == NULL) error("Could not create test3.txt!");
for (i = 0; i < n_u; i++)
writeutf8char(f, u_seq_3[i]);
fclose(f);
test_byte_file("test3.txt", b_seq_3, n_b);
}
/* Test #4 */
unicode u_seq_4[] = { 0x20ac, 0x3d, 0x10348, 0x2658 }; /* "€=ðˆâ™˜" */
byte b_seq_4[] = { 0xe2, 0x82, 0xac, '=', 0xf0, 0x90, 0x8d, 0x88,
0xe2, 0x99, 0x98};
void test_4 (void)
{
int n_u = sizeof(u_seq_4)/sizeof(u_seq_4[0]);
int n_b = sizeof(b_seq_4)/sizeof(b_seq_4[0]);
int i;
FILE *f = fopen("test4.txt", "wb");
if (f == NULL) error("Could not create test4.txt!");
for (i = 0; i < n_u; i++)
writeutf8char(f, u_seq_4[i]);
fclose(f);
test_byte_file("test4.txt", b_seq_4, n_b);
}
/* Test #5 */
void test_5 (void)
{
byte data[200];
int n_data = 0;
int n_b_seq_1 = sizeof(b_seq_1)/sizeof(b_seq_1[0]);
FILE *f;
create_byte_file ("test5.txt", b_seq_1, n_b_seq_1);
f = fopen("test5.txt", "rb");
if (f == NULL) error("Could not read test5.txt!");
while (n_data < 200) {
long b = readbyte(f);
if (b < 0) break;
data[n_data++] = (byte)b;
}
fclose(f);
compare_byte_seqs(data, n_data, b_seq_1, n_b_seq_1);
}
/* Test #6 */
void test_6 (void)
{
unicode data[200];
int n_data = 0;
int n_b_seq_2 = sizeof(b_seq_2)/sizeof(b_seq_2[0]);
int n_u_seq_2 = sizeof(u_seq_2)/sizeof(u_seq_2[0]);
FILE *f;
create_byte_file ("test6.txt", b_seq_2, n_b_seq_2);
f = fopen("test6.txt", "rb");
if (f == NULL) error("Could not read test6.txt!");
while (n_data < 200) {
long u = readutf8char(f);
if (u < 0) break;
data[n_data++] = (unicode)u;
}
fclose(f);
compare_unicode_seqs(data, n_data, u_seq_2, n_u_seq_2);
}
/* Test #7 */
void test_7 (void)
{
unicode data[200];
int n_data = 0;
int n_b_seq_3 = sizeof(b_seq_3)/sizeof(b_seq_3[0]);
int n_u_seq_3 = sizeof(u_seq_3)/sizeof(u_seq_3[0]);
FILE *f;
create_byte_file ("test7.txt", b_seq_3, n_b_seq_3);
f = fopen("test7.txt", "rb");
if (f == NULL) error("Could not read test7.txt!");
while (n_data < 200) {
long u = readutf8char(f);
if (u < 0) break;
data[n_data++] = (unicode)u;
}
fclose(f);
compare_unicode_seqs(data, n_data, u_seq_3, n_u_seq_3);
}
/* Test #8 */
void test_8 (void)
{
unicode data[200];
int n_data = 0;
int n_b_seq_4 = sizeof(b_seq_4)/sizeof(b_seq_4[0]);
int n_u_seq_4 = sizeof(u_seq_4)/sizeof(u_seq_4[0]);
FILE *f;
create_byte_file ("test8.txt", b_seq_4, n_b_seq_4);
f = fopen("test8.txt", "rb");
if (f == NULL) error("Could not read test8.txt!");
while (n_data < 200) {
long u = readutf8char(f);
if (u < 0) break;
data[n_data++] = (unicode)u;
}
fclose(f);
compare_unicode_seqs(data, n_data, u_seq_4, n_u_seq_4);
}
/* Main program */
int main (void)
{
printf("Test 1 (write a byte): "); test_1();
printf("Test 2 (write 1-byte utf-8): "); test_2();
printf("Test 3 (write 2-byte utf-8): "); test_3();
printf("Test 4 (write 3+4-byte utf-8): "); test_4();
printf("Test 5 (read a byte): "); test_5();
printf("Test 6 (read 1-byte utf-8): "); test_6();
printf("Test 7 (read 2-byte utf-8): "); test_7();
printf("Test 8 (read 3+4-byte utf-8): "); test_8();
return 0;
}