hoolang/tests/char_tests.cpp
2025-07-11 08:28:22 +05:30

68 lines
2.4 KiB
C++

#include "Compiler.hpp"
#include "Node.hpp"
#include "UTF8Char.h"
#include "llvm/IR/Constants.h"
#include <gtest/gtest.h>
// TEST(CharTest, SingleChar)
// {
// auto compiler = std::make_unique<Compiler>("'a';", "main");
// auto result = compiler->compile();
// auto charNode = std::any_cast<Node>(result);
// ASSERT_EQ(charNode.getNodeType(), NODE_LITERAL);
// ASSERT_EQ(charNode.getDataType(), DATATYPE_CHAR);
// auto value = charNode.getValue();
// auto expected_value = llvm::ConstantInt::get(llvm::Type::getInt8Ty(*compiler->getContext()), 97);
// ASSERT_EQ(value, expected_value);
// }
// TEST(CharTest, SpecialChar)
// {
// auto compiler = std::make_unique<Compiler>("'\\n';", "main");
// auto result = compiler->compile();
// auto charNode = std::any_cast<Node>(result);
// ASSERT_EQ(charNode.getNodeType(), NODE_LITERAL);
// ASSERT_EQ(charNode.getDataType(), DATATYPE_CHAR);
// auto value = charNode.getValue();
// auto expected_value = llvm::ConstantInt::get(llvm::Type::getInt8Ty(*compiler->getContext()), 10);
// ASSERT_EQ(value, expected_value);
// }
TEST(CharTest, UnicodeChar)
{
auto compiler = std::make_unique<Compiler>("'\\u00E9';", "main");
auto result = compiler->compile();
auto charNode = std::any_cast<Node>(result);
ASSERT_EQ(charNode.getNodeType(), NODE_LITERAL);
ASSERT_EQ(charNode.getDataType(), DATATYPE_CHAR);
auto value = charNode.getValue();
auto value_name = value->getType()->getStructName().str();
ASSERT_EQ(value_name, CHAR_TYPE_NAME);
auto structValue = llvm::dyn_cast<llvm::ConstantStruct>(value);
ASSERT_NE(structValue, nullptr);
llvm::Value *length = llvm::dyn_cast<llvm::ConstantInt>(structValue->getAggregateElement((unsigned int)0));
ASSERT_NE(length, nullptr);
auto length_value = llvm::dyn_cast<llvm::ConstantInt>(length)->getZExtValue();
ASSERT_EQ(length_value, 2);
llvm::ConstantDataArray *array = llvm::dyn_cast<llvm::ConstantDataArray>(structValue->getAggregateElement((unsigned int)1));
ASSERT_NE(array, nullptr);
auto byte1 = array->getElementAsConstant(0);
auto byte2 = array->getElementAsConstant(1);
auto byte1_value = llvm::dyn_cast<llvm::ConstantInt>(byte1)->getZExtValue();
ASSERT_EQ(byte1_value, 195);
auto byte2_value = llvm::dyn_cast<llvm::ConstantInt>(byte2)->getZExtValue();
ASSERT_EQ(byte2_value, 169);
}