1 /* Unit tests for grand
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
24 /* Outputs tested against the reference implementation mt19937ar.c from
25 http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
27 /* Tests for a simple seed, first number is the seed */
28 const guint32 first_numbers[] =
54 const guint32 seed_array[] =
62 /* tests for the array seed */
63 const guint32 array_outputs[] =
86 rand = g_rand_new_with_seed (first_numbers[0]);
88 for (n = 1; n < G_N_ELEMENTS (first_numbers); n++)
89 g_assert (first_numbers[n] == g_rand_int (rand));
91 g_rand_set_seed (rand, 2);
92 g_rand_set_seed_array (rand, seed_array, G_N_ELEMENTS (seed_array));
94 for (n = 0; n < G_N_ELEMENTS (array_outputs); n++)
95 g_assert (array_outputs[n] == g_rand_int (rand));
97 copy = g_rand_copy (rand);
98 for (n = 0; n < 100; n++)
99 g_assert (g_rand_int (copy) == g_rand_int (rand));
101 for (n = 1; n < 100000; n++)
107 i = g_rand_int_range (rand, 8,16);
108 g_assert (i >= 8 && i < 16);
110 i = g_random_int_range (8,16);
111 g_assert (i >= 8 && i < 16);
113 d = g_rand_double (rand);
114 g_assert (d >= 0 && d < 1);
116 d = g_random_double ();
117 g_assert (d >= 0 && d < 1);
119 d = g_rand_double_range (rand, -8, 32);
120 g_assert (d >= -8 && d < 32);
122 d = g_random_double_range (-8, 32);
123 g_assert (d >= -8 && d < 32);
125 b = g_random_boolean ();
126 g_assert (b == TRUE || b == FALSE);
128 b = g_rand_boolean (rand);
129 g_assert (b == TRUE || b == FALSE);
132 /* Statistical sanity check, count the number of ones
133 * when getting random numbers in range [0,3) and see
134 * that it must be semi-close to 0.25 with a VERY large
137 for (n = 1; n < 100000; n++)
139 if (g_random_int_range (0, 4) == 1)
143 proportion = (double)ones / (double)100000;
144 /* 0.025 is overkill, but should suffice to test for some unreasonability */
145 g_assert (ABS (proportion - 0.25) < 0.025);
155 g_test_init (&argc, &argv, NULL);
157 g_test_add_func ("/rand/test-rand", test_rand);