QtBase  v6.3.1
test-number.cc
Go to the documentation of this file.
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 
29 
30 int
31 main (int argc, char **argv)
32 {
33  {
34  const char str[] = "123";
35  const char *pp = str;
36  const char *end = str + 3;
37 
38  int pv;
39  assert (hb_parse_int (&pp, end, &pv));
40  assert (pv == 123);
41  assert (pp - str == 3);
42  assert (end - pp == 0);
43  assert (!*end);
44  }
45 
46  {
47  const char str[] = "123";
48  const char *pp = str;
49  const char *end = str + strlen (str);
50 
51  unsigned int pv;
52  assert (hb_parse_uint (&pp, end, &pv));
53  assert (pv == 123);
54  assert (pp - str == 3);
55  assert (end - pp == 0);
56  assert (!*end);
57  }
58 
59  {
60  const char str[] = "12F";
61  const char *pp = str;
62  const char *end = str + 3;
63 
64  unsigned int pv;
65  assert (hb_parse_uint (&pp, end, &pv, true, 16));
66  assert (pv == 0x12F);
67  assert (pp - str == 3);
68  assert (end - pp == 0);
69  assert (!*end);
70  }
71 
72  {
73  const char str[] = "12Fq";
74  const char *pp = str;
75  const char *end = str + 4;
76 
77  unsigned int pv;
78  assert (!hb_parse_uint (&pp, end, &pv, true, 16));
79  assert (hb_parse_uint (&pp, end, &pv, false, 16));
80  assert (pv == 0x12F);
81  assert (pp - str == 3);
82  assert (end - pp == 1);
83  assert (!*end);
84  }
85 
86  {
87  const char str[] = "-123";
88  const char *pp = str;
89  const char *end = str + 4;
90 
91  int pv;
92  assert (hb_parse_int (&pp, end, &pv));
93  assert (pv == -123);
94  assert (pp - str == 4);
95  assert (end - pp == 0);
96  assert (!*end);
97  }
98 
99  {
100  const char str[] = "123";
101  const char *pp = str;
102  assert (ARRAY_LENGTH (str) == 4);
103  const char *end = str + ARRAY_LENGTH (str);
104 
105  unsigned int pv;
106  assert (hb_parse_uint (&pp, end, &pv));
107  assert (pv == 123);
108  assert (pp - str == 3);
109  assert (end - pp == 1);
110  }
111 
112  {
113  const char str[] = "123\0";
114  const char *pp = str;
115  assert (ARRAY_LENGTH (str) == 5);
116  const char *end = str + ARRAY_LENGTH (str);
117 
118  unsigned int pv;
119  assert (hb_parse_uint (&pp, end, &pv));
120  assert (pv == 123);
121  assert (pp - str == 3);
122  assert (end - pp == 2);
123  }
124 
125  {
126  const char str[] = "123V";
127  const char *pp = str;
128  assert (ARRAY_LENGTH (str) == 5);
129  const char *end = str + ARRAY_LENGTH (str);
130 
131  unsigned int pv;
132  assert (hb_parse_uint (&pp, end, &pv));
133  assert (pv == 123);
134  assert (pp - str == 3);
135  assert (end - pp == 2);
136  }
137 
138  {
139  const char str[] = ".123";
140  const char *pp = str;
141  const char *end = str + ARRAY_LENGTH (str);
142 
143  double pv;
144  assert (hb_parse_double (&pp, end, &pv));
145  assert ((int) roundf (pv * 1000.) == 123);
146  assert (pp - str == 4);
147  assert (end - pp == 1);
148  }
149 
150  {
151  const char str[] = "0.123";
152  const char *pp = str;
153  const char *end = str + ARRAY_LENGTH (str) - 1;
154 
155  double pv;
156  assert (hb_parse_double (&pp, end, &pv));
157  assert ((int) roundf (pv * 1000.) == 123);
158  assert (pp - str == 5);
159  assert (end - pp == 0);
160  }
161 
162  {
163  const char str[] = "0.123e0";
164  const char *pp = str;
165  const char *end = str + ARRAY_LENGTH (str) - 1;
166 
167  double pv;
168  assert (hb_parse_double (&pp, end, &pv));
169  assert ((int) roundf (pv * 1000.) == 123);
170  assert (pp - str == 7);
171  assert (end - pp == 0);
172  }
173 
174  {
175  const char str[] = "123e-3";
176  const char *pp = str;
177  const char *end = str + ARRAY_LENGTH (str) - 1;
178 
179  double pv;
180  assert (hb_parse_double (&pp, end, &pv));
181  assert ((int) roundf (pv * 1000.) == 123);
182  assert (pp - str == 6);
183  assert (end - pp == 0);
184  }
185 
186  {
187  const char str[] = ".000123e+3";
188  const char *pp = str;
189  const char *end = str + ARRAY_LENGTH (str) - 1;
190 
191  double pv;
192  assert (hb_parse_double (&pp, end, &pv));
193  assert ((int) roundf (pv * 1000.) == 123);
194  assert (pp - str == 10);
195  assert (end - pp == 0);
196  }
197 
198  {
199  const char str[] = "-.000000123e6";
200  const char *pp = str;
201  const char *end = str + ARRAY_LENGTH (str) - 1;
202 
203  double pv;
204  assert (hb_parse_double (&pp, end, &pv));
205  assert ((int) roundf (pv * 1000.) == -123);
206  assert (pp - str == 13);
207  assert (end - pp == 0);
208 
209  }
210 
211  {
212  const char str[] = "-1.23E-1";
213  const char *pp = str;
214  const char *end = str + ARRAY_LENGTH (str) - 1;
215 
216  double pv;
217  assert (hb_parse_double (&pp, end, &pv));
218  assert ((int) roundf (pv * 1000.) == -123);
219  assert (pp - str == 8);
220  assert (end - pp == 0);
221  }
222 
223  return 0;
224 }
QString str
[2]
#define roundf(x)
Definition: hb-algs.hh:165
bool hb_parse_int(const char **pp, const char *end, int *pv, bool whole_buffer)
Definition: hb-number.cc:56
bool hb_parse_double(const char **pp, const char *end, double *pv, bool whole_buffer)
Definition: hb-number.cc:73
bool hb_parse_uint(const char **pp, const char *end, unsigned *pv, bool whole_buffer, int base)
Definition: hb-number.cc:64
PCRE2_SIZE PRIV() strlen(PCRE2_SPTR str)
#define assert
Definition: qcborcommon_p.h:63
#define ARRAY_LENGTH(a)
Definition: qkmsdevice.cpp:52
GLuint GLuint end
int main(int argc, char **argv)
[1]
Definition: test-number.cc:31