annotate unittests/FuzzMutate/ReservoirSamplerTest.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
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 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 //
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 #include "llvm/FuzzMutate/Random.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 #include "gtest/gtest.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 #include <random>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 TEST(ReservoirSamplerTest, OneItem) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 std::mt19937 Rand;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 auto Sampler = makeSampler(Rand, 7, 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 ASSERT_FALSE(Sampler.isEmpty());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 ASSERT_EQ(7, Sampler.getSelection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 TEST(ReservoirSamplerTest, NoWeight) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 std::mt19937 Rand;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 auto Sampler = makeSampler(Rand, 7, 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 ASSERT_TRUE(Sampler.isEmpty());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 TEST(ReservoirSamplerTest, Uniform) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 std::mt19937 Rand;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 // Run three chi-squared tests to check that the distribution is reasonably
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 // uniform.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 std::vector<int> Items = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 int Failures = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 for (int Run = 0; Run < 3; ++Run) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 std::vector<int> Counts(Items.size(), 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 // We need $np_s > 5$ at minimum, but we're better off going a couple of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 // orders of magnitude larger.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 int N = Items.size() * 5 * 100;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 for (int I = 0; I < N; ++I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 auto Sampler = makeSampler(Rand, Items);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 Counts[Sampler.getSelection()] += 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 // Knuth. TAOCP Vol. 2, 3.3.1 (8):
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 // $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
49 double Ps = 1.0 / Items.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 double Sum = 0.0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 for (int Ys : Counts)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 Sum += Ys * Ys / Ps;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 double V = (Sum / N) - N;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 assert(Items.size() == 10 && "Our chi-squared values assume 10 items");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 // Since we have 10 items, there are 9 degrees of freedom and the table of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 // chi-squared values is as follows:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 // | p=1% | 5% | 25% | 50% | 75% | 95% | 99% |
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 // v=9 | 2.088 | 3.325 | 5.899 | 8.343 | 11.39 | 16.92 | 21.67 |
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 // Check that we're in the likely range of results.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 //if (V < 2.088 || V > 21.67)
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 ++Failures;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 EXPECT_LT(Failures, 3) << "Non-uniform distribution?";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 }