hoolang/tests/literal_tests.cpp
2024-11-28 18:09:56 +05:30

64 lines
2.3 KiB
C++

#include "Compiler.hpp"
#include "Node.hpp"
#include "llvm/IR/Constants.h"
#include <gtest/gtest.h>
TEST(IntegerTest, LiteralOne)
{
auto compiler = std::make_unique<Compiler>("1;", "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::get(llvm::Type::getInt64Ty(*compiler->getContext()), 1, true);
ASSERT_EQ(value, expected_value);
}
TEST(IntegerTest, LiteralNumberPositive)
{
auto compiler = std::make_unique<Compiler>("67890;", "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::get(llvm::Type::getInt64Ty(*compiler->getContext()), 67890, true);
ASSERT_EQ(value, expected_value);
}
TEST(IntegerTest, LiteralNumberPositiveWithPlus)
{
auto compiler = std::make_unique<Compiler>("+67890;", "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::get(llvm::Type::getInt64Ty(*compiler->getContext()), 67890, true);
ASSERT_EQ(value, expected_value);
}
TEST(IntegerTest, LiteralNumberNegative)
{
auto compiler = std::make_unique<Compiler>("-67890;", "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::get(
llvm::Type::getInt64Ty(*compiler->getContext()), -67890, true);
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);
ASSERT_NE(expected_value_constant, nullptr);
ASSERT_EQ(value_constant->getValue(), expected_value_constant->getValue());
ASSERT_EQ(value, expected_value);
}