BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file parse_number_test.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <stdio.h>
31 #include <inttypes.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <time.h>
36  
37 #include <misc/parse_number.h>
38 #include <misc/debug.h>
39  
40 static void test_random (int num_digits, int digit_modulo)
41 {
42 ASSERT(num_digits > 0);
43 ASSERT(digit_modulo > 0);
44  
45 uint8_t digits[40];
46  
47 for (int i = 0; i < num_digits; i++) {
48 digits[i] = '0' + (rand() % digit_modulo);
49 }
50 digits[num_digits] = '\0';
51  
52 char *endptr;
53 uintmax_t std_num = strtoumax((const char *)digits, &endptr, 10);
54 int std_res = !*endptr && !(std_num == UINTMAX_MAX && errno == ERANGE);
55  
56 uintmax_t num = 0;
57 int res = parse_unsigned_integer(MemRef_Make((const char *)digits, num_digits), &num);
58  
59 if (res != std_res) {
60 printf("fail1 %s\n", (const char *)digits);
61 ASSERT_FORCE(0);
62 }
63  
64 if (res && num != std_num) {
65 printf("fail2 %s\n", (const char *)digits);
66 ASSERT_FORCE(0);
67 }
68  
69 if (res) {
70 uint8_t *nozero_digits = digits;
71 while (*nozero_digits == '0' && nozero_digits != &digits[num_digits - 1]) {
72 nozero_digits++;
73 }
74  
75 char buf[40];
76 int size = compute_decimal_repr_size(num);
77 generate_decimal_repr(num, buf, size);
78 buf[size] = '\0';
79 ASSERT_FORCE(!strcmp(buf, (const char *)nozero_digits));
80 }
81 }
82  
83 static void test_value (uintmax_t x)
84 {
85 char str[40];
86 sprintf(str, "%" PRIuMAX, x);
87 uintmax_t y;
88 int res = parse_unsigned_integer(MemRef_MakeCstr(str), &y);
89 ASSERT_FORCE(res);
90 ASSERT_FORCE(y == x);
91  
92 char str2[40];
93 int size = compute_decimal_repr_size(x);
94 generate_decimal_repr(x, str2, size);
95 str2[size] = '\0';
96  
97 ASSERT_FORCE(!strcmp(str2, str));
98 }
99  
100 static void test_value_range (uintmax_t start, uintmax_t count)
101 {
102 uintmax_t i = start;
103 do {
104 test_value(i);
105 i++;
106 } while (i != start + count);
107 }
108  
109 int main ()
110 {
111 srand(time(NULL));
112  
113 for (int num_digits = 1; num_digits <= 22; num_digits++) {
114 for (int i = 0; i < 1000000; i++) {
115 test_random(num_digits, 10);
116 }
117 for (int i = 0; i < 1000000; i++) {
118 test_random(num_digits, 11);
119 }
120 }
121  
122 test_value_range(UINTMAX_C(0), 5000000);
123 test_value_range(UINTMAX_C(100000000), 5000000);
124 test_value_range(UINTMAX_C(258239003), 5000000);
125 test_value_range(UINTMAX_C(8241096180752634), 5000000);
126 test_value_range(UINTMAX_C(9127982390882308083), 5000000);
127 test_value_range(UINTMAX_C(18446744073700000000), 20000000);
128  
129 return 0;
130 }