summaryrefslogtreecommitdiff
path: root/mcs/class/corlib/Test/System/RandomTest.cs
diff options
context:
space:
mode:
Diffstat (limited to 'mcs/class/corlib/Test/System/RandomTest.cs')
-rw-r--r--mcs/class/corlib/Test/System/RandomTest.cs170
1 files changed, 128 insertions, 42 deletions
diff --git a/mcs/class/corlib/Test/System/RandomTest.cs b/mcs/class/corlib/Test/System/RandomTest.cs
index a18524968f..5e49ac3866 100644
--- a/mcs/class/corlib/Test/System/RandomTest.cs
+++ b/mcs/class/corlib/Test/System/RandomTest.cs
@@ -3,9 +3,10 @@
//
// Authors:
// Bob Smith <bob@thestuff.net>
-// Sebastien Pouliot <sebastien@ximian.com>
+// 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
@@ -29,44 +30,21 @@
using NUnit.Framework;
using System;
+using System.Reflection;
namespace MonoTests.System {
[TestFixture]
public class RandomTest {
-#if false
- //
- // This test will fail, given enough runs.
- //
- // It is an ad-hoc test for distribution, and does not work
- // 100% of the time.
- //
- [Test]
- public void NextDouble ()
- {
- Random r = new Random ();
- int i;
- double c=0;
- for (i=0; i<20; i++)
- c += r.NextDouble ();
- c/=i;
- Assert.IsTrue (c.ToString () + " is out of range.", c < .7 && c > .3);
- }
-
-#endif
-
[Test]
public void CompareStreamWithSameSeed ()
{
Random r = new Random (42);
Random r2 = new Random (42);
- double c=0, c2=0;
for (int i=0; i<20; i++) {
- c += r.NextDouble ();
- c2 += r2.NextDouble ();
+ Assert.AreEqual (r.NextDouble (), r2.NextDouble (), i.ToString ());
}
- Assert.AreEqual (c, c2, "Compare");
}
[Test]
@@ -80,6 +58,13 @@ namespace MonoTests.System {
}
[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();
@@ -109,23 +94,124 @@ namespace MonoTests.System {
}
}
-/* Mono implementation is now compatible with Knuth (not MS) implementation (choice of constants)
+ class RandomSampleOverride : Random {
+
+ protected override double Sample ()
+ {
+ throw new NotImplementedException ();
+ }
+ }
+
[Test]
- public void CompareWithMS ()
+ public void Base_Int ()
{
- string[] r = new string [4];
- byte[] buffer = new byte [8];
- int x = 4;
- while (x-- > 0) {
- int seed = (x << x);
- Random random = new Random (seed);
- random.NextBytes (buffer);
- r [x] = BitConverter.ToString (buffer);
+ 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)
+ return;
+
+ // 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++;
+ }
}
- Assert.AreEqual ("43-DB-8B-AE-0A-88-A8-7B", r [3], "Seed(24)");
- Assert.AreEqual ("E7-2A-5C-44-D1-8C-7D-74", r [2], "Seed(8)");
- Assert.AreEqual ("C5-67-2A-FC-1B-4E-CD-72", r [1], "Seed(2)");
- Assert.AreEqual ("B9-D1-C4-8E-34-8F-E7-71", r [0], "Seed(0)");
- }*/
+ }
}
-}
+} \ No newline at end of file