~ubuntu-branches/ubuntu/utopic/monodevelop/utopic

« back to all changes in this revision

Viewing changes to external/guiunit/src/tests/Internal/RandomizerTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-10-10 14:50:04 UTC
  • mfrom: (10.3.4)
  • Revision ID: package-import@ubuntu.com-20131010145004-80l130sny21b17sb
Tags: 4.0.12+dfsg-1
* [5dcb6e1] Fix debian/watch for new source tarball name format
* [5c68cb5] Refresh list of files removed by get-orig-source to 
  reflect 4.0.12
* [96d60a0] Imported Upstream version 4.0.12+dfsg
* [b989752] Refresh debian/patches/no_appmenu to ensure it applies
* [2a4c351] Ensure every assembly in external/ is cleaned properly
* [92762f7] Add more excluded Mac-specific modulerefs
* [bc698ba] Add symlinks to NUnit assemblies (Closes: #714246)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ***********************************************************************
 
2
// Copyright (c) 2009 Charlie Poole
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person obtaining
 
5
// a copy of this software and associated documentation files (the
 
6
// "Software"), to deal in the Software without restriction, including
 
7
// without limitation the rights to use, copy, modify, merge, publish,
 
8
// distribute, sublicense, and/or sell copies of the Software, and to
 
9
// permit persons to whom the Software is furnished to do so, subject to
 
10
// the following conditions:
 
11
// 
 
12
// The above copyright notice and this permission notice shall be
 
13
// included in all copies or substantial portions of the Software.
 
14
// 
 
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
16
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
17
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
18
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
19
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
20
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
21
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
22
// ***********************************************************************
 
23
 
 
24
using System;
 
25
using System.Reflection;
 
26
 
 
27
namespace NUnit.Framework.Internal
 
28
{
 
29
    public class RandomizerTests
 
30
    {
 
31
        [Test]
 
32
        public void RandomSeedsAreUnique()
 
33
        {
 
34
            int[] seeds = new int[10];
 
35
            for (int i = 0; i < 10; i++)
 
36
                seeds[i] = Randomizer.RandomSeed;
 
37
 
 
38
            Assert.That(seeds, Is.Unique);
 
39
        }
 
40
 
 
41
        [Test]
 
42
        public void RandomIntsAreUnique()
 
43
        {
 
44
            Randomizer r = new Randomizer();
 
45
 
 
46
            int[] values = new int[10];
 
47
            for (int i = 0; i < 10; i++)
 
48
                values[i] = r.Next();
 
49
 
 
50
            Assert.That(values, Is.Unique);
 
51
        }
 
52
 
 
53
        [Test]
 
54
        public void RandomDoublesAreUnique()
 
55
        {
 
56
            Randomizer r = new Randomizer();
 
57
 
 
58
            double[] values = new double[10];
 
59
            for (int i = 0; i < 10; i++)
 
60
                values[i] = r.NextDouble();
 
61
 
 
62
            Assert.That(values, Is.Unique);
 
63
        }
 
64
 
 
65
        [Test]
 
66
        public void CanGetArrayOfRandomInts()
 
67
        {
 
68
            Randomizer r = new Randomizer();
 
69
 
 
70
            int[] ints = r.GetInts(1, 100, 10);
 
71
            Assert.That(ints.Length, Is.EqualTo(10));
 
72
            foreach (int i in ints)
 
73
                Assert.That(i, Is.InRange(1, 100));
 
74
        }
 
75
 
 
76
        [Test]
 
77
        public void CanGetArrayOfRandomDoubles()
 
78
        {
 
79
            Randomizer r = new Randomizer();
 
80
 
 
81
            double[] doubles = r.GetDoubles(0.5, 1.5, 10);
 
82
            Assert.That(doubles.Length, Is.EqualTo(10));
 
83
            foreach (double d in doubles)
 
84
                Assert.That(d, Is.InRange(0.5, 1.5));
 
85
 
 
86
            // Heuristic: Could fail occasionally
 
87
            Assert.That(doubles, Is.Unique);
 
88
        }
 
89
 
 
90
        [Test]
 
91
        public void CanGetArrayOfRandomEnums()
 
92
        {
 
93
            Randomizer r = new Randomizer();
 
94
 
 
95
            object[] enums = r.GetEnums(10, typeof(AttributeTargets));
 
96
            Assert.That(enums.Length, Is.EqualTo(10));
 
97
            foreach (object e in enums)
 
98
                Assert.That(e, Is.TypeOf(typeof(AttributeTargets)));
 
99
        }
 
100
 
 
101
        [Test]
 
102
        public void RandomizersWithSameSeedsReturnSameValues()
 
103
        {
 
104
            Randomizer r1 = new Randomizer(1234);
 
105
            Randomizer r2 = new Randomizer(1234);
 
106
 
 
107
            for (int i = 0; i < 10; i++)
 
108
                Assert.That(r1.NextDouble(), Is.EqualTo(r2.NextDouble()));
 
109
        }
 
110
 
 
111
        [Test]
 
112
        public void RandomizersWithDifferentSeedsReturnDifferentValues()
 
113
        {
 
114
            Randomizer r1 = new Randomizer(1234);
 
115
            Randomizer r2 = new Randomizer(4321);
 
116
 
 
117
            for (int i = 0; i < 10; i++)
 
118
                Assert.That(r1.NextDouble(), Is.Not.EqualTo(r2.NextDouble()));
 
119
        }
 
120
 
 
121
        [Test]
 
122
        public void ReturnsSameRandomizerForSameParameter()
 
123
        {
 
124
            ParameterInfo p = testMethod1.GetParameters()[0];
 
125
            Randomizer r1 = Randomizer.GetRandomizer(p);
 
126
            Randomizer r2 = Randomizer.GetRandomizer(p);
 
127
            Assert.That(r1, Is.SameAs(r2));
 
128
        }
 
129
 
 
130
        [Test]
 
131
        public void ReturnsSameRandomizerForDifferentParametersOfSameMethod()
 
132
        {
 
133
            ParameterInfo p1 = testMethod1.GetParameters()[0];
 
134
            ParameterInfo p2 = testMethod1.GetParameters()[1];
 
135
            Randomizer r1 = Randomizer.GetRandomizer(p1);
 
136
            Randomizer r2 = Randomizer.GetRandomizer(p2);
 
137
            Assert.That(r1, Is.SameAs(r2));
 
138
        }
 
139
 
 
140
        [Test]
 
141
        public void ReturnsSameRandomizerForSameMethod()
 
142
        {
 
143
            Randomizer r1 = Randomizer.GetRandomizer(testMethod1);
 
144
            Randomizer r2 = Randomizer.GetRandomizer(testMethod1);
 
145
            Assert.That(r1, Is.SameAs(r2));
 
146
        }
 
147
 
 
148
        [Test]
 
149
        public void ReturnsDifferentRandomizersForDifferentMethods()
 
150
        {
 
151
            Randomizer r1 = Randomizer.GetRandomizer(testMethod1);
 
152
            Randomizer r2 = Randomizer.GetRandomizer(testMethod2);
 
153
            Assert.That(r1, Is.Not.SameAs(r2));
 
154
        }
 
155
 
 
156
        static readonly MethodInfo testMethod1 =
 
157
            typeof(RandomizerTests).GetMethod("TestMethod1", BindingFlags.NonPublic | BindingFlags.Instance);
 
158
        private void TestMethod1(int x, int y)
 
159
        {
 
160
        }
 
161
 
 
162
        static readonly MethodInfo testMethod2 =
 
163
            typeof(RandomizerTests).GetMethod("TestMethod2", BindingFlags.NonPublic | BindingFlags.Instance);
 
164
        private void TestMethod2(int x, int y)
 
165
        {
 
166
        }
 
167
 
 
168
        private int CountUniqueValues(Array array)
 
169
        {
 
170
            int uniqueCount = 0;
 
171
 
 
172
            for (int index = 0; index < array.Length; index++)
 
173
            {
 
174
                bool isUnique = true;
 
175
                for (int index2 = 0; index2 < index; index2++)
 
176
                    if (array.GetValue(index).Equals(array.GetValue(index2)))
 
177
                        isUnique = false;
 
178
                if (isUnique)
 
179
                    uniqueCount++;
 
180
            }
 
181
 
 
182
            return uniqueCount;
 
183
        }
 
184
    }
 
185
}