forked from Alexhuszagh/rust-lexical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_py.py
955 lines (723 loc) · 37.8 KB
/
test_py.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
"""
Unittests for the Python API to lexical-core.
License
-------
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
"""
import contextlib
import ctypes
import os
import sys
import unittest
# Get path to DLL and Python wrapper.
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
INCLUDE_DIR = os.path.join(PROJECT_DIR, "include")
RELEASE_DIR = os.path.join(PROJECT_DIR, "target", "release")
# Change our working directory to the release directory, and our include
# path to include the Python source file.
sys.path.insert(0, INCLUDE_DIR)
os.chdir(RELEASE_DIR)
import lexical
class ConfigTests(unittest.TestCase):
'''Test the config functions for string literals.'''
def test_get_nan_string(self):
self.assertEqual(lexical.get_nan_string(), 'NaN')
def test_set_nan_string(self):
lexical.set_nan_string('NaN')
def test_get_inf_string(self):
self.assertEqual(lexical.get_inf_string(), 'inf')
def test_set_inf_string(self):
lexical.set_inf_string('inf')
def test_get_infinity_string(self):
self.assertEqual(lexical.get_infinity_string(), 'infinity')
def test_set_infinity_string(self):
lexical.set_infinity_string('infinity')
def test_get_exponent_default_char(self):
self.assertEqual(lexical.get_exponent_default_char(), 'e')
def test_set_exponent_default_char(self):
lexical.set_exponent_default_char('e')
def test_get_exponent_backup_char(self):
if lexical.HAVE_RADIX:
self.assertEqual(lexical.get_exponent_backup_char(), '^')
def test_set_exponent_backup_char(self):
if lexical.HAVE_RADIX:
lexical.set_exponent_backup_char('^')
def test_get_float_rounding(self):
if lexical.HAVE_ROUNDING:
self.assertEqual(lexical.get_float_rounding(), lexical.RoundingKind.NearestTieEven)
def test_set_float_rounding(self):
if lexical.HAVE_ROUNDING:
lexical.set_float_rounding(lexical.RoundingKind.NearestTieEven)
def test_number_format(self):
if lexical.HAVE_FORMAT:
format = lexical.NumberFormat.ignore(b'_')
self.assertEqual(format.digit_separator, b'_')
self.assertEqual(format.flags, lexical.NumberFormatFlags.DigitSeparatorFlagMask)
self.assertFalse(format.required_integer_digits)
self.assertTrue(format.integer_internal_digit_separator)
format = lexical.NumberFormat.permissive()
self.assertEqual(format.digit_separator, b'\x00')
self.assertEqual(format.flags, lexical.NumberFormatFlags.Permissive)
format = lexical.NumberFormat.standard()
self.assertEqual(format.digit_separator, b'\x00')
self.assertEqual(format.flags, lexical.NumberFormatFlags.Standard)
format = lexical.NumberFormat.compile(
digit_separator=b'_',
no_special=True,
integer_internal_digit_separator=True
)
self.assertEqual(format.digit_separator, b'_')
self.assertTrue(format.no_special)
self.assertTrue(format.integer_internal_digit_separator)
self.assertTrue(format.internal_digit_separator)
self.assertFalse(format.integer_leading_digit_separator)
format = lexical.NumberFormat.Json
self.assertEqual(format.digit_separator, b'\x00')
self.assertTrue(format.required_digits)
self.assertTrue(format.no_special)
class GlobalTests(unittest.TestCase):
'''Test the global config variables.'''
def test_max_size(self):
self.assertIsInstance(lexical.I8_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.I16_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.I32_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.I64_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.ISIZE_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.U8_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.U16_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.U32_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.U64_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.USIZE_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.F32_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.F64_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.I8_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.I16_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.I32_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.I64_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.ISIZE_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.U8_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.U16_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.U32_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.U64_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.USIZE_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.F32_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.F64_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.BUFFER_SIZE, int)
if lexical.HAVE_I128:
self.assertIsInstance(lexical.I128_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.U128_FORMATTED_SIZE, int)
self.assertIsInstance(lexical.I128_FORMATTED_SIZE_DECIMAL, int)
self.assertIsInstance(lexical.U128_FORMATTED_SIZE_DECIMAL, int)
class ErrorTests(unittest.TestCase):
'''Test ErrorCode and Error structures.'''
def setUp(self):
self.overflow = lexical.Error(lexical.ErrorCode.Overflow.value, 0)
self.underflow = lexical.Error(lexical.ErrorCode.Underflow.value, 0)
self.invalid_digit = lexical.Error(lexical.ErrorCode.InvalidDigit.value, 0)
self.empty = lexical.Error(lexical.ErrorCode.Empty.value, 0)
self.empty_mantissa = lexical.Error(lexical.ErrorCode.EmptyMantissa.value, 0)
self.empty_exponent = lexical.Error(lexical.ErrorCode.EmptyExponent.value, 0)
self.empty_integer = lexical.Error(lexical.ErrorCode.EmptyInteger.value, 0)
self.empty_fraction = lexical.Error(lexical.ErrorCode.EmptyFraction.value, 0)
self.invalid_positive_mantissa_sign = lexical.Error(lexical.ErrorCode.InvalidPositiveMantissaSign.value, 0)
self.missing_mantissa_sign = lexical.Error(lexical.ErrorCode.MissingMantissaSign.value, 0)
self.invalid_exponent = lexical.Error(lexical.ErrorCode.InvalidExponent.value, 0)
self.invalid_positive_exponent_sign = lexical.Error(lexical.ErrorCode.InvalidPositiveExponentSign.value, 0)
self.missing_exponent_sign = lexical.Error(lexical.ErrorCode.MissingExponentSign.value, 0)
self.exponent_without_fraction = lexical.Error(lexical.ErrorCode.ExponentWithoutFraction.value, 0)
self.invalid_leading_zeros = lexical.Error(lexical.ErrorCode.InvalidLeadingZeros.value, 0)
def test_is_overflow(self):
self.assertTrue(self.overflow.is_overflow())
self.assertFalse(self.underflow.is_overflow())
def test_is_underflow(self):
self.assertFalse(self.overflow.is_underflow())
self.assertTrue(self.underflow.is_underflow())
def test_is_invalid_digit(self):
self.assertFalse(self.overflow.is_invalid_digit())
self.assertTrue(self.invalid_digit.is_invalid_digit())
def test_is_empty(self):
self.assertFalse(self.overflow.is_empty())
self.assertTrue(self.empty.is_empty())
def test_is_empty_mantissa(self):
self.assertFalse(self.overflow.is_empty_mantissa())
self.assertTrue(self.empty_mantissa.is_empty_mantissa())
def test_is_empty_exponent(self):
self.assertFalse(self.overflow.is_empty_exponent())
self.assertTrue(self.empty_exponent.is_empty_exponent())
def test_is_empty_integer(self):
self.assertFalse(self.overflow.is_empty_integer())
self.assertTrue(self.empty_integer.is_empty_integer())
def test_is_empty_fraction(self):
self.assertFalse(self.overflow.is_empty_fraction())
self.assertTrue(self.empty_fraction.is_empty_fraction())
def test_is_invalid_positive_mantissa_sign(self):
self.assertFalse(self.overflow.is_invalid_positive_mantissa_sign())
self.assertTrue(self.invalid_positive_mantissa_sign.is_invalid_positive_mantissa_sign())
def test_is_missing_mantissa_sign(self):
self.assertFalse(self.overflow.is_missing_mantissa_sign())
self.assertTrue(self.missing_mantissa_sign.is_missing_mantissa_sign())
def test_is_invalid_exponent(self):
self.assertFalse(self.overflow.is_invalid_exponent())
self.assertTrue(self.invalid_exponent.is_invalid_exponent())
def test_is_invalid_positive_exponent_sign(self):
self.assertFalse(self.overflow.is_invalid_positive_exponent_sign())
self.assertTrue(self.invalid_positive_exponent_sign.is_invalid_positive_exponent_sign())
def test_is_missing_exponent_sign(self):
self.assertFalse(self.overflow.is_missing_exponent_sign())
self.assertTrue(self.missing_exponent_sign.is_missing_exponent_sign())
def test_is_exponent_without_fraction(self):
self.assertFalse(self.overflow.is_exponent_without_fraction())
self.assertTrue(self.exponent_without_fraction.is_exponent_without_fraction())
def test_is_invalid_leading_zeros(self):
self.assertFalse(self.overflow.is_invalid_leading_zeros())
self.assertTrue(self.invalid_leading_zeros.is_invalid_leading_zeros())
class ResultTests(unittest.TestCase):
'''Test complete and partial result types.'''
def setUp(self):
self.error = lexical.Error(lexical.ErrorCode.Overflow.value, 0)
def _complete_test(self, cls, value_type):
success_union = cls.union_type(value=value_type(1))
error_union = cls.union_type(error=self.error)
success = cls(lexical.ResultTag.Ok.value, success_union)
error = cls(lexical.ResultTag.Err.value, error_union)
self.assertEqual(success.into(), value_type(1))
with self.assertRaises(lexical.LexicalError):
error.into()
def _partial_test(self, cls, value_type):
tuple_type = cls.union_type.value_type
success_union = cls.union_type(value=tuple_type(value_type(1), 0))
error_union = cls.union_type(error=self.error)
success = cls(lexical.ResultTag.Ok.value, success_union)
error = cls(lexical.ResultTag.Err.value, error_union)
self.assertEqual(success.into(), (value_type(1), 0))
with self.assertRaises(lexical.LexicalError):
error.into()
def test_result_i8(self):
self._complete_test(lexical.ResultI8, int)
def test_result_i16(self):
self._complete_test(lexical.ResultI16, int)
def test_result_i32(self):
self._complete_test(lexical.ResultI32, int)
def test_result_i64(self):
self._complete_test(lexical.ResultI64, int)
def test_result_isize(self):
self._complete_test(lexical.ResultIsize, int)
def test_result_u8(self):
self._complete_test(lexical.ResultU8, int)
def test_result_u16(self):
self._complete_test(lexical.ResultU16, int)
def test_result_u32(self):
self._complete_test(lexical.ResultU32, int)
def test_result_u64(self):
self._complete_test(lexical.ResultU64, int)
def test_result_usize(self):
self._complete_test(lexical.ResultUsize, int)
def test_result_f32(self):
self._complete_test(lexical.ResultF32, float)
def test_result_f64(self):
self._complete_test(lexical.ResultF64, float)
def test_partial_result_i8(self):
self._partial_test(lexical.PartialResultI8, int)
def test_partial_result_i16(self):
self._partial_test(lexical.PartialResultI16, int)
def test_partial_result_i32(self):
self._partial_test(lexical.PartialResultI32, int)
def test_partial_result_i64(self):
self._partial_test(lexical.PartialResultI64, int)
def test_partial_result_isize(self):
self._partial_test(lexical.PartialResultIsize, int)
def test_partial_result_u8(self):
self._partial_test(lexical.PartialResultU8, int)
def test_partial_result_u16(self):
self._partial_test(lexical.PartialResultU16, int)
def test_partial_result_u32(self):
self._partial_test(lexical.PartialResultU32, int)
def test_partial_result_u64(self):
self._partial_test(lexical.PartialResultU64, int)
def test_partial_result_usize(self):
self._partial_test(lexical.PartialResultUsize, int)
def test_partial_result_f32(self):
self._partial_test(lexical.PartialResultF32, float)
def test_partial_result_f64(self):
self._partial_test(lexical.PartialResultF64, float)
class ToStringTests(unittest.TestCase):
'''Test number-to-string conversion routines.'''
def _test_integer(self, cb):
self.assertEqual(cb(10), '10')
def _test_integer_radix(self, cb):
self.assertEqual(cb(10, 2), '1010')
self.assertEqual(cb(10, 16), 'A')
self.assertEqual(cb(10, 10), '10')
def _test_float(self, cb):
self.assertEqual(cb(10.5), '10.5')
def _test_float_radix(self, cb):
self.assertEqual(cb(10.5, 2), '1010.1')
self.assertEqual(cb(10.5, 16), 'A.8')
self.assertEqual(cb(10.5, 10), '10.5')
def test_i8toa(self):
self._test_integer(lexical.i8toa)
def test_i16toa(self):
self._test_integer(lexical.i16toa)
def test_i32toa(self):
self._test_integer(lexical.i32toa)
def test_i64toa(self):
self._test_integer(lexical.i64toa)
def test_isizetoa(self):
self._test_integer(lexical.isizetoa)
def test_u8toa(self):
self._test_integer(lexical.u8toa)
def test_u16toa(self):
self._test_integer(lexical.u16toa)
def test_u32toa(self):
self._test_integer(lexical.u32toa)
def test_u64toa(self):
self._test_integer(lexical.u64toa)
def test_usizetoa(self):
self._test_integer(lexical.usizetoa)
def test_f32toa(self):
self._test_float(lexical.f32toa)
def test_f64toa(self):
self._test_float(lexical.f64toa)
def test_i8toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.i8toa_radix)
def test_i16toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.i16toa_radix)
def test_i32toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.i32toa_radix)
def test_i64toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.i64toa_radix)
def test_isizetoa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.isizetoa_radix)
def test_u8toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.u8toa_radix)
def test_u16toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.u16toa_radix)
def test_u32toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.u32toa_radix)
def test_u64toa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.u64toa_radix)
def test_usizetoa_radix(self):
if lexical.HAVE_RADIX:
self._test_integer_radix(lexical.usizetoa_radix)
def test_f32toa_radix(self):
if lexical.HAVE_RADIX:
self._test_float_radix(lexical.f32toa_radix)
def test_f64toa_radix(self):
if lexical.HAVE_RADIX:
self._test_float_radix(lexical.f64toa_radix)
class ParseTests(unittest.TestCase):
'''Test string-to-number conversion routines.'''
def _complete_test(self, callback, value_type, *args):
self.assertEqual(callback('10', *args), value_type(10))
with self.assertRaises(lexical.LexicalError):
callback('10a', *args)
with self.assertRaises(lexical.LexicalError):
callback('', *args)
if issubclass(value_type, float):
# Specialized tests for floats.
self.assertEqual(callback('10.5', *args), value_type(10.5))
self.assertEqual(callback('10e5', *args), value_type(10e5))
with self.assertRaises(lexical.LexicalError):
callback('.', *args)
with self.assertRaises(lexical.LexicalError):
callback('e5', *args)
with self.assertRaises(lexical.LexicalError):
callback('10e+', *args)
def _complete_radix_test(self, callback, value_type, *args):
self.assertEqual(callback('1010', 2, *args), value_type(10))
self.assertEqual(callback('10', 10, *args), value_type(10))
self.assertEqual(callback('A', 16, *args), value_type(10))
with self.assertRaises(lexical.LexicalError):
callback('10102', 2, *args)
with self.assertRaises(lexical.LexicalError):
callback('10a', 10, *args)
with self.assertRaises(lexical.LexicalError):
callback('AG', 16, *args)
with self.assertRaises(lexical.LexicalError):
callback('', 10, *args)
if issubclass(value_type, float):
# Specialized tests for floats.
self.assertEqual(callback('1010.1', 2, *args), value_type(10.5))
self.assertEqual(callback('10.5', 10, *args), value_type(10.5))
self.assertEqual(callback('A.8', 16, *args), value_type(10.5))
self.assertEqual(callback('10e5', 10, *args), value_type(10e5))
with self.assertRaises(lexical.LexicalError):
callback('.', 10, *args)
with self.assertRaises(lexical.LexicalError):
callback('e5', 10, *args)
with self.assertRaises(lexical.LexicalError):
callback('10e+', 10, *args)
def _partial_test(self, callback, value_type, *args):
self.assertEqual(callback('10', *args), (value_type(10), 2))
self.assertEqual(callback('10a', *args), (value_type(10), 2))
with self.assertRaises(lexical.LexicalError):
callback('', *args)
if issubclass(value_type, float):
# Specialized tests for floats.
self.assertEqual(callback('10.5', *args), (value_type(10.5), 4))
self.assertEqual(callback('10e5', *args), (value_type(10e5), 4))
with self.assertRaises(lexical.LexicalError):
callback('.', *args)
with self.assertRaises(lexical.LexicalError):
callback('e5', *args)
with self.assertRaises(lexical.LexicalError):
callback('10e+', *args)
def _partial_radix_test(self, callback, value_type, *args):
self.assertEqual(callback('1010', 2, *args), (value_type(10), 4))
self.assertEqual(callback('10', 10, *args), (value_type(10), 2))
self.assertEqual(callback('A', 16, *args), (value_type(10), 1))
self.assertEqual(callback('10102', 2, *args), (value_type(10), 4))
self.assertEqual(callback('10a', 10, *args), (value_type(10), 2))
self.assertEqual(callback('AG', 16, *args), (value_type(10), 1))
with self.assertRaises(lexical.LexicalError):
callback('', 10, *args)
if issubclass(value_type, float):
# Specialized tests for floats.
self.assertEqual(callback('1010.1', 2, *args), (value_type(10.5), 6))
self.assertEqual(callback('10.5', 10, *args), (value_type(10.5), 4))
self.assertEqual(callback('A.8', 16, *args), (value_type(10.5), 3))
self.assertEqual(callback('10e5', 10, *args), (value_type(10e5), 4))
with self.assertRaises(lexical.LexicalError):
callback('.', 10, *args)
with self.assertRaises(lexical.LexicalError):
callback('e5', 10, *args)
with self.assertRaises(lexical.LexicalError):
callback('10e+', 10, *args)
def test_atoi8(self):
self._complete_test(lexical.atoi8, int)
def test_atoi16(self):
self._complete_test(lexical.atoi16, int)
def test_atoi32(self):
self._complete_test(lexical.atoi32, int)
def test_atoi64(self):
self._complete_test(lexical.atoi64, int)
def test_atoisize(self):
self._complete_test(lexical.atoisize, int)
def test_atou8(self):
self._complete_test(lexical.atou8, int)
def test_atou16(self):
self._complete_test(lexical.atou16, int)
def test_atou32(self):
self._complete_test(lexical.atou32, int)
def test_atou64(self):
self._complete_test(lexical.atou64, int)
def test_atousize(self):
self._complete_test(lexical.atousize, int)
def test_atof32(self):
self._complete_test(lexical.atof32, float)
def test_atof64(self):
self._complete_test(lexical.atof64, float)
def test_atof32_lossy(self):
self._complete_test(lexical.atof32_lossy, float)
def test_atof64_lossy(self):
self._complete_test(lexical.atof64_lossy, float)
def test_atoi8_partial(self):
self._partial_test(lexical.atoi8_partial, int)
def test_atoi16_partial(self):
self._partial_test(lexical.atoi16_partial, int)
def test_atoi32_partial(self):
self._partial_test(lexical.atoi32_partial, int)
def test_atoi64_partial(self):
self._partial_test(lexical.atoi64_partial, int)
def test_atoisize_partial(self):
self._partial_test(lexical.atoisize_partial, int)
def test_atou8_partial(self):
self._partial_test(lexical.atou8_partial, int)
def test_atou16_partial(self):
self._partial_test(lexical.atou16_partial, int)
def test_atou32_partial(self):
self._partial_test(lexical.atou32_partial, int)
def test_atou64_partial(self):
self._partial_test(lexical.atou64_partial, int)
def test_atousize_partial(self):
self._partial_test(lexical.atousize_partial, int)
def test_atof32_partial(self):
self._partial_test(lexical.atof32_partial, float)
def test_atof64_partial(self):
self._partial_test(lexical.atof64_partial, float)
def test_atof32_partial_lossy(self):
self._partial_test(lexical.atof32_partial_lossy, float)
def test_atof64_partial_lossy(self):
self._partial_test(lexical.atof64_partial_lossy, float)
def test_atoi8_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atoi8_radix, int)
def test_atoi16_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atoi16_radix, int)
def test_atoi32_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atoi32_radix, int)
def test_atoi64_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atoi64_radix, int)
def test_atoisize_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atoisize_radix, int)
def test_atou8_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atou8_radix, int)
def test_atou16_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atou16_radix, int)
def test_atou32_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atou32_radix, int)
def test_atou64_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atou64_radix, int)
def test_atousize_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atousize_radix, int)
def test_atof32_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atof32_radix, float)
def test_atof64_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atof64_radix, float)
def test_atof32_lossy_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atof32_lossy_radix, float)
def test_atof64_lossy_radix(self):
if lexical.HAVE_RADIX:
self._complete_radix_test(lexical.atof64_lossy_radix, float)
def test_atoi8_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atoi8_partial_radix, int)
def test_atoi16_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atoi16_partial_radix, int)
def test_atoi32_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atoi32_partial_radix, int)
def test_atoi64_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atoi64_partial_radix, int)
def test_atoisize_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atoisize_partial_radix, int)
def test_atou8_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atou8_partial_radix, int)
def test_atou16_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atou16_partial_radix, int)
def test_atou32_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atou32_partial_radix, int)
def test_atou64_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atou64_partial_radix, int)
def test_atousize_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atousize_partial_radix, int)
def test_atof32_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atof32_partial_radix, float)
def test_atof64_partial_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atof64_partial_radix, float)
def test_atof32_partial_lossy_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atof32_partial_lossy_radix, float)
def test_atof64_partial_lossy_radix(self):
if lexical.HAVE_RADIX:
self._partial_radix_test(lexical.atof64_partial_lossy_radix, float)
def test_atoi8_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atoi8_format, int, lexical.NumberFormat.RustString)
def test_atoi16_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atoi16_format, int, lexical.NumberFormat.RustString)
def test_atoi32_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atoi32_format, int, lexical.NumberFormat.RustString)
def test_atoi64_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atoi64_format, int, lexical.NumberFormat.RustString)
def test_atoisize_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atoisize_format, int, lexical.NumberFormat.RustString)
def test_atou8_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atou8_format, int, lexical.NumberFormat.RustString)
def test_atou16_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atou16_format, int, lexical.NumberFormat.RustString)
def test_atou32_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atou32_format, int, lexical.NumberFormat.RustString)
def test_atou64_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atou64_format, int, lexical.NumberFormat.RustString)
def test_atousize_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atousize_format, int, lexical.NumberFormat.RustString)
def test_atof32_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atof32_format, float, lexical.NumberFormat.RustString)
def test_atof64_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atof64_format, float, lexical.NumberFormat.RustString)
def test_atof32_lossy_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atof32_lossy_format, float, lexical.NumberFormat.RustString)
def test_atof64_lossy_format(self):
if lexical.HAVE_FORMAT:
self._complete_test(lexical.atof64_lossy_format, float, lexical.NumberFormat.RustString)
def test_atoi8_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atoi8_partial_format, int, lexical.NumberFormat.RustString)
def test_atoi16_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atoi16_partial_format, int, lexical.NumberFormat.RustString)
def test_atoi32_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atoi32_partial_format, int, lexical.NumberFormat.RustString)
def test_atoi64_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atoi64_partial_format, int, lexical.NumberFormat.RustString)
def test_atoisize_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atoisize_partial_format, int, lexical.NumberFormat.RustString)
def test_atou8_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atou8_partial_format, int, lexical.NumberFormat.RustString)
def test_atou16_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atou16_partial_format, int, lexical.NumberFormat.RustString)
def test_atou32_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atou32_partial_format, int, lexical.NumberFormat.RustString)
def test_atou64_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atou64_partial_format, int, lexical.NumberFormat.RustString)
def test_atousize_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atousize_partial_format, int, lexical.NumberFormat.RustString)
def test_atof32_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atof32_partial_format, float, lexical.NumberFormat.RustString)
def test_atof64_partial_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atof64_partial_format, float, lexical.NumberFormat.RustString)
def test_atof32_partial_lossy_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atof32_partial_lossy_format, float, lexical.NumberFormat.RustString)
def test_atof64_partial_lossy_format(self):
if lexical.HAVE_FORMAT:
self._partial_test(lexical.atof64_partial_lossy_format, float, lexical.NumberFormat.RustString)
def test_atoi8_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atoi8_format_radix, int, lexical.NumberFormat.RustString)
def test_atoi16_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atoi16_format_radix, int, lexical.NumberFormat.RustString)
def test_atoi32_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atoi32_format_radix, int, lexical.NumberFormat.RustString)
def test_atoi64_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atoi64_format_radix, int, lexical.NumberFormat.RustString)
def test_atoisize_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atoisize_format_radix, int, lexical.NumberFormat.RustString)
def test_atou8_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atou8_format_radix, int, lexical.NumberFormat.RustString)
def test_atou16_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atou16_format_radix, int, lexical.NumberFormat.RustString)
def test_atou32_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atou32_format_radix, int, lexical.NumberFormat.RustString)
def test_atou64_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atou64_format_radix, int, lexical.NumberFormat.RustString)
def test_atousize_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atousize_format_radix, int, lexical.NumberFormat.RustString)
def test_atof32_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atof32_format_radix, float, lexical.NumberFormat.RustString)
def test_atof64_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atof64_format_radix, float, lexical.NumberFormat.RustString)
def test_atof32_lossy_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atof32_lossy_format_radix, float, lexical.NumberFormat.RustString)
def test_atof64_lossy_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._complete_radix_test(lexical.atof64_lossy_format_radix, float, lexical.NumberFormat.RustString)
def test_atoi8_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atoi8_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atoi16_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atoi16_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atoi32_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atoi32_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atoi64_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atoi64_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atoisize_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atoisize_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atou8_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atou8_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atou16_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atou16_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atou32_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atou32_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atou64_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atou64_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atousize_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atousize_partial_format_radix, int, lexical.NumberFormat.RustString)
def test_atof32_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atof32_partial_format_radix, float, lexical.NumberFormat.RustString)
def test_atof64_partial_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atof64_partial_format_radix, float, lexical.NumberFormat.RustString)
def test_atof32_partial_lossy_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atof32_partial_lossy_format_radix, float, lexical.NumberFormat.RustString)
def test_atof64_partial_lossy_format_radix(self):
if lexical.HAVE_RADIX and lexical.HAVE_FORMAT:
self._partial_radix_test(lexical.atof64_partial_lossy_format_radix, float, lexical.NumberFormat.RustString)
if __name__ == '__main__':
unittest.main()