sl@0: /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: sl@0: /* Outputs tested against the reference implementation mt19937ar.c from sl@0: http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */ sl@0: sl@0: /* Tests for a simple seed, first number is the seed */ sl@0: const guint32 first_numbers[] = sl@0: { sl@0: 0x7a7a7a7a, sl@0: 0xfdcc2d54, sl@0: 0x3a279ceb, sl@0: 0xc4d39c33, sl@0: 0xf31895cd, sl@0: 0x46ca0afc, sl@0: 0x3f5484ff, sl@0: 0x54bc9557, sl@0: 0xed2c24b1, sl@0: 0x84062503, sl@0: 0x8f6404b3, sl@0: 0x599a94b3, sl@0: 0xe46d03d5, sl@0: 0x310beb78, sl@0: 0x7bee5d08, sl@0: 0x760d09be, sl@0: 0x59b6e163, sl@0: 0xbf6d16ec, sl@0: 0xcca5fb54, sl@0: 0x5de7259b, sl@0: 0x1696330c, sl@0: }; sl@0: sl@0: /* array seed */ sl@0: const guint32 seed_array[] = sl@0: { sl@0: 0x6553375f, sl@0: 0xd6b8d43b, sl@0: 0xa1e7667f, sl@0: 0x2b10117c sl@0: }; sl@0: sl@0: /* tests for the array seed */ sl@0: const guint32 array_outputs[] = sl@0: { sl@0: 0xc22b7dc3, sl@0: 0xfdecb8ae, sl@0: 0xb4af0738, sl@0: 0x516bc6e1, sl@0: 0x7e372e91, sl@0: 0x2d38ff80, sl@0: 0x6096494a, sl@0: 0xd162d5a8, sl@0: 0x3c0aaa0d, sl@0: 0x10e736ae sl@0: }; sl@0: sl@0: const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]); sl@0: const gint seed_length = sizeof (seed_array) / sizeof (seed_array[0]); sl@0: const gint array_length = sizeof (array_outputs) / sizeof (array_outputs[0]); sl@0: sl@0: int main() sl@0: { sl@0: guint n; sl@0: guint ones; sl@0: double proportion; sl@0: sl@0: GRand* rand;// = g_rand_new_with_seed (first_numbers[0]); sl@0: GRand* copy; sl@0: sl@0: #ifdef SYMBIAN sl@0: 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); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: rand = g_rand_new_with_seed (first_numbers[0]); sl@0: sl@0: for (n = 1; n < length; n++) sl@0: g_assert (first_numbers[n] == g_rand_int (rand)); sl@0: sl@0: g_rand_set_seed (rand, 2); sl@0: g_rand_set_seed_array (rand, seed_array, seed_length); sl@0: sl@0: for (n = 0; n < array_length; n++) sl@0: g_assert (array_outputs[n] == g_rand_int (rand)); sl@0: sl@0: copy = g_rand_copy (rand); sl@0: for (n = 0; n < 100; n++) sl@0: g_assert (g_rand_int (copy) == g_rand_int (rand)); sl@0: sl@0: // for (n = 1; n < 100000; n++) sl@0: for (n = 1; n < 100000; n++) sl@0: { sl@0: gint32 i; sl@0: gdouble d; sl@0: gboolean b; sl@0: sl@0: i = g_rand_int_range (rand, 8,16); sl@0: g_assert (i >= 8 && i < 16); sl@0: sl@0: i = g_random_int_range (8,16); sl@0: g_assert (i >= 8 && i < 16); sl@0: sl@0: d = g_rand_double (rand); sl@0: g_assert (d >= 0 && d < 1); sl@0: sl@0: d = g_random_double (); sl@0: g_assert (d >= 0 && d < 1); sl@0: sl@0: d = g_rand_double_range (rand, -8, 32); sl@0: g_assert (d >= -8 && d < 32); sl@0: sl@0: d = g_random_double_range (-8, 32); sl@0: g_assert (d >= -8 && d < 32); sl@0: sl@0: b = g_random_boolean (); sl@0: g_assert (b == TRUE || b == FALSE); sl@0: sl@0: b = g_rand_boolean (rand); sl@0: g_assert (b == TRUE || b == FALSE); sl@0: } sl@0: sl@0: /* Statistical sanity check, count the number of ones sl@0: * when getting random numbers in range [0,3) and see sl@0: * that it must be semi-close to 0.25 with a VERY large sl@0: * probability */ sl@0: ones = 0; sl@0: for (n = 1; n < 100000; n++) sl@0: { sl@0: if (g_random_int_range (0, 4) == 1) sl@0: ones ++; sl@0: } sl@0: proportion = (double)ones / (double)100000; sl@0: /* 0.025 is overkill, but should suffice to test for some unreasonability */ sl@0: g_assert (ABS (proportion - 0.25) < 0.025); sl@0: sl@0: g_rand_free (rand); sl@0: g_rand_free (copy); sl@0: sl@0: #ifdef SYMBIAN sl@0: testResultXml("rand-test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return 0; sl@0: } sl@0: