annotate src/lib/test-strnum.c @ 22664:fea53c2725c0

director: Fix director_max_parallel_moves/kicks type Should be uint, not time.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 09 Nov 2017 12:24:16 +0200
parents 2e2563132d5f
children cb108f786fb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21390
2e2563132d5f Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 19676
diff changeset
1 /* Copyright (c) 2014-2017 Dovecot authors, see the included COPYING file */
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
2
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
3 #include "test-lib.h"
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
4
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
5
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
6 #define INVALID(n) { #n, -1, 0 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
7 #define VALID(n) { #n, 0, n }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
8
17813
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
9 /* always pads with leading zeros to a size of 9 digits */
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
10 static int crappy_uintmax_to_str(char *into, uintmax_t val)
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
11 {
19129
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
12 #define BIGBASE 1000000000ull
17813
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
13 #define STRINGIFY(s) #s
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
14 #define STRINGIFY2(s) STRINGIFY(s)
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
15 int len = 0;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
16 if(val >= BIGBASE) {
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
17 len = crappy_uintmax_to_str(into, val/BIGBASE);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
18 }
19129
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
19 i_snprintf(into + len, 10, "%09llu",
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
20 (unsigned long long)(val % BIGBASE));
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
21 return len + strlen(STRINGIFY2(BIGBASE))-4;
17813
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
22 #undef STRINGIFY2
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
23 #undef STRINGIFY
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
24 #undef BIGBASE
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
25 }
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
26 static void test_str_to_uintmax(void)
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
27 {
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
28 unsigned int i=0;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
29 int randrange = rand()%15+1; /* when 1, will max out on 1s */
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
30 uintmax_t value = 0, valbase = rand() * 1000ull;
17813
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
31 int len, ret;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
32 char buff[50]; /* totally assumes < 159 bits */
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
33
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
34 test_begin("str_to_uintmax in range");
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
35 while (i < sizeof(uintmax_t)*CHAR_BIT) {
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
36 uintmax_t value_back;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
37 const char *endp;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
38
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
39 value = (value << 1) + 1;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
40 if (value >= 64)
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
41 value -= rand()%randrange; /* don't always test the same numbers */
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
42 len = crappy_uintmax_to_str(buff, value);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
43 ret = str_to_uintmax(buff, &value_back);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
44 test_assert_idx(ret == 0, i);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
45 test_assert_idx(value == value_back, i);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
46
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
47 /* test with trailing noise */
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
48 buff[len] = 'x'; /* don't even null-terminate, let's be evil */
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
49 value_back = 0x1234567890123456;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
50 ret = str_to_uintmax(buff, &value_back);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
51 test_assert_idx(ret < 0, i);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
52 test_assert_idx(value_back == 0x1234567890123456, i);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
53 ret = str_parse_uintmax(buff, &value_back, &endp);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
54 test_assert_idx(ret == 0, i);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
55 test_assert_idx(value_back == value, i);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
56 test_assert_idx(endp == &buff[len], i);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
57 i++;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
58 }
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
59 test_end();
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
60
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
61 /* not knowing exactly how large a uintmax_t is, we have to construct
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
62 the troublesome near-10/9*MAX strings manually by appending digits
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
63 to a MAX/9 string which we can easily create. Do a wider range
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
64 of 30 rather than the obvious 10, just in case - all are too large.*/
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
65 test_begin("str_to_uintmax overflow corner case");
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
66 value = UINTMAX_MAX/9-1;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
67 len = crappy_uintmax_to_str(buff, value);
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
68 buff[len] = '0';
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
69 buff[len+1] = '\0';
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
70 for(i = 0; i <= 30; ++i) {
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
71 int j = len + 1;
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
72 while (buff[--j] == '9')
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
73 buff[j] = '0';
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
74 buff[j]++;
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
75 value = valbase + i;
17813
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
76 ret = str_to_uintmax(buff, &value);
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
77 test_assert_idx(ret < 0 && value == valbase + i, i);
17813
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
78 }
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
79 test_end();
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
80 }
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
81
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
82 /* always pads with leading zeros to a size of 9 digits */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
83 static int crappy_uintmax_to_str_hex(char *into, uintmax_t val)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
84 {
19129
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
85 #define BIGBASE 0x1000000000ull
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
86 #define STRINGIFY(s) #s
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
87 #define STRINGIFY2(s) STRINGIFY(s)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
88 int len = 0;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
89 if(val >= BIGBASE) {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
90 len = crappy_uintmax_to_str_hex(into, val/BIGBASE);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
91 }
19129
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
92 i_snprintf(into + len, 10, "%09llx",
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
93 (unsigned long long)(val % BIGBASE));
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
94 return len + strlen(STRINGIFY2(BIGBASE))-6;
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
95 #undef STRINGIFY2
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
96 #undef STRINGIFY
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
97 #undef BIGBASE
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
98 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
99 static void test_str_to_uintmax_hex(void)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
100 {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
101 unsigned int i=0;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
102 int randrange = rand()%15+1; /* when 1, will max out on 1s */
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
103 uintmax_t value = 0, valbase = rand() * 1000ull;
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
104 int len, ret;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
105 char buff[52]; /* totally assumes < 200 bits */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
106
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
107 test_begin("str_to_uintmax_hex in range");
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
108 while (i < sizeof(uintmax_t)*CHAR_BIT) {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
109 uintmax_t value_back;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
110 const char *endp;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
111
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
112 value = (value << 1) + 1;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
113 if (value >= 64)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
114 value -= rand()%randrange; /* don't always test the same numbers */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
115 len = crappy_uintmax_to_str_hex(buff, value);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
116 ret = str_to_uintmax_hex(buff, &value_back);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
117 test_assert_idx(ret == 0, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
118 test_assert_idx(value == value_back, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
119
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
120 /* test with trailing noise */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
121 buff[len] = 'x'; /* don't even null-terminate, let's be evil */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
122 value_back = 0x1234567890123456;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
123 ret = str_to_uintmax_hex(buff, &value_back);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
124 test_assert_idx(ret < 0, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
125 test_assert_idx(value_back == 0x1234567890123456, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
126 ret = str_parse_uintmax_hex(buff, &value_back, &endp);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
127 test_assert_idx(ret == 0, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
128 test_assert_idx(value_back == value, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
129 test_assert_idx(endp == &buff[len], i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
130 i++;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
131 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
132 test_end();
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
133
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
134 /* not knowing exactly how large a uintmax_t is, we have to construct
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
135 the troublesome near-0x10/0x0F*MAX strings manually by appending digits
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
136 to a MAX/0x0f string which we can easily create. Do a wider range
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
137 of 0x30 rather than the obvious 0x10, just in case - all are too large.*/
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
138 test_begin("str_to_uintmax_hex overflow corner case");
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
139 value = (UINTMAX_MAX/0x0f)-1;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
140 len = crappy_uintmax_to_str_hex(buff, value);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
141 buff[len] = '0';
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
142 buff[len+1] = '\0';
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
143 for(i = 0; i <= 0x30; ++i) {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
144 int j = len + 1;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
145 while (buff[--j] == 'f')
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
146 buff[j] = '0';
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
147 if (buff[j] == '9')
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
148 buff[j] = 'a';
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
149 else
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
150 buff[j]++;
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
151 value = valbase + i;
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
152 ret = str_to_uintmax_hex(buff, &value);
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
153 test_assert_idx(ret < 0 && value == valbase + i, i);
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
154 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
155 test_end();
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
156 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
157
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
158 /* always pads with leading zeros to a size of 9 digits */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
159 static int crappy_uintmax_to_str_oct(char *into, uintmax_t val)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
160 {
19129
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
161 #define BIGBASE 01000000000ull
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
162 #define STRINGIFY(s) #s
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
163 #define STRINGIFY2(s) STRINGIFY(s)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
164 int len = 0;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
165 if(val >= BIGBASE) {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
166 len = crappy_uintmax_to_str_oct(into, val/BIGBASE);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
167 }
19129
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
168 i_snprintf(into + len, 10, "%09llo",
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
169 (unsigned long long)(val % BIGBASE));
e4438e342208 lib: strnum: Fixed test suite failure on 32 bit systems.
Stephan Bosch <stephan@rename-it.nl>
parents: 19124
diff changeset
170 return len + strlen(STRINGIFY2(BIGBASE))-5;
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
171 #undef STRINGIFY2
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
172 #undef STRINGIFY
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
173 #undef BIGBASE
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
174 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
175 static void test_str_to_uintmax_oct(void)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
176 {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
177 unsigned int i=0;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
178 int randrange = rand()%15+1; /* when 1, will max out on 1s */
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
179 uintmax_t value = 0, valbase = rand() * 1000ull;
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
180 int len, ret;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
181 char buff[69]; /* totally assumes < 200 bits */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
182
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
183 test_begin("str_to_uintmax_oct in range");
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
184 while (i < sizeof(uintmax_t)*CHAR_BIT) {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
185 uintmax_t value_back;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
186 const char *endp;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
187
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
188 value = (value << 1) + 1;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
189 if (value >= 64)
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
190 value -= rand()%randrange; /* don't always test the same numbers */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
191 len = crappy_uintmax_to_str_oct(buff, value);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
192 ret = str_to_uintmax_oct(buff, &value_back);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
193 test_assert_idx(ret == 0, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
194 test_assert_idx(value == value_back, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
195
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
196 /* test with trailing noise */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
197 buff[len] = 'x'; /* don't even null-terminate, let's be evil */
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
198 value_back = 0x1234567890123456;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
199 ret = str_to_uintmax_oct(buff, &value_back);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
200 test_assert_idx(ret < 0, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
201 test_assert_idx(value_back == 0x1234567890123456, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
202 ret = str_parse_uintmax_oct(buff, &value_back, &endp);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
203 test_assert_idx(ret == 0, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
204 test_assert_idx(value_back == value, i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
205 test_assert_idx(endp == &buff[len], i);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
206 i++;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
207 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
208 test_end();
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
209
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
210 /* not knowing exactly how large a uintmax_t is, we have to construct
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
211 the troublesome near-010/007*MAX strings manually by appending digits
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
212 to a MAX/007 string which we can easily create. Do a wider range
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
213 of 030 rather than the obvious 010, just in case - all are too large.*/
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
214 test_begin("str_to_uintmax_oct overflow corner case");
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
215 value = (UINTMAX_MAX/007)-1;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
216 len = crappy_uintmax_to_str_oct(buff, value);
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
217 buff[len] = '0';
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
218 buff[len+1] = '\0';
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
219 for(i = 0; i <= 030; ++i) {
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
220 int j = len + 1;
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
221 while (buff[--j] == '7')
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
222 buff[j] = '0';
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
223 buff[j]++;
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
224 value = valbase + i;
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
225 ret = str_to_uintmax_oct(buff, &value);
19139
8687e24d9019 lib: test-strnum - do not invite undetectable errors
Phil Carmody <phil@dovecot.fi>
parents: 19129
diff changeset
226 test_assert_idx(ret < 0 && value == valbase + i, i);
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
227 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
228 test_end();
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
229 }
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
230
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
231 static void test_str_to_u64(void)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
232 {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
233 unsigned int i;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
234 const struct {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
235 const char *input;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
236 int ret;
17812
138fd3ccc9da lib: remove uintmax tests from uint64 tests
Phil Carmody <phil@dovecot.fi>
parents: 17555
diff changeset
237 uint64_t val;
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
238 } u64tests[] = {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
239 INVALID(-1),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
240 INVALID(foo),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
241 VALID(0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
242 VALID(000000000000000000000000000000000000000000000000000000000000000),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
243 { "000000000000000000000000000000000000000000000000000001000000001", 0, 1000000001 },
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
244 { "18446744073709551615", 0, 18446744073709551615ULL },
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
245 INVALID(18446744073709551616),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
246 INVALID(20496382304121724010), /* 2^64*10/9 doesn't wrap */
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
247 INVALID(20496382304121724017), /* 2^64*10/9 wraps only after addition */
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
248 INVALID(20496382304121724020), /* 2^64*10/9 wraps on multiply*/
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
249 };
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
250 test_begin("str_to_uint64");
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
251 for (i = 0; i < N_ELEMENTS(u64tests); ++i) {
17812
138fd3ccc9da lib: remove uintmax tests from uint64 tests
Phil Carmody <phil@dovecot.fi>
parents: 17555
diff changeset
252 uint64_t val = 0xBADBEEF15BADF00D;
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
253 int ret = str_to_uint64(u64tests[i].input, &val);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
254 test_assert_idx(ret == u64tests[i].ret, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
255 if (ret == 0)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
256 test_assert_idx(val == u64tests[i].val, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
257 else
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
258 test_assert_idx(val == 0xBADBEEF15BADF00D, i);
17555
85a44b4b121a lib: test-strnum - tests for the new partial-string parser
Phil Carmody <phil@dovecot.fi>
parents: 17551
diff changeset
259
85a44b4b121a lib: test-strnum - tests for the new partial-string parser
Phil Carmody <phil@dovecot.fi>
parents: 17551
diff changeset
260 if (ret == 0)
85a44b4b121a lib: test-strnum - tests for the new partial-string parser
Phil Carmody <phil@dovecot.fi>
parents: 17551
diff changeset
261 T_BEGIN {
85a44b4b121a lib: test-strnum - tests for the new partial-string parser
Phil Carmody <phil@dovecot.fi>
parents: 17551
diff changeset
262 const char *longer = t_strconcat(u64tests[i].input, "x", NULL);
17812
138fd3ccc9da lib: remove uintmax tests from uint64 tests
Phil Carmody <phil@dovecot.fi>
parents: 17555
diff changeset
263 ret = str_to_uint64(longer, &val);
17555
85a44b4b121a lib: test-strnum - tests for the new partial-string parser
Phil Carmody <phil@dovecot.fi>
parents: 17551
diff changeset
264 test_assert_idx(ret < 0, i);
85a44b4b121a lib: test-strnum - tests for the new partial-string parser
Phil Carmody <phil@dovecot.fi>
parents: 17551
diff changeset
265 } T_END;
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
266 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
267 test_end();
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
268 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
269
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
270 static void test_str_to_u32(void)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
271 {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
272 unsigned int i;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
273 const struct {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
274 const char *input;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
275 int ret;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
276 uint32_t val;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
277 } u32tests[] = {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
278 VALID(0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
279 INVALID(-0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
280 VALID(4294967295),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
281 INVALID(4294967296),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
282 INVALID(4772185880),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
283 INVALID(4772185884),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
284 INVALID(4772185890),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
285 };
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
286 test_begin("str_to_uint32");
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
287 for (i = 0; i < N_ELEMENTS(u32tests); ++i) {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
288 uint32_t val = 0xDEADF00D;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
289 int ret = str_to_uint32(u32tests[i].input, &val);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
290 test_assert_idx(ret == u32tests[i].ret, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
291 if (ret == 0)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
292 test_assert_idx(val == u32tests[i].val, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
293 else
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
294 test_assert_idx(val == 0xDEADF00D, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
295 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
296 test_end();
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
297 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
298
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
299 /* Assumes long long is 64 bit, 2's complement */
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
300 static void test_str_to_llong(void)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
301 {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
302 unsigned int i;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
303 const struct {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
304 const char *input;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
305 int ret;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
306 long long val;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
307 } i64tests[] = {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
308 VALID(0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
309 VALID(-0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
310 INVALID(--0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
311 VALID(2147483648),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
312 VALID(-2147483649),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
313 VALID(9223372036854775807),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
314 { "-9223372036854775808", 0, -9223372036854775807-1 },
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
315 INVALID(9223372036854775808),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
316 INVALID(-9223372036854775809),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
317 };
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
318 test_begin("str_to_llong");
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
319 for (i = 0; i < N_ELEMENTS(i64tests); ++i) {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
320 long long val = 123456789;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
321 int ret = str_to_llong(i64tests[i].input, &val);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
322 test_assert_idx(ret == i64tests[i].ret, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
323 if (ret == 0)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
324 test_assert_idx(val == i64tests[i].val, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
325 else
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
326 test_assert_idx(val == 123456789, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
327 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
328 test_end();
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
329 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
330
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
331 /* Assumes int is 32 bit, 2's complement */
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
332 static void test_str_to_i32(void)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
333 {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
334 unsigned int i;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
335 const struct {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
336 const char *input;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
337 int ret;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
338 int val;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
339 } i32tests[] = {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
340 VALID(0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
341 VALID(-0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
342 INVALID(--0),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
343 VALID(2147483647),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
344 VALID(-2147483648),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
345 INVALID(2147483648),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
346 INVALID(-2147483649),
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
347 };
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
348 test_begin("str_to_int");
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
349 for (i = 0; i < N_ELEMENTS(i32tests); ++i) {
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
350 int val = 123456789;
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
351 int ret = str_to_int(i32tests[i].input, &val);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
352 test_assert_idx(ret == i32tests[i].ret, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
353 if (ret == 0)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
354 test_assert_idx(val == i32tests[i].val, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
355 else
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
356 test_assert_idx(val == 123456789, i);
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
357 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
358 test_end();
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
359 }
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
360
19676
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
361 static void test_str_is_float(void)
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
362 {
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
363 test_begin("str_is_float accepts integer");
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
364 /* accepts integer */
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
365 test_assert(str_is_float("0",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
366 test_assert(str_is_float("1234",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
367 test_end();
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
368 test_begin("str_is_float accepts float");
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
369 test_assert(str_is_float("0.0",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
370 test_assert(str_is_float("1234.0",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
371 test_assert(str_is_float("0.1234",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
372 test_assert(str_is_float("1234.1234",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
373 test_assert(str_is_float("0.1234 ",' '));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
374 test_assert(str_is_float("1234.1234",'.'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
375 test_end();
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
376 test_begin("str_is_float refuses invalid values");
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
377 test_assert(!str_is_float(".",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
378 test_assert(!str_is_float(".1234",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
379 test_assert(!str_is_float("1234.",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
380 test_assert(!str_is_float("i am not a float at all",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
381 test_assert(!str_is_float("0x1234.0x1234",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
382 test_assert(!str_is_float(".0",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
383 test_assert(!str_is_float("0.",'\0'));
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
384 test_end();
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
385 }
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
386
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
387 void test_strnum(void)
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
388 {
17555
85a44b4b121a lib: test-strnum - tests for the new partial-string parser
Phil Carmody <phil@dovecot.fi>
parents: 17551
diff changeset
389 /* If the above isn't true, then we do expect some failures possibly */
17813
c58aaa79d647 lib: test-strnum - add size-oblivious str_to/parse_uintmax tests
Phil Carmody <phil@dovecot.fi>
parents: 17812
diff changeset
390 test_str_to_uintmax();
19124
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
391 test_str_to_uintmax_hex();
d85bf97ab8b7 lib: Added some tests for the new strnum _hex() and _oct() function variants.
Stephan Bosch <stephan@rename-it.nl>
parents: 18137
diff changeset
392 test_str_to_uintmax_oct();
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
393 test_str_to_u64();
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
394 test_str_to_u32();
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
395 test_str_to_llong();
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
396 test_str_to_i32();
19676
286e4218fb07 lib: Add tests for str_is_float
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
397 test_str_is_float();
17551
6c1e44033e60 lib: test-lib - add unit tests for str_to_*() helpers
Phil Carmody <phil@dovecot.fi>
parents:
diff changeset
398 }