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