#include "Compiler.hpp" #include "Node.hpp" #include "llvm/IR/Constants.h" #include // Utility function to test floating-point literal compilation void testFloatLiteral(const std::string &input, double expectedValue) { auto compiler = std::make_unique(input, "main"); auto result = compiler->compile(); auto floatNode = std::any_cast(result); ASSERT_EQ(floatNode.getNodeType(), NODE_LITERAL); ASSERT_EQ(floatNode.getDataType(), DATATYPE_FLOAT); auto value = floatNode.getValue(); auto expected_value = llvm::ConstantFP::get( llvm::Type::getDoubleTy(*compiler->getContext()), expectedValue); auto value_constant = llvm::dyn_cast(value); auto expected_value_constant = llvm::dyn_cast(expected_value); ASSERT_NE(value_constant, nullptr); ASSERT_NE(expected_value_constant, nullptr); ASSERT_EQ(value_constant->getValueAPF(), expected_value_constant->getValueAPF()); ASSERT_EQ(value, expected_value); } // Test cases TEST(FloatTest, LiteralPositive) { testFloatLiteral("3.14;", 3.14); } TEST(FloatTest, LiteralNegative) { testFloatLiteral("-2.718;", -2.718); } TEST(FloatTest, LiteralZero) { testFloatLiteral("0.0;", 0.0); } TEST(FloatTest, LiteralScientificNotationPositive) { testFloatLiteral("12300.0;", 12300.0); } TEST(FloatTest, LiteralScientificNotationNegative) { testFloatLiteral("-0.00567;", -0.00567); }