test_string_util.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <gtest/gtest.h>
31 
33 
34 TEST(StringUtilTests, ToDouble_0)
35 {
36  double value = -1;
37  EXPECT_TRUE(swri_string_util::ToDouble("0", value));
38  EXPECT_FLOAT_EQ(0, value);
39 
40  value = -1;
41  EXPECT_TRUE(swri_string_util::ToDouble(".0", value));
42  EXPECT_FLOAT_EQ(0, value);
43 
44  value = -1;
45  EXPECT_TRUE(swri_string_util::ToDouble("0.0", value));
46  EXPECT_FLOAT_EQ(0, value);
47 
48  value = -1;
49  EXPECT_TRUE(swri_string_util::ToDouble("0.00", value));
50  EXPECT_FLOAT_EQ(0, value);
51 
52  value = -1;
53  EXPECT_TRUE(swri_string_util::ToDouble("0.0e0", value));
54  EXPECT_FLOAT_EQ(0, value);
55 
56  value = -1;
57  EXPECT_TRUE(swri_string_util::ToDouble("0.0e-0", value));
58  EXPECT_FLOAT_EQ(0, value);
59 
60  value = -1;
61  EXPECT_TRUE(swri_string_util::ToDouble("0.0e+0", value));
62  EXPECT_FLOAT_EQ(0, value);
63 
64  value = -1;
65  EXPECT_TRUE(swri_string_util::ToDouble("+0", value));
66  EXPECT_FLOAT_EQ(0, value);
67 
68  value = -1;
69  EXPECT_TRUE(swri_string_util::ToDouble("+.0", value));
70  EXPECT_FLOAT_EQ(0, value);
71 
72  value = -1;
73  EXPECT_TRUE(swri_string_util::ToDouble("+0.0", value));
74  EXPECT_FLOAT_EQ(0, value);
75 
76  value = -1;
77  EXPECT_TRUE(swri_string_util::ToDouble("+0.00", value));
78  EXPECT_FLOAT_EQ(0, value);
79 
80  value = -1;
81  EXPECT_TRUE(swri_string_util::ToDouble("+0.0e0", value));
82  EXPECT_FLOAT_EQ(0, value);
83 
84  value = -1;
85  EXPECT_TRUE(swri_string_util::ToDouble("+0.0e-0", value));
86  EXPECT_FLOAT_EQ(0, value);
87 
88  value = -1;
89  EXPECT_TRUE(swri_string_util::ToDouble("+0.0e+0", value));
90  EXPECT_FLOAT_EQ(0, value);
91 
92  value = -1;
93  EXPECT_TRUE(swri_string_util::ToDouble("-0", value));
94  EXPECT_FLOAT_EQ(0, value);
95 
96  value = -1;
97  EXPECT_TRUE(swri_string_util::ToDouble("-.0", value));
98  EXPECT_FLOAT_EQ(0, value);
99 
100  value = -1;
101  EXPECT_TRUE(swri_string_util::ToDouble("-0.0", value));
102  EXPECT_FLOAT_EQ(0, value);
103 
104  value = -1;
105  EXPECT_TRUE(swri_string_util::ToDouble("-0.00", value));
106  EXPECT_FLOAT_EQ(0, value);
107 
108  value = -1;
109  EXPECT_TRUE(swri_string_util::ToDouble("-0.0e0", value));
110  EXPECT_FLOAT_EQ(0, value);
111 
112  value = -1;
113  EXPECT_TRUE(swri_string_util::ToDouble("-0.0e-0", value));
114  EXPECT_FLOAT_EQ(0, value);
115 
116  value = -1;
117  EXPECT_TRUE(swri_string_util::ToDouble("-0.0e+0", value));
118  EXPECT_FLOAT_EQ(0, value);
119 
120  value = -1;
121  EXPECT_TRUE(swri_string_util::ToDouble(" 0", value));
122  EXPECT_FLOAT_EQ(0, value);
123 
124  value = -1;
125  EXPECT_TRUE(swri_string_util::ToDouble(" .0", value));
126  EXPECT_FLOAT_EQ(0, value);
127 
128  value = -1;
129  EXPECT_TRUE(swri_string_util::ToDouble(" 0.0", value));
130  EXPECT_FLOAT_EQ(0, value);
131 
132  value = -1;
133  EXPECT_TRUE(swri_string_util::ToDouble(" 0.00", value));
134  EXPECT_FLOAT_EQ(0, value);
135 
136  value = -1;
137  EXPECT_TRUE(swri_string_util::ToDouble(" 0.0e0", value));
138  EXPECT_FLOAT_EQ(0, value);
139 
140  value = -1;
141  EXPECT_TRUE(swri_string_util::ToDouble(" 0.0e-0", value));
142  EXPECT_FLOAT_EQ(0, value);
143 
144  value = -1;
145  EXPECT_TRUE(swri_string_util::ToDouble(" 0.0e+0", value));
146  EXPECT_FLOAT_EQ(0, value);
147 
148  value = -1;
149  EXPECT_TRUE(swri_string_util::ToDouble(" +0", value));
150  EXPECT_FLOAT_EQ(0, value);
151 
152  value = -1;
153  EXPECT_TRUE(swri_string_util::ToDouble(" +.0", value));
154  EXPECT_FLOAT_EQ(0, value);
155 
156  value = -1;
157  EXPECT_TRUE(swri_string_util::ToDouble(" +0.0", value));
158  EXPECT_FLOAT_EQ(0, value);
159 
160  value = -1;
161  EXPECT_TRUE(swri_string_util::ToDouble(" +0.00", value));
162  EXPECT_FLOAT_EQ(0, value);
163 
164  value = -1;
165  EXPECT_TRUE(swri_string_util::ToDouble(" +0.0e0", value));
166  EXPECT_FLOAT_EQ(0, value);
167 
168  value = -1;
169  EXPECT_TRUE(swri_string_util::ToDouble(" +0.0e-0", value));
170  EXPECT_FLOAT_EQ(0, value);
171 
172  value = -1;
173  EXPECT_TRUE(swri_string_util::ToDouble(" +0.0e+0", value));
174  EXPECT_FLOAT_EQ(0, value);
175 
176  value = -1;
177  EXPECT_TRUE(swri_string_util::ToDouble(" -0", value));
178  EXPECT_FLOAT_EQ(0, value);
179 
180  value = -1;
181  EXPECT_TRUE(swri_string_util::ToDouble(" -.0", value));
182  EXPECT_FLOAT_EQ(0, value);
183 
184  value = -1;
185  EXPECT_TRUE(swri_string_util::ToDouble(" -0.0", value));
186  EXPECT_FLOAT_EQ(0, value);
187 
188  value = -1;
189  EXPECT_TRUE(swri_string_util::ToDouble(" -0.00", value));
190  EXPECT_FLOAT_EQ(0, value);
191 
192  value = -1;
193  EXPECT_TRUE(swri_string_util::ToDouble(" -0.0e0", value));
194  EXPECT_FLOAT_EQ(0, value);
195 
196  value = -1;
197  EXPECT_TRUE(swri_string_util::ToDouble(" -0.0e-0", value));
198  EXPECT_FLOAT_EQ(0, value);
199 
200  value = -1;
201  EXPECT_TRUE(swri_string_util::ToDouble(" -0.0e+0", value));
202  EXPECT_FLOAT_EQ(0, value);
203 }
204 
205 TEST(StringUtilTests, ToDouble_1)
206 {
207  double value = 0;
208  EXPECT_TRUE(swri_string_util::ToDouble("1", value));
209  EXPECT_FLOAT_EQ(1, value);
210 
211  value = 0;
212  EXPECT_TRUE(swri_string_util::ToDouble("1.", value));
213  EXPECT_FLOAT_EQ(1, value);
214 
215  value = 0;
216  EXPECT_TRUE(swri_string_util::ToDouble("1.0", value));
217  EXPECT_FLOAT_EQ(1, value);
218 
219  value = 0;
220  EXPECT_TRUE(swri_string_util::ToDouble("1.00", value));
221  EXPECT_FLOAT_EQ(1, value);
222 
223  value = 0;
224  EXPECT_TRUE(swri_string_util::ToDouble("1.0e0", value));
225  EXPECT_FLOAT_EQ(1, value);
226 
227  value = 0;
228  EXPECT_TRUE(swri_string_util::ToDouble("1.0e-0", value));
229  EXPECT_FLOAT_EQ(1, value);
230 
231  value = 0;
232  EXPECT_TRUE(swri_string_util::ToDouble("1.0e+0", value));
233  EXPECT_FLOAT_EQ(1, value);
234 
235  value = 0;
236  EXPECT_TRUE(swri_string_util::ToDouble("+1", value));
237  EXPECT_FLOAT_EQ(1, value);
238 
239  value = 0;
240  EXPECT_TRUE(swri_string_util::ToDouble("+1.", value));
241  EXPECT_FLOAT_EQ(1, value);
242 
243  value = 0;
244  EXPECT_TRUE(swri_string_util::ToDouble("+1.0", value));
245  EXPECT_FLOAT_EQ(1, value);
246 
247  value = 0;
248  EXPECT_TRUE(swri_string_util::ToDouble("+1.00", value));
249  EXPECT_FLOAT_EQ(1, value);
250 
251  value = 0;
252  EXPECT_TRUE(swri_string_util::ToDouble("+1.0e0", value));
253  EXPECT_FLOAT_EQ(1, value);
254 
255  value = 0;
256  EXPECT_TRUE(swri_string_util::ToDouble("+1.0e-0", value));
257  EXPECT_FLOAT_EQ(1, value);
258 
259  value = 0;
260  EXPECT_TRUE(swri_string_util::ToDouble("+1.0e+0", value));
261  EXPECT_FLOAT_EQ(1, value);
262 
263  value = 0;
264  EXPECT_TRUE(swri_string_util::ToDouble("-1", value));
265  EXPECT_FLOAT_EQ(-1, value);
266 
267  value = 0;
268  EXPECT_TRUE(swri_string_util::ToDouble("-1.", value));
269  EXPECT_FLOAT_EQ(-1, value);
270 
271  value = 0;
272  EXPECT_TRUE(swri_string_util::ToDouble("-1.0", value));
273  EXPECT_FLOAT_EQ(-1, value);
274 
275  value = 0;
276  EXPECT_TRUE(swri_string_util::ToDouble("-1.00", value));
277  EXPECT_FLOAT_EQ(-1, value);
278 
279  value = 0;
280  EXPECT_TRUE(swri_string_util::ToDouble("-1.0e0", value));
281  EXPECT_FLOAT_EQ(-1, value);
282 
283  value = 0;
284  EXPECT_TRUE(swri_string_util::ToDouble("-1.0e-0", value));
285  EXPECT_FLOAT_EQ(-1, value);
286 
287  value = 0;
288  EXPECT_TRUE(swri_string_util::ToDouble("-1.0e+0", value));
289  EXPECT_FLOAT_EQ(-1, value);
290 
291  value = 0;
292  EXPECT_TRUE(swri_string_util::ToDouble(" 1", value));
293  EXPECT_FLOAT_EQ(1, value);
294 
295  value = 0;
296  EXPECT_TRUE(swri_string_util::ToDouble(" 1.", value));
297  EXPECT_FLOAT_EQ(1, value);
298 
299  value = 0;
300  EXPECT_TRUE(swri_string_util::ToDouble(" 1.0", value));
301  EXPECT_FLOAT_EQ(1, value);
302 
303  value = 0;
304  EXPECT_TRUE(swri_string_util::ToDouble(" 1.00", value));
305  EXPECT_FLOAT_EQ(1, value);
306 
307  value = 0;
308  EXPECT_TRUE(swri_string_util::ToDouble(" 1.0e0", value));
309  EXPECT_FLOAT_EQ(1, value);
310 
311  value = 0;
312  EXPECT_TRUE(swri_string_util::ToDouble(" 1.0e-0", value));
313  EXPECT_FLOAT_EQ(1, value);
314 
315  value = 0;
316  EXPECT_TRUE(swri_string_util::ToDouble(" 1.0e+0", value));
317  EXPECT_FLOAT_EQ(1, value);
318 
319  value = 0;
320  EXPECT_TRUE(swri_string_util::ToDouble(" +1", value));
321  EXPECT_FLOAT_EQ(1, value);
322 
323  value = 0;
324  EXPECT_TRUE(swri_string_util::ToDouble(" +1.", value));
325  EXPECT_FLOAT_EQ(1, value);
326 
327  value = 0;
328  EXPECT_TRUE(swri_string_util::ToDouble(" +1.0", value));
329  EXPECT_FLOAT_EQ(1, value);
330 
331  value = 0;
332  EXPECT_TRUE(swri_string_util::ToDouble(" +1.00", value));
333  EXPECT_FLOAT_EQ(1, value);
334 
335  value = 0;
336  EXPECT_TRUE(swri_string_util::ToDouble(" +1.0e0", value));
337  EXPECT_FLOAT_EQ(1, value);
338 
339  value = 0;
340  EXPECT_TRUE(swri_string_util::ToDouble(" +1.0e-0", value));
341  EXPECT_FLOAT_EQ(1, value);
342 
343  value = 0;
344  EXPECT_TRUE(swri_string_util::ToDouble(" +1.0e+0", value));
345  EXPECT_FLOAT_EQ(1, value);
346 
347  value = 0;
348  EXPECT_TRUE(swri_string_util::ToDouble(" -1", value));
349  EXPECT_FLOAT_EQ(-1, value);
350 
351  value = 0;
352  EXPECT_TRUE(swri_string_util::ToDouble(" -1.", value));
353  EXPECT_FLOAT_EQ(-1, value);
354 
355  value = 0;
356  EXPECT_TRUE(swri_string_util::ToDouble(" -1.0", value));
357  EXPECT_FLOAT_EQ(-1, value);
358 
359  value = 0;
360  EXPECT_TRUE(swri_string_util::ToDouble(" -1.00", value));
361  EXPECT_FLOAT_EQ(-1, value);
362 
363  value = 0;
364  EXPECT_TRUE(swri_string_util::ToDouble(" -1.0e0", value));
365  EXPECT_FLOAT_EQ(-1, value);
366 
367  value = 0;
368  EXPECT_TRUE(swri_string_util::ToDouble(" -1.0e-0", value));
369  EXPECT_FLOAT_EQ(-1, value);
370 
371  value = 0;
372  EXPECT_TRUE(swri_string_util::ToDouble(" -1.0e+0", value));
373  EXPECT_FLOAT_EQ(-1, value);
374 
375  value = 0;
376  EXPECT_TRUE(swri_string_util::ToDouble(" 10.0e-1", value));
377  EXPECT_FLOAT_EQ(1, value);
378 
379  value = 0;
380  EXPECT_TRUE(swri_string_util::ToDouble(" 100.0e-2", value));
381  EXPECT_FLOAT_EQ(1, value);
382 
383  value = 0;
384  EXPECT_TRUE(swri_string_util::ToDouble(" 0.1e1", value));
385  EXPECT_FLOAT_EQ(1, value);
386 
387  value = 0;
388  EXPECT_TRUE(swri_string_util::ToDouble(" 0.1e+1", value));
389  EXPECT_FLOAT_EQ(1, value);
390 
391  value = 0;
392  EXPECT_TRUE(swri_string_util::ToDouble(" 0.01e+2", value));
393  EXPECT_FLOAT_EQ(1, value);
394 }
395 
396 TEST(StringUtilTests, ToDouble)
397 {
398  double value = 0;
399  EXPECT_TRUE(swri_string_util::ToDouble("0.123456789", value));
400  EXPECT_FLOAT_EQ(0.123456789, value);
401 
402  value = 0;
403  EXPECT_TRUE(swri_string_util::ToDouble("123456789.", value));
404  EXPECT_FLOAT_EQ(123456789, value);
405 
406  value = 0;
407  EXPECT_TRUE(swri_string_util::ToDouble("12345678.9", value));
408  EXPECT_FLOAT_EQ(12345678.9, value);
409 
410  value = 0;
411  EXPECT_TRUE(swri_string_util::ToDouble("123456789.00", value));
412  EXPECT_FLOAT_EQ(123456789, value);
413 
414  value = 0;
415  EXPECT_TRUE(swri_string_util::ToDouble("12345.6789", value));
416  EXPECT_FLOAT_EQ(12345.6789, value);
417 
418  value = 0;
419  EXPECT_TRUE(swri_string_util::ToDouble("1e308", value));
420  EXPECT_FLOAT_EQ(1e308, value);
421 
422  value = 0;
423  EXPECT_TRUE(swri_string_util::ToDouble("1e-307", value));
424  EXPECT_FLOAT_EQ(1e-307, value);
425 }
426 
427 TEST(StringUtilTests, ToDoubleInvalid)
428 {
429  double value = 0;
430 
431  EXPECT_FALSE(swri_string_util::ToDouble("a", value));
432  EXPECT_FALSE(swri_string_util::ToDouble("a0", value));
433  EXPECT_FALSE(swri_string_util::ToDouble("0a", value));
434  EXPECT_FALSE(swri_string_util::ToDouble("0 ", value));
435  EXPECT_FALSE(swri_string_util::ToDouble("1e309", value));
436  EXPECT_FALSE(swri_string_util::ToDouble("1e-308", value));
437 }
438 
439 TEST(StringUtilTests, ToFloat_0)
440 {
441  float value = -1;
442  EXPECT_TRUE(swri_string_util::ToFloat("0", value));
443  EXPECT_FLOAT_EQ(0, value);
444 
445  value = -1;
446  EXPECT_TRUE(swri_string_util::ToFloat(".0", value));
447  EXPECT_FLOAT_EQ(0, value);
448 
449  value = -1;
450  EXPECT_TRUE(swri_string_util::ToFloat("0.0", value));
451  EXPECT_FLOAT_EQ(0, value);
452 
453  value = -1;
454  EXPECT_TRUE(swri_string_util::ToFloat("0.00", value));
455  EXPECT_FLOAT_EQ(0, value);
456 
457  value = -1;
458  EXPECT_TRUE(swri_string_util::ToFloat("0.0e0", value));
459  EXPECT_FLOAT_EQ(0, value);
460 
461  value = -1;
462  EXPECT_TRUE(swri_string_util::ToFloat("0.0e-0", value));
463  EXPECT_FLOAT_EQ(0, value);
464 
465  value = -1;
466  EXPECT_TRUE(swri_string_util::ToFloat("0.0e+0", value));
467  EXPECT_FLOAT_EQ(0, value);
468 
469  value = -1;
470  EXPECT_TRUE(swri_string_util::ToFloat("+0", value));
471  EXPECT_FLOAT_EQ(0, value);
472 
473  value = -1;
474  EXPECT_TRUE(swri_string_util::ToFloat("+.0", value));
475  EXPECT_FLOAT_EQ(0, value);
476 
477  value = -1;
478  EXPECT_TRUE(swri_string_util::ToFloat("+0.0", value));
479  EXPECT_FLOAT_EQ(0, value);
480 
481  value = -1;
482  EXPECT_TRUE(swri_string_util::ToFloat("+0.00", value));
483  EXPECT_FLOAT_EQ(0, value);
484 
485  value = -1;
486  EXPECT_TRUE(swri_string_util::ToFloat("+0.0e0", value));
487  EXPECT_FLOAT_EQ(0, value);
488 
489  value = -1;
490  EXPECT_TRUE(swri_string_util::ToFloat("+0.0e-0", value));
491  EXPECT_FLOAT_EQ(0, value);
492 
493  value = -1;
494  EXPECT_TRUE(swri_string_util::ToFloat("+0.0e+0", value));
495  EXPECT_FLOAT_EQ(0, value);
496 
497  value = -1;
498  EXPECT_TRUE(swri_string_util::ToFloat("-0", value));
499  EXPECT_FLOAT_EQ(0, value);
500 
501  value = -1;
502  EXPECT_TRUE(swri_string_util::ToFloat("-.0", value));
503  EXPECT_FLOAT_EQ(0, value);
504 
505  value = -1;
506  EXPECT_TRUE(swri_string_util::ToFloat("-0.0", value));
507  EXPECT_FLOAT_EQ(0, value);
508 
509  value = -1;
510  EXPECT_TRUE(swri_string_util::ToFloat("-0.00", value));
511  EXPECT_FLOAT_EQ(0, value);
512 
513  value = -1;
514  EXPECT_TRUE(swri_string_util::ToFloat("-0.0e0", value));
515  EXPECT_FLOAT_EQ(0, value);
516 
517  value = -1;
518  EXPECT_TRUE(swri_string_util::ToFloat("-0.0e-0", value));
519  EXPECT_FLOAT_EQ(0, value);
520 
521  value = -1;
522  EXPECT_TRUE(swri_string_util::ToFloat("-0.0e+0", value));
523  EXPECT_FLOAT_EQ(0, value);
524 
525  value = -1;
526  EXPECT_TRUE(swri_string_util::ToFloat(" 0", value));
527  EXPECT_FLOAT_EQ(0, value);
528 
529  value = -1;
530  EXPECT_TRUE(swri_string_util::ToFloat(" .0", value));
531  EXPECT_FLOAT_EQ(0, value);
532 
533  value = -1;
534  EXPECT_TRUE(swri_string_util::ToFloat(" 0.0", value));
535  EXPECT_FLOAT_EQ(0, value);
536 
537  value = -1;
538  EXPECT_TRUE(swri_string_util::ToFloat(" 0.00", value));
539  EXPECT_FLOAT_EQ(0, value);
540 
541  value = -1;
542  EXPECT_TRUE(swri_string_util::ToFloat(" 0.0e0", value));
543  EXPECT_FLOAT_EQ(0, value);
544 
545  value = -1;
546  EXPECT_TRUE(swri_string_util::ToFloat(" 0.0e-0", value));
547  EXPECT_FLOAT_EQ(0, value);
548 
549  value = -1;
550  EXPECT_TRUE(swri_string_util::ToFloat(" 0.0e+0", value));
551  EXPECT_FLOAT_EQ(0, value);
552 
553  value = -1;
554  EXPECT_TRUE(swri_string_util::ToFloat(" +0", value));
555  EXPECT_FLOAT_EQ(0, value);
556 
557  value = -1;
558  EXPECT_TRUE(swri_string_util::ToFloat(" +.0", value));
559  EXPECT_FLOAT_EQ(0, value);
560 
561  value = -1;
562  EXPECT_TRUE(swri_string_util::ToFloat(" +0.0", value));
563  EXPECT_FLOAT_EQ(0, value);
564 
565  value = -1;
566  EXPECT_TRUE(swri_string_util::ToFloat(" +0.00", value));
567  EXPECT_FLOAT_EQ(0, value);
568 
569  value = -1;
570  EXPECT_TRUE(swri_string_util::ToFloat(" +0.0e0", value));
571  EXPECT_FLOAT_EQ(0, value);
572 
573  value = -1;
574  EXPECT_TRUE(swri_string_util::ToFloat(" +0.0e-0", value));
575  EXPECT_FLOAT_EQ(0, value);
576 
577  value = -1;
578  EXPECT_TRUE(swri_string_util::ToFloat(" +0.0e+0", value));
579  EXPECT_FLOAT_EQ(0, value);
580 
581  value = -1;
582  EXPECT_TRUE(swri_string_util::ToFloat(" -0", value));
583  EXPECT_FLOAT_EQ(0, value);
584 
585  value = -1;
586  EXPECT_TRUE(swri_string_util::ToFloat(" -.0", value));
587  EXPECT_FLOAT_EQ(0, value);
588 
589  value = -1;
590  EXPECT_TRUE(swri_string_util::ToFloat(" -0.0", value));
591  EXPECT_FLOAT_EQ(0, value);
592 
593  value = -1;
594  EXPECT_TRUE(swri_string_util::ToFloat(" -0.00", value));
595  EXPECT_FLOAT_EQ(0, value);
596 
597  value = -1;
598  EXPECT_TRUE(swri_string_util::ToFloat(" -0.0e0", value));
599  EXPECT_FLOAT_EQ(0, value);
600 
601  value = -1;
602  EXPECT_TRUE(swri_string_util::ToFloat(" -0.0e-0", value));
603  EXPECT_FLOAT_EQ(0, value);
604 
605  value = -1;
606  EXPECT_TRUE(swri_string_util::ToFloat(" -0.0e+0", value));
607  EXPECT_FLOAT_EQ(0, value);
608 }
609 
610 TEST(StringUtilTests, ToFloat_1)
611 {
612  float value = 0;
613  EXPECT_TRUE(swri_string_util::ToFloat("1", value));
614  EXPECT_FLOAT_EQ(1, value);
615 
616  value = 0;
617  EXPECT_TRUE(swri_string_util::ToFloat("1.", value));
618  EXPECT_FLOAT_EQ(1, value);
619 
620  value = 0;
621  EXPECT_TRUE(swri_string_util::ToFloat("1.0", value));
622  EXPECT_FLOAT_EQ(1, value);
623 
624  value = 0;
625  EXPECT_TRUE(swri_string_util::ToFloat("1.00", value));
626  EXPECT_FLOAT_EQ(1, value);
627 
628  value = 0;
629  EXPECT_TRUE(swri_string_util::ToFloat("1.0e0", value));
630  EXPECT_FLOAT_EQ(1, value);
631 
632  value = 0;
633  EXPECT_TRUE(swri_string_util::ToFloat("1.0e-0", value));
634  EXPECT_FLOAT_EQ(1, value);
635 
636  value = 0;
637  EXPECT_TRUE(swri_string_util::ToFloat("1.0e+0", value));
638  EXPECT_FLOAT_EQ(1, value);
639 
640  value = 0;
641  EXPECT_TRUE(swri_string_util::ToFloat("+1", value));
642  EXPECT_FLOAT_EQ(1, value);
643 
644  value = 0;
645  EXPECT_TRUE(swri_string_util::ToFloat("+1.", value));
646  EXPECT_FLOAT_EQ(1, value);
647 
648  value = 0;
649  EXPECT_TRUE(swri_string_util::ToFloat("+1.0", value));
650  EXPECT_FLOAT_EQ(1, value);
651 
652  value = 0;
653  EXPECT_TRUE(swri_string_util::ToFloat("+1.00", value));
654  EXPECT_FLOAT_EQ(1, value);
655 
656  value = 0;
657  EXPECT_TRUE(swri_string_util::ToFloat("+1.0e0", value));
658  EXPECT_FLOAT_EQ(1, value);
659 
660  value = 0;
661  EXPECT_TRUE(swri_string_util::ToFloat("+1.0e-0", value));
662  EXPECT_FLOAT_EQ(1, value);
663 
664  value = 0;
665  EXPECT_TRUE(swri_string_util::ToFloat("+1.0e+0", value));
666  EXPECT_FLOAT_EQ(1, value);
667 
668  value = 0;
669  EXPECT_TRUE(swri_string_util::ToFloat("-1", value));
670  EXPECT_FLOAT_EQ(-1, value);
671 
672  value = 0;
673  EXPECT_TRUE(swri_string_util::ToFloat("-1.", value));
674  EXPECT_FLOAT_EQ(-1, value);
675 
676  value = 0;
677  EXPECT_TRUE(swri_string_util::ToFloat("-1.0", value));
678  EXPECT_FLOAT_EQ(-1, value);
679 
680  value = 0;
681  EXPECT_TRUE(swri_string_util::ToFloat("-1.00", value));
682  EXPECT_FLOAT_EQ(-1, value);
683 
684  value = 0;
685  EXPECT_TRUE(swri_string_util::ToFloat("-1.0e0", value));
686  EXPECT_FLOAT_EQ(-1, value);
687 
688  value = 0;
689  EXPECT_TRUE(swri_string_util::ToFloat("-1.0e-0", value));
690  EXPECT_FLOAT_EQ(-1, value);
691 
692  value = 0;
693  EXPECT_TRUE(swri_string_util::ToFloat("-1.0e+0", value));
694  EXPECT_FLOAT_EQ(-1, value);
695 
696  value = 0;
697  EXPECT_TRUE(swri_string_util::ToFloat(" 1", value));
698  EXPECT_FLOAT_EQ(1, value);
699 
700  value = 0;
701  EXPECT_TRUE(swri_string_util::ToFloat(" 1.", value));
702  EXPECT_FLOAT_EQ(1, value);
703 
704  value = 0;
705  EXPECT_TRUE(swri_string_util::ToFloat(" 1.0", value));
706  EXPECT_FLOAT_EQ(1, value);
707 
708  value = 0;
709  EXPECT_TRUE(swri_string_util::ToFloat(" 1.00", value));
710  EXPECT_FLOAT_EQ(1, value);
711 
712  value = 0;
713  EXPECT_TRUE(swri_string_util::ToFloat(" 1.0e0", value));
714  EXPECT_FLOAT_EQ(1, value);
715 
716  value = 0;
717  EXPECT_TRUE(swri_string_util::ToFloat(" 1.0e-0", value));
718  EXPECT_FLOAT_EQ(1, value);
719 
720  value = 0;
721  EXPECT_TRUE(swri_string_util::ToFloat(" 1.0e+0", value));
722  EXPECT_FLOAT_EQ(1, value);
723 
724  value = 0;
725  EXPECT_TRUE(swri_string_util::ToFloat(" +1", value));
726  EXPECT_FLOAT_EQ(1, value);
727 
728  value = 0;
729  EXPECT_TRUE(swri_string_util::ToFloat(" +1.", value));
730  EXPECT_FLOAT_EQ(1, value);
731 
732  value = 0;
733  EXPECT_TRUE(swri_string_util::ToFloat(" +1.0", value));
734  EXPECT_FLOAT_EQ(1, value);
735 
736  value = 0;
737  EXPECT_TRUE(swri_string_util::ToFloat(" +1.00", value));
738  EXPECT_FLOAT_EQ(1, value);
739 
740  value = 0;
741  EXPECT_TRUE(swri_string_util::ToFloat(" +1.0e0", value));
742  EXPECT_FLOAT_EQ(1, value);
743 
744  value = 0;
745  EXPECT_TRUE(swri_string_util::ToFloat(" +1.0e-0", value));
746  EXPECT_FLOAT_EQ(1, value);
747 
748  value = 0;
749  EXPECT_TRUE(swri_string_util::ToFloat(" +1.0e+0", value));
750  EXPECT_FLOAT_EQ(1, value);
751 
752  value = 0;
753  EXPECT_TRUE(swri_string_util::ToFloat(" -1", value));
754  EXPECT_FLOAT_EQ(-1, value);
755 
756  value = 0;
757  EXPECT_TRUE(swri_string_util::ToFloat(" -1.", value));
758  EXPECT_FLOAT_EQ(-1, value);
759 
760  value = 0;
761  EXPECT_TRUE(swri_string_util::ToFloat(" -1.0", value));
762  EXPECT_FLOAT_EQ(-1, value);
763 
764  value = 0;
765  EXPECT_TRUE(swri_string_util::ToFloat(" -1.00", value));
766  EXPECT_FLOAT_EQ(-1, value);
767 
768  value = 0;
769  EXPECT_TRUE(swri_string_util::ToFloat(" -1.0e0", value));
770  EXPECT_FLOAT_EQ(-1, value);
771 
772  value = 0;
773  EXPECT_TRUE(swri_string_util::ToFloat(" -1.0e-0", value));
774  EXPECT_FLOAT_EQ(-1, value);
775 
776  value = 0;
777  EXPECT_TRUE(swri_string_util::ToFloat(" -1.0e+0", value));
778  EXPECT_FLOAT_EQ(-1, value);
779 
780  value = 0;
781  EXPECT_TRUE(swri_string_util::ToFloat(" 10.0e-1", value));
782  EXPECT_FLOAT_EQ(1, value);
783 
784  value = 0;
785  EXPECT_TRUE(swri_string_util::ToFloat(" 100.0e-2", value));
786  EXPECT_FLOAT_EQ(1, value);
787 
788  value = 0;
789  EXPECT_TRUE(swri_string_util::ToFloat(" 0.1e1", value));
790  EXPECT_FLOAT_EQ(1, value);
791 
792  value = 0;
793  EXPECT_TRUE(swri_string_util::ToFloat(" 0.1e+1", value));
794  EXPECT_FLOAT_EQ(1, value);
795 
796  value = 0;
797  EXPECT_TRUE(swri_string_util::ToFloat(" 0.01e+2", value));
798  EXPECT_FLOAT_EQ(1, value);
799 }
800 
801 TEST(StringUtilTests, ToFloat)
802 {
803  float value = 0;
804  EXPECT_TRUE(swri_string_util::ToFloat("0.123456789", value));
805  EXPECT_FLOAT_EQ(0.123456789, value);
806 
807  value = 0;
808  EXPECT_TRUE(swri_string_util::ToFloat("123456789.", value));
809  EXPECT_FLOAT_EQ(123456789, value);
810 
811  value = 0;
812  EXPECT_TRUE(swri_string_util::ToFloat("12345678.9", value));
813  EXPECT_FLOAT_EQ(12345678.9, value);
814 
815  value = 0;
816  EXPECT_TRUE(swri_string_util::ToFloat("123456789.00", value));
817  EXPECT_FLOAT_EQ(123456789, value);
818 
819  value = 0;
820  EXPECT_TRUE(swri_string_util::ToFloat("12345.6789", value));
821  EXPECT_FLOAT_EQ(12345.6789, value);
822 
823  value = 0;
824  EXPECT_TRUE(swri_string_util::ToFloat("1e38", value));
825  EXPECT_FLOAT_EQ(1e38, value);
826 
827  value = 0;
828  EXPECT_TRUE(swri_string_util::ToFloat("1e-37", value));
829  EXPECT_FLOAT_EQ(1e-37, value);
830 }
831 
832 TEST(StringUtilTests, ToFloatInvalid)
833 {
834  float value = 0;
835 
836  EXPECT_FALSE(swri_string_util::ToFloat("", value));
837  EXPECT_FALSE(swri_string_util::ToFloat(" ", value));
838  EXPECT_FALSE(swri_string_util::ToFloat("a", value));
839  EXPECT_FALSE(swri_string_util::ToFloat("a0", value));
840  EXPECT_FALSE(swri_string_util::ToFloat("0a", value));
841  EXPECT_FALSE(swri_string_util::ToFloat("0 ", value));
842  EXPECT_FALSE(swri_string_util::ToFloat("1e39", value));
843  EXPECT_FALSE(swri_string_util::ToFloat("1e-38", value));
844 }
845 
846 TEST(StringUtilTests, ToInt32Base10)
847 {
848  int32_t value = 1;
849  EXPECT_TRUE(swri_string_util::ToInt32("0", value));
850  EXPECT_EQ(0, value);
851 
852  EXPECT_TRUE(swri_string_util::ToInt32("00", value));
853  EXPECT_EQ(0, value);
854 
855  value = 1;
856  EXPECT_TRUE(swri_string_util::ToInt32("-0", value));
857  EXPECT_EQ(0, value);
858 
859  value = 1;
860  EXPECT_TRUE(swri_string_util::ToInt32("+0", value));
861  EXPECT_EQ(0, value);
862 
863  value = 0;
864  EXPECT_TRUE(swri_string_util::ToInt32("1", value));
865  EXPECT_EQ(1, value);
866 
867  value = 0;
868  EXPECT_TRUE(swri_string_util::ToInt32("+1", value));
869  EXPECT_EQ(1, value);
870 
871  value = 0;
872  EXPECT_TRUE(swri_string_util::ToInt32("-1", value));
873  EXPECT_EQ(-1, value);
874 
875  value = 0;
876  EXPECT_TRUE(swri_string_util::ToInt32("12345678", value));
877  EXPECT_EQ(12345678, value);
878 
879  value = 0;
880  EXPECT_TRUE(swri_string_util::ToInt32("012345678", value));
881  EXPECT_EQ(12345678, value);
882 
883  value = 0;
884  EXPECT_TRUE(swri_string_util::ToInt32("-123456789", value));
885  EXPECT_EQ(-123456789, value);
886 
887  value = 0;
888  EXPECT_TRUE(swri_string_util::ToInt32("2147483647", value));
889  EXPECT_EQ(2147483647, value);
890 }
891 
892 TEST(StringUtilTests, ToInt32Base10Invalid)
893 {
894  int32_t value = 0;
895 
896  EXPECT_FALSE(swri_string_util::ToInt32("", value));
897  EXPECT_FALSE(swri_string_util::ToInt32(" ", value));
898  EXPECT_FALSE(swri_string_util::ToInt32("a", value));
899  EXPECT_FALSE(swri_string_util::ToInt32("a0", value));
900  EXPECT_FALSE(swri_string_util::ToInt32("0a", value));
901  EXPECT_FALSE(swri_string_util::ToInt32("0 ", value));
902  EXPECT_FALSE(swri_string_util::ToInt32("1e39", value));
903  EXPECT_FALSE(swri_string_util::ToInt32("1e-38", value));
904  EXPECT_FALSE(swri_string_util::ToInt32("1e1", value));
905  EXPECT_FALSE(swri_string_util::ToInt32("1e0", value));
906  EXPECT_FALSE(swri_string_util::ToInt32(".", value));
907  EXPECT_FALSE(swri_string_util::ToInt32("0.0", value));
908  EXPECT_FALSE(swri_string_util::ToInt32("0.", value));
909  EXPECT_FALSE(swri_string_util::ToInt32("2147483648", value));
910 }
911 
912 TEST(StringUtilTests, ToInt32Base16)
913 {
914  int32_t value = 1;
915  EXPECT_TRUE(swri_string_util::ToInt32("0", value, 16));
916  EXPECT_EQ(0, value);
917 
918  EXPECT_TRUE(swri_string_util::ToInt32("00", value, 16));
919  EXPECT_EQ(0, value);
920 
921  value = 1;
922  EXPECT_TRUE(swri_string_util::ToInt32("-0", value, 16));
923  EXPECT_EQ(0, value);
924 
925  value = 1;
926  EXPECT_TRUE(swri_string_util::ToInt32("+0", value, 16));
927  EXPECT_EQ(0, value);
928 
929  value = 0;
930  EXPECT_TRUE(swri_string_util::ToInt32("1", value, 16));
931  EXPECT_EQ(1, value);
932 
933  value = 0;
934  EXPECT_TRUE(swri_string_util::ToInt32("+1", value, 16));
935  EXPECT_EQ(1, value);
936 
937  value = 0;
938  EXPECT_TRUE(swri_string_util::ToInt32("-1", value, 16));
939  EXPECT_EQ(-1, value);
940 
941  value = 0;
942  EXPECT_TRUE(swri_string_util::ToInt32("12345678", value, 16));
943  EXPECT_EQ(0x12345678, value);
944 
945  value = 0;
946  EXPECT_TRUE(swri_string_util::ToInt32("012345678", value, 16));
947  EXPECT_EQ(0x12345678, value);
948 
949  value = 0;
950  EXPECT_TRUE(swri_string_util::ToInt32("-12345678", value, 16));
951  EXPECT_EQ(-0x12345678, value);
952 
953  value = 0;
954  EXPECT_TRUE(swri_string_util::ToInt32("aff", value, 16));
955  EXPECT_EQ(0xaff, value);
956 }
957 
958 TEST(StringUtilTests, ToInt32Base16Invalid)
959 {
960  int32_t value = 0;
961 
962  EXPECT_FALSE(swri_string_util::ToInt32("", value, 16));
963  EXPECT_FALSE(swri_string_util::ToInt32(" ", value, 16));
964  EXPECT_FALSE(swri_string_util::ToInt32("g", value, 16));
965  EXPECT_FALSE(swri_string_util::ToInt32("g0", value, 16));
966  EXPECT_FALSE(swri_string_util::ToInt32("0g", value, 16));
967  EXPECT_FALSE(swri_string_util::ToInt32("0 ", value, 16));
968  EXPECT_FALSE(swri_string_util::ToInt32(".", value, 16));
969  EXPECT_FALSE(swri_string_util::ToInt32("0.0", value, 16));
970  EXPECT_FALSE(swri_string_util::ToInt32("0.", value, 16));
971 }
972 
973 TEST(StringUtilTests, ToUInt32Base10)
974 {
975  uint32_t value = 1;
976  EXPECT_TRUE(swri_string_util::ToUInt32("0", value));
977  EXPECT_EQ(0, value);
978 
979  EXPECT_TRUE(swri_string_util::ToUInt32("00", value));
980  EXPECT_EQ(0, value);
981 
982  value = 1;
983  EXPECT_TRUE(swri_string_util::ToUInt32("-0", value));
984  EXPECT_EQ(0, value);
985 
986  value = 1;
987  EXPECT_TRUE(swri_string_util::ToUInt32("+0", value));
988  EXPECT_EQ(0, value);
989 
990  value = 0;
991  EXPECT_TRUE(swri_string_util::ToUInt32("1", value));
992  EXPECT_EQ(1, value);
993 
994  value = 0;
995  EXPECT_TRUE(swri_string_util::ToUInt32("+1", value));
996  EXPECT_EQ(1, value);
997 
998  value = 0;
999  EXPECT_TRUE(swri_string_util::ToUInt32("12345678", value));
1000  EXPECT_EQ(12345678, value);
1001 
1002  value = 0;
1003  EXPECT_TRUE(swri_string_util::ToUInt32("012345678", value));
1004  EXPECT_EQ(12345678, value);
1005 
1006  value = 0;
1007  EXPECT_TRUE(swri_string_util::ToUInt32("4294967295", value));
1008  EXPECT_EQ(4294967295, value);
1009 }
1010 
1011 TEST(StringUtilTests, ToUInt32Base10Invalid)
1012 {
1013  uint32_t value = 0;
1014 
1015  EXPECT_FALSE(swri_string_util::ToUInt32("", value));
1016  EXPECT_FALSE(swri_string_util::ToUInt32(" ", value));
1017  EXPECT_FALSE(swri_string_util::ToUInt32("a", value));
1018  EXPECT_FALSE(swri_string_util::ToUInt32("a0", value));
1019  EXPECT_FALSE(swri_string_util::ToUInt32("0a", value));
1020  EXPECT_FALSE(swri_string_util::ToUInt32("0 ", value));
1021  EXPECT_FALSE(swri_string_util::ToUInt32("1e39", value));
1022  EXPECT_FALSE(swri_string_util::ToUInt32("1e-38", value));
1023  EXPECT_FALSE(swri_string_util::ToUInt32("1e1", value));
1024  EXPECT_FALSE(swri_string_util::ToUInt32("1e0", value));
1025  EXPECT_FALSE(swri_string_util::ToUInt32(".", value));
1026  EXPECT_FALSE(swri_string_util::ToUInt32("0.0", value));
1027  EXPECT_FALSE(swri_string_util::ToUInt32("0.", value));
1028  EXPECT_FALSE(swri_string_util::ToUInt32("4294967296", value));
1029  EXPECT_FALSE(swri_string_util::ToUInt32("-1", value));
1030  EXPECT_FALSE(swri_string_util::ToUInt32("-123456789", value));
1031 }
1032 
1033 // Run all the tests that were declared with TEST()
1034 int main(int argc, char **argv)
1035 {
1036  testing::InitGoogleTest(&argc, argv);
1037 
1038  return RUN_ALL_TESTS();
1039 }
int main(int argc, char **argv)
bool ToInt32(const std::string &string, int32_t &value, int32_t base=10)
Definition: string_util.cpp:80
TEST(StringUtilTests, ToDouble_0)
bool ToUInt32(const std::string &string, uint32_t &value, int32_t base=10)
bool ToFloat(const std::string &string, float &value)
Definition: string_util.cpp:59
bool ToDouble(const std::string &string, double &value)
Definition: string_util.cpp:38


swri_string_util
Author(s): Marc Alban
autogenerated on Fri Jun 7 2019 22:06:03