1 /* Unit tests for grand
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
10 * This work is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
26 /* Outputs tested against the reference implementation mt19937ar.c from
27 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
30 /* Tests for a simple seed, first number is the seed */
31 const guint32 first_numbers[] =
57 const guint32 seed_array[] =
65 /* tests for the array seed */
66 const guint32 array_outputs[] =
89 rand = g_rand_new_with_seed (first_numbers[0]);
91 for (n = 1; n < G_N_ELEMENTS (first_numbers); n++)
92 g_assert_cmpuint (first_numbers[n], ==, g_rand_int (rand));
94 g_rand_set_seed (rand, 2);
95 g_rand_set_seed_array (rand, seed_array, G_N_ELEMENTS (seed_array));
97 for (n = 0; n < G_N_ELEMENTS (array_outputs); n++)
98 g_assert_cmpuint (array_outputs[n], ==, g_rand_int (rand));
100 copy = g_rand_copy (rand);
101 for (n = 0; n < 100; n++)
102 g_assert_cmpuint (g_rand_int (copy), ==, g_rand_int (rand));
104 for (n = 1; n < 100000; n++)
110 i = g_rand_int_range (rand, 8,16);
111 g_assert_cmpint (i, >=, 8);
112 g_assert_cmpint (i, <, 16);
114 i = g_random_int_range (8,16);
115 g_assert_cmpint (i, >=, 8);
116 g_assert_cmpint (i, <, 16);
118 d = g_rand_double (rand);
119 g_assert_cmpfloat (d, >=, 0.0);
120 g_assert_cmpfloat (d, <, 1.0);
122 d = g_random_double ();
123 g_assert_cmpfloat (d, >=, 0.0);
124 g_assert_cmpfloat (d, <, 1.0);
126 d = g_rand_double_range (rand, -8, 32);
127 g_assert_cmpfloat (d, >=, -8.0);
128 g_assert_cmpfloat (d, <, 32.0);
130 d = g_random_double_range (-8, 32);
131 g_assert_cmpfloat (d, >=, -8.0);
132 g_assert_cmpfloat (d, <, 32.0);
134 b = g_random_boolean ();
135 g_assert_true (b == TRUE || b == FALSE);
137 b = g_rand_boolean (rand);
138 g_assert_true (b == TRUE || b == FALSE);
141 /* Statistical sanity check, count the number of ones
142 * when getting random numbers in range [0,3) and see
143 * that it must be semi-close to 0.25 with a VERY large
146 for (n = 1; n < 100000; n++)
148 if (g_random_int_range (0, 4) == 1)
152 proportion = (double)ones / (double)100000;
153 /* 0.025 is overkill, but should suffice to test for some unreasonability */
154 g_assert_cmpfloat (ABS (proportion - 0.25), <, 0.025);
161 test_double_range (void)
165 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=502560");
167 d = g_random_double_range (-G_MAXDOUBLE, G_MAXDOUBLE);
169 g_assert_cmpfloat (-G_MAXDOUBLE, <=, d);
170 g_assert_cmpfloat (d, <, G_MAXDOUBLE);
177 g_test_init (&argc, &argv, NULL);
179 g_test_add_func ("/rand/test-rand", test_rand);
180 g_test_add_func ("/rand/double-range", test_double_range);