#include "Compiler.hpp" #include "Node.hpp" #include "llvm/IR/Constants.h" #include // Utility function to test double literal compilation void testDoubleLiteral(const std::string &input, double expectedValue) { auto compiler = std::make_unique(input, "main"); auto result = compiler->compile(); auto doubleNode = std::any_cast(result); ASSERT_EQ(doubleNode.getNodeType(), NODE_LITERAL); ASSERT_EQ(doubleNode.getDataType(), DATATYPE_DOUBLE); auto value = doubleNode.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(DoubleTest, LiteralPositive) { testDoubleLiteral("3.14;", 3.14); } TEST(DoubleTest, LiteralNegative) { testDoubleLiteral("-2.718;", -2.718); } TEST(DoubleTest, LiteralZero) { testDoubleLiteral("0.0;", 0.0); } TEST(DoubleTest, LiteralScientificNotationPositive) { testDoubleLiteral("12300.0;", 12300.0); } TEST(DoubleTest, LiteralScientificNotationNegative) { testDoubleLiteral("-0.00567;", -0.00567); }