File: RandomTest.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (217 lines) | stat: -rw-r--r-- 6,320 bytes parent folder | download | duplicates (8)
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
//
// System.Random Test Cases
//
// Authors: 
//	Bob Smith <bob@thestuff.net>
//	Sebastien Pouliot  <sebastien@xamarin.com>
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
// Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using NUnit.Framework;
using System;
using System.Reflection;

namespace MonoTests.System {

	[TestFixture]
	public class RandomTest  {

		[Test]
		public void CompareStreamWithSameSeed ()
		{
			Random r = new Random (42);
			Random r2 = new Random (42);
			for (int i=0; i<20; i++) {
				Assert.AreEqual (r.NextDouble (), r2.NextDouble (), i.ToString ());
			}
		}

		[Test]
		public void Next ()
		{
			Random r = new Random ();
			for (int i=0; i<20; i++) {
				long c = r.Next ();
				Assert.IsTrue (c < Int32.MaxValue && c >= 0, "Next(" + i + ")");
			}
		}

		[Test]
		public void NextZero ()
		{
			Random r = new Random ();
			Assert.AreEqual (0, r.Next (0),"Next(0) failed");
		}

		[Test]
		public void NextMax()
		{
			Random r = new Random();
			for (int i=0; i<20; i++) {
				long c = r.Next (10);
				Assert.IsTrue (c < 10 && c >= 0, "NextMax(" + i + ")");
			}
		}

		[Test]
		public void NextMinMax()
		{
			Random r = new Random ();
			Assert.AreEqual (42, r.Next (42, 42), "#1 Failed where min == max");
			Assert.AreEqual (Int32.MaxValue, r.Next (Int32.MaxValue, Int32.MaxValue), "#2 Failed where min == max");
			Assert.AreEqual (Int32.MinValue, r.Next (Int32.MinValue, Int32.MinValue), "#3 Failed where min == max");
			Assert.AreEqual (0, r.Next (0, 0), "#4 Failed where min == max");
			for (int i = 1; i <= Int32.MaxValue / 2; i *= 2) {
				long c = r.Next (i, i * 2);
				Assert.IsTrue (c < i * 2, "At i=" + i + " c < i*2 failed");
				Assert.IsTrue (c >= i, "At i=" + i + " c >= i failed");
			}
			for (int i = -1; i >= Int32.MinValue / 2; i *= 2) {
				long c = r.Next (i * 2, i);
				Assert.IsTrue (c < i, "At i=" + i + " c < i*2 failed");
				Assert.IsTrue (c >= i * 2, "At i=" + i + " c >= i failed");
			}
		}

		class RandomSampleOverride : Random {

			protected override double Sample ()
			{
				throw new NotImplementedException ();
			}
		}

		[Test]
		public void Base_Int ()
		{
			var random = new RandomSampleOverride ();
			// from 2.0+ Next(), Next(int,int) and NextBytes(byte[]) do not call Sample
			// see MSDN's Notes to Inheritors
			random.Next ();
			random.Next (Int32.MinValue, Int32.MaxValue);
			random.NextBytes (new byte[1]);
		}

		[Test]
		[ExpectedException (typeof (NotImplementedException))]
		public void Base_Double ()
		{
			var random = new RandomSampleOverride ();
			random.NextDouble ();
		}

		// generate values (one for each 1024 returned values) from the original C implementation
		static uint[] jkiss_values = {
			560241513,	/* 0 */
			1281708802,	/* 1024 */
			1571324528,	/* 2048 */
			1565809406,	/* 3072 */
			1010890569,	/* 4096 */
			1778803435,	/* 5120 */
			903613637,	/* 6144 */
			3496059008,	/* 7168 */
			108603163,	/* 8192 */
			1854081276,	/* 9216 */
			3703232459,	/* 10240 */
			2191562138,	/* 11264 */
			337995793,	/* 12288 */
			1340840062,	/* 13312 */
			2364148985,	/* 14336 */
			2549812361,	/* 15360 */
			563432369,	/* 16384 */
			229365487,	/* 17408 */
			1821397325,	/* 18432 */
			3246092454,	/* 19456 */
			691032417,	/* 20480 */
			86951316,	/* 21504 */
			3029975455,	/* 22528 */
			1261370163,	/* 23552 */
			2539815382,	/* 24576 */
			3017891647,	/* 25600 */
			3877215120,	/* 26624 */
			3142958765,	/* 27648 */
			1080903191,	/* 28672 */
			2837464745,	/* 29696 */
			614275602,	/* 30720 */
			2250626199,	/* 31744 */
			729001311,	/* 32768 */
			3313769017,	/* 33792 */
			2408398670,	/* 34816 */
			3123583383,	/* 35840 */
			3346590423,	/* 36864 */
			1629546563,	/* 37888 */
			251343753,	/* 38912 */
			2695793631,	/* 39936 */
			2768993787,	/* 40960 */
			3688573224,	/* 41984 */
			2897218561,	/* 43008 */
			2725058810,	/* 44032 */
			2142061914,	/* 45056 */
			3983217096,	/* 46080 */
			3609758190,	/* 47104 */
			842060935,	/* 48128 */
			2893482035,	/* 49152 */
			2290461665,	/* 50176 */
			1709481476,	/* 51200 */
			3633857838,	/* 52224 */
			332645044,	/* 53248 */
			3522654497,	/* 54272 */
			2501348469,	/* 55296 */
			1644344287,	/* 56320 */
			3081428084,	/* 57344 */
			3114560766,	/* 58368 */
			489030597,	/* 59392 */
			367291591,	/* 60416 */
			106358682,	/* 61440 */
			3020781303,	/* 62464 */
			1209590375,	/* 63488 */
			1833282169,	/* 64512 */
			61543407,	/* 65536 */
		};

		[Test]
		public void JKISS ()
		{
			// Random.Next() returns a non-negative *signed* integer value - so it can't be used for testing
			var next = typeof(Random).GetMethod ("JKiss", BindingFlags.Instance | BindingFlags.NonPublic);

			// if the method is not present, e.g. on MS.NET, skip this test
			if (next == null)
				Assert.Ignore ("The JKiss method is not present, e.g. on MS.NET.");

			// ensure we match the original JKISS random stream
			// first 64KB but without checking every value (one each KB)
			Random r = new Random (123456789);
			int n = 0;
			int j = 0;
			while (j < 64 * 1024) {
				uint random = (uint) next.Invoke (r, null);
				if (j++ % 1024 == 0) {
					Assert.AreEqual (random, jkiss_values [n], n.ToString ());
					n++;
				}
			}
		}
	}
}