Bug 550096 – GBookmarkFile parser is not forward compatible
[platform/upstream/glib.git] / glib / tests / rand.c
1 /* Unit tests for gstring
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.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
26
27 /* Tests for a simple seed, first number is the seed */
28 const guint32 first_numbers[] = 
29 {
30   0x7a7a7a7a,
31   0xfdcc2d54,
32   0x3a279ceb,
33   0xc4d39c33,
34   0xf31895cd,
35   0x46ca0afc,
36   0x3f5484ff,
37   0x54bc9557,
38   0xed2c24b1,
39   0x84062503,
40   0x8f6404b3,
41   0x599a94b3,
42   0xe46d03d5,
43   0x310beb78,
44   0x7bee5d08,
45   0x760d09be,
46   0x59b6e163,
47   0xbf6d16ec,
48   0xcca5fb54,
49   0x5de7259b,
50   0x1696330c,
51 };
52
53 /* array seed */
54 const guint32 seed_array[] =
55 {
56   0x6553375f,
57   0xd6b8d43b,
58   0xa1e7667f,
59   0x2b10117c
60 };
61
62 /* tests for the array seed */
63 const guint32 array_outputs[] =
64 {
65   0xc22b7dc3,
66   0xfdecb8ae,
67   0xb4af0738,
68   0x516bc6e1,
69   0x7e372e91,
70   0x2d38ff80,
71   0x6096494a,
72   0xd162d5a8,
73   0x3c0aaa0d,
74   0x10e736ae
75 };
76
77 static void
78 test_rand (void)
79 {
80   guint n;
81   guint ones;
82   double proportion;
83   GRand *rand;
84   GRand *copy;
85
86   rand = g_rand_new_with_seed (first_numbers[0]);
87
88   for (n = 1; n < G_N_ELEMENTS (first_numbers); n++)
89     g_assert (first_numbers[n] == g_rand_int (rand));
90
91   g_rand_set_seed (rand, 2);
92   g_rand_set_seed_array (rand, seed_array, G_N_ELEMENTS (seed_array));
93
94   for (n = 0; n < G_N_ELEMENTS (array_outputs); n++)
95     g_assert (array_outputs[n] == g_rand_int (rand));
96
97   copy = g_rand_copy (rand);
98   for (n = 0; n < 100; n++)
99     g_assert (g_rand_int (copy) == g_rand_int (rand));
100
101   for (n = 1; n < 100000; n++)
102     {
103       gint32 i;
104       gdouble d;
105       gboolean b;
106
107       i = g_rand_int_range (rand, 8,16);
108       g_assert (i >= 8 && i < 16);
109       
110       i = g_random_int_range (8,16);
111       g_assert (i >= 8 && i < 16);
112
113       d = g_rand_double (rand);
114       g_assert (d >= 0 && d < 1);
115
116       d = g_random_double ();
117       g_assert (d >= 0 && d < 1);
118
119       d = g_rand_double_range (rand, -8, 32);
120       g_assert (d >= -8 && d < 32);
121  
122       d = g_random_double_range (-8, 32);
123       g_assert (d >= -8 && d < 32);
124  
125       b = g_random_boolean ();
126       g_assert (b == TRUE || b  == FALSE);
127  
128       b = g_rand_boolean (rand);
129       g_assert (b == TRUE || b  == FALSE);     
130     }
131
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
135    * probability */
136   ones = 0;
137   for (n = 1; n < 100000; n++)
138     {
139       if (g_random_int_range (0, 4) == 1)
140         ones ++;
141     }
142
143   proportion = (double)ones / (double)100000;
144   /* 0.025 is overkill, but should suffice to test for some unreasonability */
145   g_assert (ABS (proportion - 0.25) < 0.025);
146
147   g_rand_free (rand);
148   g_rand_free (copy);
149
150   return 0;
151 }
152
153 int
154 main (int   argc,
155       char *argv[])
156 {
157   g_test_init (&argc, &argv, NULL);
158
159   g_test_add_func ("/rand/test-rand", test_rand);
160
161   return g_test_run();
162 }