]> git.proxmox.com Git - mirror_ovs.git/blame - tests/test-random.c
random: Add entropy test.
[mirror_ovs.git] / tests / test-random.c
CommitLineData
78f3f316
BP
1/*
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18
19#include "random.h"
20
21#include <stdio.h>
22#include <string.h>
23
24int
25main(void)
26{
27 enum { N_ROUNDS = 10000 };
28 unsigned long long int total;
29 int hist16[8][16];
30 int hist2[32];
31 int i;
32
33 random_set_seed(1);
34
35 total = 0;
36 memset(hist2, 0, sizeof hist2);
37 memset(hist16, 0, sizeof hist16);
38 for (i = 0; i < N_ROUNDS; i++) {
39 uint32_t x;
40 int j;
41
42 x = random_uint32();
43
44 total += x;
45
46 for (j = 0; j < 32; j++) {
47 if (x & (1u << j)) {
48 hist2[j]++;
49 }
50 }
51
52 for (j = 0; j < 8; j++) {
53 hist16[j][(x >> (j * 4)) & 15]++;
54 }
55 }
56
57 printf("average=%08llx\n", total / N_ROUNDS);
58
59 printf("\nbit 0 1\n");
60 for (i = 0; i < 32; i++) {
61 printf("%3d %5d %5d\n", i, N_ROUNDS - hist2[i], hist2[i]);
62 }
63 printf("(expected values are %d)\n", N_ROUNDS / 2);
64
65 printf("\nnibble 0 1 2 3 4 5 6 7 8 9 10 11 12 "
66 "13 14 15\n");
67 for (i = 0; i < 8; i++) {
68 int j;
69
70 printf("%6d", i);
71 for (j = 0; j < 16; j++) {
72 printf(" %3d", hist16[i][j]);
73 }
74 printf("\n");
75 }
76 printf("(expected values are %d)\n", N_ROUNDS / 16);
77
78 return 0;
79}