Back to index

lightning-sunbird  0.9+nobinonly
randseed.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File: rngseed.c
00040 ** Description: 
00041 ** Test NSPR's Random Number Seed generator
00042 **
00043 ** Initial test: Just make sure it outputs some data.
00044 ** 
00045 ** ... more? ... check some iterations to ensure it is random (no dupes)
00046 ** ... more? ... histogram distribution of random numbers
00047 */
00048 
00049 #include "plgetopt.h"
00050 #include "nspr.h" 
00051 #include "prrng.h"
00052 #include <stdio.h>
00053 #include <stdlib.h>
00054 #include <string.h>
00055 
00056 /*
00057 ** Test harness infrastructure
00058 */
00059 PRLogModuleInfo *lm;
00060 PRLogModuleLevel msgLevel = PR_LOG_NONE;
00061 PRIntn  debug = 0;
00062 PRUint32  failed_already = 0;
00063 /* end Test harness infrastructure */
00064 
00065 PRIntn  optRandCount = 30;
00066 char    buf[40];
00067 PRSize  bufSize = sizeof(buf);
00068 PRSize  rSize;
00069 PRIntn  i;
00070 
00071 /*
00072 ** Emit help text for this test
00073 */
00074 static void Help( void )
00075 {
00076     printf("Template: Help(): display your help message(s) here");
00077     exit(1);
00078 } /* end Help() */
00079 
00080 static void PrintRand( void *buf, PRIntn size )
00081 {
00082     PRUint32 *rp = buf;
00083     PRIntn   i;
00084 
00085     printf("%4.4d--\n", size );
00086     while (size > 0 ) {
00087         switch( size )  {
00088             case 1 :
00089                 printf("%2.2X\n", *(rp++) );
00090                 size -= 4;    
00091                 break;
00092             case 2 :
00093                 printf("%4.4X\n", *(rp++) );
00094                 size -= 4;    
00095                 break;
00096             case 3 :
00097                 printf("%6.6X\n", *(rp++) );
00098                 size -= 4;    
00099                 break;
00100             default:
00101                 while ( size >= 4) {
00102                     PRIntn i = 3;
00103                     do {
00104                         printf("%8.8X ", *(rp++) );
00105                         size -= 4;    
00106                     } while( i-- );
00107                     i = 3;
00108                     printf("\n");
00109                 }
00110                 break;
00111         } /* end switch() */
00112     } /* end while() */
00113 } /* end PrintRand() */
00114 
00115 
00116 PRIntn main(PRIntn argc, char *argv[])
00117 {
00118     {
00119         /*
00120         ** Get command line options
00121         */
00122         PLOptStatus os;
00123         PLOptState *opt = PL_CreateOptState(argc, argv, "hdv");
00124 
00125            while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00126         {
00127                   if (PL_OPT_BAD == os) continue;
00128             switch (opt->option)
00129             {
00130             case 'd':  /* debug */
00131                 debug = 1;
00132                          msgLevel = PR_LOG_ERROR;
00133                 break;
00134             case 'v':  /* verbose mode */
00135                          msgLevel = PR_LOG_DEBUG;
00136                 break;
00137             case 'h':  /* help message */
00138                          Help();
00139                 break;
00140              default:
00141                 break;
00142             }
00143         }
00144            PL_DestroyOptState(opt);
00145     }
00146 
00147     lm = PR_NewLogModule("Test");       /* Initialize logging */
00148     for ( i = 0; i < optRandCount ; i++ ) {
00149         memset( buf, 0, bufSize );
00150         rSize = PR_GetRandomNoise( buf, bufSize );
00151         if (!rSize) {
00152             fprintf(stderr, "Not implemented\n" );
00153             failed_already = PR_TRUE;
00154             break;
00155         }
00156         if (debug) PrintRand( buf, rSize );
00157     }
00158 
00159     if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS");
00160     return( (failed_already == PR_TRUE )? 1 : 0 );
00161 }  /* main() */
00162 /* end template.c */
00163