nsprpub/pr/tests/randseed.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /*
     7 ** File: rngseed.c
     8 ** Description: 
     9 ** Test NSPR's Random Number Seed generator
    10 **
    11 ** Initial test: Just make sure it outputs some data.
    12 ** 
    13 ** ... more? ... check some iterations to ensure it is random (no dupes)
    14 ** ... more? ... histogram distribution of random numbers
    15 */
    17 #include "plgetopt.h"
    18 #include "nspr.h" 
    19 #include "prrng.h"
    20 #include <stdio.h>
    21 #include <stdlib.h>
    22 #include <string.h>
    24 /*
    25 ** Test harness infrastructure
    26 */
    27 PRLogModuleInfo *lm;
    28 PRLogModuleLevel msgLevel = PR_LOG_NONE;
    29 PRIntn  debug = 0;
    30 PRUint32  failed_already = 0;
    31 /* end Test harness infrastructure */
    33 PRIntn  optRandCount = 30;
    34 char    buf[40];
    35 PRSize  bufSize = sizeof(buf);
    36 PRSize  rSize;
    37 PRIntn  i;
    39 /*
    40 ** Emit help text for this test
    41 */
    42 static void Help( void )
    43 {
    44     printf("Template: Help(): display your help message(s) here");
    45     exit(1);
    46 } /* end Help() */
    48 static void PrintRand( void *buf, PRIntn size )
    49 {
    50     PRUint32 *rp = buf;
    51     PRIntn   i;
    53     printf("%4.4d--\n", size );
    54     while (size > 0 ) {
    55         switch( size )  {
    56             case 1 :
    57                 printf("%2.2X\n", *(rp++) );
    58                 size -= 4;    
    59                 break;
    60             case 2 :
    61                 printf("%4.4X\n", *(rp++) );
    62                 size -= 4;    
    63                 break;
    64             case 3 :
    65                 printf("%6.6X\n", *(rp++) );
    66                 size -= 4;    
    67                 break;
    68             default:
    69                 while ( size >= 4) {
    70                     PRIntn i = 3;
    71                     do {
    72                         printf("%8.8X ", *(rp++) );
    73                         size -= 4;    
    74                     } while( i-- );
    75                     i = 3;
    76                     printf("\n");
    77                 }
    78                 break;
    79         } /* end switch() */
    80     } /* end while() */
    81 } /* end PrintRand() */
    84 int main(int argc, char **argv)
    85 {
    86     {
    87         /*
    88         ** Get command line options
    89         */
    90         PLOptStatus os;
    91         PLOptState *opt = PL_CreateOptState(argc, argv, "hdv");
    93 	    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    94         {
    95 		    if (PL_OPT_BAD == os) continue;
    96             switch (opt->option)
    97             {
    98             case 'd':  /* debug */
    99                 debug = 1;
   100 			    msgLevel = PR_LOG_ERROR;
   101                 break;
   102             case 'v':  /* verbose mode */
   103 			    msgLevel = PR_LOG_DEBUG;
   104                 break;
   105             case 'h':  /* help message */
   106 			    Help();
   107                 break;
   108              default:
   109                 break;
   110             }
   111         }
   112 	    PL_DestroyOptState(opt);
   113     }
   115     lm = PR_NewLogModule("Test");       /* Initialize logging */
   116     for ( i = 0; i < optRandCount ; i++ ) {
   117         memset( buf, 0, bufSize );
   118         rSize = PR_GetRandomNoise( buf, bufSize );
   119         if (!rSize) {
   120             fprintf(stderr, "Not implemented\n" );
   121             failed_already = PR_TRUE;
   122             break;
   123         }
   124         if (debug) PrintRand( buf, rSize );
   125     }
   127     if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS");
   128     return( (failed_already == PR_TRUE )? 1 : 0 );
   129 }  /* main() */
   130 /* end template.c */

mercurial