kerneltest/e32utils/nistsecurerng/src/randomexcursions.cpp
branchRCL_3
changeset 294 039a3e647356
parent 268 345b1ca54e88
child 295 5460f47b94ad
equal deleted inserted replaced
268:345b1ca54e88 294:039a3e647356
     1 /*
       
     2 * Portions Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * The original NIST Statistical Test Suite code is placed in public domain.
       
    16 * (http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html) 
       
    17 * 
       
    18 * This software was developed at the National Institute of Standards and Technology by 
       
    19 * employees of the Federal Government in the course of their official duties. Pursuant
       
    20 * to title 17 Section 105 of the United States Code this software is not subject to 
       
    21 * copyright protection and is in the public domain. The NIST Statistical Test Suite is
       
    22 * an experimental system. NIST assumes no responsibility whatsoever for its use by other 
       
    23 * parties, and makes no guarantees, expressed or implied, about its quality, reliability, 
       
    24 * or any other characteristic. We would appreciate acknowledgment if the software is used.
       
    25 */
       
    26 
       
    27 #include "openc.h"
       
    28 #include "../include/externs.h"
       
    29 #include "../include/cephes.h"  
       
    30 
       
    31 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    32                      R A N D O M  E X C U R S I O N S  T E S T
       
    33  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    34 
       
    35 void
       
    36 RandomExcursions(int n)
       
    37 {
       
    38 	int		b, i, j, k, J, x;
       
    39 	int		cycleStart, cycleStop, *cycle = NULL, *S_k = NULL;
       
    40 	int		stateX[8] = { -4, -3, -2, -1, 1, 2, 3, 4 };
       
    41 	int		counter[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
       
    42 	double	p_value, sum, constraint, nu[6][8];
       
    43 	double	pi[5][6] = { {0.0000000000, 0.00000000000, 0.00000000000, 0.00000000000, 0.00000000000, 0.0000000000}, 
       
    44 						 {0.5000000000, 0.25000000000, 0.12500000000, 0.06250000000, 0.03125000000, 0.0312500000},
       
    45 						 {0.7500000000, 0.06250000000, 0.04687500000, 0.03515625000, 0.02636718750, 0.0791015625},
       
    46 						 {0.8333333333, 0.02777777778, 0.02314814815, 0.01929012346, 0.01607510288, 0.0803755143},
       
    47 						 {0.8750000000, 0.01562500000, 0.01367187500, 0.01196289063, 0.01046752930, 0.0732727051} };
       
    48 	
       
    49 	if ( ((S_k = (int *)calloc(n, sizeof(int))) == NULL) ||
       
    50 		 ((cycle = (int *)calloc(MAX(1000, n/100), sizeof(int))) == NULL) ) {
       
    51 		printf("Random Excursions Test:  Insufficent Work Space Allocated.\n");
       
    52 		if ( S_k != NULL )
       
    53 			free(S_k);
       
    54 		if ( cycle != NULL )
       
    55 			free(cycle);
       
    56 		return;
       
    57 	}
       
    58 	
       
    59 	J = 0; 					/* DETERMINE CYCLES */
       
    60 	S_k[0] = 2*(int)epsilon[0] - 1;
       
    61 	for( i=1; i<n; i++ ) {
       
    62 		S_k[i] = S_k[i-1] + 2*epsilon[i] - 1;
       
    63 		if ( S_k[i] == 0 ) {
       
    64 			J++;
       
    65 			if ( J > MAX(1000, n/100) ) {
       
    66 				printf("ERROR IN FUNCTION randomExcursions:  EXCEEDING THE MAX NUMBER OF CYCLES EXPECTED\n.");
       
    67 				free(S_k);
       
    68 				free(cycle);
       
    69 				return;
       
    70 			}
       
    71 			cycle[J] = i;
       
    72 		}
       
    73 	}
       
    74 	if ( S_k[n-1] != 0 )
       
    75 		J++;
       
    76 	cycle[J] = n;
       
    77 
       
    78 	fprintf(stats[TEST_RND_EXCURSION], "\t\t\t  RANDOM EXCURSIONS TEST\n");
       
    79 	fprintf(stats[TEST_RND_EXCURSION], "\t\t--------------------------------------------\n");
       
    80 	fprintf(stats[TEST_RND_EXCURSION], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
    81 	fprintf(stats[TEST_RND_EXCURSION], "\t\t--------------------------------------------\n");
       
    82 	fprintf(stats[TEST_RND_EXCURSION], "\t\t(a) Number Of Cycles (J) = %04d\n", J);
       
    83 	fprintf(stats[TEST_RND_EXCURSION], "\t\t(b) Sequence Length (n)  = %d\n", n);
       
    84 
       
    85 	constraint = MAX(0.005*pow(n, 0.5), 500);
       
    86 	if (J < constraint) {
       
    87 		fprintf(stats[TEST_RND_EXCURSION], "\t\t---------------------------------------------\n");
       
    88 		fprintf(stats[TEST_RND_EXCURSION], "\t\tWARNING:  TEST NOT APPLICABLE.  THERE ARE AN\n");
       
    89 		fprintf(stats[TEST_RND_EXCURSION], "\t\t\t  INSUFFICIENT NUMBER OF CYCLES.\n");
       
    90 		fprintf(stats[TEST_RND_EXCURSION], "\t\t---------------------------------------------\n");
       
    91 		for(i = 0; i < 8; i++)
       
    92 			fprintf(results[TEST_RND_EXCURSION], "%f\n", 0.0);
       
    93 	}
       
    94 	else {
       
    95 		fprintf(stats[TEST_RND_EXCURSION], "\t\t(c) Rejection Constraint = %f\n", constraint);
       
    96 		fprintf(stats[TEST_RND_EXCURSION], "\t\t-------------------------------------------\n");
       
    97 
       
    98 		cycleStart = 0;
       
    99 		cycleStop  = cycle[1];
       
   100 		for ( k=0; k<6; k++ )
       
   101 			for ( i=0; i<8; i++ )
       
   102 				nu[k][i] = 0.;
       
   103 		for ( j=1; j<=J; j++ ) {                           /* FOR EACH CYCLE */
       
   104 			for ( i=0; i<8; i++ )
       
   105 				counter[i] = 0;
       
   106 			for ( i=cycleStart; i<cycleStop; i++ ) {
       
   107 				if ( (S_k[i] >= 1 && S_k[i] <= 4) || (S_k[i] >= -4 && S_k[i] <= -1) ) {
       
   108 					if ( S_k[i] < 0 )
       
   109 						b = 4;
       
   110 					else
       
   111 						b = 3;
       
   112 					counter[S_k[i]+b]++;
       
   113 				}
       
   114 			}
       
   115 			cycleStart = cycle[j]+1;
       
   116 			if ( j < J )
       
   117 				cycleStop = cycle[j+1];
       
   118 			
       
   119 			for ( i=0; i<8; i++ ) {
       
   120 				if ( (counter[i] >= 0) && (counter[i] <= 4) )
       
   121 					nu[counter[i]][i]++;
       
   122 				else if ( counter[i] >= 5 )
       
   123 					nu[5][i]++;
       
   124 			}
       
   125 		}
       
   126 		
       
   127 		for ( i=0; i<8; i++ ) {
       
   128 			x = stateX[i];
       
   129 			sum = 0.;
       
   130 			for ( k=0; k<6; k++ )
       
   131 				sum += pow(nu[k][i] - J*pi[(int)fabs(x)][k], 2) / (J*pi[(int)fabs(x)][k]);
       
   132 			p_value = cephes_igamc(2.5, sum/2.0);
       
   133 			
       
   134 			if ( isNegative(p_value) || isGreaterThanOne(p_value) )
       
   135 				fprintf(stats[TEST_RND_EXCURSION], "WARNING:  P_VALUE IS OUT OF RANGE.\n");
       
   136 
       
   137 			fprintf(stats[TEST_RND_EXCURSION], "%s\t\tx = %2d chi^2 = %9.6f p_value = %f\n",
       
   138 					p_value < ALPHA ? "FAILURE" : "SUCCESS", x, sum, p_value);
       
   139 			fprintf(results[TEST_RND_EXCURSION], "%f\n", p_value);
       
   140 		}
       
   141 	} 
       
   142 	fprintf(stats[TEST_RND_EXCURSION], "\n");
       
   143 	free(S_k);
       
   144 	free(cycle);
       
   145 }