|
| 1 | +#include "leetcode/problems/champagne-tower.h" |
| 2 | + |
| 3 | +#include "gtest/gtest.h" |
| 4 | + |
| 5 | +namespace leetcode { |
| 6 | +namespace problem_799 { |
| 7 | + |
| 8 | +class ChampagneTowerTest : public ::testing::TestWithParam<std::string> { |
| 9 | + protected: |
| 10 | + void SetUp() override { solution.setStrategy(GetParam()); } |
| 11 | + |
| 12 | + ChampagneTowerSolution solution; |
| 13 | +}; |
| 14 | + |
| 15 | +TEST_P(ChampagneTowerTest, Example1) { |
| 16 | + // poured = 1, query_row = 1, query_glass = 1 |
| 17 | + // 倒1杯到顶层,顶层满了,没有溢出,所以第1行第1个杯子是空的 |
| 18 | + int poured = 1; |
| 19 | + int query_row = 1; |
| 20 | + int query_glass = 1; |
| 21 | + double expected = 0.0; |
| 22 | + EXPECT_DOUBLE_EQ(expected, solution.champagneTower(poured, query_row, query_glass)); |
| 23 | +} |
| 24 | + |
| 25 | +TEST_P(ChampagneTowerTest, Example2) { |
| 26 | + // poured = 2, query_row = 1, query_glass = 1 |
| 27 | + // 倒2杯到顶层,顶层满1杯,溢出1杯,平均分给下一行两个杯子,各0.5杯 |
| 28 | + int poured = 2; |
| 29 | + int query_row = 1; |
| 30 | + int query_glass = 1; |
| 31 | + double expected = 0.5; |
| 32 | + EXPECT_DOUBLE_EQ(expected, solution.champagneTower(poured, query_row, query_glass)); |
| 33 | +} |
| 34 | + |
| 35 | +TEST_P(ChampagneTowerTest, Example3) { |
| 36 | + // poured = 100000009, query_row = 33, query_glass = 17 |
| 37 | + // 大量香槟,中间位置会满 |
| 38 | + int poured = 100000009; |
| 39 | + int query_row = 33; |
| 40 | + int query_glass = 17; |
| 41 | + double expected = 1.0; |
| 42 | + EXPECT_DOUBLE_EQ(expected, solution.champagneTower(poured, query_row, query_glass)); |
| 43 | +} |
| 44 | + |
| 45 | +TEST_P(ChampagneTowerTest, ZeroPoured) { |
| 46 | + // 没有倒香槟,所有杯子都是空的 |
| 47 | + int poured = 0; |
| 48 | + int query_row = 0; |
| 49 | + int query_glass = 0; |
| 50 | + double expected = 0.0; |
| 51 | + EXPECT_DOUBLE_EQ(expected, solution.champagneTower(poured, query_row, query_glass)); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_P(ChampagneTowerTest, TopGlassFull) { |
| 55 | + // 倒1杯,顶层满了 |
| 56 | + int poured = 1; |
| 57 | + int query_row = 0; |
| 58 | + int query_glass = 0; |
| 59 | + double expected = 1.0; |
| 60 | + EXPECT_DOUBLE_EQ(expected, solution.champagneTower(poured, query_row, query_glass)); |
| 61 | +} |
| 62 | + |
| 63 | +TEST_P(ChampagneTowerTest, SecondRowBothHalf) { |
| 64 | + // 倒2杯,第1行两个杯子各0.5 |
| 65 | + int poured = 2; |
| 66 | + int query_row = 1; |
| 67 | + int query_glass = 0; |
| 68 | + double expected = 0.5; |
| 69 | + EXPECT_DOUBLE_EQ(expected, solution.champagneTower(poured, query_row, query_glass)); |
| 70 | +} |
| 71 | + |
| 72 | +INSTANTIATE_TEST_SUITE_P( |
| 73 | + LeetCode, ChampagneTowerTest, |
| 74 | + ::testing::ValuesIn(ChampagneTowerSolution().getStrategyNames())); |
| 75 | + |
| 76 | +} // namespace problem_799 |
| 77 | +} // namespace leetcode |
0 commit comments