PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/specs/math/random.ds

http://github.com/wilkie/djehuty
Unknown | 210 lines | 180 code | 30 blank | 0 comment | 0 complexity | a1da2c26f08ae47316490b5f6ea83097 MD5 | raw file
  1module specs.math.random;
  2
  3import testing.support;
  4
  5import math.random;
  6
  7import data.iterable;
  8import data.list;
  9
 10describe random() {
 11	const uint SEED = 12345678;
 12	const uint REPEATS = 1000000;
 13
 14	describe creation() {
 15		it should_have_sane_defaults() {
 16			auto r = new Random();
 17			should(r.seed >= 0);
 18		}
 19
 20		it should_not_reuse_a_seed() {
 21			auto a = new Random();
 22			auto b = new Random();
 23			shouldNot(a.seed == b.seed);
 24		}
 25
 26		it should_use_the_given_seed() {
 27			auto r = new Random(SEED);
 28			should(r.seed == SEED);
 29		}
 30	}
 31
 32	describe state() {
 33		it should_be_reproducible() {
 34			auto a = new Random(SEED);
 35			auto b = new Random(SEED);
 36			should(a.next() == b.next());
 37		}
 38	}
 39
 40	describe seed() {
 41		it should_set_and_get_the_seed() {
 42			auto r = new Random();
 43			r.seed = SEED;
 44			should(r.seed == SEED);
 45		}
 46	}
 47
 48	// TODO: Statistical tests should be implemented for all the "next" methods.
 49
 50	describe next() {
 51		it should_not_be_stuck() {
 52			auto r = new Random();
 53			shouldNot(r.next() == r.next());
 54		}
 55
 56		it should_return_zero_if_upper_bound_is_zero() {
 57			auto r = new Random();
 58			should(r.next(0) == 0);
 59		}
 60
 61		it should_return_a_nonnegative_value_less_than_upper_bound() {
 62			auto r = new Random();
 63			uint v;
 64			uint upper = 1;
 65			for (uint i = 0; i < REPEATS; i++) {
 66				v = r.next(upper);
 67				should(v >= 0);
 68				should(v < upper);
 69				upper += i;
 70			}
 71		}
 72
 73		it should_return_greatest_bound_if_bounds_overlap() {
 74			auto r = new Random();
 75			should(r.next(0, 0) == 0);
 76			should(r.next(123, 123) == 123);
 77			should(r.next(-123, -123) == -123);
 78			should(r.next(123, -123) == 123);
 79		}
 80
 81		it should_return_a_value_within_bounds() {
 82			auto r = new Random();
 83			int v;
 84			int lower = 0;
 85			int upper = 1;
 86			for (uint i = 0; i < REPEATS; i++) {
 87				v = r.next(lower, upper);
 88				should(v >= lower);
 89				should(v < upper);
 90				lower -= 12;
 91				upper += 3;
 92			}
 93		}
 94	}
 95
 96	describe nextLong() {
 97		it should_not_be_stuck() {
 98			auto r = new Random();
 99			shouldNot(r.nextLong() == r.nextLong());
100		}
101
102		it should_return_zero_if_upper_bound_is_zero() {
103			auto r = new Random();
104			should(r.nextLong(0) == 0);
105		}
106
107		it should_return_a_nonnegative_value_less_than_upper_bound() {
108			auto r = new Random();
109			ulong v;
110			ulong upper = 1;
111			for (uint i = 0; i < REPEATS; i++) {
112				v = r.nextLong(upper);
113				should(v >= 0);
114				should(v < upper);
115				upper += i;
116			}
117		}
118
119		it should_return_greatest_bound_if_bounds_overlap() {
120			auto r = new Random();
121			should(r.nextLong(0, 0) == 0);
122			should(r.nextLong(123, 123) == 123);
123			should(r.nextLong(-123, -123) == -123);
124			should(r.nextLong(123, -123) == 123);
125		}
126
127		it should_return_a_value_within_bounds() {
128			auto r = new Random();
129			long v;
130			long lower = 0;
131			long upper = 1;
132			for (uint i = 0; i < REPEATS; i++) {
133				v = r.nextLong(lower, upper);
134				should(v >= lower);
135				should(v < upper);
136				lower -= i;
137				upper += 2*i;
138			}
139		}
140	}
141
142	describe nextBoolean() {
143		it should_return_a_boolean() {
144			auto r = new Random();
145			bool to_be = r.nextBoolean();
146			should(to_be || !to_be);
147		}
148	}
149
150	describe nextDouble() {
151		it should_return_a_value_between_0_and_1() {
152			auto r = new Random();
153			double v;
154			for (uint i = 0; i < REPEATS; i++) {
155				v = r.nextDouble();
156				should(v >= 0.0);
157				should(v <= 1.0);
158			}
159		}
160	}
161
162	describe nextFloat() {
163		it should_return_a_value_between_0_and_1() {
164			auto r = new Random();
165			double v;
166			for (uint i = 0; i < REPEATS; i++) {
167				v = r.nextFloat();
168				should(v >= 0.0);
169				should(v <= 1.0);
170			}
171		}
172	}
173
174	describe choose() {
175
176		it should_fail_on_empty_list() {
177			auto r = new Random();
178
179			shouldThrow();
180			r.choose(new List!(uint));
181		}
182
183		it should_return_the_item_given_one() {
184			auto r = new Random();
185			uint[] arr = [1234];
186			should(r.choose(arr) == 1234);
187		}
188
189		it should_work_for_arrays() {
190			auto r = new Random();
191			uint[] arr = [2, 5, 6, 9, 10, 13];
192			uint v;
193			for (uint i = 0; i < REPEATS; i++) {
194				v = r.choose(arr);
195				shouldNot(member(v, arr) is null);
196			}
197		}
198
199		it should_work_for_lists() {
200			auto r = new Random();
201			List!(char) lst = new List!(char)(['a', 'e', 'i', 'o', 'u']);
202			char v;
203			for (uint i = 0; i < REPEATS; i++) {
204				v = r.choose(lst);
205				shouldNot(member(v, lst) is null);
206			}
207		}
208	}
209}
210