glib/tsrc/BC/tests/rand-test.c
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     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 #include <unistd.h>
       
     8 
       
     9 #ifdef SYMBIAN
       
    10 #include "mrt2_glib2_test.h"
       
    11 #endif /*SYMBIAN*/
       
    12 
       
    13 
       
    14 /* Outputs tested against the reference implementation mt19937ar.c from
       
    15    http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
       
    16 
       
    17 /* Tests for a simple seed, first number is the seed */
       
    18 const guint32 first_numbers[] = 
       
    19 {
       
    20   0x7a7a7a7a,
       
    21   0xfdcc2d54,
       
    22   0x3a279ceb,
       
    23   0xc4d39c33,
       
    24   0xf31895cd,
       
    25   0x46ca0afc,
       
    26   0x3f5484ff,
       
    27   0x54bc9557,
       
    28   0xed2c24b1,
       
    29   0x84062503,
       
    30   0x8f6404b3,
       
    31   0x599a94b3,
       
    32   0xe46d03d5,
       
    33   0x310beb78,
       
    34   0x7bee5d08,
       
    35   0x760d09be,
       
    36   0x59b6e163,
       
    37   0xbf6d16ec,
       
    38   0xcca5fb54,
       
    39   0x5de7259b,
       
    40   0x1696330c,
       
    41 };
       
    42 
       
    43 /* array seed */
       
    44 const guint32 seed_array[] =
       
    45 {
       
    46   0x6553375f,
       
    47   0xd6b8d43b,
       
    48   0xa1e7667f,
       
    49   0x2b10117c
       
    50 };
       
    51 
       
    52 /* tests for the array seed */
       
    53 const guint32 array_outputs[] =
       
    54 {
       
    55   0xc22b7dc3,
       
    56   0xfdecb8ae,
       
    57   0xb4af0738,
       
    58   0x516bc6e1,
       
    59   0x7e372e91,
       
    60   0x2d38ff80,
       
    61   0x6096494a,
       
    62   0xd162d5a8,
       
    63   0x3c0aaa0d,
       
    64   0x10e736ae
       
    65 };
       
    66 
       
    67 const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]);
       
    68 const gint seed_length = sizeof (seed_array) / sizeof (seed_array[0]);
       
    69 const gint array_length = sizeof (array_outputs) / sizeof (array_outputs[0]);
       
    70 
       
    71 int main()
       
    72 {
       
    73   guint n;
       
    74   guint ones;
       
    75   double proportion;
       
    76 
       
    77   GRand* rand;// = g_rand_new_with_seed (first_numbers[0]);
       
    78   GRand* copy;
       
    79 
       
    80   #ifdef SYMBIAN
       
    81   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);
       
    82   g_set_print_handler(mrtPrintHandler);
       
    83   #endif /*SYMBIAN*/
       
    84 	  
       
    85   rand = g_rand_new_with_seed (first_numbers[0]);
       
    86 
       
    87   for (n = 1; n < length; n++)
       
    88     g_assert (first_numbers[n] == g_rand_int (rand));
       
    89 
       
    90   g_rand_set_seed (rand, 2);
       
    91   g_rand_set_seed_array (rand, seed_array, seed_length);
       
    92 
       
    93   for (n = 0; n < array_length; n++)
       
    94     g_assert (array_outputs[n] == g_rand_int (rand));
       
    95 
       
    96   copy = g_rand_copy (rand);
       
    97   for (n = 0; n < 100; n++)
       
    98     g_assert (g_rand_int (copy) == g_rand_int (rand));
       
    99 
       
   100  // for (n = 1; n < 100000; n++)
       
   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   proportion = (double)ones / (double)100000;
       
   143   /* 0.025 is overkill, but should suffice to test for some unreasonability */
       
   144   g_assert (ABS (proportion - 0.25) < 0.025);
       
   145 
       
   146   g_rand_free (rand);
       
   147   g_rand_free (copy);
       
   148 
       
   149 #ifdef SYMBIAN
       
   150   testResultXml("rand-test");
       
   151 #endif /* EMULATOR */
       
   152 
       
   153   return 0;
       
   154 }
       
   155