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