MalachScript/tests/LexerTests/NumericalLexTests.cpp

85 lines
4.1 KiB
C++

#include "../../extern/doctest.hpp"
#include "../../src/Parser/Lexer/Lexer.hpp"
using namespace MalachScript::Parser;
#define INTEGER_TEST(script, expected) \
TEST_CASE("Lex " script) { \
MalachScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(u8##script, u8##script, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
REQUIRE(token->GetKind() == LexTokenKind::IntegerLiteral); \
auto value = ((const IntegerLiteral*)token)->GetValue(); \
CHECK(value == (expected)); \
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
}
#define FLOAT_TEST(script, expected) \
TEST_CASE("Lex " script) { \
MalachScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(u8##script, u8##script, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
REQUIRE(token->GetKind() == LexTokenKind::FloatLiteral); \
auto value = ((const FloatLiteral*)token)->GetValue(); \
CHECK(value == (expected)); \
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
}
// Decimal lexing
INTEGER_TEST("123456", 123456);
INTEGER_TEST("0d123456", 123456);
INTEGER_TEST("0D123456", 123456);
INTEGER_TEST("50000000000", 50000000000);
// Decimal float lexing
FLOAT_TEST("123.456", 123.456);
FLOAT_TEST("0.456", 0.456);
FLOAT_TEST("0.456e12", 0.456e12);
FLOAT_TEST("0.456E12", 0.456E12);
// Hexadecimal lexing
INTEGER_TEST("0x0", 0);
INTEGER_TEST("0xF", 15);
INTEGER_TEST("0xf", 15);
INTEGER_TEST("0xFF", 255);
INTEGER_TEST("0xfF", 255);
INTEGER_TEST("0xFFF", 4095);
INTEGER_TEST("0xFFFF", 65535);
INTEGER_TEST("0xFFFFF", 1048575);
INTEGER_TEST("0xFFFFFF", 16777215);
INTEGER_TEST("0XFFFFFF", 16777215);
// Octal lexing
INTEGER_TEST("0o0", 0);
INTEGER_TEST("0o7", 7);
INTEGER_TEST("0o77", 63);
INTEGER_TEST("0o777", 511);
INTEGER_TEST("0o7777", 4095);
INTEGER_TEST("0O7777", 4095);
// Binary lexing
INTEGER_TEST("0b0", 0);
INTEGER_TEST("0b1", 1);
INTEGER_TEST("0b11", 3);
INTEGER_TEST("0b111", 7);
INTEGER_TEST("0b1111", 15);
INTEGER_TEST("0b110011", 51);
INTEGER_TEST("0B110011", 51);
#undef INTEGER_TEST
#undef FLOAT_TEST
TEST_CASE("Lex invalid numerical base") {
MalachScript::Diagnostics::Diagnostics diag;
auto lexer = Lexer(u8"bad base", u8"0f553", &diag);
lexer.Lex();
const auto& messages = diag.GetMessages();
REQUIRE(messages.size() == 1);
CHECK(messages[0].GetType() == MalachScript::Diagnostics::DiagnosticType::InvalidNumericalBase);
CHECK(messages[0].GetLevel() == MalachScript::Diagnostics::DiagnosticLevel::Error);
CHECK(messages[0].GetSpan() == MalachScript::TextSpan(0, 2));
CHECK(messages[0].GetScriptName() == u8"bad base");
}