hoolang/tests/double_tests.cpp

54 lines
1.4 KiB
C++

#include "Compiler.hpp"
#include "Node.hpp"
#include "llvm/IR/Constants.h"
#include <gtest/gtest.h>
// Utility function to test double literal compilation
void testDoubleLiteral(const std::string &input, double expectedValue)
{
auto compiler = std::make_unique<Compiler>(input, "main");
auto result = compiler->compile();
auto doubleNode = std::any_cast<Node>(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<llvm::ConstantFP>(value);
auto expected_value_constant = llvm::dyn_cast<llvm::ConstantFP>(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);
}