@@ -360,3 +360,80 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
360360 code->run (ctx.get ());
361361 ASSERT_EQ (testing::internal::GetCapturedStdout (), expected);
362362}
363+
364+ TEST_F (LLVMCodeBuilderTest, WhileLoop)
365+ {
366+ // Const condition
367+ m_builder->beginLoopCondition ();
368+ m_builder->addConstValue (" false" );
369+ m_builder->beginWhileLoop ();
370+ m_builder->addFunctionCall (" test_unreachable" , 0 , false );
371+ m_builder->endLoop ();
372+
373+ m_builder->beginLoopCondition ();
374+ m_builder->addConstValue (false );
375+ m_builder->beginWhileLoop ();
376+ m_builder->addFunctionCall (" test_unreachable" , 0 , false );
377+ m_builder->endLoop ();
378+
379+ // Condition returned by function
380+ m_builder->addFunctionCall (" test_reset_counter" , 0 , false );
381+ m_builder->beginLoopCondition ();
382+ m_builder->addFunctionCall (" test_get_counter" , 0 , true );
383+ m_builder->addConstValue (2 );
384+ m_builder->addFunctionCall (" test_lower_than" , 2 , true );
385+ m_builder->beginWhileLoop ();
386+ m_builder->addConstValue (0 );
387+ m_builder->addFunctionCall (" test_function_1_arg" , 1 , false );
388+ m_builder->addFunctionCall (" test_increment_counter" , 0 , false );
389+ m_builder->endLoop ();
390+
391+ // Nested
392+ m_builder->addFunctionCall (" test_reset_counter" , 0 , false );
393+ m_builder->beginLoopCondition ();
394+ m_builder->addFunctionCall (" test_get_counter" , 0 , true );
395+ m_builder->addConstValue (3 );
396+ m_builder->addFunctionCall (" test_lower_than" , 2 , true );
397+ m_builder->beginWhileLoop ();
398+ {
399+ m_builder->beginLoopCondition ();
400+ m_builder->addFunctionCall (" test_get_counter" , 0 , true );
401+ m_builder->addConstValue (3 );
402+ m_builder->addFunctionCall (" test_lower_than" , 2 , true );
403+ m_builder->beginWhileLoop ();
404+ {
405+ m_builder->addConstValue (1 );
406+ m_builder->addFunctionCall (" test_function_1_arg" , 1 , false );
407+ m_builder->addFunctionCall (" test_increment_counter" , 0 , false );
408+ }
409+ m_builder->endLoop ();
410+
411+ m_builder->addConstValue (2 );
412+ m_builder->addFunctionCall (" test_function_1_arg" , 1 , false );
413+
414+ m_builder->beginLoopCondition ();
415+ m_builder->addConstValue (false );
416+ m_builder->beginWhileLoop ();
417+ {
418+ m_builder->addFunctionCall (" test_unreachable" , 0 , false );
419+ }
420+ m_builder->endLoop ();
421+ }
422+ m_builder->endLoop ();
423+
424+ auto code = m_builder->finalize ();
425+ auto ctx = code->createExecutionContext (&m_target);
426+
427+ static const std::string expected =
428+ " 1_arg 0\n "
429+ " 1_arg 0\n "
430+ " 1_arg 1\n "
431+ " 1_arg 1\n "
432+ " 1_arg 1\n "
433+ " 1_arg 2\n " ;
434+
435+ EXPECT_CALL (m_target, isStage).WillRepeatedly (Return (false ));
436+ testing::internal::CaptureStdout ();
437+ code->run (ctx.get ());
438+ ASSERT_EQ (testing::internal::GetCapturedStdout (), expected);
439+ }
0 commit comments