1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/rand-test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,155 @@
1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
1.5 +#undef G_DISABLE_ASSERT
1.6 +#undef G_LOG_DOMAIN
1.7 +
1.8 +#include <glib.h>
1.9 +#include <stdio.h>
1.10 +#include <unistd.h>
1.11 +
1.12 +#ifdef SYMBIAN
1.13 +#include "mrt2_glib2_test.h"
1.14 +#endif /*SYMBIAN*/
1.15 +
1.16 +
1.17 +/* Outputs tested against the reference implementation mt19937ar.c from
1.18 + http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
1.19 +
1.20 +/* Tests for a simple seed, first number is the seed */
1.21 +const guint32 first_numbers[] =
1.22 +{
1.23 + 0x7a7a7a7a,
1.24 + 0xfdcc2d54,
1.25 + 0x3a279ceb,
1.26 + 0xc4d39c33,
1.27 + 0xf31895cd,
1.28 + 0x46ca0afc,
1.29 + 0x3f5484ff,
1.30 + 0x54bc9557,
1.31 + 0xed2c24b1,
1.32 + 0x84062503,
1.33 + 0x8f6404b3,
1.34 + 0x599a94b3,
1.35 + 0xe46d03d5,
1.36 + 0x310beb78,
1.37 + 0x7bee5d08,
1.38 + 0x760d09be,
1.39 + 0x59b6e163,
1.40 + 0xbf6d16ec,
1.41 + 0xcca5fb54,
1.42 + 0x5de7259b,
1.43 + 0x1696330c,
1.44 +};
1.45 +
1.46 +/* array seed */
1.47 +const guint32 seed_array[] =
1.48 +{
1.49 + 0x6553375f,
1.50 + 0xd6b8d43b,
1.51 + 0xa1e7667f,
1.52 + 0x2b10117c
1.53 +};
1.54 +
1.55 +/* tests for the array seed */
1.56 +const guint32 array_outputs[] =
1.57 +{
1.58 + 0xc22b7dc3,
1.59 + 0xfdecb8ae,
1.60 + 0xb4af0738,
1.61 + 0x516bc6e1,
1.62 + 0x7e372e91,
1.63 + 0x2d38ff80,
1.64 + 0x6096494a,
1.65 + 0xd162d5a8,
1.66 + 0x3c0aaa0d,
1.67 + 0x10e736ae
1.68 +};
1.69 +
1.70 +const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]);
1.71 +const gint seed_length = sizeof (seed_array) / sizeof (seed_array[0]);
1.72 +const gint array_length = sizeof (array_outputs) / sizeof (array_outputs[0]);
1.73 +
1.74 +int main()
1.75 +{
1.76 + guint n;
1.77 + guint ones;
1.78 + double proportion;
1.79 +
1.80 + GRand* rand;// = g_rand_new_with_seed (first_numbers[0]);
1.81 + GRand* copy;
1.82 +
1.83 + #ifdef SYMBIAN
1.84 + 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);
1.85 + g_set_print_handler(mrtPrintHandler);
1.86 + #endif /*SYMBIAN*/
1.87 +
1.88 + rand = g_rand_new_with_seed (first_numbers[0]);
1.89 +
1.90 + for (n = 1; n < length; n++)
1.91 + g_assert (first_numbers[n] == g_rand_int (rand));
1.92 +
1.93 + g_rand_set_seed (rand, 2);
1.94 + g_rand_set_seed_array (rand, seed_array, seed_length);
1.95 +
1.96 + for (n = 0; n < array_length; n++)
1.97 + g_assert (array_outputs[n] == g_rand_int (rand));
1.98 +
1.99 + copy = g_rand_copy (rand);
1.100 + for (n = 0; n < 100; n++)
1.101 + g_assert (g_rand_int (copy) == g_rand_int (rand));
1.102 +
1.103 + // for (n = 1; n < 100000; n++)
1.104 + for (n = 1; n < 100000; n++)
1.105 + {
1.106 + gint32 i;
1.107 + gdouble d;
1.108 + gboolean b;
1.109 +
1.110 + i = g_rand_int_range (rand, 8,16);
1.111 + g_assert (i >= 8 && i < 16);
1.112 +
1.113 + i = g_random_int_range (8,16);
1.114 + g_assert (i >= 8 && i < 16);
1.115 +
1.116 + d = g_rand_double (rand);
1.117 + g_assert (d >= 0 && d < 1);
1.118 +
1.119 + d = g_random_double ();
1.120 + g_assert (d >= 0 && d < 1);
1.121 +
1.122 + d = g_rand_double_range (rand, -8, 32);
1.123 + g_assert (d >= -8 && d < 32);
1.124 +
1.125 + d = g_random_double_range (-8, 32);
1.126 + g_assert (d >= -8 && d < 32);
1.127 +
1.128 + b = g_random_boolean ();
1.129 + g_assert (b == TRUE || b == FALSE);
1.130 +
1.131 + b = g_rand_boolean (rand);
1.132 + g_assert (b == TRUE || b == FALSE);
1.133 + }
1.134 +
1.135 + /* Statistical sanity check, count the number of ones
1.136 + * when getting random numbers in range [0,3) and see
1.137 + * that it must be semi-close to 0.25 with a VERY large
1.138 + * probability */
1.139 + ones = 0;
1.140 + for (n = 1; n < 100000; n++)
1.141 + {
1.142 + if (g_random_int_range (0, 4) == 1)
1.143 + ones ++;
1.144 + }
1.145 + proportion = (double)ones / (double)100000;
1.146 + /* 0.025 is overkill, but should suffice to test for some unreasonability */
1.147 + g_assert (ABS (proportion - 0.25) < 0.025);
1.148 +
1.149 + g_rand_free (rand);
1.150 + g_rand_free (copy);
1.151 +
1.152 +#ifdef SYMBIAN
1.153 + testResultXml("rand-test");
1.154 +#endif /* EMULATOR */
1.155 +
1.156 + return 0;
1.157 +}
1.158 +