119 219 222
Some checks failed
Gitea CTest Workflow / test (push) Failing after 3m20s

This commit is contained in:
2026-03-09 19:58:49 +08:00
parent 6e2206cabe
commit 7d45cb5978
12 changed files with 408 additions and 39 deletions

49
tests/test_119.cpp Normal file
View File

@@ -0,0 +1,49 @@
// This file is generated by mkfile.py
// Date: 2026-03-09
#include <gtest/gtest.h>
#include <solution/119.h>
TEST(GetRowTest, Example1)
{
int returnSize = 0;
int *result = getRow(3, &returnSize);
int expected[] = {1, 3, 3, 1};
ASSERT_EQ(returnSize, 4);
for (int i = 0; i < returnSize; i++)
{
EXPECT_EQ(result[i], expected[i]);
}
free(result);
}
TEST(GetRowTest, Example2)
{
int returnSize = 0;
int *result = getRow(0, &returnSize);
int expected[] = {1};
ASSERT_EQ(returnSize, 1);
for (int i = 0; i < returnSize; i++)
{
EXPECT_EQ(result[i], expected[i]);
}
free(result);
}
TEST(GetRowTest, Example3)
{
int returnSize = 0;
int *result = getRow(1, &returnSize);
int expected[] = {1, 1};
ASSERT_EQ(returnSize, 2);
for (int i = 0; i < returnSize; i++)
{
EXPECT_EQ(result[i], expected[i]);
}
free(result);
}

106
tests/test_219.cpp Normal file
View File

@@ -0,0 +1,106 @@
// This file is generated by mkfile.py
// Date: 2026-03-09
#include <fstream>
#include <gtest/gtest.h>
#include <solution/219.h>
int *loadArrayFromFile(const std::string &path, int *len)
{
std::ifstream file(path);
std::string s;
if (!file.is_open())
return NULL;
std::getline(file, s);
*len = 1;
for (char c : s)
if (c == ',')
(*len)++;
int *arr = new int[*len];
int index = 0;
int num = 0;
bool reading = false;
for (char c : s)
{
if (isdigit(c))
{
num = num * 10 + (c - '0');
reading = true;
}
else
{
if (reading)
{
arr[index++] = num;
num = 0;
reading = false;
}
}
}
return arr;
}
// 示例 1
TEST(ContainsNearbyDuplicateTest, Example1)
{
int nums[] = {1, 2, 3, 1};
bool result = containsNearbyDuplicate(nums, 4, 3);
EXPECT_TRUE(result);
}
// 示例 2
TEST(ContainsNearbyDuplicateTest, Example2)
{
int nums[] = {1, 0, 1, 1};
bool result = containsNearbyDuplicate(nums, 4, 1);
EXPECT_TRUE(result);
}
// 示例 3
TEST(ContainsNearbyDuplicateTest, Example3)
{
int nums[] = {1, 2, 3, 1, 2, 3};
bool result = containsNearbyDuplicate(nums, 6, 2);
EXPECT_FALSE(result);
}
TEST(ContainsNearbyDuplicateTest, Example4)
{
int nums[] = {1, 5, 1, 0};
bool result = containsNearbyDuplicate(nums, 4, 2);
EXPECT_TRUE(result);
}
TEST(ContainsNearbyDuplicateTest, Example5)
{
int nums[] = {0, 1, 2, 3, 4, 0, 0, 7, 8, 9, 10, 11, 12, 0};
bool result = containsNearbyDuplicate(nums, 14, 1);
EXPECT_TRUE(result);
}
TEST(ContainsNearbyDuplicateTest, Example6)
{
int len;
auto arr = loadArrayFromFile("../../file/test_219_6", &len);
ASSERT_TRUE(arr);
bool result = containsNearbyDuplicate(arr, len, 35000);
EXPECT_FALSE(result);
delete[] arr;
}
TEST(ContainsNearbyDuplicateTest, Example7)
{
int len;
auto arr = loadArrayFromFile("../../file/test_219_7", &len);
ASSERT_TRUE(arr);
bool result = containsNearbyDuplicate(arr, len, 99999);
EXPECT_TRUE(result);
delete[] arr;
}

59
tests/test_222.cpp Normal file
View File

@@ -0,0 +1,59 @@
// This file is generated by mkfile.py
// Date: 2026-03-09
#include <gtest/gtest.h>
#include <solution/222.h>
class CountNodesTest : public ::testing::Test
{
protected:
TreeNode *createNode(int val)
{
TreeNode *node = new TreeNode;
node->val = val;
node->left = nullptr;
node->right = nullptr;
return node;
}
void freeTree(TreeNode *root)
{
if (!root)
return;
freeTree(root->left);
freeTree(root->right);
delete root;
}
};
// 示例 1
TEST_F(CountNodesTest, Example1)
{
// 构建树: [1,2,3,4,5,6]
TreeNode *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
EXPECT_EQ(countNodes(root), 6);
freeTree(root);
}
// 示例 2
TEST_F(CountNodesTest, Example2)
{
TreeNode *root = nullptr;
EXPECT_EQ(countNodes(root), 0);
}
// 示例 3
TEST_F(CountNodesTest, Example3)
{
TreeNode *root = createNode(1);
EXPECT_EQ(countNodes(root), 1);
freeTree(root);
}