blob: d56d84be1ad7ff31cd7528f005f3fbcdb01c352d [file] [log] [blame]
Alexander Afanasyev75088672014-07-14 11:58:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyev75088672014-07-14 11:58:30 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/random.hpp"
23
24#include "boost-test.hpp"
25#include <boost/mpl/vector.hpp>
26
27#include <cmath>
28
29namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080030namespace tests {
Alexander Afanasyev75088672014-07-14 11:58:30 -070031
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032BOOST_AUTO_TEST_SUITE(UtilRandom)
Alexander Afanasyev75088672014-07-14 11:58:30 -070033
34class PseudoRandomWord32
35{
36public:
37 static uint32_t
38 generate()
39 {
40 return random::generateWord32();
41 }
42};
43
44class PseudoRandomWord64
45{
46public:
47 static uint64_t
48 generate()
49 {
50 return random::generateWord64();
51 }
52};
53
54class SecureRandomWord32
55{
56public:
57 static uint32_t
58 generate()
59 {
60 return random::generateSecureWord32();
61 }
62};
63
64class SecureRandomWord64
65{
66public:
67 static uint64_t
68 generate()
69 {
70 return random::generateSecureWord64();
71 }
72};
73
74typedef boost::mpl::vector<PseudoRandomWord32,
75 PseudoRandomWord64,
76 SecureRandomWord32,
77 SecureRandomWord64> RandomGenerators;
78
79BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators)
80{
81 // Kolmogorov-Smirnov Goodness-of-Fit Test
82 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
83
84 const size_t MAX_BINS = 32;
85 const uint32_t MAX_ITERATIONS = 35;
86
87 std::vector<uint32_t> counts(MAX_BINS, 0);
88
89 for (uint32_t i = 0; i < MAX_ITERATIONS; i++) {
90 counts[RandomGenerator::generate() % MAX_BINS]++;
91 }
92
93 std::vector<double> edf(MAX_BINS, 0.0);
94 double probability = 0.0;
95 for (size_t i = 0; i < MAX_BINS; i++) {
96 probability += 1.0 * counts[i] / MAX_ITERATIONS;
97 edf[i] = probability;
98 }
99
100 double t = 0.0;
101 for (size_t i = 0; i < MAX_BINS; i++) {
102 t = std::max(t, std::abs(edf[i] - (i * 1.0 / MAX_BINS)));
103 }
104
105 // Check if it is uniform distribution with confidence 0.95
106 // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7
107 BOOST_WARN_LE(t, 0.230);
108}
109
110BOOST_AUTO_TEST_SUITE_END()
111
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800112} // namespace tests
Alexander Afanasyev75088672014-07-14 11:58:30 -0700113} // namespace ndn