nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* Unit tests for grand |
2 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
||
3 | * |
||
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. |
||
7 | * |
||
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. |
||
11 | * |
||
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. |
||
20 | */ |
||
21 | |||
22 | #include "glib.h" |
||
23 | |||
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 |
||
26 | */ |
||
27 | |||
28 | /* Tests for a simple seed, first number is the seed */ |
||
29 | const guint32 first_numbers[] = |
||
30 | { |
||
31 | 0x7a7a7a7a, |
||
32 | 0xfdcc2d54, |
||
33 | 0x3a279ceb, |
||
34 | 0xc4d39c33, |
||
35 | 0xf31895cd, |
||
36 | 0x46ca0afc, |
||
37 | 0x3f5484ff, |
||
38 | 0x54bc9557, |
||
39 | 0xed2c24b1, |
||
40 | 0x84062503, |
||
41 | 0x8f6404b3, |
||
42 | 0x599a94b3, |
||
43 | 0xe46d03d5, |
||
44 | 0x310beb78, |
||
45 | 0x7bee5d08, |
||
46 | 0x760d09be, |
||
47 | 0x59b6e163, |
||
48 | 0xbf6d16ec, |
||
49 | 0xcca5fb54, |
||
50 | 0x5de7259b, |
||
51 | 0x1696330c, |
||
52 | }; |
||
53 | |||
54 | /* array seed */ |
||
55 | const guint32 seed_array[] = |
||
56 | { |
||
57 | 0x6553375f, |
||
58 | 0xd6b8d43b, |
||
59 | 0xa1e7667f, |
||
60 | 0x2b10117c |
||
61 | }; |
||
62 | |||
63 | /* tests for the array seed */ |
||
64 | const guint32 array_outputs[] = |
||
65 | { |
||
66 | 0xc22b7dc3, |
||
67 | 0xfdecb8ae, |
||
68 | 0xb4af0738, |
||
69 | 0x516bc6e1, |
||
70 | 0x7e372e91, |
||
71 | 0x2d38ff80, |
||
72 | 0x6096494a, |
||
73 | 0xd162d5a8, |
||
74 | 0x3c0aaa0d, |
||
75 | 0x10e736ae |
||
76 | }; |
||
77 | |||
78 | static void |
||
79 | test_rand (void) |
||
80 | { |
||
81 | guint n; |
||
82 | guint ones; |
||
83 | double proportion; |
||
84 | GRand *rand; |
||
85 | GRand *copy; |
||
86 | |||
87 | rand = g_rand_new_with_seed (first_numbers[0]); |
||
88 | |||
89 | for (n = 1; n < G_N_ELEMENTS (first_numbers); n++) |
||
90 | g_assert (first_numbers[n] == g_rand_int (rand)); |
||
91 | |||
92 | g_rand_set_seed (rand, 2); |
||
93 | g_rand_set_seed_array (rand, seed_array, G_N_ELEMENTS (seed_array)); |
||
94 | |||
95 | for (n = 0; n < G_N_ELEMENTS (array_outputs); n++) |
||
96 | g_assert (array_outputs[n] == g_rand_int (rand)); |
||
97 | |||
98 | copy = g_rand_copy (rand); |
||
99 | for (n = 0; n < 100; n++) |
||
100 | g_assert (g_rand_int (copy) == g_rand_int (rand)); |
||
101 | |||
102 | for (n = 1; n < 100000; n++) |
||
103 | { |
||
104 | gint32 i; |
||
105 | gdouble d; |
||
106 | gboolean b; |
||
107 | |||
108 | i = g_rand_int_range (rand, 8,16); |
||
109 | g_assert (i >= 8 && i < 16); |
||
110 | |||
111 | i = g_random_int_range (8,16); |
||
112 | g_assert (i >= 8 && i < 16); |
||
113 | |||
114 | d = g_rand_double (rand); |
||
115 | g_assert (d >= 0 && d < 1); |
||
116 | |||
117 | d = g_random_double (); |
||
118 | g_assert (d >= 0 && d < 1); |
||
119 | |||
120 | d = g_rand_double_range (rand, -8, 32); |
||
121 | g_assert (d >= -8 && d < 32); |
||
122 | |||
123 | d = g_random_double_range (-8, 32); |
||
124 | g_assert (d >= -8 && d < 32); |
||
125 | |||
126 | b = g_random_boolean (); |
||
127 | g_assert (b == TRUE || b == FALSE); |
||
128 | |||
129 | b = g_rand_boolean (rand); |
||
130 | g_assert (b == TRUE || b == FALSE); |
||
131 | } |
||
132 | |||
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 |
||
136 | * probability */ |
||
137 | ones = 0; |
||
138 | for (n = 1; n < 100000; n++) |
||
139 | { |
||
140 | if (g_random_int_range (0, 4) == 1) |
||
141 | ones ++; |
||
142 | } |
||
143 | |||
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); |
||
147 | |||
148 | g_rand_free (rand); |
||
149 | g_rand_free (copy); |
||
150 | } |
||
151 | |||
152 | static void |
||
153 | test_double_range (void) |
||
154 | { |
||
155 | gdouble d; |
||
156 | |||
157 | g_test_bug ("502560"); |
||
158 | |||
159 | d = g_random_double_range (-G_MAXDOUBLE, G_MAXDOUBLE); |
||
160 | |||
161 | g_assert (-G_MAXDOUBLE <= d); |
||
162 | g_assert (d < G_MAXDOUBLE); |
||
163 | } |
||
164 | |||
165 | int |
||
166 | main (int argc, |
||
167 | char *argv[]) |
||
168 | { |
||
169 | g_test_init (&argc, &argv, NULL); |
||
170 | g_test_bug_base ("http://bugzilla.gnome.org/"); |
||
171 | |||
172 | g_test_add_func ("/rand/test-rand", test_rand); |
||
173 | g_test_add_func ("/rand/double-range", test_double_range); |
||
174 | |||
175 | return g_test_run(); |
||
176 | } |