1+ using Noggog ;
2+ using Shouldly ;
3+
4+ namespace CSharpExt . UnitTests ;
5+
6+ public class PercentTests
7+ {
8+ [ Fact ]
9+ public void Typical ( )
10+ {
11+ var p = new Percent ( 0.5 ) ;
12+ p . Value . ShouldBe ( 0.5 ) ;
13+ }
14+
15+ [ Fact ]
16+ public void OutOfRange ( )
17+ {
18+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) =>
19+ {
20+ new Percent ( - 0.1 ) ;
21+ } ) ;
22+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) =>
23+ {
24+ new Percent ( 1.1 ) ;
25+ } ) ;
26+ }
27+
28+ [ Fact ]
29+ public void Inverse ( )
30+ {
31+ var p = new Percent ( 0.7 ) ;
32+ p . Inverse . Value . EqualsWithin ( 0.3 ) . ShouldBeTrue ( ) ;
33+ p . Inverse . Inverse . Value . EqualsWithin ( 0.7 ) . ShouldBeTrue ( ) ;
34+ }
35+
36+ [ Fact ]
37+ public void Add ( )
38+ {
39+ var p = new Percent ( 0.15d ) ;
40+ var p2 = new Percent ( 0.25d ) ;
41+ var p3 = p + p2 ;
42+ p3 . Value . ShouldBe ( 0.4d ) ;
43+ var p4 = new Percent ( 0.7 ) ;
44+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) =>
45+ {
46+ var p5 = p3 + p4 ;
47+ } ) ;
48+ }
49+
50+ [ Fact ]
51+ public void Subtract ( )
52+ {
53+ var p = new Percent ( 0.7d ) ;
54+ var p2 = new Percent ( 0.25d ) ;
55+ var p3 = p - p2 ;
56+ p3 . Value . EqualsWithin ( 0.45d ) . ShouldBeTrue ( ) ;
57+ var p4 = new Percent ( 0.7 ) ;
58+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) =>
59+ {
60+ var p5 = p3 - p4 ;
61+ } ) ;
62+ }
63+
64+ [ Fact ]
65+ public void Multiply ( )
66+ {
67+ var p = new Percent ( 0.5d ) ;
68+ var p2 = new Percent ( 0.25d ) ;
69+ var p3 = p * p2 ;
70+ p3 . Value . ShouldBe ( 0.125d ) ;
71+ }
72+
73+ [ Fact ]
74+ public void Divide ( )
75+ {
76+ var p = new Percent ( 0.5d ) ;
77+ var p2 = new Percent ( 0.25d ) ;
78+ var p3 = p / p2 ;
79+ p3 . ShouldBe ( 2d ) ;
80+ }
81+
82+ [ Fact ]
83+ public void PutInRange ( )
84+ {
85+ var p1 = Percent . FactoryPutInRange ( 1.1 ) ;
86+ p1 . Value . ShouldBe ( 1 ) ;
87+ var p2 = Percent . FactoryPutInRange ( - 0.1 ) ;
88+ p2 . Value . ShouldBe ( 0 ) ;
89+ var p3 = Percent . FactoryPutInRange ( 0.5 ) ;
90+ p3 . Value . ShouldBe ( 0.5 ) ;
91+ }
92+
93+ [ Fact ]
94+ public void PutInRangeWithMax ( )
95+ {
96+ var p1 = Percent . FactoryPutInRange ( 500 , 255 ) ;
97+ p1 . Value . ShouldBe ( 1 ) ;
98+ var p2 = Percent . FactoryPutInRange ( - 5 , 255 ) ;
99+ p2 . Value . ShouldBe ( 0 ) ;
100+ var p3 = Percent . FactoryPutInRange ( 50 , 255 ) ;
101+ p3 . Value . EqualsWithin ( 0.196078431372549 ) . ShouldBeTrue ( ) ;
102+ }
103+
104+ [ Fact ]
105+ public void PutInRangeWithMaxLong ( )
106+ {
107+ var p1 = Percent . FactoryPutInRange ( 500L , 255L ) ;
108+ p1 . Value . ShouldBe ( 1 ) ;
109+ var p2 = Percent . FactoryPutInRange ( - 5L , 255L ) ;
110+ p2 . Value . ShouldBe ( 0 ) ;
111+ var p3 = Percent . FactoryPutInRange ( 50L , 255L ) ;
112+ p3 . Value . EqualsWithin ( 0.196078431372549 ) . ShouldBeTrue ( ) ;
113+ }
114+
115+ [ Fact ]
116+ public void Equality ( )
117+ {
118+ var p1 = new Percent ( 0.123 ) ;
119+ var p2 = new Percent ( 0.123 ) ;
120+ p1 . ShouldBe ( p2 ) ;
121+ p1 . GetHashCode ( ) . ShouldBe ( p2 . GetHashCode ( ) ) ;
122+ var b = p1 == p2 ;
123+ b . ShouldBeTrue ( ) ;
124+ var b2 = p1 != p2 ;
125+ b2 . ShouldBeFalse ( ) ;
126+ }
127+
128+ [ Fact ]
129+ public void NonEquality ( )
130+ {
131+ var p1 = new Percent ( 0.123 ) ;
132+ var p2 = new Percent ( 0.124 ) ;
133+ p1 . ShouldNotBe ( p2 ) ;
134+ p1 . GetHashCode ( ) . ShouldNotBe ( p2 . GetHashCode ( ) ) ;
135+ var b = p1 == p2 ;
136+ b . ShouldBeFalse ( ) ;
137+ var b2 = p1 != p2 ;
138+ b2 . ShouldBeTrue ( ) ;
139+ }
140+
141+ [ Fact ]
142+ public void LessThanCompare ( )
143+ {
144+ var p1 = new Percent ( 0.1 ) ;
145+ var p2 = new Percent ( 0.2 ) ;
146+ var c = p1 < p2 ;
147+ c . ShouldBeTrue ( ) ;
148+ c = p2 < p1 ;
149+ c . ShouldBeFalse ( ) ;
150+ c = p1 < p1 ;
151+ c . ShouldBeFalse ( ) ;
152+ }
153+
154+ [ Fact ]
155+ public void LessThanOrEqualCompare ( )
156+ {
157+ var p1 = new Percent ( 0.1 ) ;
158+ var p2 = new Percent ( 0.2 ) ;
159+ var c = p1 <= p2 ;
160+ c . ShouldBeTrue ( ) ;
161+ c = p2 <= p1 ;
162+ c . ShouldBeFalse ( ) ;
163+ c = p1 <= p1 ;
164+ c . ShouldBeTrue ( ) ;
165+ }
166+
167+ [ Fact ]
168+ public void GreaterThanOrEqualCompare ( )
169+ {
170+ var p1 = new Percent ( 0.1 ) ;
171+ var p2 = new Percent ( 0.2 ) ;
172+ var c = p1 >= p2 ;
173+ c . ShouldBeFalse ( ) ;
174+ c = p2 >= p1 ;
175+ c . ShouldBeTrue ( ) ;
176+ c = p1 >= p1 ;
177+ c . ShouldBeTrue ( ) ;
178+ }
179+
180+ [ Fact ]
181+ public void GreaterThanCompare ( )
182+ {
183+ var p1 = new Percent ( 0.1 ) ;
184+ var p2 = new Percent ( 0.2 ) ;
185+ var c = p1 > p2 ;
186+ c . ShouldBeFalse ( ) ;
187+ c = p2 > p1 ;
188+ c . ShouldBeTrue ( ) ;
189+ c = p1 > p1 ;
190+ c . ShouldBeFalse ( ) ;
191+ }
192+ }
0 commit comments