From 35be166f8e860d9c08cec049ed673048e1926899 Mon Sep 17 00:00:00 2001 From: Benoy Bose Date: Fri, 29 Nov 2024 19:04:41 +0530 Subject: [PATCH] Added floating point tests --- .vscode/launch.json | 28 +++++++++++- CMakeLists.txt | 3 +- HooLexer.g4 | 4 +- src/Visitor.cpp | 10 ++--- tests/CMakeLists.txt | 10 +++-- tests/floatingpoint_literal_tests.cpp | 53 ++++++++++++++++++++++ tests/integer_literal_tests.cpp | 54 +++++++++++++++++++++++ tests/literal_tests.cpp | 63 --------------------------- 8 files changed, 150 insertions(+), 75 deletions(-) create mode 100644 tests/floatingpoint_literal_tests.cpp create mode 100644 tests/integer_literal_tests.cpp delete mode 100644 tests/literal_tests.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index a7c5817..bf5c415 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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}", diff --git a/CMakeLists.txt b/CMakeLists.txt index 4263dc9..0b14218 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) + diff --git a/HooLexer.g4 b/HooLexer.g4 index 15f92fa..e91b2a5 100644 --- a/HooLexer.g4 +++ b/HooLexer.g4 @@ -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'; diff --git a/src/Visitor.cpp b/src/Visitor.cpp index 8bbb64d..9f097fe 100644 --- a/src/Visitor.cpp +++ b/src/Visitor.cpp @@ -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(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1cd5d77..310f9c3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/floatingpoint_literal_tests.cpp b/tests/floatingpoint_literal_tests.cpp new file mode 100644 index 0000000..b171daf --- /dev/null +++ b/tests/floatingpoint_literal_tests.cpp @@ -0,0 +1,53 @@ +#include "Compiler.hpp" +#include "Node.hpp" +#include "llvm/IR/Constants.h" +#include + +// Utility function to test floating-point literal compilation +void testFloatLiteral(const std::string &input, double expectedValue) +{ + auto compiler = std::make_unique(input, "main"); + auto result = compiler->compile(); + auto floatNode = std::any_cast(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(value); + auto expected_value_constant = llvm::dyn_cast(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); +} diff --git a/tests/integer_literal_tests.cpp b/tests/integer_literal_tests.cpp new file mode 100644 index 0000000..81cf58a --- /dev/null +++ b/tests/integer_literal_tests.cpp @@ -0,0 +1,54 @@ +#include "Compiler.hpp" +#include "Node.hpp" +#include "llvm/IR/Constants.h" + +#include + +#include "Compiler.hpp" +#include "Node.hpp" +#include "llvm/IR/Constants.h" +#include + +// Utility function to test integer literal compilation +void testIntegerLiteral(const std::string &input, int64_t expectedValue) +{ + auto compiler = std::make_unique(input, "main"); + auto result = compiler->compile(); + auto integer = std::any_cast(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(value); + auto expected_value_constant = llvm::dyn_cast(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); +} diff --git a/tests/literal_tests.cpp b/tests/literal_tests.cpp deleted file mode 100644 index 8ace715..0000000 --- a/tests/literal_tests.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "Compiler.hpp" -#include "Node.hpp" -#include "llvm/IR/Constants.h" - -#include - -TEST(IntegerTest, LiteralOne) -{ - auto compiler = std::make_unique("1;", "main"); - auto result = compiler->compile(); - auto integer = std::any_cast(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("67890;", "main"); - auto result = compiler->compile(); - auto integer = std::any_cast(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("+67890;", "main"); - auto result = compiler->compile(); - auto integer = std::any_cast(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("-67890;", "main"); - auto result = compiler->compile(); - auto integer = std::any_cast(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(value); - auto expected_value_constant = llvm::dyn_cast(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); -}