Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance test_lexer.py with Tests for New Bitwise Operators and Data Types #66

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions tests/test_translator/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,129 @@ def test_function_call_tokenization():
tokenize_code(code)
except SyntaxError:
pytest.fail("Function call tokenization not implemented.")


# New tests for the additional tokens


def test_bitwise_operators_tokenization():
code = """
int a = 5 & 3;
int b = 5 | 3;
int c = 5 ^ 3;
int d = ~5;
int e = 5 << 1;
int f = 5 >> 1;
"""
tokens = tokenize_code(code)
expected_tokens = [
("INT", "int"),
("IDENTIFIER", "a"),
("EQUALS", "="),
("NUMBER", "5"),
("BITWISE_AND", "&"),
("NUMBER", "3"),
("SEMICOLON", ";"),
("INT", "int"),
("IDENTIFIER", "b"),
("EQUALS", "="),
("NUMBER", "5"),
("BITWISE_OR", "|"),
("NUMBER", "3"),
("SEMICOLON", ";"),
("INT", "int"),
("IDENTIFIER", "c"),
("EQUALS", "="),
("NUMBER", "5"),
("BITWISE_XOR", "^"),
("NUMBER", "3"),
("SEMICOLON", ";"),
("INT", "int"),
("IDENTIFIER", "d"),
("EQUALS", "="),
("BITWISE_NOT", "~"),
("NUMBER", "5"),
("SEMICOLON", ";"),
("INT", "int"),
("IDENTIFIER", "e"),
("EQUALS", "="),
("NUMBER", "5"),
("BITWISE_SHIFT_LEFT", "<<"),
("NUMBER", "1"),
("SEMICOLON", ";"),
("INT", "int"),
("IDENTIFIER", "f"),
("EQUALS", "="),
("NUMBER", "5"),
("BITWISE_SHIFT_RIGHT", ">>"),
("NUMBER", "1"),
("SEMICOLON", ";"),
]
assert tokens == expected_tokens


def test_assignment_operators_tokenization():
code = """
int a = 5;
a &= 3;
a |= 3;
a ^= 3;
a %= 3;
a <<= 1;
a >>= 1;
"""
tokens = tokenize_code(code)
expected_tokens = [
("INT", "int"),
("IDENTIFIER", "a"),
("EQUALS", "="),
("NUMBER", "5"),
("SEMICOLON", ";"),
("IDENTIFIER", "a"),
("ASSIGN_AND", "&="),
("NUMBER", "3"),
("SEMICOLON", ";"),
("IDENTIFIER", "a"),
("ASSIGN_OR", "|="),
("NUMBER", "3"),
("SEMICOLON", ";"),
("IDENTIFIER", "a"),
("ASSIGN_XOR", "^="),
("NUMBER", "3"),
("SEMICOLON", ";"),
("IDENTIFIER", "a"),
("ASSIGN_MOD", "%="),
("NUMBER", "3"),
("SEMICOLON", ";"),
("IDENTIFIER", "a"),
("ASSIGN_SHIFT_LEFT", "<<="),
("NUMBER", "1"),
("SEMICOLON", ";"),
("IDENTIFIER", "a"),
("ASSIGN_SHIFT_RIGHT", ">>="),
("NUMBER", "1"),
("SEMICOLON", ";"),
]
assert tokens == expected_tokens


def test_data_types_and_const_tokenization():
code = """
const double pi = 3.14159;
unsigned int maxVal = 255;
"""
tokens = tokenize_code(code)
expected_tokens = [
("CONST", "const"),
("DOUBLE", "double"),
("IDENTIFIER", "pi"),
("EQUALS", "="),
("FLOAT_NUMBER", "3.14159"),
("SEMICOLON", ";"),
("UNSIGNED_INT", "unsigned int"),
("IDENTIFIER", "maxVal"),
("EQUALS", "="),
("NUMBER", "255"),
("SEMICOLON", ";"),
]
assert tokens == expected_tokens
Loading