/
Fraction_specs.cs
956 lines (812 loc) · 27 KB
/
Fraction_specs.cs
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
using Qowaiv.Specs.TestTools;
namespace Mathematics.Fraction_specs;
public class Defines
{
[TestCase("0", "0")]
[TestCase("17/3", "17/3")]
[TestCase("-7/3", "-7/3")]
public void plus(Fraction fraction, Fraction negated) => (+fraction).Should().Be(negated);
[TestCase("0", "0")]
[TestCase("-7/3", "+7/3")]
[TestCase("+7/3", "-7/3")]
public void negation(Fraction fraction, Fraction negated) => (-fraction).Should().Be(negated);
[Test]
public void increment()
{
var fraction = Svo.Fraction;
fraction++;
fraction.Should().Be(-52.DividedBy(17));
}
[Test]
public void decrement()
{
var fraction = Svo.Fraction;
fraction--;
fraction.Should().Be(-86.DividedBy(17));
}
public class Multiplication
{
[TestCase("1/3", "0", "0")]
[TestCase("0", "5/7", "0")]
[TestCase("1/3", "1/4", "1/12")]
[TestCase("-1/3", "-1/4", "1/12")]
[TestCase("1/4", "4/7", "1/7")]
[TestCase("2/5", "11/16", "11/40")]
[TestCase("-2/5", "4", "-8/5")]
[TestCase("2/3", "-8/9", "-16/27")]
public void between_fractions(Fraction left, Fraction right, Fraction product)
=> (left * right).Should().Be(product);
[Test]
public void between_fractions_and_longs()
=> (1.DividedBy(3) * 4L).Should().Be(4.DividedBy(3));
[Test]
public void between_fractions_and_ints()
=> (1.DividedBy(3) * 4).Should().Be(4.DividedBy(3));
[Test]
public void between_longs_and_fractions()
=> (4L * 1.DividedBy(3)).Should().Be(4.DividedBy(3));
[Test]
public void between_ints_and_fractions()
=> (4 * 1.DividedBy(3)).Should().Be(4.DividedBy(3));
}
public class Division
{
[TestCase("0", "1/3", "0")]
[TestCase("1/3", "1/4", "4/3")]
[TestCase("-1/3", "-1/4", "4/3")]
[TestCase("1/4", "4/7", "7/16")]
[TestCase("2/5", "11/16", "32/55")]
[TestCase("-2/5", "4", "-1/10")]
[TestCase("2/3", "-8/9", "-3/4")]
public void between_Fractions(Fraction left, Fraction right, Fraction division)
=> (left / right).Should().Be(division);
[Test]
public void between_fractions([RandomFraction(3)] Fraction left, [RandomFraction(3, false)] Fraction right)
{
var division = ((decimal)left) / ((decimal)right);
(left / right).Should().Be(division.Fraction());
}
[Test]
public void between_fractions_and_longs()
=> (1.DividedBy(3) / 4L).Should().Be(1.DividedBy(12));
[Test]
public void between_fractions_and_ints()
=> (1.DividedBy(3) / 4).Should().Be(1.DividedBy(12));
[Test]
public void between_longs_and_fractions()
=> (5L / 2.DividedBy(3)).Should().Be(15.DividedBy(2));
[Test]
public void between_ints_and_fractions()
=> (5 / 2.DividedBy(3)).Should().Be(15.DividedBy(2));
}
public class Addition
{
[TestCase("1/4", "0", "1/4")]
[TestCase("0", "1/4", "1/4")]
[TestCase("5/7", "0", "5/7")]
[TestCase("1/3", "1/4", "7/12")]
[TestCase("1/4", "1/3", "7/12")]
[TestCase("1/4", "1/12", "1/3")]
[TestCase("-1/4", "-1/12", "-1/3")]
[TestCase("-1/4", "1/12", "-1/6")]
[TestCase("1/5", "2/5", "3/5")]
[TestCase("8/3", "1/2", "19/6")]
public void between_fractions(Fraction left, Fraction right, Fraction addition)
=> (left + right).Should().Be(addition);
[Test]
public void between_fractions([RandomFraction(3)] Fraction left, [RandomFraction(3)] Fraction right)
{
var sum = ((decimal)left) + ((decimal)right);
(left + right).Should().Be(sum.Fraction());
}
[Test]
public void between_fractions_and_longs()
=> (1.DividedBy(3) + 4L).Should().Be(13.DividedBy(3));
[Test]
public void between_fractions_and_ints()
=> (1.DividedBy(3) + 4).Should().Be(13.DividedBy(3));
[Test]
public void between_longs_and_fractions()
=> (4L + 1.DividedBy(3)).Should().Be(13.DividedBy(3));
[Test]
public void between_ints_and_fractions()
=> (4 + 1.DividedBy(3)).Should().Be(13.DividedBy(3));
}
public class Subtraction
{
[TestCase("1/4", "0", "1/4")]
[TestCase("0", "1/4", "-1/4")]
[TestCase("1/3", "1/4", "1/12")]
[TestCase("1/4", "1/3", "-1/12")]
[TestCase("1/4", "1/12", "1/6")]
[TestCase("-1/4", "-1/12", "-1/6")]
[TestCase("-1/4", "1/12", "-1/3")]
public void between_fractions(Fraction left, Fraction right, Fraction subtraction)
=> (left - right).Should().Be(subtraction);
[Test]
public void between_fractions([RandomFraction(3)] Fraction left, [RandomFraction(3)] Fraction right)
{
var subtraction = ((decimal)left) - ((decimal)right);
(left - right).Should().Be(subtraction.Fraction());
}
[Test]
public void between_fractions_and_longs()
=> (1.DividedBy(3) - 4L).Should().Be(-11.DividedBy(3));
[Test]
public void between_fractions_and_ints()
=> (1.DividedBy(3) - 4).Should().Be(-11.DividedBy(3));
[Test]
public void between_longs_and_fractions()
=> (4L - 1.DividedBy(3)).Should().Be(11.DividedBy(3));
[Test]
public void between_ints_and_fractions()
=> (4 - 1.DividedBy(3)).Should().Be(11.DividedBy(3));
}
public class Modulation
{
[TestCase("5/4", "1/1", "1/4")]
[TestCase("-5/4", "1/1", "-1/4")]
[TestCase("5/3", "2/3", "1/3")]
public void between_fractions(Fraction fraction, Fraction divider, Fraction remainder)
=> (fraction % divider).Should().Be(remainder);
[Test]
public void between_fractions([RandomFraction(3)] Fraction fraction, [RandomFraction(3, false)] Fraction divider)
{
var modulo = ((decimal)fraction) % ((decimal)divider);
(fraction % divider).Should().Be(modulo.Fraction());
}
[Test]
public void between_fractions_and_longs()
=> (1.DividedBy(3) % 4L).Should().Be(1.DividedBy(3));
[Test]
public void between_fractions_and_ints()
=> (1.DividedBy(3) % 4).Should().Be(1.DividedBy(3));
[Test]
public void between_longs_and_fractions()
=> (4L % 3.DividedBy(1)).Should().Be(1.DividedBy(1));
[Test]
public void between_ints_and_fractions()
=> (4 % 3.DividedBy(1)).Should().Be(1.DividedBy(1));
}
[TestCase("0", "0")]
[TestCase("17/3", "17/3")]
[TestCase("-7/3", "+7/3")]
public void abs(Fraction fraction, Fraction absolute)
=> fraction.Abs().Should().Be(absolute);
[TestCase("+1/4", "4/1")]
[TestCase("-2/3", "-3/2")]
public void inverse(Fraction faction, Fraction inverse)
=> faction.Inverse().Should().Be(inverse);
[TestCase("0", 0)]
[TestCase("-1/4", -1)]
[TestCase("+1/4", +1)]
public void sign(Fraction fraction, int sign)
=> fraction.Sign().Should().Be(sign);
[TestCase("-69/17", 1)]
[TestCase("+69/17", 1)]
[TestCase("0", 0)]
public void remainder_as_positive_value(Fraction fraction, int remainder)
=> fraction.Remainder.Should().Be(remainder);
[TestCase("-69/17", -4)]
[TestCase("+69/17", +4)]
[TestCase("0", 0)]
public void whole(Fraction fraction, int remainder)
=> fraction.Whole.Should().Be(remainder);
}
public class Has_constant
{
[Test]
public void Zero_represent_0_divided_by_1()
=> Fraction.Zero.Should().BeEquivalentTo(new
{
Numerator = 0L,
Denominator = 1L,
});
[Test]
public void Zero_equal_to_default()
=> Fraction.Zero.Should().Be(default);
[Test]
public void One_represent_0_divided_by_1()
=> Fraction.One.Should().BeEquivalentTo(new
{
Numerator = 1L,
Denominator = 1L,
});
[Test]
public void Epsilon_represent_0_divided_by_1()
=> Fraction.Epsilon.Should().BeEquivalentTo(new
{
Numerator = 1L,
Denominator = long.MaxValue,
});
[Test]
public void MinValue_represent_0_divided_by_1()
=> Fraction.MinValue.Should().BeEquivalentTo(new
{
Numerator = -long.MaxValue,
Denominator = 1L,
});
[Test]
public void MaxValue_represent_0_divided_by_1()
=> Fraction.MaxValue.Should().BeEquivalentTo(new
{
Numerator = long.MaxValue,
Denominator = 1L,
});
}
public class Prevents_overflow
{
[Test]
public void on_additions()
{
var l = 1.DividedBy(4_000_000_000L);
var r = 1.DividedBy(8_000_000_000L);
(l + r).Should().Be(3.DividedBy(8_000_000_000L));
}
[Test]
public void on_multiplications()
{
var l = 1.DividedBy(4_000_000_000L);
var r = 8_000_000_000L.DividedBy(3);
(l * r).Should().Be(2.DividedBy(3));
}
}
public class Does_not_define
{
[Test]
public void inverse_on_zero()
=> Fraction.Zero.Invoking(f => f.Inverse()).Should().Throw<DivideByZeroException>();
}
public class Throws_when
{
[Test]
public void multiplication_can_not_be_represented_by_a_long()
{
var x = (long.MaxValue - 3).DividedBy(1);
var y = (long.MaxValue - 4).DividedBy(1);
x.Invoking(_ => x * y)
.Should().Throw<OverflowException>()
.WithMessage("Arithmetic operation resulted in an overflow.*");
}
[Test]
public void division_can_not_be_represented_by_a_long()
{
var x = (long.MaxValue - 3).DividedBy(1);
var y = (long.MaxValue - 4).DividedBy(long.MaxValue - 7);
x.Invoking(_ => x / y)
.Should().Throw<OverflowException>()
.WithMessage("Arithmetic operation resulted in an overflow.*");
}
[Test]
public void addition_can_not_be_represented_by_a_long()
{
var x = 17.DividedBy(long.MaxValue - 3);
var y = 13.DividedBy(long.MaxValue - 4);
x.Invoking(_ => x + y)
.Should().Throw<OverflowException>()
.WithMessage("Arithmetic operation resulted in an overflow.*");
}
[Test]
public void subtraction_can_not_be_represented_by_a_long()
{
var x = 17.DividedBy(long.MaxValue - 3);
var y = 13.DividedBy(long.MaxValue - 4);
x.Invoking(_ => x - y)
.Should().Throw<OverflowException>()
.WithMessage("Arithmetic operation resulted in an overflow.*");
}
[TestCase(-19223372036854775809.0)]
[TestCase(+19223372036854775809.0)]
public void double_can_not_be_casted_to_fraction(double dbl)
=> dbl.Invoking(d => (Fraction)d).Should().Throw<OverflowException>()
.WithMessage("Value was either too large or too small for a Fraction.");
[TestCase(-9223372036854775808.0)]
[TestCase(+9223372036854775808.0)]
public void decimal_can_not_be_casted_to_fraction(decimal dec)
=> dec.Invoking(d => (Fraction)d).Should().Throw<OverflowException>()
.WithMessage("Value was either too large or too small for a Fraction.");
[TestCase(-9223372036854775808.0)]
[TestCase(+9223372036854775808.0)]
public void fraction_can_no_be_created_form_double(double dbl)
=> dbl.Invoking(Fraction.Create).Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("Value was either too large or too small for a Fraction. *");
[TestCase(-9223372036854775808.0)]
[TestCase(+9223372036854775808.0)]
public void fraction_can_no_be_created_form_decimal(decimal dec)
=> dec.Invoking(Fraction.Create).Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("Value was either too large or too small for a Fraction. *");
[Test]
public void invalid_input_is_parsed()
=> "NaN".Invoking(Fraction.Parse)
.Should().Throw<FormatException>()
.WithMessage("Not a valid fraction");
}
public class Is_equal_by_value
{
[Test]
public void not_equal_to_null()
=> Svo.Fraction.Equals(null).Should().BeFalse();
[Test]
public void not_equal_to_other_type()
=> Svo.Fraction.Equals(new object()).Should().BeFalse();
[Test]
public void not_equal_to_different_value()
=> Svo.Fraction.Equals(17.DividedBy(42)).Should().BeFalse();
[Test]
public void equal_to_same_value()
=> Svo.Fraction.Equals(-69.DividedBy(17)).Should().BeTrue();
[Test]
public void equal_operator_returns_true_for_same_values()
=> (Svo.Fraction == -69.DividedBy(17)).Should().BeTrue();
[Test]
public void equal_operator_returns_false_for_different_values()
=> (Svo.Fraction == 17.DividedBy(42)).Should().BeFalse();
[Test]
public void not_equal_operator_returns_false_for_same_values()
=> (Svo.Fraction != -69.DividedBy(17)).Should().BeFalse();
[Test]
public void not_equal_operator_returns_true_for_different_values()
=> (Svo.Fraction != 17.DividedBy(42)).Should().BeTrue();
[TestCase(0, 0)]
[TestCase("17/42", 490960136)]
public void hash_code_is_value_based(Fraction svo, int hash)
{
using (Hash.WithoutRandomizer())
{
svo.GetHashCode().Should().Be(hash);
}
}
}
public class Can_be_parsed
{
[TestCase(17, 1, "17")]
[TestCase(17, 1, "+17")]
[TestCase(-12, 1, "-12")]
public void from_integer(long numerator, long denominator, string num)
=> Fraction.Parse(num, CultureInfo.InvariantCulture).Should().Be(numerator.DividedBy(denominator));
[TestCase(12_345, 1, "12,345")]
[TestCase(-1, 4, "-0.25")]
public void from_decimal(long numerator, long denominator, string dec)
=> Fraction.Parse(dec, CultureInfo.InvariantCulture).Should().Be(numerator.DividedBy(denominator));
[TestCase(487, 1000, "48.70%")]
[TestCase(487, 1000, "487.0‰")]
public void from_percentage(long numerator, long denominator, string percentage)
=> Fraction.Parse(percentage, CultureInfo.InvariantCulture).Should().Be(numerator.DividedBy(denominator));
[TestCase(+1, 2, "½")]
[TestCase(-1, 2, "-½")]
[TestCase(+3, 4, "¾")]
[TestCase(11, 4, "2¾")]
[TestCase(11, 4, "2 ¾")]
public void from_vulgar(long numerator, long denominator, string vulgar)
=> Fraction.Parse(vulgar).Should().Be(numerator.DividedBy(denominator));
[TestCase(9, 7, "1²/₇")]
[TestCase(3, 7, "³/7")]
[TestCase(9, 7, "1 ²/7")]
[TestCase(23, 47, "²³/₄₇")]
public void from_super_script_numerator(long numerator, long denominator, string superScript)
=> Fraction.Parse(superScript).Should().Be(numerator.DividedBy(denominator));
[TestCase(-2, 7, "-²/₇")]
[TestCase(+9, 7, "1 2/₇")]
[TestCase(+3, 7, "3/₇")]
[TestCase(23, 47, "23/₄₇")]
public void from_sub_script_denominator(long numerator, long denominator, string subScript)
=> Fraction.Parse(subScript).Should().Be(numerator.DividedBy(denominator));
[TestCase(1, 3, "+1/3")]
[TestCase(-1, 3, "-1/3")]
[TestCase(11, 43, "11/43")]
[TestCase(4, 3, "1 1/3")]
[TestCase(21, 2, "10 1/2")]
public void from_fraction_strings(long numerator, long denominator, string str)
=> Fraction.Parse(str, CultureInfo.InvariantCulture).Should().Be(numerator.DividedBy(denominator));
[TestCase("1/3")]
[TestCase("1:3")]
[TestCase("1÷3")]
[TestCase("1⁄3")]
[TestCase("1⁄3")]
[TestCase("1⁄3")]
[TestCase("1∕3")]
public void from_multiple_bar_chars(string bar)
=> Fraction.Parse(bar, CultureInfo.InvariantCulture).Should().Be(1.DividedBy(3));
[Test]
public void without_specifying_format_provider()
{
Fraction.TryParse("-69/17", out var fraction).Should().BeTrue();
fraction.Should().Be(Svo.Fraction);
}
[Test]
public void using_pure_try_parse() => Fraction.TryParse("-69/17").Should().Be(Svo.Fraction);
}
public class Can_not_be_parsed
{
[Test]
public void string_empty() => Fraction.TryParse(string.Empty).Should().BeNull();
[Test]
public void @null() => Fraction.TryParse(null).Should().BeNull();
[TestCase("--3/7")]
[TestCase("-+3/7")]
[TestCase("+-3/7")]
[TestCase("++3/7")]
public void multiple_signs(string multiple) => Fraction.TryParse(multiple).Should().BeNull();
[TestCase("3/+7")]
public void plus_sign_denominator(string denominator) => Fraction.TryParse(denominator).Should().BeNull();
[TestCase("3/0")]
[TestCase("²/₀")]
public void zero_denominator(string divideByZero) => Fraction.TryParse(divideByZero).Should().BeNull();
[TestCase("NaN", "NaN")]
[TestCase("-Infinity", "-Infinity")]
[TestCase("+Infinity", "+Infinity")]
[TestCase("0xFF", "Hexa-decimal")]
[TestCase("15/", "Ends with an operator")]
[TestCase("1//4", "Two division operators")]
[TestCase("1/½", "Vulgar with division operator")]
[TestCase("½1", "Vulgar not at the end")]
[TestCase("²3/₇", "Normal and superscript mixed")]
[TestCase("²/₇3", "Normal and subscript mixed")]
[TestCase("²/3₇", "Normal and subscript mixed")]
[TestCase("₇/3", "Subscript first")]
[TestCase("92233720368547758 17/32464364", "Numerator overflow")]
[TestCase("9223372036854775808", "Long.MaxValue + 1")]
[TestCase("-9223372036854775808", "Long.MinValue")]
[TestCase("-9223372036854775809", "Long.MinValue - 1")]
public void non_fractional_strings(string str, string because)
=> Fraction.TryParse(str).Should().BeNull(because);
[Test]
public void retrieving_null_for_invalid_input() => Fraction.TryParse("invalid").Should().BeNull();
}
public class Can_be_created
{
[TestCase("0/1", 0, 8, "Should set zero")]
[TestCase("1/4", 2, 8, "Should reduce")]
[TestCase("-1/4", -2, 8, "Should reduce")]
[TestCase("1/4", 3, 12, "Should reduce")]
[TestCase("-1/4", -3, 12, "Should reduce")]
[TestCase("3/7", -3, -7, "Should have no signs")]
[TestCase("-3/7", 3, -7, "Should have no sign on denominator")]
[TestCase("-3/7", -3, 7, "Should have no sign on denominator")]
public void with_constructor(Fraction fraction, long numerator, long denominator, string because)
=> new Fraction(numerator, denominator).Should().Be(fraction, because);
[TestCase(0, 1, "0")]
[TestCase(00000003, 000000010, "0.3")]
[TestCase(00000033, 000000100, "0.33")]
[TestCase(00000333, 000001000, "0.333")]
[TestCase(00003333, 000010000, "0.3333")]
[TestCase(00033333, 000100000, "0.33333")]
[TestCase(00333333, 001000000, "0.333333")]
[TestCase(03333333, 010000000, "0.3333333")]
[TestCase(33333333, 100000000, "0.33333333")]
[TestCase(333333333, 1000000000, "0.333333333")]
[TestCase(1, 3, "0.33333333333333333333333")]
public void from_decimals(long numerator, long denominator, decimal number)
=> Fraction.Create(number).Should().Be(numerator.DividedBy(denominator));
[TestCase(0, 1, 0.5, 0.6)]
[TestCase(1, 1, 0.6, 0.5)]
public void from_decimals_with_error(long numerator, long denominator, decimal number, decimal error)
=> Fraction.Create(number, error).Should().Be(numerator.DividedBy(denominator));
[TestCase(100)]
public void from_decimals_without_precision_loss(int runs)
{
var rnd = new Random();
var failures = new List<Fraction>(runs);
foreach (var fraction in Enumerable.Range(0, runs).Select(i => rnd.Next(int.MinValue, int.MaxValue).DividedBy(rnd.Next(3, int.MaxValue))))
{
var created = Fraction.Create((decimal)fraction);
if (created != fraction)
{
failures.Add(fraction);
}
}
failures.Should().BeEmpty();
}
[Test]
public void applying_greatest_common_divisor()
=> 60.DividedBy(420).Should().BeEquivalentTo(new
{
Numerator = 1L,
Denominator = 7L,
});
}
public class Can_not_be_created
{
[TestCase(-10e18)]
[TestCase(+10e18)]
public void from_decimal_out_of_long_range(decimal dec)
{
Func<Fraction> create = () => Fraction.Create(dec);
create.Should().Throw<ArgumentOutOfRangeException>();
}
[TestCase(1e-19)]
[TestCase(+1.000001)]
public void from_decimal_with_error_out_of_range(decimal error)
{
Func<Fraction> create = () => Fraction.Create(0, error);
create.Should().Throw<ArgumentOutOfRangeException>();
}
}
public class Can_be_casted_explicit
{
[TestCase(0, 0)]
[TestCase("-69/17", -69 / 17)]
public void to_Int32(Fraction fraction, int casted) => ((int)fraction).Should().Be(casted);
[TestCase(0, 0)]
[TestCase("-69/17", -69 / 17)]
public void to_Int64(Fraction fraction, long casted) => ((long)fraction).Should().Be(casted);
[TestCase(0, 0)]
[TestCase("-69/17", -69.0 / 17.0)]
public void to_double(Fraction fraction, double casted) => ((double)fraction).Should().Be(casted);
[TestCase(0, 0)]
[TestCase(-84, -84)]
public void to_decimal(Fraction fraction, decimal casted) => ((decimal)fraction).Should().Be(casted);
[Test]
public void to_percent() => ((Percentage)1.DividedBy(4)).Should().Be(25.Percent());
[Test]
public void from_Int32() => ((Fraction)42).Should().Be(42.DividedBy(1));
[Test]
public void from_Int64() => ((Fraction)42L).Should().Be(42.DividedBy(1));
[Test]
public void from_double() => ((Fraction)(-1.0 / 16.0)).Should().Be(-1.DividedBy(16));
[Test]
public void from_decimal() => ((Fraction)(-69m / 17m)).Should().Be(Svo.Fraction);
[Test]
public void from_percent() => ((Fraction)25.Percent()).Should().Be(1.DividedBy(4));
}
public class Is_comparable
{
[Test]
public void to_null_is_1() => Svo.Fraction.CompareTo(Nil.Object).Should().Be(1);
[Test]
public void to_Fraction_as_object()
{
object obj = Svo.Fraction;
Svo.Fraction.CompareTo(obj).Should().Be(0);
}
[Test]
public void to_Fraction_only()
=> new object().Invoking(Svo.Fraction.CompareTo).Should().Throw<ArgumentException>();
[Test]
public void can_be_sorted_using_compare()
{
var sorted = new[]
{
(-1).DividedBy(12),
Fraction.Zero,
1.DividedBy(42),
1.DividedBy(17),
1.DividedBy(11),
201.DividedBy(42),
};
var list = new List<Fraction> { sorted[3], sorted[4], sorted[5], sorted[2], sorted[0], sorted[1] };
list.Sort();
list.Should().BeEquivalentTo(sorted);
}
[Test]
public void by_operators_for_different_values()
{
var smaller = 1.DividedBy(17);
var bigger = 2.DividedBy(3);
(smaller < bigger).Should().BeTrue();
(smaller <= bigger).Should().BeTrue();
(smaller > bigger).Should().BeFalse();
(smaller >= bigger).Should().BeFalse();
}
[Test]
public void by_operators_for_equal_values()
{
var left = 1.DividedBy(17);
var right = 1.DividedBy(17);
(left < right).Should().BeFalse();
(left <= right).Should().BeTrue();
(left > right).Should().BeFalse();
(left >= right).Should().BeTrue();
}
}
public class Has_custom_formatting
{
[Test]
public void _default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.Fraction.ToString().Should().Be("-69/17");
}
}
[Test]
public void with_null_pattern_equal_to_default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.Fraction.ToString().Should().Be(Svo.Fraction.ToString(default(string)));
}
}
[Test]
public void with_string_empty_pattern_equal_to_default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.Fraction.ToString().Should().Be(Svo.Fraction.ToString(string.Empty));
}
}
[Test]
public void default_value_is_represented_as_zero()
=> default(Fraction).ToString().Should().Be("0/1");
[Test]
public void with_empty_format_provider()
{
using (TestCultures.es_EC.Scoped())
{
Svo.Fraction.ToString(FormatProvider.Empty).Should().Be("-69/17");
}
}
[Test]
public void custom_format_provider_is_applied()
{
var formatted = Svo.Fraction.ToString("[0]super⁄sub", FormatProvider.CustomFormatter);
formatted.Should().Be("Unit Test Formatter, value: '-4¹⁄₁₇', format: '[0]super⁄sub'");
}
[TestCase(null, /*............*/ "-2/7", "-2/7")]
[TestCase("", /*..............*/ "-2/7", "-2/7")]
[TestCase("0:0", /*...........*/ "-2:7", "-2/7")]
[TestCase("0÷0", /*...........*/ "4÷3", "4/3")]
[TestCase("[0]0/0", /*........*/ "1 1/3", "4/3")]
[TestCase("[0]0/0", /*........*/ "-1 1/3", "-4/3")]
[TestCase("[0 ]0/0",/*........*/ "-1 1/3", "-4/3")]
[TestCase("#.00", /*..........*/ ".33", "1/3")]
[TestCase("[0]super⁄sub", /*..*/ "5¹¹⁄₁₂", "71/12")]
[TestCase("[0]super⁄0", /*....*/ "5¹¹⁄12", "71/12")]
[TestCase("[0] 0⁄sub", /*.....*/ "5 11⁄₁₂", "71/12")]
[TestCase("[0]super⁄sub", /*..*/ "-3¹⁄₂", "-7/2")]
[TestCase("[0 ]super⁄sub", /*.*/ "-3 ¹⁄₂", "-7/2")]
[TestCase("[#]super⁄sub", /*..*/ "-¹⁄₂", "-1/2")]
[TestCase("[0]super⁄sub", /*..*/ "-0¹⁄₂", "-1/2")]
[TestCase("super⁄sub", /*.....*/ "⁷¹⁄₁₂", "71/12")]
[TestCase("super⁄sub", /*.....*/ "-⁷⁄₂", "-7/2")]
public void for_format(string format, string formatted, Fraction fraction)
=> fraction.ToString(format, CultureInfo.InvariantCulture).Should().Be(formatted);
[Test]
public void that_throws_for_invalid_formats()
=> "/invalid".Invoking(Svo.Fraction.ToString).Should().Throw<FormatException>();
}
public class Has_humanizer_creators
{
[Test]
public void int_DividedBy_int()
=> 12.DividedBy(24).Should().Be(new Fraction(1, 2));
[Test]
public void long_DividedBy_long()
=> 12L.DividedBy(24L).Should().Be(new Fraction(1, 2));
[Test]
public void Fraction_from_double()
=> 0.5.Fraction().Should().Be(new Fraction(1, 2));
[Test]
public void Fraction_from_decimal()
=> 0.5m.Fraction().Should().Be(new Fraction(1, 2));
}
public class Supports_type_conversion
{
[Test]
public void via_TypeConverter_registered_with_attribute()
=> typeof(Fraction).Should().HaveTypeConverterDefined();
[Test]
public void from_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From("-69/17").To<Fraction>().Should().Be(Svo.Fraction);
}
}
[Test]
public void to_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.ToString().From(Svo.Fraction).Should().Be("-69/17");
}
}
}
public class Supports_JSON_serialization
{
#if NET6_0_OR_GREATER
[TestCase(4L, "4/1")]
[TestCase(0.25d, "1/4")]
[TestCase("13%", "13/100")]
[TestCase("14/42", "1/3")]
public void System_Text_JSON_deserialization(object json, Fraction svo)
=> JsonTester.Read_System_Text_JSON<Fraction>(json).Should().Be(svo);
[TestCase("1/3", "1/3")]
[TestCase("4/3", "4/3")]
public void System_Text_JSON_serialization(Fraction svo, object json)
=> JsonTester.Write_System_Text_JSON(svo).Should().Be(json);
#endif
[TestCase(4L, "4/1")]
[TestCase(3d, "3/1")]
[TestCase("13%", "13/100")]
[TestCase("14/42", "1/3")]
public void convention_based_deserialization(object json, Fraction svo)
=> JsonTester.Read<Fraction>(json).Should().Be(svo);
[TestCase("1/3", "1/3")]
[TestCase("4/3", "4/3")]
public void convention_based_serialization(Fraction svo, object json)
=> JsonTester.Write(svo).Should().Be(json);
[TestCase(double.MaxValue, typeof(OverflowException))]
[TestCase(double.MinValue, typeof(OverflowException))]
[TestCase("Invalid input", typeof(FormatException))]
[TestCase("2017-06-11", typeof(FormatException))]
public void throws_for_invalid_json(object json, Type exceptionType)
=> json
.Invoking(JsonTester.Read<Fraction>)
.Should().Throw<Exception>()
.And.Should().BeOfType(exceptionType);
}
#if NET8_0_OR_GREATER
#else
public class Supports_binary_serialization
{
[Test]
[Obsolete("Usage of the binary formatter is considered harmful.")]
public void using_BinaryFormatter()
{
var round_tripped = SerializeDeserialize.Binary(Svo.Fraction);
round_tripped.Should().Be(Svo.Fraction);
}
[Test]
public void storing_values_in_SerializationInfo()
{
var info = Serialize.GetInfo(Svo.Fraction);
info.GetInt64("numerator").Should().Be(-69);
info.GetInt64("denominator").Should().Be(17);
}
}
#endif
public class Supports_XML_serialization
{
[Test]
public void using_XmlSerializer_to_serialize()
{
var xml = Serialize.Xml(Svo.Fraction);
xml.Should().Be("-69/17");
}
[Test]
public void using_XmlSerializer_to_deserialize()
{
var svo = Deserialize.Xml<Fraction>("-69/17");
svo.Should().Be(Svo.Fraction);
}
[Test]
public void using_DataContractSerializer()
{
var round_tripped = SerializeDeserialize.DataContract(Svo.Fraction);
Svo.Fraction.Should().Be(round_tripped);
}
[Test]
public void as_part_of_a_structure()
{
var structure = XmlStructure.New(Svo.Fraction);
var round_tripped = SerializeDeserialize.Xml(structure);
structure.Should().Be(round_tripped);
}
[Test]
public void has_no_custom_XML_schema()
{
IXmlSerializable obj = Svo.Fraction;
obj.GetSchema().Should().BeNull();
}
}
public class Is_Open_API_data_type
{
[Test]
public void with_info()
=> OpenApiDataType.FromType(typeof(Fraction))
.Should().Be(new OpenApiDataType(
dataType: typeof(Fraction),
description: "Faction",
type: "string",
format: "faction",
pattern: "-?[0-9]+(/[0-9]+)?",
example: "13/42"));
}
public class Debugger
{
[TestCase("⁰⁄₁ = 0", "0")]
[TestCase("-⁴²⁄₁₇ = -2.47058824", "-42/17")]
public void has_custom_display(object display, Fraction svo)
=> svo.Should().HaveDebuggerDisplay(display);
}