First public contribution.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
10 #include "mrt2_glib2_test.h"
14 /* Outputs tested against the reference implementation mt19937ar.c from
15 http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
17 /* Tests for a simple seed, first number is the seed */
18 const guint32 first_numbers[] =
44 const guint32 seed_array[] =
52 /* tests for the array seed */
53 const guint32 array_outputs[] =
67 const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]);
68 const gint seed_length = sizeof (seed_array) / sizeof (seed_array[0]);
69 const gint array_length = sizeof (array_outputs) / sizeof (array_outputs[0]);
77 GRand* rand;// = g_rand_new_with_seed (first_numbers[0]);
81 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
82 g_set_print_handler(mrtPrintHandler);
85 rand = g_rand_new_with_seed (first_numbers[0]);
87 for (n = 1; n < length; n++)
88 g_assert (first_numbers[n] == g_rand_int (rand));
90 g_rand_set_seed (rand, 2);
91 g_rand_set_seed_array (rand, seed_array, seed_length);
93 for (n = 0; n < array_length; n++)
94 g_assert (array_outputs[n] == g_rand_int (rand));
96 copy = g_rand_copy (rand);
97 for (n = 0; n < 100; n++)
98 g_assert (g_rand_int (copy) == g_rand_int (rand));
100 // for (n = 1; n < 100000; n++)
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)
142 proportion = (double)ones / (double)100000;
143 /* 0.025 is overkill, but should suffice to test for some unreasonability */
144 g_assert (ABS (proportion - 0.25) < 0.025);
150 testResultXml("rand-test");
151 #endif /* EMULATOR */