nsprpub/pr/tests/randseed.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/randseed.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 +** File: rngseed.c
    1.11 +** Description: 
    1.12 +** Test NSPR's Random Number Seed generator
    1.13 +**
    1.14 +** Initial test: Just make sure it outputs some data.
    1.15 +** 
    1.16 +** ... more? ... check some iterations to ensure it is random (no dupes)
    1.17 +** ... more? ... histogram distribution of random numbers
    1.18 +*/
    1.19 +
    1.20 +#include "plgetopt.h"
    1.21 +#include "nspr.h" 
    1.22 +#include "prrng.h"
    1.23 +#include <stdio.h>
    1.24 +#include <stdlib.h>
    1.25 +#include <string.h>
    1.26 +
    1.27 +/*
    1.28 +** Test harness infrastructure
    1.29 +*/
    1.30 +PRLogModuleInfo *lm;
    1.31 +PRLogModuleLevel msgLevel = PR_LOG_NONE;
    1.32 +PRIntn  debug = 0;
    1.33 +PRUint32  failed_already = 0;
    1.34 +/* end Test harness infrastructure */
    1.35 +
    1.36 +PRIntn  optRandCount = 30;
    1.37 +char    buf[40];
    1.38 +PRSize  bufSize = sizeof(buf);
    1.39 +PRSize  rSize;
    1.40 +PRIntn  i;
    1.41 +
    1.42 +/*
    1.43 +** Emit help text for this test
    1.44 +*/
    1.45 +static void Help( void )
    1.46 +{
    1.47 +    printf("Template: Help(): display your help message(s) here");
    1.48 +    exit(1);
    1.49 +} /* end Help() */
    1.50 +
    1.51 +static void PrintRand( void *buf, PRIntn size )
    1.52 +{
    1.53 +    PRUint32 *rp = buf;
    1.54 +    PRIntn   i;
    1.55 +
    1.56 +    printf("%4.4d--\n", size );
    1.57 +    while (size > 0 ) {
    1.58 +        switch( size )  {
    1.59 +            case 1 :
    1.60 +                printf("%2.2X\n", *(rp++) );
    1.61 +                size -= 4;    
    1.62 +                break;
    1.63 +            case 2 :
    1.64 +                printf("%4.4X\n", *(rp++) );
    1.65 +                size -= 4;    
    1.66 +                break;
    1.67 +            case 3 :
    1.68 +                printf("%6.6X\n", *(rp++) );
    1.69 +                size -= 4;    
    1.70 +                break;
    1.71 +            default:
    1.72 +                while ( size >= 4) {
    1.73 +                    PRIntn i = 3;
    1.74 +                    do {
    1.75 +                        printf("%8.8X ", *(rp++) );
    1.76 +                        size -= 4;    
    1.77 +                    } while( i-- );
    1.78 +                    i = 3;
    1.79 +                    printf("\n");
    1.80 +                }
    1.81 +                break;
    1.82 +        } /* end switch() */
    1.83 +    } /* end while() */
    1.84 +} /* end PrintRand() */
    1.85 +
    1.86 +
    1.87 +int main(int argc, char **argv)
    1.88 +{
    1.89 +    {
    1.90 +        /*
    1.91 +        ** Get command line options
    1.92 +        */
    1.93 +        PLOptStatus os;
    1.94 +        PLOptState *opt = PL_CreateOptState(argc, argv, "hdv");
    1.95 +
    1.96 +	    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    1.97 +        {
    1.98 +		    if (PL_OPT_BAD == os) continue;
    1.99 +            switch (opt->option)
   1.100 +            {
   1.101 +            case 'd':  /* debug */
   1.102 +                debug = 1;
   1.103 +			    msgLevel = PR_LOG_ERROR;
   1.104 +                break;
   1.105 +            case 'v':  /* verbose mode */
   1.106 +			    msgLevel = PR_LOG_DEBUG;
   1.107 +                break;
   1.108 +            case 'h':  /* help message */
   1.109 +			    Help();
   1.110 +                break;
   1.111 +             default:
   1.112 +                break;
   1.113 +            }
   1.114 +        }
   1.115 +	    PL_DestroyOptState(opt);
   1.116 +    }
   1.117 +
   1.118 +    lm = PR_NewLogModule("Test");       /* Initialize logging */
   1.119 +    for ( i = 0; i < optRandCount ; i++ ) {
   1.120 +        memset( buf, 0, bufSize );
   1.121 +        rSize = PR_GetRandomNoise( buf, bufSize );
   1.122 +        if (!rSize) {
   1.123 +            fprintf(stderr, "Not implemented\n" );
   1.124 +            failed_already = PR_TRUE;
   1.125 +            break;
   1.126 +        }
   1.127 +        if (debug) PrintRand( buf, rSize );
   1.128 +    }
   1.129 +
   1.130 +    if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS");
   1.131 +    return( (failed_already == PR_TRUE )? 1 : 0 );
   1.132 +}  /* main() */
   1.133 +/* end template.c */
   1.134 +

mercurial