hoolang/tests/integer_literal_tests.cpp

55 lines
1.7 KiB
C++

#include "Compiler.hpp"
#include "Node.hpp"
#include "llvm/IR/Constants.h"
#include <gtest/gtest.h>
#include "Compiler.hpp"
#include "Node.hpp"
#include "llvm/IR/Constants.h"
#include <gtest/gtest.h>
// Utility function to test integer literal compilation
void testIntegerLiteral(const std::string &input, int64_t expectedValue)
{
auto compiler = std::make_unique<Compiler>(input, "main");
auto result = compiler->compile();
auto integer = std::any_cast<Node>(result);
ASSERT_EQ(integer.getNodeType(), NODE_LITERAL);
ASSERT_EQ(integer.getDataType(), DATATYPE_INTEGER);
auto value = integer.getValue();
auto expected_value = llvm::ConstantInt::getSigned(
llvm::Type::getInt64Ty(*compiler->getContext()), expectedValue);
auto value_constant = llvm::dyn_cast<llvm::ConstantInt>(value);
auto expected_value_constant = llvm::dyn_cast<llvm::ConstantInt>(expected_value);
ASSERT_NE(value_constant, nullptr); // Ensure value is llvm::ConstantInt
ASSERT_NE(expected_value_constant, nullptr); // Ensure expected_value is llvm::ConstantInt
ASSERT_EQ(value_constant->getValue(), expected_value_constant->getValue()); // Compare values
ASSERT_EQ(value, expected_value); // Pointer equality
}
// Test cases
TEST(IntegerTest, LiteralOne)
{
testIntegerLiteral("1;", 1);
}
TEST(IntegerTest, LiteralNumberPositive)
{
testIntegerLiteral("67890;", 67890);
}
TEST(IntegerTest, LiteralNumberPositiveWithPlus)
{
testIntegerLiteral("+67890;", 67890);
}
TEST(IntegerTest, LiteralNumberNegative)
{
testIntegerLiteral("-67890;", -67890);
}