1 /*
2 * Copyright © 2019 Ebrahim Byagowi
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 */
25
26 #include "hb.hh"
27 #include "hb-number.hh"
28 #include "hb-number-parser.hh"
29
30
31 int
main(int argc,char ** argv)32 main (int argc, char **argv)
33 {
34 {
35 const char str[] = "123";
36 const char *pp = str;
37 const char *end = str + 3;
38
39 int pv;
40 assert (hb_parse_int (&pp, end, &pv));
41 assert (pv == 123);
42 assert (pp - str == 3);
43 assert (end - pp == 0);
44 assert (!*end);
45 }
46
47 {
48 const char str[] = "123";
49 const char *pp = str;
50 const char *end = str + strlen (str);
51
52 unsigned int pv;
53 assert (hb_parse_uint (&pp, end, &pv));
54 assert (pv == 123);
55 assert (pp - str == 3);
56 assert (end - pp == 0);
57 assert (!*end);
58 }
59
60 {
61 const char str[] = "12F";
62 const char *pp = str;
63 const char *end = str + 3;
64
65 unsigned int pv;
66 assert (hb_parse_uint (&pp, end, &pv, true, 16));
67 assert (pv == 0x12F);
68 assert (pp - str == 3);
69 assert (end - pp == 0);
70 assert (!*end);
71 }
72
73 {
74 const char str[] = "12Fq";
75 const char *pp = str;
76 const char *end = str + 4;
77
78 unsigned int pv;
79 assert (!hb_parse_uint (&pp, end, &pv, true, 16));
80 assert (hb_parse_uint (&pp, end, &pv, false, 16));
81 assert (pv == 0x12F);
82 assert (pp - str == 3);
83 assert (end - pp == 1);
84 assert (!*end);
85 }
86
87 {
88 const char str[] = "-123";
89 const char *pp = str;
90 const char *end = str + 4;
91
92 int pv;
93 assert (hb_parse_int (&pp, end, &pv));
94 assert (pv == -123);
95 assert (pp - str == 4);
96 assert (end - pp == 0);
97 assert (!*end);
98 }
99
100 {
101 const char str[] = "123";
102 const char *pp = str;
103 assert (ARRAY_LENGTH (str) == 4);
104 const char *end = str + ARRAY_LENGTH (str);
105
106 unsigned int pv;
107 assert (hb_parse_uint (&pp, end, &pv));
108 assert (pv == 123);
109 assert (pp - str == 3);
110 assert (end - pp == 1);
111 }
112
113 {
114 const char str[] = "123\0";
115 const char *pp = str;
116 assert (ARRAY_LENGTH (str) == 5);
117 const char *end = str + ARRAY_LENGTH (str);
118
119 unsigned int pv;
120 assert (hb_parse_uint (&pp, end, &pv));
121 assert (pv == 123);
122 assert (pp - str == 3);
123 assert (end - pp == 2);
124 }
125
126 {
127 const char str[] = "123V";
128 const char *pp = str;
129 assert (ARRAY_LENGTH (str) == 5);
130 const char *end = str + ARRAY_LENGTH (str);
131
132 unsigned int pv;
133 assert (hb_parse_uint (&pp, end, &pv));
134 assert (pv == 123);
135 assert (pp - str == 3);
136 assert (end - pp == 2);
137 }
138
139 {
140 const char str[] = ".123";
141 const char *pp = str;
142 const char *end = str + ARRAY_LENGTH (str);
143
144 double pv;
145 assert (hb_parse_double (&pp, end, &pv));
146 assert ((int) roundf (pv * 1000.) == 123);
147 assert (pp - str == 4);
148 assert (end - pp == 1);
149
150 /* Test strtod_rl even if libc's strtod_l is used */
151 char *pend;
152 assert ((int) roundf (strtod_rl (str, &pend) * 1000.) == 123);
153 assert (pend - str == 4);
154 }
155
156 {
157 const char str[] = "0.123";
158 const char *pp = str;
159 const char *end = str + ARRAY_LENGTH (str) - 1;
160
161 double pv;
162 assert (hb_parse_double (&pp, end, &pv));
163 assert ((int) roundf (pv * 1000.) == 123);
164 assert (pp - str == 5);
165 assert (end - pp == 0);
166
167 char *pend;
168 assert ((int) roundf (strtod_rl (str, &pend) * 1000.) == 123);
169 assert (pend - str == 5);
170 }
171
172 {
173 const char str[] = "0.123e0";
174 const char *pp = str;
175 const char *end = str + ARRAY_LENGTH (str) - 1;
176
177 double pv;
178 assert (hb_parse_double (&pp, end, &pv));
179 assert ((int) roundf (pv * 1000.) == 123);
180 assert (pp - str == 7);
181 assert (end - pp == 0);
182
183 char *pend;
184 assert ((int) roundf (strtod_rl (str, &pend) * 1000.) == 123);
185 assert (pend - str == 7);
186 }
187
188 {
189 const char str[] = "123e-3";
190 const char *pp = str;
191 const char *end = str + ARRAY_LENGTH (str) - 1;
192
193 double pv;
194 assert (hb_parse_double (&pp, end, &pv));
195 assert ((int) roundf (pv * 1000.) == 123);
196 assert (pp - str == 6);
197 assert (end - pp == 0);
198
199 char *pend;
200 assert ((int) roundf (strtod_rl (str, &pend) * 1000.) == 123);
201 assert (pend - str == 6);
202 }
203
204 {
205 const char str[] = ".000123e+3";
206 const char *pp = str;
207 const char *end = str + ARRAY_LENGTH (str) - 1;
208
209 double pv;
210 assert (hb_parse_double (&pp, end, &pv));
211 assert ((int) roundf (pv * 1000.) == 123);
212 assert (pp - str == 10);
213 assert (end - pp == 0);
214
215 char *pend;
216 assert ((int) roundf (strtod_rl (str, &pend) * 1000.) == 123);
217 assert (pend - str == 10);
218 }
219
220 {
221 const char str[] = "-.000000123e6";
222 const char *pp = str;
223 const char *end = str + ARRAY_LENGTH (str) - 1;
224
225 double pv;
226 assert (hb_parse_double (&pp, end, &pv));
227 assert ((int) roundf (pv * 1000.) == -123);
228 assert (pp - str == 13);
229 assert (end - pp == 0);
230
231 char *pend;
232 assert ((int) roundf (strtod_rl (str, &pend) * 1000.) == -123);
233 assert (pend - str == 13);
234 }
235
236 {
237 const char str[] = "-1.23E-1";
238 const char *pp = str;
239 const char *end = str + ARRAY_LENGTH (str) - 1;
240
241 double pv;
242 assert (hb_parse_double (&pp, end, &pv));
243 assert ((int) roundf (pv * 1000.) == -123);
244 assert (pp - str == 8);
245 assert (end - pp == 0);
246
247 char *pend;
248 assert ((int) roundf (strtod_rl (str, &pend) * 1000.) == -123);
249 assert (pend - str == 8);
250 }
251
252 return 0;
253 }
254