Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / corlib / Test / System / RandomTest.cs
1 //
2 // System.Random Test Cases
3 //
4 // Authors: 
5 //      Bob Smith <bob@thestuff.net>
6 //      Sebastien Pouliot  <sebastien@xamarin.com>
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Reflection;
34
35 namespace MonoTests.System {
36
37         [TestFixture]
38         public class RandomTest  {
39
40                 [Test]
41                 public void CompareStreamWithSameSeed ()
42                 {
43                         Random r = new Random (42);
44                         Random r2 = new Random (42);
45                         for (int i=0; i<20; i++) {
46                                 Assert.AreEqual (r.NextDouble (), r2.NextDouble (), i.ToString ());
47                         }
48                 }
49
50                 [Test]
51                 public void Next ()
52                 {
53                         Random r = new Random ();
54                         for (int i=0; i<20; i++) {
55                                 long c = r.Next ();
56                                 Assert.IsTrue (c < Int32.MaxValue && c >= 0, "Next(" + i + ")");
57                         }
58                 }
59
60                 [Test]
61                 public void NextZero ()
62                 {
63                         Random r = new Random ();
64                         Assert.AreEqual (0, r.Next (0),"Next(0) failed");
65                 }
66
67                 [Test]
68                 public void NextMax()
69                 {
70                         Random r = new Random();
71                         for (int i=0; i<20; i++) {
72                                 long c = r.Next (10);
73                                 Assert.IsTrue (c < 10 && c >= 0, "NextMax(" + i + ")");
74                         }
75                 }
76
77                 [Test]
78                 public void NextMinMax()
79                 {
80                         Random r = new Random ();
81                         Assert.AreEqual (42, r.Next (42, 42), "#1 Failed where min == max");
82                         Assert.AreEqual (Int32.MaxValue, r.Next (Int32.MaxValue, Int32.MaxValue), "#2 Failed where min == max");
83                         Assert.AreEqual (Int32.MinValue, r.Next (Int32.MinValue, Int32.MinValue), "#3 Failed where min == max");
84                         Assert.AreEqual (0, r.Next (0, 0), "#4 Failed where min == max");
85                         for (int i = 1; i <= Int32.MaxValue / 2; i *= 2) {
86                                 long c = r.Next (i, i * 2);
87                                 Assert.IsTrue (c < i * 2, "At i=" + i + " c < i*2 failed");
88                                 Assert.IsTrue (c >= i, "At i=" + i + " c >= i failed");
89                         }
90                         for (int i = -1; i >= Int32.MinValue / 2; i *= 2) {
91                                 long c = r.Next (i * 2, i);
92                                 Assert.IsTrue (c < i, "At i=" + i + " c < i*2 failed");
93                                 Assert.IsTrue (c >= i * 2, "At i=" + i + " c >= i failed");
94                         }
95                 }
96
97                 class RandomSampleOverride : Random {
98
99                         protected override double Sample ()
100                         {
101                                 throw new NotImplementedException ();
102                         }
103                 }
104
105                 [Test]
106                 public void Base_Int ()
107                 {
108                         var random = new RandomSampleOverride ();
109                         // from 2.0+ Next(), Next(int,int) and NextBytes(byte[]) do not call Sample
110                         // see MSDN's Notes to Inheritors
111                         random.Next ();
112                         random.Next (Int32.MinValue, Int32.MaxValue);
113                         random.NextBytes (new byte[1]);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (NotImplementedException))]
118                 public void Base_Double ()
119                 {
120                         var random = new RandomSampleOverride ();
121                         random.NextDouble ();
122                 }
123
124                 // generate values (one for each 1024 returned values) from the original C implementation
125                 static uint[] jkiss_values = {
126                         560241513,      /* 0 */
127                         1281708802,     /* 1024 */
128                         1571324528,     /* 2048 */
129                         1565809406,     /* 3072 */
130                         1010890569,     /* 4096 */
131                         1778803435,     /* 5120 */
132                         903613637,      /* 6144 */
133                         3496059008,     /* 7168 */
134                         108603163,      /* 8192 */
135                         1854081276,     /* 9216 */
136                         3703232459,     /* 10240 */
137                         2191562138,     /* 11264 */
138                         337995793,      /* 12288 */
139                         1340840062,     /* 13312 */
140                         2364148985,     /* 14336 */
141                         2549812361,     /* 15360 */
142                         563432369,      /* 16384 */
143                         229365487,      /* 17408 */
144                         1821397325,     /* 18432 */
145                         3246092454,     /* 19456 */
146                         691032417,      /* 20480 */
147                         86951316,       /* 21504 */
148                         3029975455,     /* 22528 */
149                         1261370163,     /* 23552 */
150                         2539815382,     /* 24576 */
151                         3017891647,     /* 25600 */
152                         3877215120,     /* 26624 */
153                         3142958765,     /* 27648 */
154                         1080903191,     /* 28672 */
155                         2837464745,     /* 29696 */
156                         614275602,      /* 30720 */
157                         2250626199,     /* 31744 */
158                         729001311,      /* 32768 */
159                         3313769017,     /* 33792 */
160                         2408398670,     /* 34816 */
161                         3123583383,     /* 35840 */
162                         3346590423,     /* 36864 */
163                         1629546563,     /* 37888 */
164                         251343753,      /* 38912 */
165                         2695793631,     /* 39936 */
166                         2768993787,     /* 40960 */
167                         3688573224,     /* 41984 */
168                         2897218561,     /* 43008 */
169                         2725058810,     /* 44032 */
170                         2142061914,     /* 45056 */
171                         3983217096,     /* 46080 */
172                         3609758190,     /* 47104 */
173                         842060935,      /* 48128 */
174                         2893482035,     /* 49152 */
175                         2290461665,     /* 50176 */
176                         1709481476,     /* 51200 */
177                         3633857838,     /* 52224 */
178                         332645044,      /* 53248 */
179                         3522654497,     /* 54272 */
180                         2501348469,     /* 55296 */
181                         1644344287,     /* 56320 */
182                         3081428084,     /* 57344 */
183                         3114560766,     /* 58368 */
184                         489030597,      /* 59392 */
185                         367291591,      /* 60416 */
186                         106358682,      /* 61440 */
187                         3020781303,     /* 62464 */
188                         1209590375,     /* 63488 */
189                         1833282169,     /* 64512 */
190                         61543407,       /* 65536 */
191                 };
192
193                 [Test]
194                 public void JKISS ()
195                 {
196                         // Random.Next() returns a non-negative *signed* integer value - so it can't be used for testing
197                         var next = typeof(Random).GetMethod ("JKiss", BindingFlags.Instance | BindingFlags.NonPublic);
198
199                         // if the method is not present, e.g. on MS.NET, skip this test
200                         if (next == null)
201                                 Assert.Ignore ("The JKiss method is not present, e.g. on MS.NET.");
202
203                         // ensure we match the original JKISS random stream
204                         // first 64KB but without checking every value (one each KB)
205                         Random r = new Random (123456789);
206                         int n = 0;
207                         int j = 0;
208                         while (j < 64 * 1024) {
209                                 uint random = (uint) next.Invoke (r, null);
210                                 if (j++ % 1024 == 0) {
211                                         Assert.AreEqual (random, jkiss_values [n], n.ToString ());
212                                         n++;
213                                 }
214                         }
215                 }
216         }
217 }