kerneltest/e32utils/nistsecurerng/src/cusum.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 		    C U M U L A T I V E  S U M S  T E S T
       
    33  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    34 
       
    35 void
       
    36 CumulativeSums(int n)
       
    37 {
       
    38 	int	   S, sup, inf;
       
    39 	int    z = 0;
       
    40 	int    zrev = 0;
       
    41 	int    k;
       
    42 	double	sum1, sum2, p_value;
       
    43 
       
    44 	S = 0;
       
    45 	sup = 0;
       
    46 	inf = 0;
       
    47 	for ( k=0; k<n; k++ ) {
       
    48 		epsilon[k] ? S++ : S--;
       
    49 		if ( S > sup )
       
    50 			sup++;
       
    51 		if ( S < inf )
       
    52 			inf--;
       
    53 		z = (sup > -inf) ? sup : -inf;
       
    54 		zrev = (sup-S > S-inf) ? sup-S : S-inf;
       
    55 	}
       
    56 	
       
    57 	// forward
       
    58 	sum1 = 0.0;
       
    59 	for ( k=(-n/z+1)/4; k<=(n/z-1)/4; k++ ) {
       
    60 		sum1 += cephes_normal(((4*k+1)*z)/sqrt(n));
       
    61 		sum1 -= cephes_normal(((4*k-1)*z)/sqrt(n));
       
    62 	}
       
    63 	sum2 = 0.0;
       
    64 	for ( k=(-n/z-3)/4; k<=(n/z-1)/4; k++ ) {
       
    65 		sum2 += cephes_normal(((4*k+3)*z)/sqrt(n));
       
    66 		sum2 -= cephes_normal(((4*k+1)*z)/sqrt(n));
       
    67 	}
       
    68 
       
    69 	p_value = 1.0 - sum1 + sum2;
       
    70 	
       
    71 	fprintf(stats[TEST_CUSUM], "\t\t      CUMULATIVE SUMS (FORWARD) TEST\n");
       
    72 	fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n");
       
    73 	fprintf(stats[TEST_CUSUM], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
    74 	fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n");
       
    75 	fprintf(stats[TEST_CUSUM], "\t\t(a) The maximum partial sum = %d\n", z);
       
    76 	fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n");
       
    77 
       
    78 	if ( isNegative(p_value) || isGreaterThanOne(p_value) )
       
    79 		fprintf(stats[TEST_CUSUM], "\t\tWARNING:  P_VALUE IS OUT OF RANGE\n");
       
    80 
       
    81 	fprintf(stats[TEST_CUSUM], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value);
       
    82 	fprintf(results[TEST_CUSUM], "%f\n", p_value);
       
    83 		
       
    84 	// backwards
       
    85 	sum1 = 0.0;
       
    86 	for ( k=(-n/zrev+1)/4; k<=(n/zrev-1)/4; k++ ) {
       
    87 		sum1 += cephes_normal(((4*k+1)*zrev)/sqrt(n));
       
    88 		sum1 -= cephes_normal(((4*k-1)*zrev)/sqrt(n));
       
    89 	}
       
    90 	sum2 = 0.0;
       
    91 	for ( k=(-n/zrev-3)/4; k<=(n/zrev-1)/4; k++ ) {
       
    92 		sum2 += cephes_normal(((4*k+3)*zrev)/sqrt(n));
       
    93 		sum2 -= cephes_normal(((4*k+1)*zrev)/sqrt(n));
       
    94 	}
       
    95 	p_value = 1.0 - sum1 + sum2;
       
    96 
       
    97 	fprintf(stats[TEST_CUSUM], "\t\t      CUMULATIVE SUMS (REVERSE) TEST\n");
       
    98 	fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n");
       
    99 	fprintf(stats[TEST_CUSUM], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
   100 	fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n");
       
   101 	fprintf(stats[TEST_CUSUM], "\t\t(a) The maximum partial sum = %d\n", zrev);
       
   102 	fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n");
       
   103 
       
   104 	if ( isNegative(p_value) || isGreaterThanOne(p_value) )
       
   105 		fprintf(stats[TEST_CUSUM], "\t\tWARNING:  P_VALUE IS OUT OF RANGE\n");
       
   106 
       
   107 	fprintf(stats[TEST_CUSUM], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value);
       
   108 	fprintf(results[TEST_CUSUM], "%f\n", p_value);
       
   109 }