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