3
3
using RandomDataGenerator . Randomizers ;
4
4
using Xunit . Abstractions ;
5
5
6
- namespace RandomDataGenerator . Tests
6
+ namespace RandomDataGenerator . Tests ;
7
+
8
+ public class CityRandomizerTests
7
9
{
8
- public class CityRandomizerTests
9
- {
10
- private readonly ITestOutputHelper _output ;
11
- static readonly Random random = new System . Random ( 420 ) ;
12
- static readonly object randLock = new object ( ) ;
10
+ private readonly ITestOutputHelper _output ;
11
+ private static readonly Random Random = new ( 420 ) ;
12
+ private static readonly object RandLock = new ( ) ;
13
+
14
+ public CityRandomizerTests ( ITestOutputHelper output )
15
+ {
16
+ _output = output ;
17
+ }
18
+
19
+ [ Theory ]
20
+ [ InlineData ( 1 ) ]
21
+ [ InlineData ( 2 ) ]
22
+ [ InlineData ( 4 ) ]
23
+ [ InlineData ( 8 ) ]
24
+ public void CityDistributionMustBeUniform ( int degree )
25
+ {
26
+ var locationGenerator = RandomizerFactory . GetRandomizer ( new FieldOptionsCity { ValueAsString = true , UseNullValues = false } ) ;
27
+ var concurrentDictionary = new ConcurrentDictionary < string , long > ( ) ;
28
+ var options = new ParallelOptions { MaxDegreeOfParallelism = degree } ;
29
+ Parallel . For ( 0 , 1000 , options , i =>
30
+ {
31
+ Parallel . For ( 0 , 1000 , options , j =>
32
+ {
33
+ var location = locationGenerator . Generate ( ) ;
34
+ concurrentDictionary . AddOrUpdate ( location , _ => 1 , ( k , v ) => v + 1 ) ;
35
+ } ) ;
36
+ } ) ;
37
+ var topCount = concurrentDictionary . OrderByDescending ( pair => pair . Value ) . First ( ) ;
38
+ var bottomCount = concurrentDictionary . OrderBy ( pair => pair . Value ) . First ( ) ;
39
+
40
+ _output . WriteLine ( $ "{ topCount } ") ;
41
+ _output . WriteLine ( $ "{ bottomCount } ") ;
42
+
43
+ Assert . True ( topCount . Value / bottomCount . Value < 2 ) ;
44
+ Assert . NotEqual ( topCount . Key , bottomCount . Key ) ;
45
+ }
13
46
14
- public CityRandomizerTests ( ITestOutputHelper output )
15
- {
16
- this . _output = output ;
17
- }
47
+ [ Fact ]
48
+ public void TwoRandomCitiesMustNotBeTheSame ( )
49
+ {
50
+ var locationGenerator = RandomizerFactory . GetRandomizer ( new FieldOptionsCity { ValueAsString = true , UseNullValues = false } ) ;
51
+ var locationOne = locationGenerator . Generate ( ) ;
52
+ var locationTwo = locationGenerator . Generate ( ) ;
18
53
54
+ Assert . NotEqual ( locationOne , locationTwo ) ;
55
+ }
19
56
20
- [ Theory ]
21
- [ InlineData ( 1 ) ]
22
- [ InlineData ( 2 ) ]
23
- [ InlineData ( 4 ) ]
24
- [ InlineData ( 8 ) ]
25
- public void CityDistributionMustBeUniform ( int degree )
26
- {
27
- var locationGenerator = RandomizerFactory . GetRandomizer ( new FieldOptionsCity { ValueAsString = true , UseNullValues = false } ) ;
28
- var concurrentDictionary = new ConcurrentDictionary < string , long > ( ) ;
29
- var options = new ParallelOptions { MaxDegreeOfParallelism = degree } ;
30
- Parallel . For ( 0 , 1000 , options , i =>
31
- {
32
- Parallel . For ( 0 , 1000 , options , j =>
33
- {
34
- var location = locationGenerator . Generate ( ) ;
35
- concurrentDictionary . AddOrUpdate ( location , _ => 1 , ( k , v ) => v + 1 ) ;
36
- } ) ;
37
- } ) ;
38
- var topCount = concurrentDictionary . OrderByDescending ( pair => pair . Value ) . First ( ) ;
39
- var bottomCount = concurrentDictionary . OrderBy ( pair => pair . Value ) . First ( ) ;
40
- _output . WriteLine ( $ "{ topCount } ") ;
41
- _output . WriteLine ( $ "{ bottomCount } ") ;
42
- Assert . True ( topCount . Value / bottomCount . Value < 2 ) ;
43
- Assert . NotEqual ( topCount . Key , bottomCount . Key ) ;
44
- }
57
+ [ Fact ]
58
+ public void SystemRandomDistributionMustBeUniform ( )
59
+ {
60
+ var concurrentDictionary = new ConcurrentDictionary < int , long > ( ) ;
61
+ Parallel . For ( 0 , 1000 , i =>
62
+ {
63
+ Parallel . For ( 0 , 1000 , j =>
64
+ {
65
+ int index ;
66
+ lock ( RandLock )
67
+ {
68
+ index = Random . Next ( 0 , 2000 ) ;
69
+ }
45
70
46
- [ Fact ]
47
- public void TwoRandomCitiesMustNotBeTheSame ( )
48
- {
49
- var locationGenerator = RandomizerFactory . GetRandomizer ( new FieldOptionsCity { ValueAsString = true , UseNullValues = false } ) ;
50
- var locationOne = locationGenerator . Generate ( ) ;
51
- var locationTwo = locationGenerator . Generate ( ) ;
52
- Assert . NotEqual ( locationOne , locationTwo ) ;
53
- }
71
+ concurrentDictionary . AddOrUpdate ( index , _ => 1 , ( k , v ) => v + 1 ) ;
72
+ } ) ;
73
+ } ) ;
54
74
55
- [ Fact ]
56
- public void SystemRandomDistributionMustBeUniform ( )
57
- {
58
- var concurrentDictionary = new ConcurrentDictionary < int , long > ( ) ;
59
- Parallel . For ( 0 , 1000 , i =>
60
- {
61
- Parallel . For ( 0 , 1000 , j =>
62
- {
63
- int index ;
64
- lock ( randLock )
65
- {
66
- index = random . Next ( 0 , 2000 ) ;
67
- }
75
+ var topCount = concurrentDictionary . OrderByDescending ( pair => pair . Value ) . First ( ) ;
76
+ var bottomCount = concurrentDictionary . OrderBy ( pair => pair . Value ) . First ( ) ;
68
77
69
- concurrentDictionary . AddOrUpdate ( index , _ => 1 , ( k , v ) => v + 1 ) ;
70
- } ) ;
71
- } ) ;
72
- var topCount = concurrentDictionary . OrderByDescending ( pair => pair . Value ) . First ( ) ;
73
- var bottomCount = concurrentDictionary . OrderBy ( pair => pair . Value ) . First ( ) ;
74
- _output . WriteLine ( $ "{ topCount } ") ;
75
- _output . WriteLine ( $ "{ bottomCount } ") ;
76
- Assert . True ( topCount . Value / bottomCount . Value < 2 ) ;
77
- Assert . NotEqual ( topCount . Key , bottomCount . Key ) ;
78
+ _output . WriteLine ( $ "{ topCount } ") ;
79
+ _output . WriteLine ( $ "{ bottomCount } ") ;
78
80
79
- }
80
- }
81
+ Assert . True ( topCount . Value / bottomCount . Value < 2 ) ;
82
+ Assert . NotEqual ( topCount . Key , bottomCount . Key ) ;
83
+ }
81
84
}
0 commit comments