michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** File: rngseed.c michael@0: ** Description: michael@0: ** Test NSPR's Random Number Seed generator michael@0: ** michael@0: ** Initial test: Just make sure it outputs some data. michael@0: ** michael@0: ** ... more? ... check some iterations to ensure it is random (no dupes) michael@0: ** ... more? ... histogram distribution of random numbers michael@0: */ michael@0: michael@0: #include "plgetopt.h" michael@0: #include "nspr.h" michael@0: #include "prrng.h" michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* michael@0: ** Test harness infrastructure michael@0: */ michael@0: PRLogModuleInfo *lm; michael@0: PRLogModuleLevel msgLevel = PR_LOG_NONE; michael@0: PRIntn debug = 0; michael@0: PRUint32 failed_already = 0; michael@0: /* end Test harness infrastructure */ michael@0: michael@0: PRIntn optRandCount = 30; michael@0: char buf[40]; michael@0: PRSize bufSize = sizeof(buf); michael@0: PRSize rSize; michael@0: PRIntn i; michael@0: michael@0: /* michael@0: ** Emit help text for this test michael@0: */ michael@0: static void Help( void ) michael@0: { michael@0: printf("Template: Help(): display your help message(s) here"); michael@0: exit(1); michael@0: } /* end Help() */ michael@0: michael@0: static void PrintRand( void *buf, PRIntn size ) michael@0: { michael@0: PRUint32 *rp = buf; michael@0: PRIntn i; michael@0: michael@0: printf("%4.4d--\n", size ); michael@0: while (size > 0 ) { michael@0: switch( size ) { michael@0: case 1 : michael@0: printf("%2.2X\n", *(rp++) ); michael@0: size -= 4; michael@0: break; michael@0: case 2 : michael@0: printf("%4.4X\n", *(rp++) ); michael@0: size -= 4; michael@0: break; michael@0: case 3 : michael@0: printf("%6.6X\n", *(rp++) ); michael@0: size -= 4; michael@0: break; michael@0: default: michael@0: while ( size >= 4) { michael@0: PRIntn i = 3; michael@0: do { michael@0: printf("%8.8X ", *(rp++) ); michael@0: size -= 4; michael@0: } while( i-- ); michael@0: i = 3; michael@0: printf("\n"); michael@0: } michael@0: break; michael@0: } /* end switch() */ michael@0: } /* end while() */ michael@0: } /* end PrintRand() */ michael@0: michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: { michael@0: /* michael@0: ** Get command line options michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "hdv"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug */ michael@0: debug = 1; michael@0: msgLevel = PR_LOG_ERROR; michael@0: break; michael@0: case 'v': /* verbose mode */ michael@0: msgLevel = PR_LOG_DEBUG; michael@0: break; michael@0: case 'h': /* help message */ michael@0: Help(); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: } michael@0: michael@0: lm = PR_NewLogModule("Test"); /* Initialize logging */ michael@0: for ( i = 0; i < optRandCount ; i++ ) { michael@0: memset( buf, 0, bufSize ); michael@0: rSize = PR_GetRandomNoise( buf, bufSize ); michael@0: if (!rSize) { michael@0: fprintf(stderr, "Not implemented\n" ); michael@0: failed_already = PR_TRUE; michael@0: break; michael@0: } michael@0: if (debug) PrintRand( buf, rSize ); michael@0: } michael@0: michael@0: if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS"); michael@0: return( (failed_already == PR_TRUE )? 1 : 0 ); michael@0: } /* main() */ michael@0: /* end template.c */ michael@0: