Debian Codegraph @

skeletons/tests/check-UTF8String.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <assert.h>
3 #include <sys/time.h>
4
5 #include <UTF8String.h>
6
7 static void
8 check(int expect_length, char *buf, int buflen) {
9 UTF8String_t st;
10 int ret;
11
12 if(buflen < 0) buflen = strlen(buf);
13
14 st.buf = (uint8_t *)buf;
15 st.size = buflen;
16 printf("[");
17
18 for(ret = 0; ret < buflen; ret++)
19 printf("%c", buf[ret]);
20 ret = UTF8String_length(&st);
21 printf("]: size=%d, expect=%d, got=%d\n",
22 buflen, expect_length, ret);
23 assert(ret == expect_length);
24 }
25
26 static int
27 check_speed() {
28 int cycles = 1000000;
29 double start, stop;
30 struct timeval tv;
31 UTF8String_t st;
32 char long_test[] =
33 "a\303\237a\303\237a\303\237a\303\237"
34 "a\303\237a\303\237a\303\237a\303\237"
35 "a\303\237a\303\237a\303\237a\303\237"
36 "a\303\237a\303\237a\303\237a\303\237"
37 "a\303\237a\303\237a\303\237a\303\237";
38 int ret;
39 int i;
40
41 st.buf = (uint8_t *)long_test;
42 st.size = sizeof(long_test) - 1;
43
44 ret = UTF8String_length(&st);
45 assert(ret == 40);
46 printf("Now wait a bit...\n");
47
48 gettimeofday(&tv, 0);
49 start = tv.tv_sec + tv.tv_usec / 1000000.0;
50 for(i = 0; i < cycles; i++) {
51 ret += UTF8String_length(&st);
52 }
53 gettimeofday(&tv, 0);
54 stop = tv.tv_sec + tv.tv_usec / 1000000.0;
55
56 printf("%d cycles in %.3fms\n", cycles, stop - start);
57
58 return ret;
59 }
60
61 int
62 main() {
63
64 check(0, "", 0);
65 check(1, "\0", 1);
66 check(1, "a", 1);
67 check(2, "ab", 2);
68 check(3, "abc", 3);
69 assert(sizeof("a\303\237cd") == 6);
70 check(4, "a\303\237cd", 5);
71 check(3, "a\370\211\200\201\257c", 7);
72 check(3, "\320\273\320\265\320\262", 6);
73
74 check(-1, "a\303", 2); /* Truncated */
75 check(-2, "\377", 1); /* Invalid UTF-8 sequence start */
76 check(-2, "\200", 1);
77 check(-2, "\320\273\265\320\262", 5);
78 check(-3, "\320c", 2); /* Not continuation */
79 check(-3, "a\370\200\200\200c", 6);
80 check(-4, "a\370\200\200\200\257c", 7);
81 check(-4, "\320\273\320\265\340\200\262", 7);
82 check(-5, 0, 0);
83
84 check_speed();
85
86 return 0;
87 }
88