leetcode/tests/jianzhi_offer/list_node_test.cpp
2023-05-21 22:37:07 +08:00

46 lines
1.3 KiB
C++

#include <gtest/gtest.h>
#include "jianzhi_offer.h"
/**
* 剑指 Offer 06. 从尾到头打印链表
*/
TEST(NodeListTest, BasicAssertions) {
yaha::ListNode *node = new yaha::ListNode(1);
node->next = new yaha::ListNode(3);
node->next->next = new yaha::ListNode(2);
auto res = node->reverse_print(node);
ASSERT_EQ(2, res[0]);
ASSERT_EQ(3, res[1]);
ASSERT_EQ(1, res[2]);
}
TEST(NodeListStackTest, BasicAssertions) {
yaha::ListNode *node = new yaha::ListNode(1);
node->next = new yaha::ListNode(3);
node->next->next = new yaha::ListNode(2);
std::vector<int> res;
node->reverse_stack(node, res);
ASSERT_EQ(2, res[0]);
ASSERT_EQ(3, res[1]);
ASSERT_EQ(1, res[2]);
}
/**
* 剑指 Offer 24. 反转链表
*/
TEST(ListReverseTest, BasicAssertions) {
yaha::ListNode *node = new yaha::ListNode(1);
node->next = new yaha::ListNode(2);
node->next->next = new yaha::ListNode(3);
node->next->next->next = new yaha::ListNode(4);
node->next->next->next->next = new yaha::ListNode(5);
auto l = node->reverse_list(node);
ASSERT_EQ(5, l->val);
ASSERT_EQ(4, l->next->val);
ASSERT_EQ(3, l->next->next->val);
ASSERT_EQ(2, l->next->next->next->val);
ASSERT_EQ(1, l->next->next->next->next->val);
}