Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 4 | * |
| 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 | |
| 29 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 30 | namespace tests { |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 31 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 32 | BOOST_AUTO_TEST_SUITE(UtilRandom) |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 33 | |
| 34 | class PseudoRandomWord32 |
| 35 | { |
| 36 | public: |
| 37 | static uint32_t |
| 38 | generate() |
| 39 | { |
| 40 | return random::generateWord32(); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | class PseudoRandomWord64 |
| 45 | { |
| 46 | public: |
| 47 | static uint64_t |
| 48 | generate() |
| 49 | { |
| 50 | return random::generateWord64(); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | class SecureRandomWord32 |
| 55 | { |
| 56 | public: |
| 57 | static uint32_t |
| 58 | generate() |
| 59 | { |
| 60 | return random::generateSecureWord32(); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class SecureRandomWord64 |
| 65 | { |
| 66 | public: |
| 67 | static uint64_t |
| 68 | generate() |
| 69 | { |
| 70 | return random::generateSecureWord64(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | typedef boost::mpl::vector<PseudoRandomWord32, |
| 75 | PseudoRandomWord64, |
| 76 | SecureRandomWord32, |
| 77 | SecureRandomWord64> RandomGenerators; |
| 78 | |
| 79 | BOOST_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 | |
| 110 | BOOST_AUTO_TEST_SUITE_END() |
| 111 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 112 | } // namespace tests |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 113 | } // namespace ndn |