blob: 64b5d2e151a8b68653363c080cfeee8484428eb9 [file] [log] [blame]
Lev Walkin4eceeba2007-07-23 06:48:26 +00001#include <stdio.h>
2#include <assert.h>
Lev Walkin263b0282004-10-02 11:37:09 +00003#include <sys/time.h>
4
Lev Walkin4eceeba2007-07-23 06:48:26 +00005#include <UTF8String.h>
6
Lev Walkin263b0282004-10-02 11:37:09 +00007static void
8check(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]);
Lev Walkin45169132004-10-02 15:54:09 +000020 ret = UTF8String_length(&st);
Lev Walkin263b0282004-10-02 11:37:09 +000021 printf("]: size=%d, expect=%d, got=%d\n",
22 buflen, expect_length, ret);
23 assert(ret == expect_length);
24}
25
26static int
27check_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
Lev Walkin535612a2005-07-03 05:32:40 +000041 st.buf = (uint8_t *)long_test;
Lev Walkin263b0282004-10-02 11:37:09 +000042 st.size = sizeof(long_test) - 1;
43
Lev Walkin45169132004-10-02 15:54:09 +000044 ret = UTF8String_length(&st);
Lev Walkin263b0282004-10-02 11:37:09 +000045 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++) {
Lev Walkin45169132004-10-02 15:54:09 +000051 ret += UTF8String_length(&st);
Lev Walkin263b0282004-10-02 11:37:09 +000052 }
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
61int
62main() {
63
64 check(0, "", 0);
65 check(1, "\0", 1);
Lev Walkin263b0282004-10-02 11:37:09 +000066 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);
Lev Walkin45169132004-10-02 15:54:09 +000071 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);
Lev Walkin263b0282004-10-02 11:37:09 +000083
84 check_speed();
85
86 return 0;
87}
88