Added floating point tests
This commit is contained in:
parent
1a80064f9b
commit
35be166f8e
28
.vscode/launch.json
vendored
28
.vscode/launch.json
vendored
@ -5,10 +5,34 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(gdb) Launch",
|
||||
"name": "Integer Test",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/build/tests/literal_tests",
|
||||
"program": "${workspaceFolder}/build/tests/integer_literal_tests",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${fileDirname}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
},
|
||||
{
|
||||
"description": "Set Disassembly Flavor to Intel",
|
||||
"text": "-gdb-set disassembly-flavor intel",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Float Test",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/build/tests/floatingpoint_literal_tests",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${fileDirname}",
|
||||
|
||||
@ -24,4 +24,5 @@ ADD_SUBDIRECTORY(src)
|
||||
ADD_SUBDIRECTORY(tests)
|
||||
|
||||
ENABLE_TESTING()
|
||||
ADD_TEST(NAME literal_tests COMMAND literal_tests)
|
||||
ADD_TEST(NAME integer_literal_tests COMMAND integer_literal_tests)
|
||||
|
||||
|
||||
@ -6,7 +6,9 @@ fragment DIGIT: [0-9];
|
||||
|
||||
DECIMAL_LITERAL: '0' | ('+' | '-')? [1-9] [0-9]*;
|
||||
|
||||
FLOAT_LITERAL: (Digits '.' Digits? | '.' Digits);
|
||||
FLOAT_LITERAL: ('+' | '-')? Digits+ '.' Digits+ (
|
||||
[eE] [+-]? Digits+
|
||||
)?;
|
||||
|
||||
BOOL_LITERAL: 'true' | 'false';
|
||||
|
||||
|
||||
@ -31,11 +31,11 @@ std::any Visitor::visitLiteral(HooParser::LiteralContext *ctx)
|
||||
value = ctx->FLOAT_LITERAL();
|
||||
if (value)
|
||||
{
|
||||
auto floatText = value->getText();
|
||||
float floatValue = std::stof(floatText);
|
||||
llvm::Type *floatType = llvm::Type::getFloatTy(*_context);
|
||||
llvm::Constant *floatConstant = llvm::ConstantFP::get(floatType, floatValue);
|
||||
return std::any{Node(NODE_LITERAL, DATATYPE_FLOAT, floatConstant)};
|
||||
auto doubleText = value->getText();
|
||||
auto doubleValue = std::stod(doubleText);
|
||||
llvm::Constant *doubleConstant = llvm::ConstantFP::get(
|
||||
llvm::Type::getDoubleTy(*_context), doubleValue);
|
||||
return std::any{Node(NODE_LITERAL, DATATYPE_FLOAT, doubleConstant)};
|
||||
}
|
||||
|
||||
value = ctx->BOOL_LITERAL();
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
FIND_PACKAGE (GTest REQUIRED)
|
||||
|
||||
ADD_EXECUTABLE(literal_tests literal_tests.cpp)
|
||||
TARGET_LINK_LIBRARIES(literal_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
|
||||
TARGET_INCLUDE_DIRECTORIES(literal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
|
||||
ADD_EXECUTABLE(integer_literal_tests integer_literal_tests.cpp)
|
||||
TARGET_LINK_LIBRARIES(integer_literal_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
|
||||
TARGET_INCLUDE_DIRECTORIES(integer_literal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
|
||||
|
||||
ADD_EXECUTABLE(floatingpoint_literal_tests floatingpoint_literal_tests.cpp)
|
||||
TARGET_LINK_LIBRARIES(floatingpoint_literal_tests GTest::GTest GTest::Main hoocore antlr4-runtime LLVMCore LLVMSupport)
|
||||
TARGET_INCLUDE_DIRECTORIES(floatingpoint_literal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
|
||||
|
||||
53
tests/floatingpoint_literal_tests.cpp
Normal file
53
tests/floatingpoint_literal_tests.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "Compiler.hpp"
|
||||
#include "Node.hpp"
|
||||
#include "llvm/IR/Constants.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
// Utility function to test floating-point literal compilation
|
||||
void testFloatLiteral(const std::string &input, double expectedValue)
|
||||
{
|
||||
auto compiler = std::make_unique<Compiler>(input, "main");
|
||||
auto result = compiler->compile();
|
||||
auto floatNode = std::any_cast<Node>(result);
|
||||
|
||||
ASSERT_EQ(floatNode.getNodeType(), NODE_LITERAL);
|
||||
ASSERT_EQ(floatNode.getDataType(), DATATYPE_FLOAT);
|
||||
|
||||
auto value = floatNode.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(FloatTest, LiteralPositive)
|
||||
{
|
||||
testFloatLiteral("3.14;", 3.14);
|
||||
}
|
||||
|
||||
TEST(FloatTest, LiteralNegative)
|
||||
{
|
||||
testFloatLiteral("-2.718;", -2.718);
|
||||
}
|
||||
|
||||
TEST(FloatTest, LiteralZero)
|
||||
{
|
||||
testFloatLiteral("0.0;", 0.0);
|
||||
}
|
||||
|
||||
TEST(FloatTest, LiteralScientificNotationPositive)
|
||||
{
|
||||
testFloatLiteral("12300.0;", 12300.0);
|
||||
}
|
||||
|
||||
TEST(FloatTest, LiteralScientificNotationNegative)
|
||||
{
|
||||
testFloatLiteral("-0.00567;", -0.00567);
|
||||
}
|
||||
54
tests/integer_literal_tests.cpp
Normal file
54
tests/integer_literal_tests.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#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);
|
||||
}
|
||||
@ -1,63 +0,0 @@
|
||||
#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);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user