Skip to content

Commit 6243928

Browse files
committed
LLVMCodeBuilder: Make operator tests easier to debug
1 parent 9752ff9 commit 6243928

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using namespace libscratchcpp;
1010

1111
using ::testing::Return;
12+
using ::testing::Eq;
1213

1314
class LLVMCodeBuilderTest : public testing::Test
1415
{
@@ -205,6 +206,8 @@ TEST_F(LLVMCodeBuilderTest, Add)
205206
std::string expected;
206207

207208
auto addOpTest = [this, &expected](Value v1, Value v2, double expectedResult) {
209+
createBuilder(true);
210+
208211
m_builder->addConstValue(v1);
209212
m_builder->addConstValue(v2);
210213
m_builder->createAdd();
@@ -218,11 +221,17 @@ TEST_F(LLVMCodeBuilderTest, Add)
218221
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
219222

220223
std::string str = Value(expectedResult).toString() + '\n';
221-
expected += str;
222-
expected += str;
223-
};
224+
std::string expected = str + str;
224225

225-
createBuilder(true);
226+
auto code = m_builder->finalize();
227+
auto ctx = code->createExecutionContext(&m_target);
228+
229+
testing::internal::CaptureStdout();
230+
code->run(ctx.get());
231+
const std::string quotes1 = v1.isString() ? "\"" : "";
232+
const std::string quotes2 = v2.isString() ? "\"" : "";
233+
ASSERT_THAT(testing::internal::GetCapturedStdout(), Eq(expected)) << quotes1 << v1.toString() << quotes1 << " " << quotes2 << v2.toString() << quotes2;
234+
};
226235

227236
addOpTest(50, 25, 75);
228237
addOpTest(-500, 25, -475);
@@ -236,20 +245,15 @@ TEST_F(LLVMCodeBuilderTest, Add)
236245
addOpTest("-Infinity", "-Infinity", -std::numeric_limits<double>::infinity());
237246
addOpTest(1, "NaN", 1);
238247
addOpTest("NaN", 1, 1);
239-
240-
auto code = m_builder->finalize();
241-
auto ctx = code->createExecutionContext(&m_target);
242-
243-
testing::internal::CaptureStdout();
244-
code->run(ctx.get());
245-
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
246248
}
247249

248250
TEST_F(LLVMCodeBuilderTest, Subtract)
249251
{
250252
std::string expected;
251253

252254
auto addOpTest = [this, &expected](Value v1, Value v2, double expectedResult) {
255+
createBuilder(true);
256+
253257
m_builder->addConstValue(v1);
254258
m_builder->addConstValue(v2);
255259
m_builder->createSub();
@@ -263,11 +267,17 @@ TEST_F(LLVMCodeBuilderTest, Subtract)
263267
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
264268

265269
std::string str = Value(expectedResult).toString() + '\n';
266-
expected += str;
267-
expected += str;
268-
};
270+
std::string expected = str + str;
269271

270-
createBuilder(true);
272+
auto code = m_builder->finalize();
273+
auto ctx = code->createExecutionContext(&m_target);
274+
275+
testing::internal::CaptureStdout();
276+
code->run(ctx.get());
277+
const std::string quotes1 = v1.isString() ? "\"" : "";
278+
const std::string quotes2 = v2.isString() ? "\"" : "";
279+
ASSERT_THAT(testing::internal::GetCapturedStdout(), Eq(expected)) << quotes1 << v1.toString() << quotes1 << " " << quotes2 << v2.toString() << quotes2;
280+
};
271281

272282
addOpTest(50, 25, 25);
273283
addOpTest(-500, 25, -525);
@@ -281,20 +291,15 @@ TEST_F(LLVMCodeBuilderTest, Subtract)
281291
addOpTest("-Infinity", "-Infinity", std::numeric_limits<double>::quiet_NaN());
282292
addOpTest(1, "NaN", 1);
283293
addOpTest("NaN", 1, -1);
284-
285-
auto code = m_builder->finalize();
286-
auto ctx = code->createExecutionContext(&m_target);
287-
288-
testing::internal::CaptureStdout();
289-
code->run(ctx.get());
290-
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
291294
}
292295

293296
TEST_F(LLVMCodeBuilderTest, Multiply)
294297
{
295298
std::string expected;
296299

297300
auto addOpTest = [this, &expected](Value v1, Value v2, double expectedResult) {
301+
createBuilder(true);
302+
298303
m_builder->addConstValue(v1);
299304
m_builder->addConstValue(v2);
300305
m_builder->createMul();
@@ -308,11 +313,17 @@ TEST_F(LLVMCodeBuilderTest, Multiply)
308313
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
309314

310315
std::string str = Value(expectedResult).toString() + '\n';
311-
expected += str;
312-
expected += str;
313-
};
316+
std::string expected = str + str;
314317

315-
createBuilder(true);
318+
auto code = m_builder->finalize();
319+
auto ctx = code->createExecutionContext(&m_target);
320+
321+
testing::internal::CaptureStdout();
322+
code->run(ctx.get());
323+
const std::string quotes1 = v1.isString() ? "\"" : "";
324+
const std::string quotes2 = v2.isString() ? "\"" : "";
325+
ASSERT_THAT(testing::internal::GetCapturedStdout(), Eq(expected)) << quotes1 << v1.toString() << quotes1 << " " << quotes2 << v2.toString() << quotes2;
326+
};
316327

317328
addOpTest(50, 2, 100);
318329
addOpTest(-500, 25, -12500);
@@ -331,20 +342,15 @@ TEST_F(LLVMCodeBuilderTest, Multiply)
331342
addOpTest("-Infinity", "-Infinity", std::numeric_limits<double>::infinity());
332343
addOpTest(1, "NaN", 0);
333344
addOpTest("NaN", 1, 0);
334-
335-
auto code = m_builder->finalize();
336-
auto ctx = code->createExecutionContext(&m_target);
337-
338-
testing::internal::CaptureStdout();
339-
code->run(ctx.get());
340-
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
341345
}
342346

343347
TEST_F(LLVMCodeBuilderTest, Divide)
344348
{
345349
std::string expected;
346350

347351
auto addOpTest = [this, &expected](Value v1, Value v2, double expectedResult) {
352+
createBuilder(true);
353+
348354
m_builder->addConstValue(v1);
349355
m_builder->addConstValue(v2);
350356
m_builder->createDiv();
@@ -358,11 +364,17 @@ TEST_F(LLVMCodeBuilderTest, Divide)
358364
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
359365

360366
std::string str = Value(expectedResult).toString() + '\n';
361-
expected += str;
362-
expected += str;
363-
};
367+
std::string expected = str + str;
364368

365-
createBuilder(true);
369+
auto code = m_builder->finalize();
370+
auto ctx = code->createExecutionContext(&m_target);
371+
372+
testing::internal::CaptureStdout();
373+
code->run(ctx.get());
374+
const std::string quotes1 = v1.isString() ? "\"" : "";
375+
const std::string quotes2 = v2.isString() ? "\"" : "";
376+
ASSERT_THAT(testing::internal::GetCapturedStdout(), Eq(expected)) << quotes1 << v1.toString() << quotes1 << " " << quotes2 << v2.toString() << quotes2;
377+
};
366378

367379
addOpTest(50, 2, 25);
368380
addOpTest(-500, 25, -20);
@@ -390,13 +402,6 @@ TEST_F(LLVMCodeBuilderTest, Divide)
390402
addOpTest(5, 0, std::numeric_limits<double>::infinity());
391403
addOpTest(-5, 0, -std::numeric_limits<double>::infinity());
392404
addOpTest(0, 0, std::numeric_limits<double>::quiet_NaN());
393-
394-
auto code = m_builder->finalize();
395-
auto ctx = code->createExecutionContext(&m_target);
396-
397-
testing::internal::CaptureStdout();
398-
code->run(ctx.get());
399-
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
400405
}
401406

402407
TEST_F(LLVMCodeBuilderTest, Yield)

0 commit comments

Comments
 (0)