Skip to content

Commit 54e684e

Browse files
committed
fix: c++ char compare
special handle char type
1 parent 0a36063 commit 54e684e

File tree

1 file changed

+75
-39
lines changed

1 file changed

+75
-39
lines changed

cpp/TestMain.cpp

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ vector<TestCase> LoadTestCases(const string &path) {
7878
}
7979

8080
class LeetCodeSuiteSet : public testing::Test {
81-
public:
81+
public:
8282
// All of these optional, just like in regular macro usage.
8383
static void SetUpTestSuite() {}
8484

@@ -90,13 +90,13 @@ class LeetCodeSuiteSet : public testing::Test {
9090
};
9191

9292
class LeetCodeTest : public LeetCodeSuiteSet {
93-
public:
93+
public:
9494
explicit LeetCodeTest(TestCase data) : data_(std::move(data)) {}
9595

9696
void TestBody() override {
9797
bool isEqual = false;
9898
int retries = 0;
99-
const int maxRetries = 1e5; // Set the maximum number of retries
99+
const int maxRetries = 1e5; // Set the maximum number of retries
100100
cout << "Input: " << data_.GetInput() << endl;
101101
cout << "Expected: " << data_.GetExpected() << endl;
102102
auto output = leetcode::qubh::Solve(data_.GetInput());
@@ -120,18 +120,52 @@ class LeetCodeTest : public LeetCodeSuiteSet {
120120
}
121121
}
122122

123-
if (data_.GetExpected().is_number_float()) {
124-
ASSERT_LE(std::abs(output.get<double>() - data_.GetExpected().get<double>()), 1e-5);
123+
auto expected = data_.GetExpected();
124+
125+
if (expected.is_number_float()) {
126+
ASSERT_LE(std::abs(output.get<double>() - expected.get<double>()), 1e-5);
125127
} else {
126-
if (output.is_array() && !data_.GetExpected().is_array()) {
127-
ASSERT_EQ(output[0], data_.GetExpected());
128+
if (output.is_array() && !expected.is_array()) {
129+
ASSERT_EQ(output[0], expected);
128130
} else {
129-
ASSERT_EQ(output, data_.GetExpected());
131+
// handle char
132+
if (expected.is_string() && output.is_number_integer()) {
133+
ASSERT_EQ(static_cast<char>(output.get<int>()),
134+
expected.get<string>()[0]);
135+
} else if (expected.is_array() && expected.size() > 0 &&
136+
expected[0].is_string() && output.is_array() &&
137+
output.size() > 0 && output[0].is_number_integer()) {
138+
ASSERT_EQ(output.size(), expected.size());
139+
for (size_t i = 0; i < expected.size(); i++) {
140+
ASSERT_EQ(static_cast<char>(output[i].get<int>()),
141+
expected[0].get<string>()[i]);
142+
}
143+
} else if (expected.is_array() && expected.size() > 0 &&
144+
expected[0].is_array() && expected[0].size() > 0 &&
145+
expected[0][0].is_string() && output.is_array() &&
146+
output.size() > 0 && output[0].is_array() &&
147+
output[0].size() > 0 && output[0][0].is_number_integer()) {
148+
ASSERT_EQ(output.size(), expected.size());
149+
for (size_t i = 0; i < expected.size(); i++) {
150+
ASSERT_EQ(output[i].size(), expected[i].size());
151+
for (size_t j = 0; j < expected[i].size(); j++) {
152+
if (expected[i][j].is_string() &&
153+
output[i][j].is_number_integer()) {
154+
ASSERT_EQ(static_cast<char>(output[i][j].get<int>()),
155+
expected[i][j].get<string>()[0]);
156+
} else {
157+
ASSERT_EQ(output[i][j], expected[i][j]);
158+
}
159+
}
160+
}
161+
} else {
162+
ASSERT_EQ(output, expected);
163+
}
130164
}
131165
}
132166
}
133167

134-
private:
168+
private:
135169
TestCase data_;
136170
};
137171

@@ -147,39 +181,41 @@ void RegisterMyTests(const vector<TestCase> &values) {
147181
[=]() -> LeetCodeSuiteSet * { return new LeetCodeTest(values[i]); });
148182
}
149183
}
150-
} // namespace qubh
151-
} // namespace LeetCode
184+
} // namespace qubh
185+
} // namespace LeetCode
152186

153187
int main(int argc, char **argv) {
154-
try {
155-
// 检查是否提供了测试用例路径
156-
string testcasePath;
157-
if (argc >= 2) {
158-
testcasePath = argv[1];
159-
} else {
160-
// 尝试从环境变量获取路径
161-
const char* env_path = std::getenv("TESTCASE_FILE");
162-
if (env_path) {
163-
testcasePath = env_path;
164-
} else {
165-
cerr << "Error: Testcase path not provided and TESTCASE_FILE environment variable not set." << endl;
166-
cerr << "Usage: " << argv[0] << " <testcase_path>" << endl;
167-
return 1;
168-
}
169-
}
188+
try {
189+
// 检查是否提供了测试用例路径
190+
string testcasePath;
191+
if (argc >= 2) {
192+
testcasePath = argv[1];
193+
} else {
194+
// 尝试从环境变量获取路径
195+
const char *env_path = std::getenv("TESTCASE_FILE");
196+
if (env_path) {
197+
testcasePath = env_path;
198+
} else {
199+
cerr << "Error: Testcase path not provided and TESTCASE_FILE "
200+
"environment variable not set."
201+
<< endl;
202+
cerr << "Usage: " << argv[0] << " <testcase_path>" << endl;
203+
return 1;
204+
}
205+
}
170206

171-
cout << "Loading testcases from: " << testcasePath << endl;
207+
cout << "Loading testcases from: " << testcasePath << endl;
172208

173-
// 加载测试用例
174-
vector<LeetCode::qubh::TestCase> testcases =
175-
LeetCode::qubh::LoadTestCases(testcasePath);
209+
// 加载测试用例
210+
vector<LeetCode::qubh::TestCase> testcases =
211+
LeetCode::qubh::LoadTestCases(testcasePath);
176212

177-
// 初始化并运行测试
178-
testing::InitGoogleTest(&argc, argv);
179-
LeetCode::qubh::RegisterMyTests(testcases);
180-
return RUN_ALL_TESTS();
181-
} catch (const std::exception &e) {
182-
cerr << "Error: " << e.what() << endl;
183-
return 1;
184-
}
213+
// 初始化并运行测试
214+
testing::InitGoogleTest(&argc, argv);
215+
LeetCode::qubh::RegisterMyTests(testcases);
216+
return RUN_ALL_TESTS();
217+
} catch (const std::exception &e) {
218+
cerr << "Error: " << e.what() << endl;
219+
return 1;
220+
}
185221
}

0 commit comments

Comments
 (0)