Repository navigation
Expand file tree
/
Copy pathWordsToNumberTests.cs
More file actions
2014 lines (1859 loc) · 86 KB
/
Copy pathWordsToNumberTests.cs
File metadata and controls
2014 lines (1859 loc) · 86 KB
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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Frozen;
namespace Humanizer.Tests;
[UseCulture("en-US")]
public class WordsToNumberTests_US
{
[Fact]
public void SupportsLongRangeValues_US()
{
const string words = "two billion one hundred forty seven million four hundred eighty three thousand six hundred forty eight";
const long expected = 2147483648L;
Assert.Equal(expected, words.ToNumber(CultureInfo.CurrentCulture));
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expected, parsedNumber);
Assert.Null(unrecognizedWord);
}
[Theory]
[InlineData("zero", 0)]
[InlineData("one", 1)]
[InlineData("minus five", -5)]
[InlineData("eleven", 11)]
[InlineData("ninety five", 95)]
[InlineData("hundred five", 105)]
[InlineData("one hundred ninety six", 196)]
[InlineData("minus one hundred and five", -105)]
[InlineData("seventeenth", 17)]
[InlineData("thirtieth", 30)]
[InlineData("twenty-seventh", 27)]
[InlineData("thirty-first", 31)]
[InlineData("minus twenty-first", -21)]
[InlineData("two thousand twenty three", 2023)]
[InlineData("one million two hundred thirty four thousand five hundred sixty seven", 1234567)]
[InlineData("three billion", 3000000000)]
[InlineData("negative three billion", -3000000000)]
[InlineData("one hundred and third", 103)]
[InlineData("two hundred and first", 201)]
[InlineData("five thousand and ninth", 5009)]
[InlineData("17th", 17)]
[InlineData("31st", 31)]
[InlineData("100th", 100)]
[InlineData("203rd", 203)]
[InlineData("minus 21st", -21)]
[InlineData("negative five", -5)]
[InlineData("negative one hundred and five", -105)]
[InlineData("negative twenty-first", -21)]
public void ToNumber_US(string words, long expectedNumber) => Assert.Equal(expectedNumber, words.ToNumber(CultureInfo.CurrentCulture));
[Theory]
[InlineData("three billion", 3000000000L)]
[InlineData("negative three billion", -3000000000L)]
public void ToNumber_US_SupportsLongRangeValues(string words, long expectedNumber) =>
Assert.Equal(expectedNumber, words.ToNumber(CultureInfo.CurrentCulture));
[Theory]
[InlineData("zero", 0, null)]
[InlineData("one", 1, null)]
[InlineData("minus five", -5, null)]
[InlineData("eleven", 11, null)]
[InlineData("ninety five", 95, null)]
[InlineData("hundred five", 105, null)]
[InlineData("one hundred ninety six", 196, null)]
[InlineData("minus one hundred and five", -105, null)]
[InlineData("seventeenth", 17, null)]
[InlineData("thirtieth", 30, null)]
[InlineData("twenty-seventh", 27, null)]
[InlineData("thirty-first", 31, null)]
[InlineData("minus twenty-first", -21, null)]
[InlineData("two thousand twenty three", 2023, null)]
[InlineData("one million two hundred thirty four thousand five hundred sixty seven", 1234567, null)]
[InlineData("three billion", 3000000000, null)]
[InlineData("negative three billion", -3000000000, null)]
[InlineData("one hundred and third", 103, null)]
[InlineData("two hundred and first", 201, null)]
[InlineData("five thousand and ninth", 5009, null)]
[InlineData("17th", 17, null)]
[InlineData("31st", 31, null)]
[InlineData("100th", 100, null)]
[InlineData("203rd", 203, null)]
[InlineData("minus 21st", -21, null)]
[InlineData("negative five", -5, null)]
[InlineData("negative one hundred and five", -105, null)]
[InlineData("negative twenty-first", -21, null)]
public void TryToNumber_ValidInput_US(string words, long expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(unrecognizedWord, expectedUnrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("three billion", 3000000000L, null)]
[InlineData("negative three billion", -3000000000L, null)]
public void TryToNumber_ValidInput_US_SupportsLongRangeValues(string words, long expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("twenty nine hello", 0, "hello")]
[InlineData("mister three", 0, "mister")]
[InlineData("tenn", 0, "tenn")]
[InlineData("twenty sveen", 0, "sveen")]
[InlineData("minus fift five", 0, "fift")]
[InlineData("sixty two j", 0, "j")]
[InlineData("two hundred , ninetyy sevennn", 0, "ninetyy")]
[InlineData("invalidinput", 0, "invalidinput")]
[InlineData("30rmd", 0, "30rmd")]
[InlineData("negative energy", 0, "energy")]
public void TryToNumber_InvalidInput_US(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(unrecognizedWord, expectedUnrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("en-GB")]
public class WordsToNumberTests_GB
{
[Theory]
[InlineData("zero", 0, null)]
[InlineData("one", 1, null)]
[InlineData("minus five", -5, null)]
[InlineData("eleven", 11, null)]
[InlineData("ninety five", 95, null)]
[InlineData("hundred five", 105, null)]
[InlineData("one hundred ninety six", 196, null)]
[InlineData("minus one hundred and five", -105, null)]
[InlineData("seventeenth", 17, null)]
[InlineData("thirtieth", 30, null)]
[InlineData("twenty-seventh", 27, null)]
[InlineData("thirty-first", 31, null)]
[InlineData("minus twenty-first", -21, null)]
[InlineData("two thousand twenty three", 2023, null)]
[InlineData("one million two hundred thirty four thousand five hundred sixty seven", 1234567, null)]
[InlineData("three billion", 3000000000, null)]
[InlineData("negative three billion", -3000000000, null)]
[InlineData("one hundred and third", 103, null)]
[InlineData("two hundred and first", 201, null)]
[InlineData("five thousand and ninth", 5009, null)]
[InlineData("17th", 17, null)]
[InlineData("31st", 31, null)]
[InlineData("100th", 100, null)]
[InlineData("203rd", 203, null)]
[InlineData("minus 21st", -21, null)]
[InlineData("negative five", -5, null)]
[InlineData("negative one hundred and five", -105, null)]
[InlineData("negative twenty-first", -21, null)]
public void TryToNumber_ValidInput_GB(string words, long expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(unrecognizedWord, expectedUnrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("twenty nine hello", 0, "hello")]
[InlineData("mister three", 0, "mister")]
[InlineData("tenn", 0, "tenn")]
[InlineData("twenty sveen", 0, "sveen")]
[InlineData("minus fift five", 0, "fift")]
[InlineData("sixty two j", 0, "j")]
[InlineData("two hundred , ninetyy sevennn", 0, "ninetyy")]
[InlineData("invalidinput", 0, "invalidinput")]
[InlineData("30rmd", 0, "30rmd")]
[InlineData("negative energy", 0, "energy")]
public void TryToNumber_InvalidInput_GB(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(unrecognizedWord, expectedUnrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("de-DE")]
public class WordsToNumberTests_German
{
[Theory]
[InlineData("null", 0, null)]
[InlineData("minus fünf", -5, null)]
[InlineData("einundzwanzig", 21, null)]
[InlineData("zweiunddreißig", 32, null)]
[InlineData("einhundertneunundvierzig", 149, null)]
[InlineData("zweitausendachtundzwanzig", 2028, null)]
[InlineData("dreißigste", 30, null)]
[InlineData("einundzwanzigste", 21, null)]
public void TryToNumber_ValidInput_German(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("zwanzig foo", 0, "foo")]
[InlineData("minus xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_German(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("de-CH")]
[InlineData("de-LI")]
public void ResolveForSwissVariants(string cultureName)
{
var culture = new CultureInfo(cultureName);
Assert.Equal(21, "einundzwanzig".ToNumber(culture));
}
}
[UseCulture("ca")]
public class WordsToNumberTests_Catalan
{
[Theory]
[InlineData("zero", 0, null)]
[InlineData("menys cinc", -5, null)]
[InlineData("vint-i-u", 21, null)]
[InlineData("trenta-una", 31, null)]
[InlineData("dos-cents", 200, null)]
[InlineData("dues-centes", 200, null)]
[InlineData("dos cents i una", 201, null)]
[InlineData("cent vint-i-una", 121, null)]
[InlineData("dos mil vint-i-u", 2021, null)]
[InlineData("un milió una", 1000001, null)]
[InlineData("primer", 1, null)]
[InlineData("segona", 2, null)]
[InlineData("21è", 21, null)]
[InlineData("22a", 22, null)]
public void TryToNumber_ValidInput_Catalan(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("vint foo", 0, "foo")]
[InlineData("menys xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_Catalan(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("fr-FR")]
public class WordsToNumberTests_French
{
[Theory]
[InlineData("zéro", 0, null)]
[InlineData("moins cinq", -5, null)]
[InlineData("vingt et un", 21, null)]
[InlineData("trente-et-un", 31, null)]
[InlineData("soixante et onze", 71, null)]
[InlineData("quatre-vingt-un", 81, null)]
[InlineData("quatre-vingt-dix-neuf", 99, null)]
[InlineData("cent vingt-deux", 122, null)]
[InlineData("mille deux cent trente-quatre", 1234, null)]
[InlineData("premier", 1, null)]
[InlineData("première", 1, null)]
public void TryToNumber_ValidInput_French(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("vingt foo", 0, "foo")]
[InlineData("moins xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_French(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("it")]
public class WordsToNumberTests_Italian
{
[Theory]
[InlineData("zero", 0, null)]
[InlineData("meno cinque", -5, null)]
[InlineData("uno", 1, null)]
[InlineData("ventuno", 21, null)]
[InlineData("venti uno", 21, null)]
[InlineData("venti-otto", 28, null)]
[InlineData("trentotto", 38, null)]
[InlineData("centoventidue", 122, null)]
[InlineData("duemilatrecentoquaranta", 2340, null)]
[InlineData("un milione duecentomila", 1200000, null)]
[InlineData("quattromilacinquecentosessantasette", 4567, null)]
[InlineData("venti e uno", 21, null)]
[InlineData("trenta ed otto", 38, null)]
[InlineData("ventitré", 23, null)]
[InlineData("cento e uno", 101, null)]
public void TryToNumber_ValidInput_Italian(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("primo", 1, null)]
[InlineData("prima", 1, null)]
[InlineData("secondo", 2, null)]
[InlineData("seconda", 2, null)]
[InlineData("ventunesimo", 21, null)]
[InlineData("ventunesima", 21, null)]
[InlineData("centounesimo", 101, null)]
[InlineData("centounesima", 101, null)]
public void TryToNumber_ValidOrdinalInput_Italian(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("venti foo", 0, "foo")]
[InlineData("meno xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_Italian(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("it")]
public class WordsToNumberTests_ItalianOrdinalAbbreviations
{
[Theory]
[InlineData("1º", 1, null)]
[InlineData("2ª", 2, null)]
[InlineData("21º", 21, null)]
public void TryToNumber_OrdinalAbbreviations(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("3z", 0, "3z")]
public void TryToNumber_InvalidOrdinalAbbreviations(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("pt-PT")]
public class WordsToNumberTests_Portuguese
{
[Theory]
[InlineData("zero", 0)]
[InlineData("menos cinco", -5)]
[InlineData("vinte e um", 21)]
[InlineData("cento e cinco", 105)]
[InlineData("duzentos e uma", 201)]
[InlineData("mil e vinte", 1020)]
[InlineData("um milhão e dois", 1000002)]
[InlineData("primeiro", 1)]
[InlineData("primeira", 1)]
[InlineData("vigésimo primeiro", 21)]
[InlineData("milésimo", 1000)]
[InlineData("1º", 1)]
[InlineData("1ª", 1)]
public void ToNumber_Portuguese(string words, int expectedNumber) =>
Assert.Equal(expectedNumber, words.ToNumber(CultureInfo.CurrentCulture));
[Theory]
[InlineData("zero", 0, null)]
[InlineData("menos cinco", -5, null)]
[InlineData("vinte e um", 21, null)]
[InlineData("cento e cinco", 105, null)]
[InlineData("duzentos e uma", 201, null)]
[InlineData("mil e vinte", 1020, null)]
[InlineData("um milhão e dois", 1000002, null)]
[InlineData("primeiro", 1, null)]
[InlineData("primeira", 1, null)]
[InlineData("vigésimo primeiro", 21, null)]
[InlineData("milésimo", 1000, null)]
[InlineData("1º", 1, null)]
[InlineData("1ª", 1, null)]
public void TryToNumber_ValidInput_Portuguese(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("vinte foo", 0, "foo")]
[InlineData("menos xyz", 0, "xyz")]
[InlineData("treze e direfente", 0, "direfente")]
[InlineData("123abc", 0, "123abc")]
public void TryToNumber_InvalidInput_Portuguese(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("pt-BR")]
public class WordsToNumberTests_BrazilianPortuguese
{
[Theory]
[InlineData("milésimo", 1000, null)]
public void TryToNumber_ValidInput_BrazilianPortuguese(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("es-ES")]
public class WordsToNumberTests_Spanish
{
[Theory]
[InlineData("cero", 0, null)]
[InlineData("uno", 1, null)]
[InlineData("un", 1, null)]
[InlineData("veintiuno", 21, null)]
[InlineData("veintiún", 21, null)]
[InlineData("veintiuna", 21, null)]
[InlineData("veintidós", 22, null)]
[InlineData("treinta y cuatro", 34, null)]
[InlineData("ciento uno", 101, null)]
[InlineData("quinientos cuarenta y dos", 542, null)]
[InlineData("mil doscientos treinta y cuatro", 1234, null)]
[InlineData("dos millones trescientos mil", 2300000, null)]
[InlineData("menos cinco", -5, null)]
public void TryToNumber_ValidInput_Spanish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("primero", 1, null)]
[InlineData("primera", 1, null)]
[InlineData("segundo", 2, null)]
[InlineData("vigésimo primero", 21, null)]
[InlineData("vigésima primera", 21, null)]
[InlineData("milésimo", 1000, null)]
[InlineData("dosmilésimo", 2000, null)]
public void TryToNumber_ValidOrdinalInput_Spanish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("1.º", 1, null)]
[InlineData("1.ª", 1, null)]
[InlineData("21.º", 21, null)]
public void TryToNumber_OrdinalAbbreviations_Spanish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("veinte foo", 0, "foo")]
[InlineData("menos dos mil xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_Spanish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("nl-NL")]
public class WordsToNumberTests_Dutch
{
[Theory]
[InlineData("nul", 0, null)]
[InlineData("min tien", -10, null)]
[InlineData("eenentwintig", 21, null)]
[InlineData("honderdacht", 108, null)]
[InlineData("duizend honderdelf", 1111, null)]
[InlineData("een miljoen", 1000000, null)]
[InlineData("eerste", 1, null)]
[InlineData("eenentwintigste", 21, null)]
public void TryToNumber_ValidInput_Dutch(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("twintig foo", 0, "foo")]
[InlineData("min xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_Dutch(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("sv-SE")]
public class WordsToNumberTests_Swedish
{
[Theory]
[InlineData("noll", 0, null)]
[InlineData("minus tio", -10, null)]
[InlineData("tjugoett", 21, null)]
[InlineData("hundraåtta", 108, null)]
[InlineData("ett tusen", 1000, null)]
[InlineData("etttusenfem", 1005, null)]
[InlineData("ett tusen hundraelva", 1111, null)]
[InlineData("en miljon", 1000000, null)]
[InlineData("första", 1, null)]
[InlineData("tjugoförsta", 21, null)]
public void TryToNumber_ValidInput_Swedish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("tjugo foo", 0, "foo")]
[InlineData("minus xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_Swedish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("sv-SE", "tusen", "minus ")]
[InlineData("nb-NO", "tusen", "minus ")]
[InlineData("is-IS", "þúsund", "mínus ")]
public void TryToNumber_BoundsCompoundScaleRecursion(string cultureName, string scale, string negativePrefix)
{
var culture = CultureInfo.GetCultureInfo(cultureName);
var legitimateWords = string.Concat(Enumerable.Repeat(scale, 64));
Assert.Equal(64000, legitimateWords.ToNumber(culture));
Assert.Equal(64000.1m, $"{legitimateWords} komma {1.ToWords(culture)}".ToDecimalNumber(culture));
var words = string.Concat(Enumerable.Repeat(scale, 20_000));
var allocatedBefore = GC.GetAllocatedBytesForCurrentThread();
Assert.False(words.TryToNumber(out var parsedNumber, culture, out var unrecognizedWord));
var allocated = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore;
Assert.Equal(0, parsedNumber);
Assert.NotNull(unrecognizedWord);
Assert.True(allocated < 8_000_000, $"Compound-scale rejection allocated {allocated:N0} bytes.");
Assert.Throws<ArgumentException>(() => words.ToNumber(culture));
Assert.False($"{negativePrefix}{words}".TryToNumber(out parsedNumber, culture, out unrecognizedWord));
Assert.Equal(0, parsedNumber);
Assert.NotNull(unrecognizedWord);
Assert.False($"{words} komma {1.ToWords(culture)}".TryToDecimalNumber(out var parsedDecimal, culture, out unrecognizedWord));
Assert.Equal(0, parsedDecimal);
Assert.NotNull(unrecognizedWord);
}
}
[UseCulture("is-IS")]
public class WordsToNumberTests_Icelandic
{
[Theory]
[InlineData("núll", 0, null)]
[InlineData("mínus fimm", -5, null)]
[InlineData("tuttugu og eitt", 21, null)]
[InlineData("tuttugu og tvö", 22, null)]
[InlineData("fjörutíu og fimm", 45, null)]
[InlineData("tvö hundruð sextíu", 260, null)]
[InlineData("eitt hundrað og þrjú", 103, null)]
[InlineData("þúsund tvö hundruð þrjátíu og fjögur", 1234, null)]
[InlineData("milljón tvö þúsund", 1002000, null)]
[InlineData("tveir milljónir", 2000000, null)]
[InlineData("milljarður", 1000000000, null)]
public void TryToNumber_ValidInput_Icelandic(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("tuttugu foo", 0, "foo")]
[InlineData("hundrað og xxx", 0, "xxx")]
[InlineData("tvö þúsund abc", 0, "abc")]
[InlineData("milljón og foo", 0, "foo")]
[InlineData("fimmz", 0, "fimmz")]
public void TryToNumber_InvalidInput_Icelandic(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(10)]
[InlineData(21)]
[InlineData(31)]
[InlineData(100)]
public void TryToNumber_OrdinalRoundTrip_Icelandic(int number)
{
var converter = Configurator.NumberToWordsConverters.ResolveForCulture(CultureInfo.CurrentCulture);
var ordinal = converter.ConvertToOrdinal(number);
Assert.True(ordinal.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Null(unrecognizedWord);
Assert.Equal(number, parsedNumber);
}
}
[UseCulture("da")]
public class WordsToNumberTests_Danish
{
[Theory]
[InlineData("nul", 0, null)]
[InlineData("minus femoghalvtreds", -55, null)]
[InlineData("enogtyve", 21, null)]
[InlineData("nioghalvfems", 99, null)]
[InlineData("et hundrede og en", 101, null)]
[InlineData("to hundrede og toogtres", 262, null)]
[InlineData("et tusind og en", 1001, null)]
[InlineData("to millioner", 2_000_000, null)]
public void TryToNumber_ValidInput_Danish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("enogtyve foo", 0, "foo")]
[InlineData("minus xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_Danish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("bg")]
public class WordsToNumberTests_Bulgarian
{
[Theory]
[InlineData("нула", 0, null)]
[InlineData("сто и единадесет", 111, null)]
[InlineData("петстотин петдесет и пет", 555, null)]
[InlineData("четири хиляди двеста и тринадесет", 4213, null)]
[InlineData("четири милиона двеста и десет", 4_000_210, null)]
public void TryToNumber_ValidInput_Bulgarian(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("петдесет foo", 0, "foo")]
public void TryToNumber_InvalidInput_Bulgarian(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("bn-BD")]
public class WordsToNumberTests_Bengali
{
[Theory]
[InlineData("এক", 1, null)]
[InlineData("একশ বাইশ", 122, null)]
[InlineData("তিন হাজার পাঁচশ এক", 3501, null)]
[InlineData("এক কোটি তেইশ লক্ষ পঁয়তাল্লিশ হাজার ছয়শ আটাত্তর", 12345678, null)]
[InlineData("ঋণাত্মক একশ তেইশ কোটি পঁয়তাল্লিশ লক্ষ সাতষট্টি হাজার আটশ নব্বই", -1234567890, null)]
public void TryToNumber_ValidInput_Bengali(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("fi-FI")]
public class WordsToNumberTests_Finnish
{
[Theory]
[InlineData("nolla", 0, null)]
[InlineData("satayksi", 101, null)]
[InlineData("tuhat kaksi", 1002, null)]
[InlineData("yksitoista", 11, null)]
[InlineData("miinus viisitoista", -15, null)]
public void TryToNumber_ValidInput_Finnish(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Fact]
public void TryToNumber_PreservesSuffixScaleSemantics()
{
var legitimateWords = string.Concat(Enumerable.Repeat("tuhat", 130));
Assert.Equal(130_000, legitimateWords.ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(130_000.1m, $"{legitimateWords} pilkku yksi".ToDecimalNumber(CultureInfo.CurrentCulture));
Assert.Equal(1_129_001, $"{legitimateWords}tuhattayksi".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(131_000, $"{legitimateWords}tuhatta".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(130_000_000_000, $"{legitimateWords}miljoonaa".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(130_002_000_000, $"{legitimateWords}kaksimiljoonaa".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(130_000_000_001, $"{legitimateWords}miljoonaayksi".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(130_200, $"{legitimateWords}kaksisataa".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(130_021, $"{legitimateWords}kaksikymmentayksi".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(130_011, $"{legitimateWords}yksitoista".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(1_000_001, "tuhattuhattayksi".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(1_000_000_000_001, "miljoonamiljoonaayksi".ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(9_000_000_000_000_000_000, string.Concat(Enumerable.Repeat("triljoona", 9)).ToNumber(CultureInfo.CurrentCulture));
}
[Fact(Timeout = 60_000)]
public void TryToNumber_BoundsSuffixScaleWork()
{
var scaleWords = string.Concat(Enumerable.Repeat("tuhat", 100_000));
AssertParsedBounded(scaleWords, 100_000_000);
AssertParsedBounded(string.Concat(Enumerable.Repeat("kaksituhatta", 200)), 400_000);
AssertParsedBounded(string.Concat(Enumerable.Repeat("tuhatsata", 200)), 220_000);
AssertRejectedBounded($"{scaleWords}mysteeri");
void AssertParsedBounded(string words, long expected)
{
var allocatedBefore = GC.GetAllocatedBytesForCurrentThread();
Assert.True(words.TryToNumber(out var parsed, CultureInfo.CurrentCulture, out var unrecognizedWord), unrecognizedWord);
var allocated = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore;
var allocationLimit = checked((long)words.Length * 64);
Assert.Equal(expected, parsed);
Assert.Null(unrecognizedWord);
// Normalization has a fixed per-character cost; this linear ceiling still rejects quadratic growth.
Assert.True(allocated < allocationLimit, $"Suffix-scale parsing allocated {allocated:N0} bytes for {words.Length:N0} characters.");
}
void AssertRejectedBounded(string words)
{
var allocatedBefore = GC.GetAllocatedBytesForCurrentThread();
Assert.False(words.TryToNumber(out var parsed, CultureInfo.CurrentCulture, out var unrecognizedWord));
var allocated = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore;
var allocationLimit = checked((long)words.Length * 64);
Assert.Equal(0, parsed);
Assert.NotNull(unrecognizedWord);
Assert.True(allocated < allocationLimit, $"Suffix-scale rejection allocated {allocated:N0} bytes for {words.Length:N0} characters.");
}
}
[Fact(Timeout = 30_000)]
public void TryToNumber_RejectsSuffixScaleOverflow()
{
AssertRejected(string.Concat(Enumerable.Repeat("triljoona", 10)));
AssertRejected(string.Concat(Enumerable.Repeat("triljoona", 20)));
AssertRejected(string.Concat(Enumerable.Repeat("tuhatta", 130)));
void AssertRejected(string words)
{
var allocatedBefore = GC.GetAllocatedBytesForCurrentThread();
Assert.False(words.TryToNumber(out var parsed, CultureInfo.CurrentCulture, out var unrecognizedWord));
var allocated = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore;
var allocationLimit = Math.Max(8_000_000L, checked((long)words.Length * 64));
Assert.Equal(0, parsed);
Assert.NotNull(unrecognizedWord);
Assert.True(allocated < allocationLimit, $"Suffix-scale rejection allocated {allocated:N0} bytes for {words.Length:N0} characters.");
Assert.Throws<ArgumentException>(() => words.ToNumber(CultureInfo.CurrentCulture));
}
}
}
[UseCulture("af")]
public class WordsToNumberTests_Afrikaans
{
[Theory]
[InlineData("een", 1, null)]
[InlineData("een honderd twee en twintig", 122, null)]
[InlineData("drie duisend vyf honderd en een", 3501, null)]
[InlineData("een miljard een honderd en elf miljoen een honderd en elf duisend een honderd en elf", 1111111111, null)]
public void TryToNumber_ValidInput_Afrikaans(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("ar")]
public class WordsToNumberTests_Arabic
{
[Theory]
[InlineData("صفر", 0, null)]
[InlineData("اثنان و عشرون", 22, null)]
[InlineData("ثلاثة آلاف و خمس مئة و واحد", 3501, null)]
[InlineData("مليون و واحد", 1000001, null)]
[InlineData("سالب أحد عشر", -11, null)]
public void TryToNumber_ValidInput_Arabic(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("cs-CZ")]
public class WordsToNumberTests_Czech
{
[Theory]
[InlineData("nula", 0, null)]
[InlineData("sto dvanáct", 112, null)]
[InlineData("dvě sta pět", 205, null)]
[InlineData("jeden tisíc", 1000, null)]
[InlineData("dvě miliardy sto čtyřicet sedm milionů čtyři sta osmdesát tři tisíc šest set čtyřicet sedm", 2147483647, null)]
[InlineData("mínus jedna miliarda pět set jedna milionů jeden tisíc osm set devadesát dva", -1501001892, null)]
public void TryToNumber_ValidInput_Czech(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("el")]
public class WordsToNumberTests_Greek
{
[Theory]
[InlineData("ένα", 1, null)]
[InlineData("πενήντα τρία", 53, null)]
[InlineData("χίλια τετρακόσια εξήντα εννέα", 1469, null)]
[InlineData("δεκατέσσερα εκατομμύρια", 14000000, null)]
[InlineData("ένα δισεκατομμύριο ένα", 1000000001, null)]
public void TryToNumber_ValidInput_Greek(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("he")]
public class WordsToNumberTests_Hebrew
{
[Theory]
[InlineData("אפס", 0, null)]
[InlineData("מינוס שתיים", -2, null)]
[InlineData("מאה ואחת", 101, null)]
[InlineData("מאתיים ארבעים ואחת", 241, null)]
[InlineData("שבע מאות עשרים וחמש", 725, null)]
[InlineData("אלף ואחת עשרה", 1011, null)]
[InlineData("שבעת אלפים עשרים ואחת", 7021, null)]
[InlineData("עשרים ושמונה אלף מאה עשרים ושלוש", 28123, null)]
[InlineData("מיליון ואחת", 1_000_001, null)]
[InlineData("מיליארד ואחד", 1_000_000_001, null)]
[InlineData("שני מיליון ארבע מאות ושמונה", 2_000_408, null)]
public void TryToNumber_ValidInput_Hebrew(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Theory]
[InlineData("עשרים ושמונה hello", 0, "hello")]
[InlineData("מינוס xyz", 0, "xyz")]
public void TryToNumber_InvalidInput_Hebrew(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.False(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("hy")]
public class WordsToNumberTests_Armenian
{
[Theory]
[InlineData("մեկ", 1, null)]
[InlineData("տասներկու", 12, null)]
[InlineData("երեք հազար հինգ հարյուր մեկ", 3501, null)]
[InlineData("մեկ միլիոն", 1000000, null)]
[InlineData("մինուս հինգ հարյուր", -500, null)]
public void TryToNumber_ValidInput_Armenian(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
}
[UseCulture("hu-HU")]
public class WordsToNumberTests_Hungarian
{
[Theory]
[InlineData("nulla", 0, null)]
[InlineData("huszonegy", 21, null)]
[InlineData("száztizenegy", 111, null)]
[InlineData("kétezer-egy", 2001, null)]
[InlineData("egymilliárd-kétszázharmincnégymillió-ötszázhatvanhétezer-nyolcszázkilencven", 1234567890, null)]
[InlineData("mínusz tizennyolc", -18, null)]
public void TryToNumber_ValidInput_Hungarian(string words, int expectedNumber, string? expectedUnrecognizedWord)
{
Assert.True(words.TryToNumber(out var parsedNumber, CultureInfo.CurrentCulture, out var unrecognizedWord));
Assert.Equal(expectedUnrecognizedWord, unrecognizedWord);
Assert.Equal(expectedNumber, parsedNumber);
}
[Fact]
public void TryToNumber_BoundsPrefixedScaleRecursion()
{
var legitimateWords = string.Concat(Enumerable.Repeat("szaz", 130));
Assert.Equal(13000, legitimateWords.ToNumber(CultureInfo.CurrentCulture));
Assert.Equal(13000.1m, $"{legitimateWords} egész egy".ToDecimalNumber(CultureInfo.CurrentCulture));
Assert.Equal(1_000_081, "milliónyolcvannullamillióegy".ToNumber(CultureInfo.CurrentCulture));
var scaleWords = string.Concat(Enumerable.Repeat("szaz", 32_000));
AssertParsedBounded(scaleWords, 3_200_000);
AssertParsedBounded(string.Join("-", Enumerable.Repeat("szaz", 32_000)), 3_200_000);
AssertParsedBounded(string.Concat(Enumerable.Repeat("ketszaz", 32_000)), 6_400_000);
Assert.Equal(9_000_000_000_000_000_000, string.Concat(Enumerable.Repeat("trillio", 9)).ToNumber(CultureInfo.CurrentCulture));
AssertRejected(string.Concat(Enumerable.Repeat("trillio", 10)));
AssertParsedBounded($"mínusz {scaleWords}", -3_200_000);
AssertRejected(string.Concat(Enumerable.Repeat("tizen", 20_000)) + "egy");
AssertRejected(string.Concat(Enumerable.Repeat("harminc", 15_000)) + "egy");
AssertRejected(string.Concat(Enumerable.Repeat("tizen", 256)) + "egyszaz");
Assert.True($"{scaleWords} egész egy".TryToDecimalNumber(
out var parsedDecimal,
CultureInfo.CurrentCulture,
out var unrecognizedWord));
Assert.Equal(3_200_000.1m, parsedDecimal);
Assert.Null(unrecognizedWord);
void AssertParsedBounded(string words, long expected)
{
var allocatedBefore = GC.GetAllocatedBytesForCurrentThread();
Assert.True(words.TryToNumber(out var parsed, CultureInfo.CurrentCulture, out var unrecognizedWord));
var allocated = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore;
Assert.Equal(expected, parsed);
Assert.Null(unrecognizedWord);
Assert.True(allocated < 8_000_000, $"Prefixed-scale parsing allocated {allocated:N0} bytes.");
Assert.Equal(expected, words.ToNumber(CultureInfo.CurrentCulture));
}
void AssertRejected(string words)
{
var allocatedBefore = GC.GetAllocatedBytesForCurrentThread();
Assert.False(words.TryToNumber(out var parsed, CultureInfo.CurrentCulture, out var unrecognizedWord));
var allocated = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore;
Assert.Equal(0, parsed);
Assert.NotNull(unrecognizedWord);
Assert.True(allocated < 8_000_000, $"Prefixed-scale rejection allocated {allocated:N0} bytes.");
Assert.Throws<ArgumentException>(() => words.ToNumber(CultureInfo.CurrentCulture));
}
}
}
[UseCulture("ku")]
public class WordsToNumberTests_Kurdish
{
[Theory]
[InlineData("یەک", 1, null)]
[InlineData("سەد و بیست و دوو", 122, null)]
[InlineData("سێ هەزار و پێنج سەد و یەک", 3501, null)]