xref: /llvm-project/llvm/unittests/FuzzMutate/ReservoirSamplerTest.cpp (revision db2aa9f2d8885e8e3b62add4e88b5857703a8fd2)
17d449d31SJustin Bogner //===- ReservoirSampler.cpp - Tests for the ReservoirSampler --------------===//
27d449d31SJustin Bogner //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67d449d31SJustin Bogner //
77d449d31SJustin Bogner //===----------------------------------------------------------------------===//
87d449d31SJustin Bogner 
97d449d31SJustin Bogner #include "llvm/FuzzMutate/Random.h"
107d449d31SJustin Bogner #include "gtest/gtest.h"
117d449d31SJustin Bogner #include <random>
127d449d31SJustin Bogner 
137d449d31SJustin Bogner using namespace llvm;
147d449d31SJustin Bogner 
TEST(ReservoirSamplerTest,OneItem)157d449d31SJustin Bogner TEST(ReservoirSamplerTest, OneItem) {
167d449d31SJustin Bogner   std::mt19937 Rand;
177d449d31SJustin Bogner   auto Sampler = makeSampler(Rand, 7, 1);
187d449d31SJustin Bogner   ASSERT_FALSE(Sampler.isEmpty());
197d449d31SJustin Bogner   ASSERT_EQ(7, Sampler.getSelection());
207d449d31SJustin Bogner }
217d449d31SJustin Bogner 
TEST(ReservoirSamplerTest,NoWeight)227d449d31SJustin Bogner TEST(ReservoirSamplerTest, NoWeight) {
237d449d31SJustin Bogner   std::mt19937 Rand;
247d449d31SJustin Bogner   auto Sampler = makeSampler(Rand, 7, 0);
257d449d31SJustin Bogner   ASSERT_TRUE(Sampler.isEmpty());
267d449d31SJustin Bogner }
277d449d31SJustin Bogner 
TEST(ReservoirSamplerTest,Uniform)287d449d31SJustin Bogner TEST(ReservoirSamplerTest, Uniform) {
297d449d31SJustin Bogner   std::mt19937 Rand;
307d449d31SJustin Bogner 
317d449d31SJustin Bogner   // Run three chi-squared tests to check that the distribution is reasonably
327d449d31SJustin Bogner   // uniform.
337d449d31SJustin Bogner   std::vector<int> Items = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
347d449d31SJustin Bogner 
357d449d31SJustin Bogner   int Failures = 0;
367d449d31SJustin Bogner   for (int Run = 0; Run < 3; ++Run) {
377d449d31SJustin Bogner     std::vector<int> Counts(Items.size(), 0);
387d449d31SJustin Bogner 
397d449d31SJustin Bogner     // We need $np_s > 5$ at minimum, but we're better off going a couple of
407d449d31SJustin Bogner     // orders of magnitude larger.
417d449d31SJustin Bogner     int N = Items.size() * 5 * 100;
427d449d31SJustin Bogner     for (int I = 0; I < N; ++I) {
437d449d31SJustin Bogner       auto Sampler = makeSampler(Rand, Items);
447d449d31SJustin Bogner       Counts[Sampler.getSelection()] += 1;
457d449d31SJustin Bogner     }
467d449d31SJustin Bogner 
477d449d31SJustin Bogner     // Knuth. TAOCP Vol. 2, 3.3.1 (8):
487d449d31SJustin Bogner     // $V = \frac{1}{n} \sum_{s=1}^{k} \left(\frac{Y_s^2}{p_s}\right) - n$
497d449d31SJustin Bogner     double Ps = 1.0 / Items.size();
507d449d31SJustin Bogner     double Sum = 0.0;
517d449d31SJustin Bogner     for (int Ys : Counts)
527d449d31SJustin Bogner       Sum += Ys * Ys / Ps;
537d449d31SJustin Bogner     double V = (Sum / N) - N;
547d449d31SJustin Bogner 
557d449d31SJustin Bogner     assert(Items.size() == 10 && "Our chi-squared values assume 10 items");
567d449d31SJustin Bogner     // Since we have 10 items, there are 9 degrees of freedom and the table of
577d449d31SJustin Bogner     // chi-squared values is as follows:
587d449d31SJustin Bogner     //
597d449d31SJustin Bogner     //     | p=1%  |   5%  |  25%  |  50%  |  75%  |  95%  |  99%  |
607d449d31SJustin Bogner     // v=9 | 2.088 | 3.325 | 5.899 | 8.343 | 11.39 | 16.92 | 21.67 |
617d449d31SJustin Bogner     //
627d449d31SJustin Bogner     // Check that we're in the likely range of results.
637d449d31SJustin Bogner     // if (V < 2.088 || V > 21.67)
647d449d31SJustin Bogner     if (V < 2.088 || V > 21.67)
657d449d31SJustin Bogner       ++Failures;
667d449d31SJustin Bogner   }
677d449d31SJustin Bogner   EXPECT_LT(Failures, 3) << "Non-uniform distribution?";
687d449d31SJustin Bogner }
69