| 34 | static void TestPickAt(int items, const int32* weights); |
| 35 | |
| 36 | TEST(WeightedPicker, Simple) { |
| 37 | PhiloxRandom philox(testing::RandomSeed(), 17); |
| 38 | SimplePhilox rnd(&philox); |
| 39 | |
| 40 | { |
| 41 | VLOG(0) << "======= Zero-length picker"; |
| 42 | WeightedPicker picker(0); |
| 43 | EXPECT_EQ(picker.Pick(&rnd), -1); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | VLOG(0) << "======= Singleton picker"; |
| 48 | WeightedPicker picker(1); |
| 49 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 50 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 51 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 52 | } |
| 53 | |
| 54 | { |
| 55 | VLOG(0) << "======= Grown picker"; |
| 56 | WeightedPicker picker(0); |
| 57 | for (int i = 0; i < 10; i++) { |
| 58 | picker.Append(1); |
| 59 | } |
| 60 | CheckUniform(&rnd, &picker, 100000); |
| 61 | } |
| 62 | |
| 63 | { |
| 64 | VLOG(0) << "======= Grown picker with zero weights"; |
| 65 | WeightedPicker picker(1); |
| 66 | picker.Resize(10); |
| 67 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 68 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 69 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 70 | } |
| 71 | |
| 72 | { |
| 73 | VLOG(0) << "======= Shrink picker and check weights"; |
| 74 | WeightedPicker picker(1); |
| 75 | picker.Resize(10); |
| 76 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 77 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 78 | EXPECT_EQ(picker.Pick(&rnd), 0); |
| 79 | for (int i = 0; i < 10; i++) { |
| 80 | picker.set_weight(i, i); |
| 81 | } |
| 82 | EXPECT_EQ(picker.total_weight(), 45); |
| 83 | picker.Resize(5); |
| 84 | EXPECT_EQ(picker.total_weight(), 10); |
| 85 | picker.Resize(2); |
| 86 | EXPECT_EQ(picker.total_weight(), 1); |
| 87 | picker.Resize(1); |
| 88 | EXPECT_EQ(picker.total_weight(), 0); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | TEST(WeightedPicker, BigWeights) { |
| 93 | PhiloxRandom philox(testing::RandomSeed() + 1, 17); |
nothing calls this directly
no test coverage detected