@@ -1279,6 +1279,49 @@ TEST_F(LLVMCodeBuilderTest, Ceil)
12791279 addOpTest (nan, 0 );
12801280}
12811281
1282+ TEST_F (LLVMCodeBuilderTest, Sqrt)
1283+ {
1284+ std::string expected;
1285+
1286+ auto addOpTest = [this , &expected](Value v1, double expectedResult) {
1287+ createBuilder (true );
1288+
1289+ m_builder->addConstValue (v1);
1290+ m_builder->createSqrt ();
1291+ m_builder->addFunctionCall (" test_print_number" , Compiler::StaticType::Void, { Compiler::StaticType::Number });
1292+
1293+ m_builder->addConstValue (v1);
1294+ m_builder->addFunctionCall (" test_const_number" , Compiler::StaticType::Number, { Compiler::StaticType::Number });
1295+ m_builder->createSqrt ();
1296+ m_builder->addFunctionCall (" test_print_number" , Compiler::StaticType::Void, { Compiler::StaticType::Number });
1297+
1298+ std::stringstream stream;
1299+ stream << expectedResult;
1300+ std::string str = stream.str () + ' \n ' ;
1301+ std::string expected = str + str;
1302+
1303+ auto code = m_builder->finalize ();
1304+ auto ctx = code->createExecutionContext (&m_target);
1305+
1306+ testing::internal::CaptureStdout ();
1307+ code->run (ctx.get ());
1308+ const std::string quotes1 = v1.isString () ? " \" " : " " ;
1309+ ASSERT_THAT (testing::internal::GetCapturedStdout (), Eq (expected)) << quotes1 << v1.toString () << quotes1;
1310+ };
1311+
1312+ static const double inf = std::numeric_limits<double >::infinity ();
1313+ static const double nan = std::numeric_limits<double >::quiet_NaN ();
1314+
1315+ addOpTest (16.0 , 4.0 );
1316+ addOpTest (0.04 , 0.2 );
1317+ addOpTest (0.0 , 0.0 );
1318+ addOpTest (-0.0 , 0.0 );
1319+ addOpTest (-4.0 , -nan); // negative NaN shouldn't be a problem
1320+ addOpTest (inf, inf);
1321+ addOpTest (-inf, -nan);
1322+ addOpTest (nan, 0 );
1323+ }
1324+
12821325TEST_F (LLVMCodeBuilderTest, Yield)
12831326{
12841327 auto build = [this ]() {
0 commit comments