Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 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 { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(UtilTestRandom) |
| 32 | |
| 33 | class PseudoRandomWord32 |
| 34 | { |
| 35 | public: |
| 36 | static uint32_t |
| 37 | generate() |
| 38 | { |
| 39 | return random::generateWord32(); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | class PseudoRandomWord64 |
| 44 | { |
| 45 | public: |
| 46 | static uint64_t |
| 47 | generate() |
| 48 | { |
| 49 | return random::generateWord64(); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | class SecureRandomWord32 |
| 54 | { |
| 55 | public: |
| 56 | static uint32_t |
| 57 | generate() |
| 58 | { |
| 59 | return random::generateSecureWord32(); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | class SecureRandomWord64 |
| 64 | { |
| 65 | public: |
| 66 | static uint64_t |
| 67 | generate() |
| 68 | { |
| 69 | return random::generateSecureWord64(); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | typedef boost::mpl::vector<PseudoRandomWord32, |
| 74 | PseudoRandomWord64, |
| 75 | SecureRandomWord32, |
| 76 | SecureRandomWord64> RandomGenerators; |
| 77 | |
| 78 | BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators) |
| 79 | { |
| 80 | // Kolmogorov-Smirnov Goodness-of-Fit Test |
| 81 | // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm |
| 82 | |
| 83 | const size_t MAX_BINS = 32; |
| 84 | const uint32_t MAX_ITERATIONS = 35; |
| 85 | |
| 86 | std::vector<uint32_t> counts(MAX_BINS, 0); |
| 87 | |
| 88 | for (uint32_t i = 0; i < MAX_ITERATIONS; i++) { |
| 89 | counts[RandomGenerator::generate() % MAX_BINS]++; |
| 90 | } |
| 91 | |
| 92 | std::vector<double> edf(MAX_BINS, 0.0); |
| 93 | double probability = 0.0; |
| 94 | for (size_t i = 0; i < MAX_BINS; i++) { |
| 95 | probability += 1.0 * counts[i] / MAX_ITERATIONS; |
| 96 | edf[i] = probability; |
| 97 | } |
| 98 | |
| 99 | double t = 0.0; |
| 100 | for (size_t i = 0; i < MAX_BINS; i++) { |
| 101 | t = std::max(t, std::abs(edf[i] - (i * 1.0 / MAX_BINS))); |
| 102 | } |
| 103 | |
| 104 | // Check if it is uniform distribution with confidence 0.95 |
| 105 | // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7 |
| 106 | BOOST_WARN_LE(t, 0.230); |
| 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_SUITE_END() |
| 110 | |
| 111 | } // namespace ndn |