|
2 | 2 | #include <scratchcpp/value_functions.h>
|
3 | 3 | #include <scratchcpp/variable.h>
|
4 | 4 | #include <scratchcpp/list.h>
|
| 5 | +#include <scratchcpp/dev/promise.h> |
5 | 6 | #include <dev/engine/internal/llvm/llvmexecutablecode.h>
|
6 | 7 | #include <dev/engine/internal/llvm/llvmexecutioncontext.h>
|
7 | 8 | #include <llvm/Support/TargetSelect.h>
|
@@ -144,3 +145,94 @@ TEST_F(LLVMExecutableCodeTest, MainFunction)
|
144 | 145 | ASSERT_TRUE(code.isFinished(anotherCtx.get()));
|
145 | 146 | ASSERT_FALSE(code.isFinished(ctx.get()));
|
146 | 147 | }
|
| 148 | + |
| 149 | +TEST_F(LLVMExecutableCodeTest, Promise) |
| 150 | +{ |
| 151 | + auto f = beginMainFunction(); |
| 152 | + addTestFunction(f); |
| 153 | + endFunction(nullPointer()); |
| 154 | + |
| 155 | + beginResumeFunction(); |
| 156 | + endFunction(m_builder->getInt1(true)); |
| 157 | + |
| 158 | + LLVMExecutableCode code(std::move(m_module)); |
| 159 | + auto ctx = code.createExecutionContext(&m_target); |
| 160 | + ASSERT_FALSE(code.isFinished(ctx.get())); |
| 161 | + |
| 162 | + // run() |
| 163 | + auto promise = std::make_shared<Promise>(); |
| 164 | + ctx->setPromise(promise); |
| 165 | + EXPECT_CALL(m_mock, f).Times(0); |
| 166 | + |
| 167 | + for (int i = 0; i < 10; i++) { |
| 168 | + code.run(ctx.get()); |
| 169 | + ASSERT_FALSE(code.isFinished(ctx.get())); |
| 170 | + } |
| 171 | + |
| 172 | + promise->resolve(); |
| 173 | + |
| 174 | + EXPECT_CALL(m_mock, f); |
| 175 | + code.run(ctx.get()); |
| 176 | + ASSERT_TRUE(code.isFinished(ctx.get())); |
| 177 | + ASSERT_EQ(ctx->promise(), nullptr); |
| 178 | + code.reset(ctx.get()); |
| 179 | + |
| 180 | + // kill() |
| 181 | + promise = std::make_shared<Promise>(); |
| 182 | + ctx->setPromise(promise); |
| 183 | + EXPECT_CALL(m_mock, f).Times(0); |
| 184 | + |
| 185 | + for (int i = 0; i < 10; i++) { |
| 186 | + code.run(ctx.get()); |
| 187 | + ASSERT_FALSE(code.isFinished(ctx.get())); |
| 188 | + } |
| 189 | + |
| 190 | + code.kill(ctx.get()); |
| 191 | + ASSERT_TRUE(code.isFinished(ctx.get())); |
| 192 | + ASSERT_EQ(ctx->promise(), nullptr); |
| 193 | + code.reset(ctx.get()); |
| 194 | + |
| 195 | + // reset() |
| 196 | + promise = std::make_shared<Promise>(); |
| 197 | + ctx->setPromise(promise); |
| 198 | + EXPECT_CALL(m_mock, f).Times(0); |
| 199 | + |
| 200 | + for (int i = 0; i < 10; i++) { |
| 201 | + code.run(ctx.get()); |
| 202 | + ASSERT_FALSE(code.isFinished(ctx.get())); |
| 203 | + } |
| 204 | + |
| 205 | + code.reset(ctx.get()); |
| 206 | + ASSERT_FALSE(code.isFinished(ctx.get())); |
| 207 | + ASSERT_EQ(ctx->promise(), nullptr); |
| 208 | + |
| 209 | + EXPECT_CALL(m_mock, f); |
| 210 | + code.run(ctx.get()); |
| 211 | + ASSERT_TRUE(code.isFinished(ctx.get())); |
| 212 | + |
| 213 | + // Test with another context |
| 214 | + Target anotherTarget; |
| 215 | + auto anotherCtx = code.createExecutionContext(&anotherTarget); |
| 216 | + ASSERT_FALSE(code.isFinished(anotherCtx.get())); |
| 217 | + ASSERT_TRUE(code.isFinished(ctx.get())); |
| 218 | + |
| 219 | + promise = std::make_shared<Promise>(); |
| 220 | + anotherCtx->setPromise(promise); |
| 221 | + EXPECT_CALL(m_mock, f).Times(0); |
| 222 | + |
| 223 | + for (int i = 0; i < 10; i++) { |
| 224 | + code.run(anotherCtx.get()); |
| 225 | + ASSERT_FALSE(code.isFinished(anotherCtx.get())); |
| 226 | + } |
| 227 | + |
| 228 | + promise->resolve(); |
| 229 | + |
| 230 | + EXPECT_CALL(m_mock, f); |
| 231 | + code.run(anotherCtx.get()); |
| 232 | + ASSERT_TRUE(code.isFinished(anotherCtx.get())); |
| 233 | + ASSERT_TRUE(code.isFinished(ctx.get())); |
| 234 | + |
| 235 | + code.reset(ctx.get()); |
| 236 | + ASSERT_TRUE(code.isFinished(anotherCtx.get())); |
| 237 | + ASSERT_FALSE(code.isFinished(ctx.get())); |
| 238 | +} |
0 commit comments