1/* Unit tests for grand
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
7 *
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
20 */
21
22#include "glib.h"
23
24/* Outputs tested against the reference implementation mt19937ar.c from
25 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
26 */
27
28/* Tests for a simple seed, first number is the seed */
29const guint32 first_numbers[] =
30{
31 0x7a7a7a7a,
32 0xfdcc2d54,
33 0x3a279ceb,
34 0xc4d39c33,
35 0xf31895cd,
36 0x46ca0afc,
37 0x3f5484ff,
38 0x54bc9557,
39 0xed2c24b1,
40 0x84062503,
41 0x8f6404b3,
42 0x599a94b3,
43 0xe46d03d5,
44 0x310beb78,
45 0x7bee5d08,
46 0x760d09be,
47 0x59b6e163,
48 0xbf6d16ec,
49 0xcca5fb54,
50 0x5de7259b,
51 0x1696330c,
52};
53
54/* array seed */
55const guint32 seed_array[] =
56{
57 0x6553375f,
58 0xd6b8d43b,
59 0xa1e7667f,
60 0x2b10117c
61};
62
63/* tests for the array seed */
64const guint32 array_outputs[] =
65{
66 0xc22b7dc3,
67 0xfdecb8ae,
68 0xb4af0738,
69 0x516bc6e1,
70 0x7e372e91,
71 0x2d38ff80,
72 0x6096494a,
73 0xd162d5a8,
74 0x3c0aaa0d,
75 0x10e736ae
76};
77
78static void
79test_rand (void)
80{
81 guint n;
82 guint ones;
83 double proportion;
84 GRand *rand;
85 GRand *copy;
86
87 rand = g_rand_new_with_seed (seed: first_numbers[0]);
88
89 for (n = 1; n < G_N_ELEMENTS (first_numbers); n++)
90 g_assert_cmpuint (first_numbers[n], ==, g_rand_int (rand));
91
92 g_rand_set_seed (rand_: rand, seed: 2);
93 g_rand_set_seed_array (rand_: rand, seed: seed_array, G_N_ELEMENTS (seed_array));
94
95 for (n = 0; n < G_N_ELEMENTS (array_outputs); n++)
96 g_assert_cmpuint (array_outputs[n], ==, g_rand_int (rand));
97
98 copy = g_rand_copy (rand_: rand);
99 for (n = 0; n < 100; n++)
100 g_assert_cmpuint (g_rand_int (copy), ==, g_rand_int (rand));
101
102 for (n = 1; n < 100000; n++)
103 {
104 gint32 i;
105 gdouble d;
106 gboolean b;
107
108 i = g_rand_int_range (rand_: rand, begin: 8,end: 16);
109 g_assert_cmpint (i, >=, 8);
110 g_assert_cmpint (i, <, 16);
111
112 i = g_random_int_range (begin: 8,end: 16);
113 g_assert_cmpint (i, >=, 8);
114 g_assert_cmpint (i, <, 16);
115
116 d = g_rand_double (rand_: rand);
117 g_assert_cmpfloat (d, >=, 0.0);
118 g_assert_cmpfloat (d, <, 1.0);
119
120 d = g_random_double ();
121 g_assert_cmpfloat (d, >=, 0.0);
122 g_assert_cmpfloat (d, <, 1.0);
123
124 d = g_rand_double_range (rand_: rand, begin: -8, end: 32);
125 g_assert_cmpfloat (d, >=, -8.0);
126 g_assert_cmpfloat (d, <, 32.0);
127
128 d = g_random_double_range (begin: -8, end: 32);
129 g_assert_cmpfloat (d, >=, -8.0);
130 g_assert_cmpfloat (d, <, 32.0);
131
132 b = g_random_boolean ();
133 g_assert_true (b == TRUE || b == FALSE);
134
135 b = g_rand_boolean (rand);
136 g_assert_true (b == TRUE || b == FALSE);
137 }
138
139 /* Statistical sanity check, count the number of ones
140 * when getting random numbers in range [0,3) and see
141 * that it must be semi-close to 0.25 with a VERY large
142 * probability */
143 ones = 0;
144 for (n = 1; n < 100000; n++)
145 {
146 if (g_random_int_range (begin: 0, end: 4) == 1)
147 ones ++;
148 }
149
150 proportion = (double)ones / (double)100000;
151 /* 0.025 is overkill, but should suffice to test for some unreasonability */
152 g_assert_cmpfloat (ABS (proportion - 0.25), <, 0.025);
153
154 g_rand_free (rand_: rand);
155 g_rand_free (rand_: copy);
156}
157
158static void
159test_double_range (void)
160{
161 gdouble d;
162
163 g_test_bug (bug_uri_snippet: "502560");
164
165 d = g_random_double_range (begin: -G_MAXDOUBLE, G_MAXDOUBLE);
166
167 g_assert_cmpfloat (-G_MAXDOUBLE, <=, d);
168 g_assert_cmpfloat (d, <, G_MAXDOUBLE);
169}
170
171int
172main (int argc,
173 char *argv[])
174{
175 g_test_init (argc: &argc, argv: &argv, NULL);
176 g_test_bug_base (uri_pattern: "http://bugzilla.gnome.org/");
177
178 g_test_add_func (testpath: "/rand/test-rand", test_func: test_rand);
179 g_test_add_func (testpath: "/rand/double-range", test_func: test_double_range);
180
181 return g_test_run();
182}
183

source code of gtk/subprojects/glib/glib/tests/rand.c