Testing utility functions.
More...
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include <check.h>
#include "p1-lexer.h"
Go to the source code of this file.
|
#define | TEST_INVALID(NAME, TEXT) |
| Define a test case with text containing an invalid token. More...
|
|
#define | TEST_0TOKENS(NAME, TEXT) |
| Define a test with a no tokens. More...
|
|
#define | TEST_1TOKEN(NAME, TEXT, ETYPE, ETEXT) |
| Define a test with a single valid token. More...
|
|
#define | TEST_2TOKENS(NAME, TEXT, ETYPE1, ETEXT1, ETYPE2, ETEXT2) |
| Define a test with two valid tokens. More...
|
|
#define | TEST_TOKENS(NAME, TEXT, NTOKENS, ETOKENS) |
| Define a text with an arbitrary number of tokens. More...
|
|
#define | TEST(NAME) tcase_add_test (tc, NAME) |
| Add a test to the test suite.
|
|
|
TokenQueue * | run_lexer (char *text) |
| Run lexer on given text. More...
|
|
TokenQueue * | run_lexer_check_size (char *text, size_t expected_length) |
| Run lexer on given text and check length of resulting token queue. More...
|
|
bool | invalid_tokens (char *text) |
| Run lexer on given text and verify that it throws an exception. More...
|
|
bool | valid_0tokens (char *text) |
| Run lexer on given text and verify that it returns no tokens (but also does not throw an exception). More...
|
|
bool | valid_1token (char *text, TokenType expected_type, const char *expected_text) |
| Run lexer on given text and verify that it returns a single correct token. More...
|
|
bool | valid_2tokens (char *text, TokenType expected_type1, const char *expected_text1, TokenType expected_type2, const char *expected_text2) |
| Run lexer on given text and verify that it returns two correct tokens. More...
|
|
bool | valid_tokens (char *text, size_t ntokens, Token expected_tokens[]) |
| Run lexer on given text and verify that it returns tokens of the appropriate type. More...
|
|
Testing utility functions.
◆ TEST_0TOKENS
#define TEST_0TOKENS |
( |
|
NAME, |
|
|
|
TEXT |
|
) |
| |
Value:START_TEST (NAME) \
END_TEST
Define a test with a no tokens.
◆ TEST_1TOKEN
#define TEST_1TOKEN |
( |
|
NAME, |
|
|
|
TEXT, |
|
|
|
ETYPE, |
|
|
|
ETEXT |
|
) |
| |
Value:START_TEST (NAME) \
END_TEST
Define a test with a single valid token.
◆ TEST_2TOKENS
#define TEST_2TOKENS |
( |
|
NAME, |
|
|
|
TEXT, |
|
|
|
ETYPE1, |
|
|
|
ETEXT1, |
|
|
|
ETYPE2, |
|
|
|
ETEXT2 |
|
) |
| |
Value:START_TEST (NAME) \
{ ck_assert (
valid_2tokens(TEXT, ETYPE1, ETEXT1, ETYPE2, ETEXT2)); } \
END_TEST
Define a test with two valid tokens.
◆ TEST_INVALID
#define TEST_INVALID |
( |
|
NAME, |
|
|
|
TEXT |
|
) |
| |
Value:START_TEST (NAME) \
END_TEST
Define a test case with text containing an invalid token.
◆ TEST_TOKENS
#define TEST_TOKENS |
( |
|
NAME, |
|
|
|
TEXT, |
|
|
|
NTOKENS, |
|
|
|
ETOKENS |
|
) |
| |
Value:START_TEST (NAME) \
END_TEST
Define a text with an arbitrary number of tokens.
◆ invalid_tokens()
bool invalid_tokens |
( |
char * |
text | ) |
|
Run lexer on given text and verify that it throws an exception.
- Parameters
-
- Returns
- True if and only if the lexer threw an exception
◆ run_lexer()
Run lexer on given text.
The difference between this and the main lex() function is that this version catches exceptions and returns NULL
instead of propogating the exception.
- Parameters
-
- Returns
- Queue of tokens or
NULL
if there was a lexing error
◆ run_lexer_check_size()
TokenQueue* run_lexer_check_size |
( |
char * |
text, |
|
|
size_t |
expected_length |
|
) |
| |
Run lexer on given text and check length of resulting token queue.
This function is identical to run_lexer() except that it also returns NULL
if the text was successfully lexed but the resulting queue is not of the expected length.
- Parameters
-
text | Code to lex |
expected_length | Expected token queue length |
- Returns
- Queue of tokens or
NULL
if there was a lexing error or the resulting queue's length doesn't match expected_length
◆ valid_0tokens()
bool valid_0tokens |
( |
char * |
text | ) |
|
Run lexer on given text and verify that it returns no tokens (but also does not throw an exception).
- Parameters
-
- Returns
- True if and only if the text was lexed successfully as no tokens
◆ valid_1token()
bool valid_1token |
( |
char * |
text, |
|
|
TokenType |
expected_type, |
|
|
const char * |
expected_text |
|
) |
| |
Run lexer on given text and verify that it returns a single correct token.
- Parameters
-
text | Code to lex |
expected_type | Expected type of the token |
expected_text | Expected text of the token |
- Returns
- True if and only if the token was lexed successfully as the expected type
◆ valid_2tokens()
bool valid_2tokens |
( |
char * |
text, |
|
|
TokenType |
expected_type1, |
|
|
const char * |
expected_text1, |
|
|
TokenType |
expected_type2, |
|
|
const char * |
expected_text2 |
|
) |
| |
Run lexer on given text and verify that it returns two correct tokens.
- Parameters
-
text | Code to lex |
expected_type1 | Expected type of the first token |
expected_text1 | Expected text of the first token |
expected_type2 | Expected type of the second token |
expected_text2 | Expected text of the second token |
- Returns
- True if and only if the tokens were lexed successfully as the expected types
◆ valid_tokens()
bool valid_tokens |
( |
char * |
text, |
|
|
size_t |
ntokens, |
|
|
Token |
expected_tokens[] |
|
) |
| |
Run lexer on given text and verify that it returns tokens of the appropriate type.
- Parameters
-
text | Code to lex |
ntokens | Expected number of tokens |
expected_tokens | Array of expected token info (type and text) |
- Returns
- True if and only if the tokens were lexed successfully as the expected types
bool valid_tokens(char *text, size_t ntokens, Token expected_tokens[])
Run lexer on given text and verify that it returns tokens of the appropriate type.
Definition: testsuite.c:81
bool valid_1token(char *text, TokenType expected_type, const char *expected_text)
Run lexer on given text and verify that it returns a single correct token.
Definition: testsuite.c:50
bool valid_2tokens(char *text, TokenType expected_type1, const char *expected_text1, TokenType expected_type2, const char *expected_text2)
Run lexer on given text and verify that it returns two correct tokens.
Definition: testsuite.c:63
bool valid_0tokens(char *text)
Run lexer on given text and verify that it returns no tokens (but also does not throw an exception).
Definition: testsuite.c:41
bool invalid_tokens(char *text)
Run lexer on given text and verify that it throws an exception.
Definition: testsuite.c:36