randseed.c 3.4 KB

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