MalachScript/tests/LexerTests/NumericalLexTests.cpp

72 lines
3.5 KiB
C++
Raw Normal View History

#include "../../extern/doctest.hpp"
#include "../../src/Parser/Lexer/Lexer.hpp"
2020-10-05 15:45:00 +00:00
using namespace MalachScript::Parser;
#define INTEGER_TEST(script, expected) \
TEST_CASE("Lex " script) { \
2021-01-01 22:31:30 +00:00
MalachScript::Diagnostics::Logger diag; \
auto lexer = Lexer(u8##script, u8##script, &diag); \
const auto* token = lexer.Lex(); \
2020-10-04 17:38:13 +00:00
CHECK(diag.GetMessages().empty()); \
2020-10-04 15:15:28 +00:00
REQUIRE(token->GetKind() == LexTokenKind::IntegerLiteral); \
auto value = ((const IntegerLiteral*)token)->GetValue(); \
CHECK(value == (expected)); \
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
}
2020-10-04 15:15:28 +00:00
#define FLOAT_TEST(script, expected) \
TEST_CASE("Lex " script) { \
2021-01-01 22:31:30 +00:00
MalachScript::Diagnostics::Logger diag; \
auto lexer = Lexer(u8##script, u8##script, &diag); \
const auto* token = lexer.Lex(); \
2020-10-04 17:38:13 +00:00
CHECK(diag.GetMessages().empty()); \
2020-10-04 15:15:28 +00:00
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.456L);
FLOAT_TEST("0.456", 0.456L);
FLOAT_TEST("0.456e12", 0.456e12L);
FLOAT_TEST("0.456E12", 0.456E12L);
// 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);
2020-10-04 15:15:28 +00:00
INTEGER_TEST("0b110011", 51);
INTEGER_TEST("0B110011", 51);
2020-10-04 15:15:28 +00:00
#undef INTEGER_TEST
#undef FLOAT_TEST