Files
leetcode/tests/test_1290.cpp
2025-07-14 19:23:17 +08:00

48 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <gtest/gtest.h>
#include <solution/1290.h>
class GetDecimalValueTest : public ::testing::Test
{
protected:
// 构建链表vector -> ListNode*
ListNode *buildList(const std::vector<int> &values)
{
ListNode dummy{0, nullptr};
ListNode *current = &dummy;
for (int v : values)
{
current->next = new ListNode{v, nullptr};
current = current->next;
}
return dummy.next;
}
// 释放链表
void freeList(ListNode *head)
{
while (head)
{
ListNode *tmp = head;
head = head->next;
delete tmp;
}
}
};
// 示例 1: [1, 0, 1] → 5
TEST_F(GetDecimalValueTest, Binary101)
{
ListNode *head = buildList({1, 0, 1});
int expected = 5;
EXPECT_EQ(getDecimalValue(head), expected);
freeList(head);
}
// 示例 2: [0] → 0
TEST_F(GetDecimalValueTest, Binary0)
{
ListNode *head = buildList({0});
int expected = 0;
EXPECT_EQ(getDecimalValue(head), expected);
freeList(head);
}