2 // System.Random Test Cases
5 // Bob Smith <bob@thestuff.net>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
33 namespace MonoTests.System {
36 public class RandomTest {
40 // This test will fail, given enough runs.
42 // It is an ad-hoc test for distribution, and does not work
46 public void NextDouble ()
48 Random r = new Random ();
54 Assert.IsTrue (c.ToString () + " is out of range.", c < .7 && c > .3);
60 public void CompareStreamWithSameSeed ()
62 Random r = new Random (42);
63 Random r2 = new Random (42);
65 for (int i=0; i<20; i++) {
67 c2 += r2.NextDouble ();
69 Assert.AreEqual (c, c2, "Compare");
75 Random r = new Random ();
76 for (int i=0; i<20; i++) {
78 Assert.IsTrue (c < Int32.MaxValue && c >= 0, "Next(" + i + ")");
85 Random r = new Random();
86 for (int i=0; i<20; i++) {
88 Assert.IsTrue (c < 10 && c >= 0, "NextMax(" + i + ")");
93 public void NextMinMax()
95 Random r = new Random ();
96 Assert.AreEqual (42, r.Next (42, 42), "#1 Failed where min == max");
97 Assert.AreEqual (Int32.MaxValue, r.Next (Int32.MaxValue, Int32.MaxValue), "#2 Failed where min == max");
98 Assert.AreEqual (Int32.MinValue, r.Next (Int32.MinValue, Int32.MinValue), "#3 Failed where min == max");
99 Assert.AreEqual (0, r.Next (0, 0), "#4 Failed where min == max");
100 for (int i = 1; i <= Int32.MaxValue / 2; i *= 2) {
101 long c = r.Next (i, i * 2);
102 Assert.IsTrue (c < i * 2, "At i=" + i + " c < i*2 failed");
103 Assert.IsTrue (c >= i, "At i=" + i + " c >= i failed");
105 for (int i = -1; i >= Int32.MinValue / 2; i *= 2) {
106 long c = r.Next (i * 2, i);
107 Assert.IsTrue (c < i, "At i=" + i + " c < i*2 failed");
108 Assert.IsTrue (c >= i * 2, "At i=" + i + " c >= i failed");
112 /* Mono implementation is now compatible with Knuth (not MS) implementation (choice of constants)
114 public void CompareWithMS ()
116 string[] r = new string [4];
117 byte[] buffer = new byte [8];
121 Random random = new Random (seed);
122 random.NextBytes (buffer);
123 r [x] = BitConverter.ToString (buffer);
125 Assert.AreEqual ("43-DB-8B-AE-0A-88-A8-7B", r [3], "Seed(24)");
126 Assert.AreEqual ("E7-2A-5C-44-D1-8C-7D-74", r [2], "Seed(8)");
127 Assert.AreEqual ("C5-67-2A-FC-1B-4E-CD-72", r [1], "Seed(2)");
128 Assert.AreEqual ("B9-D1-C4-8E-34-8F-E7-71", r [0], "Seed(0)");