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"
#include "p2-parser.h"

Go to the source code of this file.

Macros

#define TEST_VALID(NAME, TEXT)
 Define a test case with a valid program. More...
 
#define TEST_INVALID(NAME, TEXT)
 Define a test case with an invalid program. More...
 
#define TEST_VALID_MAIN(NAME, TEXT)
 Define a test case with a valid main function. More...
 
#define TEST_INVALID_MAIN(NAME, TEXT)
 Define a test case with an invalid main function. More...
 
#define TEST_VALID_EXPR(NAME, TEXT)
 Define a test case with a valid main function returning an expression. More...
 
#define TEST_INVALID_EXPR(NAME, TEXT)
 Define a test case with an invalid main function returning an expression. More...
 
#define TEST_INT_LITERAL(NAME, TEXT, VALUE)
 Define a test case for an integer literal. More...
 
#define TEST_STR_LITERAL(NAME, TEXT, VALUE)
 Define a test case for a string literal. More...
 
#define TEST(NAME)   tcase_add_test (tc, NAME)
 Add a test to the test suite.
 

Functions

ASTNoderun_parser (char *text)
 Run lexer and parser on given text. More...
 
bool invalid_program (char *text)
 Run lexer and parser on given text and verify that it throws an exception. More...
 
bool valid_program (char *text)
 Run lexer and parser on given text and verify that it returns an AST and does not throw an exception. More...
 

Detailed Description

Testing utility functions.

Macro Definition Documentation

◆ TEST_INT_LITERAL

#define TEST_INT_LITERAL (   NAME,
  TEXT,
  VALUE 
)
Value:
START_TEST (NAME) \
{ ASTNode* p = run_parser("def int main() { return " TEXT " ; }"); \
int value = p->program.functions->head->funcdecl.body->block.statements->head->funcreturn.value->literal.integer; \
ck_assert_int_eq(value, VALUE); } \
END_TEST

Define a test case for an integer literal.

◆ TEST_INVALID

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

Define a test case with an invalid program.

◆ TEST_INVALID_EXPR

#define TEST_INVALID_EXPR (   NAME,
  TEXT 
)
Value:
START_TEST (NAME) \
{ ck_assert (invalid_program("def int main () { return " TEXT " ; }")); } \
END_TEST

Define a test case with an invalid main function returning an expression.

◆ TEST_INVALID_MAIN

#define TEST_INVALID_MAIN (   NAME,
  TEXT 
)
Value:
START_TEST (NAME) \
{ ck_assert (invalid_program("def int main () { " TEXT " }")); } \
END_TEST

Define a test case with an invalid main function.

◆ TEST_STR_LITERAL

#define TEST_STR_LITERAL (   NAME,
  TEXT,
  VALUE 
)
Value:
START_TEST (NAME) \
{ ASTNode* p = run_parser("def int main() { return " TEXT " ; }"); \
char* value = p->program.functions->head->funcdecl.body->block.statements->head->funcreturn.value->literal.string; \
ck_assert_str_eq(value, VALUE); } \
END_TEST

Define a test case for a string literal.

◆ TEST_VALID

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

Define a test case with a valid program.

◆ TEST_VALID_EXPR

#define TEST_VALID_EXPR (   NAME,
  TEXT 
)
Value:
START_TEST (NAME) \
{ ck_assert (valid_program("def int main () { return " TEXT " ; }")); } \
END_TEST

Define a test case with a valid main function returning an expression.

◆ TEST_VALID_MAIN

#define TEST_VALID_MAIN (   NAME,
  TEXT 
)
Value:
START_TEST (NAME) \
{ ck_assert (valid_program("def int main () { " TEXT " }")); } \
END_TEST

Define a test case with a valid main function.

Function Documentation

◆ invalid_program()

bool invalid_program ( char *  text)

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

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

◆ run_parser()

ASTNode* run_parser ( char *  text)

Run lexer and parser on given text.

The difference between this and the main parse() function is that this version catches exceptions and returns NULL instead of propogating the exception.

Parameters
textCode to lex and parse
Returns
AST or NULL if there was an error

◆ valid_program()

bool valid_program ( char *  text)

Run lexer and parser on given text and verify that it returns an AST and does not throw an exception.

Parameters
textCode to lex and parse
Returns
True if and only if the text was lexed and parsed successfully
ASTNode
Main AST node structure.
Definition: ast.h:465
FuncDeclNode::body
struct ASTNode * body
Function body block.
Definition: ast.h:140
LiteralNode::string
char string[256]
String value (if type is STR)
Definition: ast.h:383
NodeList::head
struct ASTNode * head
First element in list (or NULL if empty)
Definition: ast.h:493
LiteralNode::integer
int integer
Integer value (if type is INT)
Definition: ast.h:381
BlockNode::statements
struct NodeList * statements
List of statements in the block.
Definition: ast.h:161
ReturnNode::value
struct ASTNode * value
Return value (can be NULL for void returns)
Definition: ast.h:239
ProgramNode::functions
struct NodeList * functions
List of function declarations.
Definition: ast.h:74
valid_program
bool valid_program(char *text)
Run lexer and parser on given text and verify that it returns an AST and does not throw an exception.
Definition: testsuite.c:21
run_parser
ASTNode * run_parser(char *text)
Run lexer and parser on given text.
Definition: testsuite.c:10
invalid_program
bool invalid_program(char *text)
Run lexer and parser on given text and verify that it throws an exception.
Definition: testsuite.c:26