1//
2//   Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17//
18//
19// Tests for random behaviour. This tests only the randomness of the output
20// and the range of results. Tests for Math.random() actionscript correctness
21// are in Math.as.
22//
23// compile this test case with Ming makeswf, and then
24// execute it like this gnash -1 -r 0 -v out.swf
25
26// For obvious reasons, tests of randomness can fail even if there is
27// nothing wrong (though with carefully-designed pseudo-randomness this
28// is very unlikely). The chance of a failure is very small if Gnash
29// is working correctly.
30
31rcsid="Random.as";
32#include "check.as"
33
34// Number of random numbers to generate.
35var max = 1000;
36
37/*
38Tests for random(n).
39
40This AS function should return an integer from 0 up to but not
41including n.
42*/
43
44// With n = integer (how it should be used...)
45var tally = new Array();
46
47for (var i = 0; i < max; i++)
48{
49	rnd = random(4);
50	if (typeof(tally[rnd]) == "undefined") { tally[rnd] = 0; }
51	tally[rnd] += 1;
52}
53
54// Check proportion of each number exceeds 10%; should be about
55// 25%.
56check (tally[0] > (max / 10));
57check (tally[1] > (max / 10));
58check (tally[2] > (max / 10));
59check (tally[3] > (max / 10));
60
61check_equals(typeof(tally[4]), "undefined"); // Should not exist
62
63// With n = non-integer
64
65var tally = new Array();
66for (var i = 0; i < max; i++)
67{
68	rnd = random(4.5);
69	if (typeof(tally[rnd]) == "undefined") { tally[rnd] = 0; }
70	tally[rnd] += 1;
71}
72
73// Check proportion of each number exceeds 10%; should be about
74// 25%.
75check (tally[0] > (max / 10));
76check (tally[1] > (max / 10));
77check (tally[2] > (max / 10));
78check (tally[3] > (max / 10));
79
80check_equals(typeof(tally[4]), "undefined"); // Should not exist
81
82// With n = negative number
83
84var tally = new Array();
85for (var i = 0; i < max / 5; i++)
86{
87	rnd = random(-1);
88	if (typeof(tally[rnd]) == "undefined") { tally[rnd] = 0; }
89	tally[rnd] += 1;
90}
91
92check_equals (tally[0], max / 5 );
93check_equals(typeof(tally[1]), "undefined"); // Should not exist
94
95/*
96
97Tests for Math.random()
98
99Returns double n where 0 <= n < 1.
100
101*/
102
103
104// Note: test also relies on Math.round()!
105var tally = new Array();
106
107for (i = 0; i < max; i++)
108{
109	rnd = Math.round(Math.random() * 10 + 0.5);
110	if (typeof(tally[rnd]) == "undefined") { tally[rnd] = 0; }
111	tally[rnd] += 1;
112}
113
114check_equals(typeof(tally[0]), "undefined"); // Should not exist
115note(tally[0]);
116
117check (tally[1] > (max / 20));
118note(tally[1]);
119
120check (tally[2] > (max / 20));
121note(tally[2]);
122
123check (tally[3] > (max / 20));
124note(tally[3]);
125
126check (tally[4] > (max / 20));
127note(tally[4]);
128
129check (tally[5] > (max / 20));
130note(tally[5]);
131
132check (tally[6] > (max / 20));
133note(tally[6]);
134
135check (tally[7] > (max / 20));
136note(tally[7]);
137
138check (tally[8] > (max / 20));
139note(tally[8]);
140
141check (tally[9] > (max / 20));
142note(tally[9]);
143
144check (tally[10] > (max / 20));
145note(tally[10]);
146
147check_equals(typeof(tally[11]), "undefined"); // Should not exist
148note(tally[11]);
149
150/* End of tests */
151check_totals(24);
152