|
8 | 8 |
|
9 | 9 | using namespace libscratchcpp; |
10 | 10 |
|
| 11 | +using ::testing::Return; |
| 12 | + |
11 | 13 | class LLVMCodeBuilderTest : public testing::Test |
12 | 14 | { |
13 | 15 | public: |
@@ -103,3 +105,172 @@ TEST_F(LLVMCodeBuilderTest, Yield) |
103 | 105 | ASSERT_EQ(testing::internal::GetCapturedStdout(), expected2); |
104 | 106 | ASSERT_TRUE(code->isFinished(ctx.get())); |
105 | 107 | } |
| 108 | + |
| 109 | +TEST_F(LLVMCodeBuilderTest, IfStatement) |
| 110 | +{ |
| 111 | + // Without else branch (const condition) |
| 112 | + m_builder->addConstValue("true"); |
| 113 | + m_builder->beginIfStatement(); |
| 114 | + m_builder->addFunctionCall("test_function_no_args", 0, false); |
| 115 | + m_builder->endIf(); |
| 116 | + |
| 117 | + m_builder->addConstValue("false"); |
| 118 | + m_builder->beginIfStatement(); |
| 119 | + m_builder->addFunctionCall("test_function_no_args", 0, false); |
| 120 | + m_builder->endIf(); |
| 121 | + |
| 122 | + // Without else branch (condition returned by function) |
| 123 | + m_builder->addFunctionCall("test_function_no_args_ret", 0, true); |
| 124 | + m_builder->addConstValue("no_args_output"); |
| 125 | + m_builder->addFunctionCall("test_equals", 2, true); |
| 126 | + m_builder->beginIfStatement(); |
| 127 | + m_builder->addConstValue(0); |
| 128 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 129 | + m_builder->endIf(); |
| 130 | + |
| 131 | + m_builder->addFunctionCall("test_function_no_args_ret", 0, true); |
| 132 | + m_builder->addConstValue(""); |
| 133 | + m_builder->addFunctionCall("test_equals", 2, true); |
| 134 | + m_builder->beginIfStatement(); |
| 135 | + m_builder->addConstValue(1); |
| 136 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 137 | + m_builder->endIf(); |
| 138 | + |
| 139 | + // With else branch (const condition) |
| 140 | + m_builder->addConstValue("true"); |
| 141 | + m_builder->beginIfStatement(); |
| 142 | + m_builder->addConstValue(2); |
| 143 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 144 | + m_builder->beginElseBranch(); |
| 145 | + m_builder->addConstValue(3); |
| 146 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 147 | + m_builder->endIf(); |
| 148 | + |
| 149 | + m_builder->addConstValue("false"); |
| 150 | + m_builder->beginIfStatement(); |
| 151 | + m_builder->addConstValue(4); |
| 152 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 153 | + m_builder->beginElseBranch(); |
| 154 | + m_builder->addConstValue(5); |
| 155 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 156 | + m_builder->endIf(); |
| 157 | + |
| 158 | + // With else branch (condition returned by function) |
| 159 | + m_builder->addFunctionCall("test_function_no_args_ret", 0, true); |
| 160 | + m_builder->addConstValue("no_args_output"); |
| 161 | + m_builder->addFunctionCall("test_equals", 2, true); |
| 162 | + m_builder->beginIfStatement(); |
| 163 | + m_builder->addConstValue(6); |
| 164 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 165 | + m_builder->beginElseBranch(); |
| 166 | + m_builder->addConstValue(7); |
| 167 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 168 | + m_builder->endIf(); |
| 169 | + |
| 170 | + m_builder->addFunctionCall("test_function_no_args_ret", 0, true); |
| 171 | + m_builder->addConstValue(""); |
| 172 | + m_builder->addFunctionCall("test_equals", 2, true); |
| 173 | + m_builder->beginIfStatement(); |
| 174 | + m_builder->addConstValue(8); |
| 175 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 176 | + m_builder->beginElseBranch(); |
| 177 | + m_builder->addConstValue(9); |
| 178 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 179 | + m_builder->endIf(); |
| 180 | + |
| 181 | + // Nested 1 |
| 182 | + m_builder->addConstValue(true); |
| 183 | + m_builder->beginIfStatement(); |
| 184 | + { |
| 185 | + m_builder->addConstValue(false); |
| 186 | + m_builder->beginIfStatement(); |
| 187 | + { |
| 188 | + m_builder->addConstValue(0); |
| 189 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 190 | + } |
| 191 | + m_builder->beginElseBranch(); |
| 192 | + { |
| 193 | + m_builder->addConstValue(1); |
| 194 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 195 | + |
| 196 | + m_builder->addConstValue(false); |
| 197 | + m_builder->beginIfStatement(); |
| 198 | + m_builder->beginElseBranch(); |
| 199 | + { |
| 200 | + m_builder->addConstValue(2); |
| 201 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 202 | + } |
| 203 | + m_builder->endIf(); |
| 204 | + } |
| 205 | + m_builder->endIf(); |
| 206 | + } |
| 207 | + m_builder->beginElseBranch(); |
| 208 | + { |
| 209 | + m_builder->addConstValue(true); |
| 210 | + m_builder->beginIfStatement(); |
| 211 | + { |
| 212 | + m_builder->addConstValue(3); |
| 213 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 214 | + } |
| 215 | + m_builder->beginElseBranch(); |
| 216 | + { |
| 217 | + m_builder->addConstValue(4); |
| 218 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 219 | + } |
| 220 | + m_builder->endIf(); |
| 221 | + } |
| 222 | + m_builder->endIf(); |
| 223 | + |
| 224 | + // Nested 2 |
| 225 | + m_builder->addConstValue(false); |
| 226 | + m_builder->beginIfStatement(); |
| 227 | + { |
| 228 | + m_builder->addConstValue(false); |
| 229 | + m_builder->beginIfStatement(); |
| 230 | + { |
| 231 | + m_builder->addConstValue(5); |
| 232 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 233 | + } |
| 234 | + m_builder->beginElseBranch(); |
| 235 | + { |
| 236 | + m_builder->addConstValue(6); |
| 237 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 238 | + } |
| 239 | + m_builder->endIf(); |
| 240 | + } |
| 241 | + m_builder->beginElseBranch(); |
| 242 | + { |
| 243 | + m_builder->addConstValue(true); |
| 244 | + m_builder->beginIfStatement(); |
| 245 | + { |
| 246 | + m_builder->addConstValue(7); |
| 247 | + m_builder->addFunctionCall("test_function_1_arg", 1, false); |
| 248 | + } |
| 249 | + m_builder->beginElseBranch(); |
| 250 | + m_builder->endIf(); |
| 251 | + } |
| 252 | + m_builder->endIf(); |
| 253 | + |
| 254 | + auto code = m_builder->finalize(); |
| 255 | + auto ctx = code->createExecutionContext(&m_target); |
| 256 | + |
| 257 | + static const std::string expected = |
| 258 | + "no_args\n" |
| 259 | + "no_args_ret\n" |
| 260 | + "1_arg 0\n" |
| 261 | + "no_args_ret\n" |
| 262 | + "1_arg 2\n" |
| 263 | + "1_arg 5\n" |
| 264 | + "no_args_ret\n" |
| 265 | + "1_arg 6\n" |
| 266 | + "no_args_ret\n" |
| 267 | + "1_arg 9\n" |
| 268 | + "1_arg 1\n" |
| 269 | + "1_arg 2\n" |
| 270 | + "1_arg 7\n"; |
| 271 | + |
| 272 | + EXPECT_CALL(m_target, isStage).WillRepeatedly(Return(false)); |
| 273 | + testing::internal::CaptureStdout(); |
| 274 | + code->run(ctx.get()); |
| 275 | + ASSERT_EQ(testing::internal::GetCapturedStdout(), expected); |
| 276 | +} |
0 commit comments