do a head vs tail test on the random buffer.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@23046 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
		| @@ -55,10 +55,13 @@ static int version_flag; | |||||||
| static int help_flag; | static int help_flag; | ||||||
| static int len = 1024 * 1024; | static int len = 1024 * 1024; | ||||||
| static char *rand_method; | static char *rand_method; | ||||||
|  | static char *filename; | ||||||
|  |  | ||||||
| static struct getargs args[] = { | static struct getargs args[] = { | ||||||
|     { "length",	0,	arg_integer,	&len, |     { "length",	0,	arg_integer,	&len, | ||||||
|       "length", NULL }, |       "length", NULL }, | ||||||
|  |     { "file",	0,	arg_string,	&filename, | ||||||
|  |       "file name", NULL }, | ||||||
|     { "method",	0,	arg_string,	&rand_method, |     { "method",	0,	arg_string,	&rand_method, | ||||||
|       "method", NULL }, |       "method", NULL }, | ||||||
|     { "version",	0,	arg_flag,	&version_flag, |     { "version",	0,	arg_flag,	&version_flag, | ||||||
| @@ -81,7 +84,7 @@ usage (int ret) | |||||||
|     arg_printusage (args, |     arg_printusage (args, | ||||||
| 		    sizeof(args)/sizeof(args[0]), | 		    sizeof(args)/sizeof(args[0]), | ||||||
| 		    NULL, | 		    NULL, | ||||||
| 		    "out-random-file"); | 		    ""); | ||||||
|     exit (ret); |     exit (ret); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -108,7 +111,7 @@ main(int argc, char **argv) | |||||||
|     argc -= idx; |     argc -= idx; | ||||||
|     argv += idx; |     argv += idx; | ||||||
|  |  | ||||||
|     if (argc < 1) |     if (argc != 0) | ||||||
| 	usage(1); | 	usage(1); | ||||||
|  |  | ||||||
|     buffer = emalloc(len); |     buffer = emalloc(len); | ||||||
| @@ -133,14 +136,46 @@ main(int argc, char **argv) | |||||||
|     if (RAND_bytes(buffer, len) != 1) |     if (RAND_bytes(buffer, len) != 1) | ||||||
| 	errx(1, "RAND_bytes"); | 	errx(1, "RAND_bytes"); | ||||||
|  |  | ||||||
|     rk_dumpdata(argv[0], buffer, len); |     if (filename) | ||||||
|  | 	rk_dumpdata(filename, buffer, len); | ||||||
|  |  | ||||||
|  |     /* head vs tail */ | ||||||
|  |     if (len >= 100000) { | ||||||
|  | 	int bit, i; | ||||||
|  | 	double res; | ||||||
|  | 	int bits[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | ||||||
|  |  | ||||||
|  | 	for (i = 0; i < len; i++) { | ||||||
|  | 	    unsigned char c = ((unsigned char *)buffer)[i]; | ||||||
|  | 	    for (bit = 0; bit < 8 && c; bit++) { | ||||||
|  | 		if (c & 1) | ||||||
|  | 		    bits[bit]++; | ||||||
|  | 		c = c >> 1; | ||||||
|  | 	    } | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	for (bit = 0; bit < 8; bit++) { | ||||||
|  |  | ||||||
|  | 	    res = ((double)abs(len - bits[bit] * 2)) / (double)len; | ||||||
|  | 	    if (res > 0.005) | ||||||
|  | 		errx(1, "head%d vs tail%d > 0.5%%%% %lf == %d vs %d",  | ||||||
|  | 		     bit, bit, res, len, bits[bit]); | ||||||
|  |  | ||||||
|  | 	    printf("head vs tails bit%d: %lf\n", bit, res); | ||||||
|  | 	} | ||||||
|  |     } | ||||||
|  |  | ||||||
|     free(buffer); |     free(buffer); | ||||||
|  |  | ||||||
|     if (RAND_write_file("test.file") != 1) |     /* test write random file */ | ||||||
| 	errx(1, "RAND_write_file"); |     { | ||||||
|     if (RAND_load_file("test.file", 1024) != 1) | 	static const char *file = "test.file"; | ||||||
| 	errx(1, "RAND_load_file"); | 	if (RAND_write_file(file) != 1) | ||||||
|  | 	    errx(1, "RAND_write_file"); | ||||||
|  | 	if (RAND_load_file(file, 1024) != 1) | ||||||
|  | 	    errx(1, "RAND_load_file"); | ||||||
|  | 	unlink(file); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Love Hörnquist Åstrand
					Love Hörnquist Åstrand