#include #include #include #include #include #include #include #include #include #include #include #include /// ////////////////////////////////////////////////////////////////////////////// /// Check that outputs from a UniformRandomBitGenerator are roughly divisible /// between a number of fixed width buckets. /// /// This is not anything close to a strict statistical test. It is more aimed /// at link testing and basic functionality verification within a small /// resource footprint (ie, fast unit testing). template static void test_buckets (cruft::TAP::logger &tap, Args&& ...args) { constexpr unsigned BUCKET_BITS = 8u; constexpr size_t BUCKET_COUNT = 1u << BUCKET_BITS; constexpr unsigned EXPECTED = 1024u; constexpr unsigned ITERATIONS = BUCKET_COUNT * EXPECTED; GeneratorT gen (std::forward (args)...); std::uniform_int_distribution dist (0, BUCKET_COUNT - 1); unsigned buckets[BUCKET_COUNT] = {}; for (unsigned i = 0; i < ITERATIONS; ++i) ++buckets[dist (gen)]; tap.expect ( std::find_if (std::cbegin (buckets), std::cend (buckets), [] (auto v) { return v < EXPECTED * 7 / 8; }) == std::cend (buckets), "bucket counts for {:s}", cruft::introspection::name::full () ); } /////////////////////////////////////////////////////////////////////////////// int main (int,char**) { cruft::TAP::logger tap; test_buckets (tap, 0x1234u); test_buckets (tap, 0x1234u); test_buckets> (tap, 0x1234u); test_buckets> (tap, 0x1234u); test_buckets (tap, 0x1234u); test_buckets (tap, 0x1234u); test_buckets> (tap, 0x1234u); test_buckets (tap); test_buckets (tap); return tap.status (); }