MalachScript/tests/LexerTests/NumericalLexTests.cpp

66 lines
3.2 KiB
C++
Raw Normal View History

#include "../../extern/doctest.hpp"
#include "../../src/Parser/Lexer/Lexer.hpp"
using namespace ElohimScript::Parser;
#define INTEGER_TEST(script, expected) \
TEST_CASE("Lex " script) { \
auto lexer = Lexer(script); \
const auto* token = lexer.Lex(); \
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); \
delete token; \
}
2020-10-04 15:15:28 +00:00
#define FLOAT_TEST(script, expected) \
TEST_CASE("Lex " script) { \
auto lexer = Lexer(script); \
const auto* token = lexer.Lex(); \
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); \
delete token; \
}
// Decimal lexing
INTEGER_TEST("123456", 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);
// Octal lexing
INTEGER_TEST("0o0", 0);
INTEGER_TEST("0o7", 7);
INTEGER_TEST("0o77", 63);
INTEGER_TEST("0o777", 511);
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);
#undef INTEGER_TEST
#undef FLOAT_TEST