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.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
28 /* Tests for a simple seed, first number is the seed */
29 const guint32 first_numbers
[] =
55 const guint32 seed_array
[] =
63 /* tests for the array seed */
64 const guint32 array_outputs
[] =
87 rand
= g_rand_new_with_seed (first_numbers
[0]);
89 for (n
= 1; n
< G_N_ELEMENTS (first_numbers
); n
++)
90 g_assert (first_numbers
[n
] == g_rand_int (rand
));
92 g_rand_set_seed (rand
, 2);
93 g_rand_set_seed_array (rand
, seed_array
, G_N_ELEMENTS (seed_array
));
95 for (n
= 0; n
< G_N_ELEMENTS (array_outputs
); n
++)
96 g_assert (array_outputs
[n
] == g_rand_int (rand
));
98 copy
= g_rand_copy (rand
);
99 for (n
= 0; n
< 100; n
++)
100 g_assert (g_rand_int (copy
) == g_rand_int (rand
));
102 for (n
= 1; n
< 100000; n
++)
108 i
= g_rand_int_range (rand
, 8,16);
109 g_assert (i
>= 8 && i
< 16);
111 i
= g_random_int_range (8,16);
112 g_assert (i
>= 8 && i
< 16);
114 d
= g_rand_double (rand
);
115 g_assert (d
>= 0 && d
< 1);
117 d
= g_random_double ();
118 g_assert (d
>= 0 && d
< 1);
120 d
= g_rand_double_range (rand
, -8, 32);
121 g_assert (d
>= -8 && d
< 32);
123 d
= g_random_double_range (-8, 32);
124 g_assert (d
>= -8 && d
< 32);
126 b
= g_random_boolean ();
127 g_assert (b
== TRUE
|| b
== FALSE
);
129 b
= g_rand_boolean (rand
);
130 g_assert (b
== TRUE
|| b
== FALSE
);
133 /* Statistical sanity check, count the number of ones
134 * when getting random numbers in range [0,3) and see
135 * that it must be semi-close to 0.25 with a VERY large
138 for (n
= 1; n
< 100000; n
++)
140 if (g_random_int_range (0, 4) == 1)
144 proportion
= (double)ones
/ (double)100000;
145 /* 0.025 is overkill, but should suffice to test for some unreasonability */
146 g_assert (ABS (proportion
- 0.25) < 0.025);
153 test_double_range (void)
157 g_test_bug ("502560");
159 d
= g_random_double_range (-G_MAXDOUBLE
, G_MAXDOUBLE
);
161 g_assert (-G_MAXDOUBLE
<= d
);
162 g_assert (d
< G_MAXDOUBLE
);
169 g_test_init (&argc
, &argv
, NULL
);
170 g_test_bug_base ("http://bugzilla.gnome.org/");
172 g_test_add_func ("/rand/test-rand", test_rand
);
173 g_test_add_func ("/rand/double-range", test_double_range
);