kerneltest/e32utils/nistsecurerng/src/longestRunOfOnes.cpp
changeset 152 657f875b013e
equal deleted inserted replaced
139:95f71bcdcdb7 152:657f875b013e
       
     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 /* got rid of unused 'k' */
       
    28 
       
    29 #include "openc.h"
       
    30 #include "../include/externs.h"
       
    31 #include "../include/cephes.h"  
       
    32 
       
    33 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    34                       L O N G E S T  R U N S  T E S T
       
    35  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    36 
       
    37 void
       
    38 LongestRunOfOnes(int n)
       
    39 {
       
    40 	double			pval, chi2, pi[7];
       
    41 	int				run, v_n_obs, N, i, j, K, M, V[7];
       
    42 	unsigned int	nu[7] = { 0, 0, 0, 0, 0, 0, 0 };
       
    43 
       
    44 	if ( n < 128 ) {
       
    45 		fprintf(stats[TEST_LONGEST_RUN], "\t\t\t  LONGEST RUNS OF ONES TEST\n");
       
    46 		fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n");
       
    47 		fprintf(stats[TEST_LONGEST_RUN], "\t\t   n=%d is too short\n", n);
       
    48 		return;
       
    49 	}
       
    50 	if ( n < 6272 ) {
       
    51 		K = 3;
       
    52 		M = 8;
       
    53 		V[0] = 1; V[1] = 2; V[2] = 3; V[3] = 4;
       
    54 		pi[0] = 0.21484375;
       
    55 		pi[1] = 0.3671875;
       
    56 		pi[2] = 0.23046875;
       
    57 		pi[3] = 0.1875;
       
    58 	}
       
    59 	else if ( n < 750000 ) {
       
    60 		K = 5;
       
    61 		M = 128;
       
    62 		V[0] = 4; V[1] = 5; V[2] = 6; V[3] = 7; V[4] = 8; V[5] = 9;
       
    63 		pi[0] = 0.1174035788;
       
    64 		pi[1] = 0.242955959;
       
    65 		pi[2] = 0.249363483;
       
    66 		pi[3] = 0.17517706;
       
    67 		pi[4] = 0.102701071;
       
    68 		pi[5] = 0.112398847;
       
    69 	}
       
    70 	else {
       
    71 		K = 6;
       
    72 		M = 10000;
       
    73 			V[0] = 10; V[1] = 11; V[2] = 12; V[3] = 13; V[4] = 14; V[5] = 15; V[6] = 16;
       
    74 		pi[0] = 0.0882;
       
    75 		pi[1] = 0.2092;
       
    76 		pi[2] = 0.2483;
       
    77 		pi[3] = 0.1933;
       
    78 		pi[4] = 0.1208;
       
    79 		pi[5] = 0.0675;
       
    80 		pi[6] = 0.0727;
       
    81 	}
       
    82 	
       
    83 	N = n/M;
       
    84 	for ( i=0; i<N; i++ ) {
       
    85 		v_n_obs = 0;
       
    86 		run = 0;
       
    87 		for ( j=0; j<M; j++ ) {
       
    88 			if ( epsilon[i*M+j] == 1 ) {
       
    89 				run++;
       
    90 				if ( run > v_n_obs )
       
    91 					v_n_obs = run;
       
    92 			}
       
    93 			else
       
    94 				run = 0;
       
    95 		}
       
    96 		if ( v_n_obs < V[0] )
       
    97 			nu[0]++;
       
    98 		for ( j=0; j<=K; j++ ) {
       
    99 			if ( v_n_obs == V[j] )
       
   100 				nu[j]++;
       
   101 		}
       
   102 		if ( v_n_obs > V[K] )
       
   103 			nu[K]++;
       
   104 	}
       
   105 
       
   106 	chi2 = 0.0;
       
   107 	for ( i=0; i<=K; i++ )
       
   108 		chi2 += ((nu[i] - N * pi[i]) * (nu[i] - N * pi[i])) / (N * pi[i]);
       
   109 
       
   110 	pval = cephes_igamc((double)(K/2.0), chi2 / 2.0);
       
   111 
       
   112 	fprintf(stats[TEST_LONGEST_RUN], "\t\t\t  LONGEST RUNS OF ONES TEST\n");
       
   113 	fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n");
       
   114 	fprintf(stats[TEST_LONGEST_RUN], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
   115 	fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n");
       
   116 	fprintf(stats[TEST_LONGEST_RUN], "\t\t(a) N (# of substrings)  = %d\n", N);
       
   117 	fprintf(stats[TEST_LONGEST_RUN], "\t\t(b) M (Substring Length) = %d\n", M);
       
   118 	fprintf(stats[TEST_LONGEST_RUN], "\t\t(c) Chi^2                = %f\n", chi2);
       
   119 	fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n");
       
   120 	fprintf(stats[TEST_LONGEST_RUN], "\t\t      F R E Q U E N C Y\n");
       
   121 	fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n");
       
   122 
       
   123 	if ( K == 3 ) {
       
   124 		fprintf(stats[TEST_LONGEST_RUN], "\t\t  <=1     2     3    >=4   P-value  Assignment");
       
   125 		fprintf(stats[TEST_LONGEST_RUN], "\n\t\t %3d %3d %3d  %3d ", nu[0], nu[1], nu[2], nu[3]);
       
   126 	}
       
   127 	else if ( K == 5 ) {
       
   128 		fprintf(stats[TEST_LONGEST_RUN], "\t\t<=4  5  6  7  8  >=9 P-value  Assignment");
       
   129 		fprintf(stats[TEST_LONGEST_RUN], "\n\t\t %3d %3d %3d %3d %3d  %3d ", nu[0], nu[1], nu[2],
       
   130 				nu[3], nu[4], nu[5]);
       
   131 	}
       
   132 	else {
       
   133 		fprintf(stats[TEST_LONGEST_RUN],"\t\t<=10  11  12  13  14  15 >=16 P-value  Assignment");
       
   134 		fprintf(stats[TEST_LONGEST_RUN],"\n\t\t %3d %3d %3d %3d %3d %3d  %3d ", nu[0], nu[1], nu[2],
       
   135 				nu[3], nu[4], nu[5], nu[6]);
       
   136 	}
       
   137 	if ( isNegative(pval) || isGreaterThanOne(pval) )
       
   138 		fprintf(stats[TEST_LONGEST_RUN], "WARNING:  P_VALUE IS OUT OF RANGE.\n");
       
   139 
       
   140 	fprintf(stats[TEST_LONGEST_RUN], "%s\t\tp_value = %f\n\n", pval < ALPHA ? "FAILURE" : "SUCCESS", pval);
       
   141 	fprintf(results[TEST_LONGEST_RUN], "%f\n", pval);
       
   142 }