Skip to content

Commit 78bd34b

Browse files
committed
Add test for raw value casting
1 parent ffbaf94 commit 78bd34b

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,123 @@ TEST_F(LLVMCodeBuilderTest, ConstCasting)
6363
{
6464
m_builder->addConstValue(5.2);
6565
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number });
66+
m_builder->addConstValue("-24.156");
67+
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number });
68+
69+
m_builder->addConstValue(true);
70+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
71+
72+
m_builder->addConstValue(0);
73+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
74+
75+
m_builder->addConstValue("false");
76+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
77+
78+
m_builder->addConstValue("123");
79+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
80+
81+
m_builder->addConstValue("hello world");
82+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
83+
84+
auto code = m_builder->finalize();
85+
auto ctx = code->createExecutionContext(&m_target);
86+
87+
static const std::string expected =
88+
"5.2\n"
89+
"-24.156\n"
90+
"1\n"
91+
"0\n"
92+
"0\n"
93+
"123\n"
94+
"hello world\n";
95+
96+
testing::internal::CaptureStdout();
97+
code->run(ctx.get());
98+
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
99+
}
100+
101+
TEST_F(LLVMCodeBuilderTest, RawValueCasting)
102+
{
103+
// Number -> number
104+
m_builder->addConstValue(5.2);
105+
m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number });
106+
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number });
107+
108+
// Number -> bool
66109
m_builder->addConstValue(-24.156);
110+
m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number });
111+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
112+
113+
m_builder->addConstValue(0);
114+
m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number });
115+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
116+
117+
// Number -> string
118+
m_builder->addConstValue(59.8);
119+
m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number });
120+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
121+
122+
// Bool -> number
123+
m_builder->addConstValue(true);
124+
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool });
125+
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number });
126+
127+
m_builder->addConstValue(false);
128+
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool });
67129
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number });
68130

131+
// Bool -> bool
69132
m_builder->addConstValue(true);
133+
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool });
70134
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
71135

72136
m_builder->addConstValue(false);
137+
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool });
138+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
139+
140+
// Bool -> string
141+
m_builder->addConstValue(true);
142+
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool });
143+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
144+
145+
m_builder->addConstValue(false);
146+
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool });
147+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
148+
149+
// String -> number
150+
m_builder->addConstValue("5.2");
151+
m_builder->addFunctionCall("test_const_string", Compiler::StaticType::String, { Compiler::StaticType::String });
152+
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number });
153+
154+
// String -> bool
155+
m_builder->addConstValue("abc");
156+
m_builder->addFunctionCall("test_const_string", Compiler::StaticType::String, { Compiler::StaticType::String });
157+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
158+
159+
m_builder->addConstValue("false");
160+
m_builder->addFunctionCall("test_const_string", Compiler::StaticType::String, { Compiler::StaticType::String });
73161
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool });
74162

163+
// String -> string
75164
m_builder->addConstValue("hello world");
165+
m_builder->addFunctionCall("test_const_string", Compiler::StaticType::String, { Compiler::StaticType::String });
76166
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String });
77167

78168
auto code = m_builder->finalize();
79169
auto ctx = code->createExecutionContext(&m_target);
80170

81171
static const std::string expected =
82172
"5.2\n"
83-
"-24.156\n"
173+
"1\n"
174+
"0\n"
175+
"59.8\n"
176+
"1\n"
177+
"0\n"
178+
"1\n"
179+
"0\n"
180+
"true\n"
181+
"false\n"
182+
"5.2\n"
84183
"1\n"
85184
"0\n"
86185
"hello world\n";
@@ -332,7 +431,7 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
332431

333432
// Count returned by function
334433
m_builder->addConstValue(2);
335-
m_builder->addFunctionCall("test_const", Compiler::StaticType::Number, { Compiler::StaticType::Number });
434+
m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number });
336435
m_builder->beginRepeatLoop();
337436
m_builder->addFunctionCall("test_function_no_args", Compiler::StaticType::Void, {});
338437
m_builder->endLoop();

test/dev/llvm/testfunctions.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,22 @@ extern "C"
8383
return a < b;
8484
}
8585

86-
double test_const(Target *target, double v)
86+
double test_const_number(Target *target, double v)
8787
{
8888
return v;
8989
}
9090

91+
bool test_const_bool(Target *target, bool v)
92+
{
93+
return v;
94+
}
95+
96+
char *test_const_string(Target *target, const char *v)
97+
{
98+
Value value(v);
99+
return value_toCString(&value.data());
100+
}
101+
91102
bool test_not(Target *target, bool arg)
92103
{
93104
return !arg;

test/dev/llvm/testfunctions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ extern "C"
2222
bool test_equals(Target *target, const char *a, const char *b);
2323
bool test_lower_than(Target *target, double a, double b);
2424
bool test_not(Target *target, bool arg);
25-
double test_const(Target *target, double v);
25+
double test_const_number(Target *target, double v);
26+
bool test_const_bool(Target *target, bool v);
27+
char *test_const_string(Target *target, const char *v);
2628

2729
void test_unreachable(Target *target);
2830

0 commit comments

Comments
 (0)