Merge branch 'master' of https://github.com/mono/mono into issue4328
[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@ximian.com>
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using NUnit.Framework;
31 using System;
32
33 namespace MonoTests.System {
34
35         [TestFixture]
36         public class RandomTest  {
37
38 #if false
39                 //
40                 // This test will fail, given enough runs.
41                 //
42                 // It is an ad-hoc test for distribution, and does not work
43                 // 100% of the time.
44                 //
45                 [Test]
46                 public void NextDouble ()
47                 {
48                         Random r = new Random ();
49                         int i;
50                         double c=0;
51                         for (i=0; i<20; i++) 
52                                 c += r.NextDouble ();
53                         c/=i;
54                         Assert.IsTrue (c.ToString () + " is out of range.", c < .7 && c > .3);
55                 }
56
57 #endif
58
59                 [Test]
60                 public void CompareStreamWithSameSeed ()
61                 {
62                         Random r = new Random (42);
63                         Random r2 = new Random (42);
64                         double c=0, c2=0;
65                         for (int i=0; i<20; i++) {
66                                 c += r.NextDouble ();
67                                 c2 += r2.NextDouble ();
68                         }
69                         Assert.AreEqual (c, c2, "Compare");
70                 }
71
72                 [Test]
73                 public void Next ()
74                 {
75                         Random r = new Random ();
76                         for (int i=0; i<20; i++) {
77                                 long c = r.Next ();
78                                 Assert.IsTrue (c < Int32.MaxValue && c >= 0, "Next(" + i + ")");
79                         }
80                 }
81
82                 [Test]
83                 public void NextMax()
84                 {
85                         Random r = new Random();
86                         for (int i=0; i<20; i++) {
87                                 long c = r.Next (10);
88                                 Assert.IsTrue (c < 10 && c >= 0, "NextMax(" + i + ")");
89                         }
90                 }
91
92                 [Test]
93                 public void NextMinMax()
94                 {
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");
104                         }
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");
109                         }
110                 }
111
112 /* Mono implementation is now compatible with Knuth (not MS) implementation (choice of constants)
113                 [Test]
114                 public void CompareWithMS () 
115                 {
116                         string[] r = new string [4];
117                         byte[] buffer = new byte [8];
118                         int x = 4;
119                         while (x-- > 0) {
120                                 int seed = (x << x);
121                                 Random random = new Random (seed);
122                                 random.NextBytes (buffer);
123                                 r [x] = BitConverter.ToString (buffer);
124                         }
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)");
129                 }*/
130         }
131 }