Decaf Compiler
Macros | Functions
testsuite.h File Reference

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.

Macros

#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.
 

Functions

TokenQueuerun_lexer (char *text)
 Run lexer on given text. More...
 
TokenQueuerun_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...
 

Detailed Description

Testing utility functions.

Macro Definition Documentation

◆ TEST_0TOKENS

#define TEST_0TOKENS (   NAME,
  TEXT 
)
Value:
START_TEST (NAME) \
{ ck_assert (valid_0tokens(TEXT)); } \
END_TEST

Define a test with a no tokens.

◆ TEST_1TOKEN

#define TEST_1TOKEN (   NAME,
  TEXT,
  ETYPE,
  ETEXT 
)
Value:
START_TEST (NAME) \
{ ck_assert (valid_1token(TEXT, ETYPE, ETEXT)); } \
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) \
{ ck_assert (invalid_tokens(TEXT)); } \
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) \
{ ck_assert (valid_tokens(TEXT, NTOKENS, ETOKENS)); } \
END_TEST

Define a text with an arbitrary number of tokens.

Function Documentation

◆ invalid_tokens()

bool invalid_tokens ( char *  text)

Run lexer on given text and verify that it throws an exception.

Parameters
textCode to lex
Returns
True if and only if the lexer threw an exception

◆ run_lexer()

TokenQueue* run_lexer ( char *  text)

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
textCode to lex
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
textCode to lex
expected_lengthExpected 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
textCode to lex
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
textCode to lex
expected_typeExpected type of the token
expected_textExpected 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
textCode to lex
expected_type1Expected type of the first token
expected_text1Expected text of the first token
expected_type2Expected type of the second token
expected_text2Expected 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
textCode to lex
ntokensExpected number of tokens
expected_tokensArray of expected token info (type and text)
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.
Definition: testsuite.c:81
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.
Definition: testsuite.c:50
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.
Definition: testsuite.c:63
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).
Definition: testsuite.c:41
invalid_tokens
bool invalid_tokens(char *text)
Run lexer on given text and verify that it throws an exception.
Definition: testsuite.c:36