Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RandomTest.cs « System « Test « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 74146dea1c6e6d0fe28fe0c2c3f61e6e2fab0059 (plain)
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
//
// System.Random Test Cases
//
// Author: Bob Smith <bob@thestuff.net>
//

using NUnit.Framework;
using System;

namespace MonoTests.System {

public class RandomTest : TestCase
{
	public static ITest Suite {
		get {
			return new TestSuite(typeof(RandomTest));
		}
	}

	public RandomTest() : base ("MonoTests.System.RandomTest testcase") {}
        public RandomTest(string name): base(name){}
        public void TestDouble()
        {
                Random r = new Random();
                int i;
                double c=0;
                for (i=0; i<20; i++) c+=r.NextDouble();
                c/=i;
                Assert (c.ToString() + " is out of range.", c < .7 && c > .3);
        }
        public void TestSeed()
        {
                Random r = new Random(42);
                Random r2 = new Random(42);
                int i;
                double c=0, c2=0;
                for (i=0; i<20; i++)
                {
                        c += r.NextDouble();
                        c2 += r2.NextDouble();
                }
                AssertEquals(c, c2);
        }
        public void TestNext()
        {
                Random r = new Random();
                int i;
                long c;
                for (i=0; i<20; i++)
                {
                        c = r.Next();
                        Assert (c < Int32.MaxValue && c >= 0);
                }
        }
        public void TestNextMax()
        {
                Random r = new Random();
                int i;
                long c;
                for (i=0; i<20; i++)
                {
                        c = r.Next(10);
                        Assert (c < 10 && c >= 0);
                }
        }
        public void TestNextMinMax()
        {
                Random r = new Random();
                int i;
                long c;
		AssertEquals ("#1 Failed where min == max", 42, r.Next (42, 42));
		AssertEquals ("#2 Failed where min == max", Int32.MaxValue, r.Next (Int32.MaxValue,Int32.MaxValue));
		AssertEquals ("#3 Failed where min == max", Int32.MinValue, r.Next (Int32.MinValue,Int32.MinValue));
		AssertEquals ("#4 Failed where min == max", 0, r.Next (0, 0));
                for (i = 1; i <= Int32.MaxValue / 2; i *= 2)
                {
                        c = r.Next (i, i * 2);
			Assert ("At i=" + i + " c < i*2 failed", c < i * 2);
                        Assert ("At i=" + i + " c >= i failed", c >= i);
                }
                for (i = -1; i >= Int32.MinValue / 2; i *= 2)
                {
                        c = r.Next (i * 2, i);
			Assert ("At i=" + i + " c < i*2 failed", c < i);
                        Assert ("At i=" + i + " c >= i failed", c >= i * 2);
                }
        }
}

}