/builds/xfbs/passgen/src/tests/utf8.c
Line | Count | Source |
1 | | #include "passgen/util/utf8.h" |
2 | | #include "tests.h" |
3 | | |
4 | | // test that we can decode an empty string. |
5 | 1 | test_result test_utf8_can_decode_empty(void) { |
6 | 1 | size_t input_len = 0; |
7 | 1 | const uint8_t input[input_len + 1]; |
8 | 1 | size_t output_len = 0; |
9 | 1 | uint32_t output[output_len + 1]; |
10 | 1 | const uint8_t *input_pos = &input[0]; |
11 | 1 | uint32_t *output_pos = &output[0]; |
12 | 1 | |
13 | 1 | int ret = passgen_utf8_decode( |
14 | 1 | &output_pos, |
15 | 1 | output_len, |
16 | 1 | NULL, |
17 | 1 | &input_pos, |
18 | 1 | input_len); |
19 | 1 | |
20 | 1 | assert(ret == PASSGEN_UTF8_SUCCESS); |
21 | 1 | assert(output_pos == &output[0]); |
22 | 1 | assert(input_pos == &input[0]); |
23 | 1 | |
24 | 1 | return test_ok; |
25 | 1 | } |
26 | | |
27 | | // test that we can decode a simple utf-8 sequence. |
28 | 1 | test_result test_utf8_can_decode_simple(void) { |
29 | 1 | // üðÂÂÂõ |
30 | 1 | const uint8_t input[] = |
31 | 1 | {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a}; |
32 | 1 | size_t input_len = sizeof(input); |
33 | 1 | |
34 | 1 | size_t output_len = 100; |
35 | 1 | uint32_t output[output_len]; |
36 | 1 | |
37 | 1 | const uint8_t *input_pos = &input[0]; |
38 | 1 | uint32_t *output_pos = &output[0]; |
39 | 1 | |
40 | 1 | int ret = passgen_utf8_decode( |
41 | 1 | &output_pos, |
42 | 1 | output_len, |
43 | 1 | NULL, |
44 | 1 | &input_pos, |
45 | 1 | input_len); |
46 | 1 | |
47 | 1 | // succesful return |
48 | 1 | assert(ret == PASSGEN_UTF8_SUCCESS); |
49 | 1 | |
50 | 1 | // parsed all data |
51 | 1 | assert(input_pos == &input[input_len]); |
52 | 1 | assert(output_pos == &output[4]); |
53 | 1 | |
54 | 1 | // parsed right characters |
55 | 1 | assert(output[0] == 0xFC); |
56 | 1 | assert(output[1] == 0x1F602); |
57 | 1 | assert(output[2] == 0xB5); |
58 | 1 | assert(output[3] == 0x0A); |
59 | 1 | |
60 | 1 | return test_ok; |
61 | 1 | } |
62 | | |
63 | | // test that we can decode a simple utf-8 sequence with character widths. |
64 | 1 | test_result test_utf8_can_decode_simple_widths(void) { |
65 | 1 | // üðÂÂÂõ |
66 | 1 | const uint8_t input[] = |
67 | 1 | {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a}; |
68 | 1 | size_t input_len = sizeof(input); |
69 | 1 | |
70 | 1 | size_t output_len = 100; |
71 | 1 | uint32_t output[output_len]; |
72 | 1 | uint8_t widths[output_len]; |
73 | 1 | |
74 | 1 | const uint8_t *input_pos = &input[0]; |
75 | 1 | uint32_t *output_pos = &output[0]; |
76 | 1 | |
77 | 1 | int ret = passgen_utf8_decode( |
78 | 1 | &output_pos, |
79 | 1 | output_len, |
80 | 1 | &widths[0], |
81 | 1 | &input_pos, |
82 | 1 | input_len); |
83 | 1 | |
84 | 1 | // successful return |
85 | 1 | assert(ret == PASSGEN_UTF8_SUCCESS); |
86 | 1 | |
87 | 1 | // processed all input data |
88 | 1 | assert(input_pos == &input[input_len]); |
89 | 1 | assert(output_pos == &output[4]); |
90 | 1 | |
91 | 1 | // parsed characters properly |
92 | 1 | assert(output[0] == 0xFC); |
93 | 1 | assert(output[1] == 0x1F602); |
94 | 1 | assert(output[2] == 0xB5); |
95 | 1 | assert(output[3] == 0x0A); |
96 | 1 | |
97 | 1 | // parsed widths correctly |
98 | 1 | assert(widths[0] == 2); |
99 | 1 | assert(widths[1] == 4); |
100 | 1 | assert(widths[2] == 2); |
101 | 1 | assert(widths[3] == 1); |
102 | 1 | |
103 | 1 | return test_ok; |
104 | 1 | } |
105 | | |
106 | | // test that we can decode a simple UTF-8 string, if there is not enough |
107 | | // space in the output array, by restarting the decoding. |
108 | 1 | test_result test_utf8_decode_short_output(void) { |
109 | 1 | // üðÂÂÂõ |
110 | 1 | const uint8_t input[] = |
111 | 1 | {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a}; |
112 | 1 | size_t input_len = sizeof(input); |
113 | 1 | |
114 | 1 | size_t output_len = 3; |
115 | 1 | uint32_t output[output_len]; |
116 | 1 | |
117 | 1 | const uint8_t *input_pos = &input[0]; |
118 | 1 | uint32_t *output_pos = &output[0]; |
119 | 1 | |
120 | 1 | int ret = passgen_utf8_decode( |
121 | 1 | &output_pos, |
122 | 1 | output_len, |
123 | 1 | NULL, |
124 | 1 | &input_pos, |
125 | 1 | input_len); |
126 | 1 | |
127 | 1 | // on first run, it should only decode the first three characters. it |
128 | 1 | // should return larger than zero because there is still content to be |
129 | 1 | // decoded. |
130 | 1 | assert(ret == PASSGEN_UTF8_OUTPUT_SIZE); |
131 | 1 | assert(input_pos == &input[8]); |
132 | 1 | assert(output_pos == &output[output_len]); |
133 | 1 | assert(output[0] == 0xFC); |
134 | 1 | assert(output[1] == 0x1F602); |
135 | 1 | assert(output[2] == 0xB5); |
136 | 1 | |
137 | 1 | // reset where the current output pointer is at |
138 | 1 | output_pos = &output[0]; |
139 | 1 | |
140 | 1 | // continue parsing the rest of the utf-8 sequence. |
141 | 1 | ret = passgen_utf8_decode( |
142 | 1 | &output_pos, |
143 | 1 | output_len, |
144 | 1 | NULL, |
145 | 1 | &input_pos, |
146 | 1 | input_len - 8); |
147 | 1 | |
148 | 1 | // on second run, the final character is decoded. |
149 | 1 | assert(ret == PASSGEN_UTF8_SUCCESS); |
150 | 1 | assert(input_pos == &input[input_len]); |
151 | 1 | assert(output_pos == &output[1]); |
152 | 1 | assert(output[0] == 0x0A); |
153 | 1 | |
154 | 1 | return test_ok; |
155 | 1 | } |
156 | | |
157 | 1 | test_result test_utf8_encode_simple(void) { |
158 | 1 | // test that we can decode a simple UTF-8 string. |
159 | 1 | |
160 | 1 | // üðÂÂÂõ |
161 | 1 | const uint8_t expected[] = |
162 | 1 | {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a}; |
163 | 1 | |
164 | 1 | const uint32_t input[] = {0xFC, 0x1F602, 0xB5, 0x0A}; |
165 | 1 | |
166 | 1 | size_t output_len = 15; |
167 | 1 | uint8_t output[output_len]; |
168 | 1 | |
169 | 1 | size_t in_pos = 0; |
170 | 1 | size_t out_pos = 0; |
171 | 1 | |
172 | 1 | int ret = passgen_utf8_encode( |
173 | 1 | output, |
174 | 1 | output_len, |
175 | 1 | &out_pos, |
176 | 1 | input, |
177 | 1 | sizeof(input) / sizeof(input[0]), |
178 | 1 | &in_pos); |
179 | 1 | |
180 | 1 | assert(ret == PASSGEN_UTF8_SUCCESS); |
181 | 1 | assert(in_pos == (sizeof(input) / sizeof(input[0]))); |
182 | 1 | assert(out_pos == (sizeof(expected) / sizeof(expected[0]))); |
183 | 10 | for(size_t i = 0; i < out_pos; i++9 ) { |
184 | 9 | assert(output[i] == expected[i]); |
185 | 9 | } |
186 | 1 | |
187 | 1 | return test_ok; |
188 | 1 | } |