University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-oblig3.c 6.4KB

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