|
| 1 | +#include "leetcode/problems/find-kth-bit-in-nth-binary-string.h" |
| 2 | + |
| 3 | +#include "gtest/gtest.h" |
| 4 | + |
| 5 | +namespace leetcode { |
| 6 | +namespace problem_1545 { |
| 7 | + |
| 8 | +class FindKthBitInNthBinaryStringTest : public ::testing::TestWithParam<std::string> { |
| 9 | + protected: |
| 10 | + void SetUp() override { solution.setStrategy(GetParam()); } |
| 11 | + |
| 12 | + FindKthBitInNthBinaryStringSolution solution; |
| 13 | +}; |
| 14 | + |
| 15 | +TEST_P(FindKthBitInNthBinaryStringTest, Example1) { |
| 16 | + // n = 3, k = 1, S₃ = "0111001", 第1位是 "0" |
| 17 | + EXPECT_EQ('0', solution.findKthBit(3, 1)); |
| 18 | +} |
| 19 | + |
| 20 | +TEST_P(FindKthBitInNthBinaryStringTest, Example2) { |
| 21 | + // n = 4, k = 11, S₄ = "011100110110001", 第11位是 "1" |
| 22 | + EXPECT_EQ('1', solution.findKthBit(4, 11)); |
| 23 | +} |
| 24 | + |
| 25 | +TEST_P(FindKthBitInNthBinaryStringTest, MiddlePosition) { |
| 26 | + // 测试中间位置,应该是 "1" |
| 27 | + // S₂ = "011", 中间位置 k = 2 |
| 28 | + EXPECT_EQ('1', solution.findKthBit(2, 2)); |
| 29 | + // S₃ = "0111001", 中间位置 k = 4 |
| 30 | + EXPECT_EQ('1', solution.findKthBit(3, 4)); |
| 31 | +} |
| 32 | + |
| 33 | +TEST_P(FindKthBitInNthBinaryStringTest, LastPosition) { |
| 34 | + // 测试最后一个位置 |
| 35 | + // S₂ = "011", 最后一位 k = 3 是 "1" |
| 36 | + EXPECT_EQ('1', solution.findKthBit(2, 3)); |
| 37 | + // S₃ = "0111001", 最后一位 k = 7 是 "1" |
| 38 | + EXPECT_EQ('1', solution.findKthBit(3, 7)); |
| 39 | +} |
| 40 | + |
| 41 | +TEST_P(FindKthBitInNthBinaryStringTest, FirstPosition) { |
| 42 | + // S₁ = "0", 第一位 k = 1 是 "0" |
| 43 | + EXPECT_EQ('0', solution.findKthBit(1, 1)); |
| 44 | + // Sₙ 的第一位总是 "0" |
| 45 | + EXPECT_EQ('0', solution.findKthBit(5, 1)); |
| 46 | + EXPECT_EQ('0', solution.findKthBit(10, 1)); |
| 47 | +} |
| 48 | + |
| 49 | +TEST_P(FindKthBitInNthBinaryStringTest, SmallCases) { |
| 50 | + // S₂ = "011" |
| 51 | + EXPECT_EQ('0', solution.findKthBit(2, 1)); |
| 52 | + EXPECT_EQ('1', solution.findKthBit(2, 2)); |
| 53 | + EXPECT_EQ('1', solution.findKthBit(2, 3)); |
| 54 | + |
| 55 | + // S₃ = "0111001" |
| 56 | + EXPECT_EQ('0', solution.findKthBit(3, 1)); |
| 57 | + EXPECT_EQ('1', solution.findKthBit(3, 2)); |
| 58 | + EXPECT_EQ('1', solution.findKthBit(3, 3)); |
| 59 | + EXPECT_EQ('1', solution.findKthBit(3, 4)); |
| 60 | + EXPECT_EQ('0', solution.findKthBit(3, 5)); |
| 61 | + EXPECT_EQ('0', solution.findKthBit(3, 6)); |
| 62 | + EXPECT_EQ('1', solution.findKthBit(3, 7)); |
| 63 | +} |
| 64 | + |
| 65 | +INSTANTIATE_TEST_SUITE_P( |
| 66 | + LeetCode, FindKthBitInNthBinaryStringTest, |
| 67 | + ::testing::ValuesIn(FindKthBitInNthBinaryStringSolution().getStrategyNames())); |
| 68 | + |
| 69 | +} // namespace problem_1545 |
| 70 | +} // namespace leetcode |
0 commit comments