annotate unittests/FuzzMutate/ReservoirSamplerTest.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- ReservoirSampler.cpp - Tests for the ReservoirSampler --------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 #include "llvm/FuzzMutate/Random.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 #include "gtest/gtest.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 #include <random>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 TEST(ReservoirSamplerTest, OneItem) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 std::mt19937 Rand;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 auto Sampler = makeSampler(Rand, 7, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 ASSERT_FALSE(Sampler.isEmpty());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 ASSERT_EQ(7, Sampler.getSelection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 TEST(ReservoirSamplerTest, NoWeight) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 std::mt19937 Rand;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 auto Sampler = makeSampler(Rand, 7, 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 ASSERT_TRUE(Sampler.isEmpty());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 TEST(ReservoirSamplerTest, Uniform) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 std::mt19937 Rand;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 // Run three chi-squared tests to check that the distribution is reasonably
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 // uniform.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 std::vector<int> Items = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 int Failures = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 for (int Run = 0; Run < 3; ++Run) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 std::vector<int> Counts(Items.size(), 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 // We need $np_s > 5$ at minimum, but we're better off going a couple of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 // orders of magnitude larger.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 int N = Items.size() * 5 * 100;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 for (int I = 0; I < N; ++I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 auto Sampler = makeSampler(Rand, Items);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 Counts[Sampler.getSelection()] += 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 // Knuth. TAOCP Vol. 2, 3.3.1 (8):
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 // $V = \frac{1}{n} \sum_{s=1}^{k} \left(\frac{Y_s^2}{p_s}\right) - n$
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 double Ps = 1.0 / Items.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 double Sum = 0.0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 for (int Ys : Counts)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 Sum += Ys * Ys / Ps;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 double V = (Sum / N) - N;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 assert(Items.size() == 10 && "Our chi-squared values assume 10 items");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 // Since we have 10 items, there are 9 degrees of freedom and the table of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 // chi-squared values is as follows:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 // | p=1% | 5% | 25% | 50% | 75% | 95% | 99% |
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 // v=9 | 2.088 | 3.325 | 5.899 | 8.343 | 11.39 | 16.92 | 21.67 |
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 // Check that we're in the likely range of results.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 //if (V < 2.088 || V > 21.67)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 if (V < 2.088 || V > 21.67)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 ++Failures;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 EXPECT_LT(Failures, 3) << "Non-uniform distribution?";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 }