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