|
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ |
|
2 #undef G_DISABLE_ASSERT |
|
3 #undef G_LOG_DOMAIN |
|
4 |
|
5 #include <glib.h> |
|
6 #include <stdio.h> |
|
7 |
|
8 #ifdef __SYMBIAN32__ |
|
9 #include "mrt2_glib2_test.h" |
|
10 #endif /*__SYMBIAN32__*/ |
|
11 |
|
12 |
|
13 /* Outputs tested against the reference implementation mt19937ar.c from |
|
14 http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */ |
|
15 |
|
16 /* Tests for a simple seed, first number is the seed */ |
|
17 const guint32 first_numbers[] = |
|
18 { |
|
19 0x7a7a7a7a, |
|
20 0xfdcc2d54, |
|
21 0x3a279ceb, |
|
22 0xc4d39c33, |
|
23 0xf31895cd, |
|
24 0x46ca0afc, |
|
25 0x3f5484ff, |
|
26 0x54bc9557, |
|
27 0xed2c24b1, |
|
28 0x84062503, |
|
29 0x8f6404b3, |
|
30 0x599a94b3, |
|
31 0xe46d03d5, |
|
32 0x310beb78, |
|
33 0x7bee5d08, |
|
34 0x760d09be, |
|
35 0x59b6e163, |
|
36 0xbf6d16ec, |
|
37 0xcca5fb54, |
|
38 0x5de7259b, |
|
39 0x1696330c, |
|
40 }; |
|
41 |
|
42 /* array seed */ |
|
43 const guint32 seed_array[] = |
|
44 { |
|
45 0x6553375f, |
|
46 0xd6b8d43b, |
|
47 0xa1e7667f, |
|
48 0x2b10117c |
|
49 }; |
|
50 |
|
51 /* tests for the array seed */ |
|
52 const guint32 array_outputs[] = |
|
53 { |
|
54 0xc22b7dc3, |
|
55 0xfdecb8ae, |
|
56 0xb4af0738, |
|
57 0x516bc6e1, |
|
58 0x7e372e91, |
|
59 0x2d38ff80, |
|
60 0x6096494a, |
|
61 0xd162d5a8, |
|
62 0x3c0aaa0d, |
|
63 0x10e736ae |
|
64 }; |
|
65 |
|
66 const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]); |
|
67 const gint seed_length = sizeof (seed_array) / sizeof (seed_array[0]); |
|
68 const gint array_length = sizeof (array_outputs) / sizeof (array_outputs[0]); |
|
69 |
|
70 int main() |
|
71 { |
|
72 guint n; |
|
73 guint ones; |
|
74 double proportion; |
|
75 |
|
76 GRand* rand;// = g_rand_new_with_seed (first_numbers[0]); |
|
77 GRand* copy; |
|
78 |
|
79 #ifdef __SYMBIAN32__ |
|
80 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); |
|
81 g_set_print_handler(mrtPrintHandler); |
|
82 #endif /*__SYMBIAN32__*/ |
|
83 |
|
84 rand = g_rand_new_with_seed (first_numbers[0]); |
|
85 |
|
86 for (n = 1; n < length; n++) |
|
87 g_assert (first_numbers[n] == g_rand_int (rand)); |
|
88 |
|
89 g_rand_set_seed (rand, 2); |
|
90 g_rand_set_seed_array (rand, seed_array, seed_length); |
|
91 |
|
92 for (n = 0; n < array_length; n++) |
|
93 g_assert (array_outputs[n] == g_rand_int (rand)); |
|
94 |
|
95 copy = g_rand_copy (rand); |
|
96 for (n = 0; n < 100; n++) |
|
97 g_assert (g_rand_int (copy) == g_rand_int (rand)); |
|
98 |
|
99 // for (n = 1; n < 100000; n++) |
|
100 for (n = 1; n < 100000; n++) |
|
101 { |
|
102 gint32 i; |
|
103 gdouble d; |
|
104 gboolean b; |
|
105 |
|
106 i = g_rand_int_range (rand, 8,16); |
|
107 g_assert (i >= 8 && i < 16); |
|
108 |
|
109 i = g_random_int_range (8,16); |
|
110 g_assert (i >= 8 && i < 16); |
|
111 |
|
112 d = g_rand_double (rand); |
|
113 g_assert (d >= 0 && d < 1); |
|
114 |
|
115 d = g_random_double (); |
|
116 g_assert (d >= 0 && d < 1); |
|
117 |
|
118 d = g_rand_double_range (rand, -8, 32); |
|
119 g_assert (d >= -8 && d < 32); |
|
120 |
|
121 d = g_random_double_range (-8, 32); |
|
122 g_assert (d >= -8 && d < 32); |
|
123 |
|
124 b = g_random_boolean (); |
|
125 g_assert (b == TRUE || b == FALSE); |
|
126 |
|
127 b = g_rand_boolean (rand); |
|
128 g_assert (b == TRUE || b == FALSE); |
|
129 } |
|
130 |
|
131 /* Statistical sanity check, count the number of ones |
|
132 * when getting random numbers in range [0,3) and see |
|
133 * that it must be semi-close to 0.25 with a VERY large |
|
134 * probability */ |
|
135 ones = 0; |
|
136 for (n = 1; n < 100000; n++) |
|
137 { |
|
138 if (g_random_int_range (0, 4) == 1) |
|
139 ones ++; |
|
140 } |
|
141 proportion = (double)ones / (double)100000; |
|
142 /* 0.025 is overkill, but should suffice to test for some unreasonability */ |
|
143 g_assert (ABS (proportion - 0.25) < 0.025); |
|
144 |
|
145 g_rand_free (rand); |
|
146 g_rand_free (copy); |
|
147 |
|
148 #ifdef __SYMBIAN32__ |
|
149 testResultXml("rand-test"); |
|
150 #endif /* EMULATOR */ |
|
151 |
|
152 return 0; |
|
153 } |
|
154 |