Skip to content

RALF Language Unit Tests

lunkpeter edited this page Jun 30, 2015 · 7 revisions

#RALF Language Unit Tests

At this point the following two main test case categories are defined:

  • Validator test cases: These unit tests check if the validator component properly detects invalid code segments.
  • Parser test cases: These unit tests check is the parser creates the correct AST, which represents the input code. Typically both test case categories are aiming at covering all the possible input combinations of the expression or statement under test.

Validator Test Cases

Expression test cases

In the following section, the test cases covering the basic expressions will be presented in order of precedence.

Literal Expression Validator Tests

The following primitive literals types are defined in rALF: Integer, String, Real, Boolean. These test cases check if these literals can be properly defined. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
naturalLiteralExpressionDecimal() 123; Checks if a well-formed decimal integer literal can be defined OK
naturalLiteralExpressionBinary() 0b010101010101; Checks if a well-formed binary integer literal can be defined OK
stringLiteralExpressionBreak() "AB\\C"; Checks if a well-formed String literal with breaked \ can be defined OK
...

Parentheses, Numeric- and Boolean Unary Expression Validator Tests

The following test cases check if parentheses and unary operators are properly validated. These test cases can be found here

Parentheses

The test cases regarding parentheses check that parentheses can indeed contain other expressions, including themselves.

Some relevant example test cases:

Test case name Input code Description Expected output
parenthesisInteger() (1); Checks if an integer literal can be written in parentheses OK
parenthesisConditionalTest() (true ? "test" : "test2"); Checks if a conditional test expression can be written in parentheses OK
...

Boolean Unary Not

These test cases check tat boolean not expressions can only contain expressions with a boolean return value.

Some relevant example test cases:

Test case name Input code Description Expected output
unaryBooleanParenthesis() !(true); Checks if parentheses containing a boolean literal can be negated OK
unaryBooleanString() !"String"; Checks invalid string typed parameter is detected ERROR
unaryBooleanNameInvalid() String x = "1"; !x; Checks if invalid typed variables are detected ERROR
...

Numeric Unary

The test cases regarding numeric negate expressions check that only numeric values can be negated. Some test cases can be seen below as an example.

Some relevant example test cases:

Test case name Input code Description Expected output
unaryNumericInteger() -1; Checks if integer values can be negated OK
unaryNumericStringVariable() String x = " "; -x; Checks if trying to negate a string variable indeed results in a validation error ERROR
...

Affix Increment and Decrement Expression Validator Tests

The following test cases check if affix increment and decrement operators are properly validated. It should be noted that affix operators follow the C/C++/Java semantics, meaning that they can only contain variable expressions. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
affixIncrementIntegerVariable() Integer x = 1; ++x; Checks if integer variables can be incremented. OK
affixIncrementInvalidTypeVariable() String x = "1"; ++x; Checks if trying to increase the value of a string variable indeed results in a validation error ERROR
affixIncrementIntegerLiteral() ++1; Checks if trying to increase the value of an integer literal indeed results in a validation error ERROR
...

Postfix Increment and Decrement Expression Validator Tests

The following test cases check if postfix increment and decrement operators are properly validated. It should be noted that postfix operators follow the C/C++/Java semantics, meaning that they can only contain variable expressions. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
postfixIncrementIntegerVariable() Integer x = 1; x++; Checks if integer variables can be incremented. OK
postfixIncrementInvalidTypeVariable() String x = "1"; x++; Checks if trying to increase the value of a string variable indeed results in a validation error ERROR
postfixIncrementIntegerLiteral() 1++; Checks if trying to increase the value of an integer literal indeed results in a validation error ERROR
...

Additive Arithmetical Expression Validator Tests

The following test cases check if additive arithmetical (+,-) operators are properly validated. It should be noted that the + operator can be used to concatenate string variables and literals. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
additionExpressionInteger() 1+2; Checks if integer literals can serve as operands of the addition expression. OK
additionExpressionString() "1"+"2"; Checks if string literals can be concatenated. OK
additionExpressionString() "1"-"2"; Checks that string literals and variables cannot be divided. ERROR
...

Multiplicative Arithmetical Expression Validator Tests

The following test cases check if multiplicative arithmetical (*,/,%) operators are properly validated. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
multiplicationExpressionInteger() 1*2; Checks if integer literals can serve as operands of the multiplication expression. OK
multiplicationExpressionString() "1"*"2"; Checks that string literals cannot be multiplied. ERROR
moduloExpressionReal() 1.3 % 2.3; Checks that real literals cannot serve as input for modulo expressions. ERROR
...

Shift Expression Validator Tests

The following test cases check if shift (>>,<<,>>>,<<<) operators are properly validated. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
shiftExpressionInteger() 1>>2; Checks if integer literals can serve as operands of the shift expression. OK
shiftExpressionReal() 1.3>>2.3; Checks that real values cannot be shifted. ERROR
shiftExpressionMultiplicativeIntegerDivision() 1 >> 1/2; Checks that results of integer division can be shifted, hence they are integer values. ERROR
...

Relational Expression Validator Tests

The following test cases check if relational (>,<,>=,<=) operators are properly validated. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
relationalExpressionInteger() 1 > 2; Checks if integer literals can be compared. OK
relationalExpressionNumericUnary() 1 > -2; Checks that numeric negation expressions can serve ans an operand of the relational expression. OK
relationalExpressionIntegerString() 1 > "2"; Checks that only numeric values can be compared with relational operators. ERROR
...

Equality Expression Validator Tests

The following test cases check if the equality (==) operator is properly validated. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
equalityExpressionInteger() 1 == 2; Checks if integer literals can be compared. OK
equalityExpressionBoolean() true == false; Checks if boolean literals can be compared. OK
equalityExpressionIntegerString() 1 == "2"; Checks that only values of the same type can be compared with equality operators. ERROR
...

Conditional And, Or and Conditional Test Expression Validator Tests

The following test cases check if logical conditional (||,&&) operators and conditional test expressions are properly validated. These test cases can be found here

Conditional And / Or

Some relevant example test cases:

Test case name Input code Description Expected output
andExpressionBoolean() true && false; Checks if an boolean literal can serve as operands of the conditional expressions. OK
andExpressionNumericUnary() true && -2; Checks that only boolean values can be operands. ERROR
andExpressionRelational() true && 2>3; Checks that relational expressions can be operands as well. OK
...

Conditional test expression

Some relevant example test cases:

Test case name Input code Description Expected output
condTestExpressionReal() (true) ? 1.1 : 2.1; Checks if conditional test expressions can contain real values as non-conditional operands. OK
condTestExpressionConditionString() ("1") ? 1 : 2; Checks that conditional test expressions can only contain boolean values as conditional operands. ERROR
...

Assignment Expression Validator Tests

The following test cases check if the assignment (=) operator is properly validated. These test cases can be found here

Some relevant example test cases:

Test case name Input code Description Expected output
assignmentExpressionLiteral() 1 = 2; Checks that literals cannot be left hand side operators of an assignment. Error
assignmentExpressionInteger() Integer x = 1; x=2; Checks that variables can be. OK
assignmentExpressionVariableInvalidType() String x = "1"; Integer y = 2; y=x; Checks that the two sides of the assignment operator must be of the same type. ERROR
...

Statement test cases

Simple Statement Validator Tests

In these test cases, the validation of empty-, expression-, break- and return statements are tested. These test cases can be found here

Break Statement

Test case name Input code Description Expected output
breakStatementInvalid() break; Break statements are only applicable in switch and different loop statements. Error
breakStatementValidInSwitch() ... Checks that break statements are valid in switch cases. OK
breakStatementValidInWhile() while(true){break;} Checks that break statements are valid in while loops. OK
...

Local Variable Declaration Statement Validator Tests

In these test cases, the validation of local variable declaration statements are tested. These test cases can be found here

Test case name Input code Description Expected output
localVariableSimple() Integer x = 1; Tests simple variable declarations. OK
localVariableDifferentType() Integer y = "String"; Checks that the initial value of a variable must be of the same type. ERROR
localVariableSameName() Integer x = 1; Integer x = 1; Two local variables with the same name, in the same block is illegal. ERROR
localVariableSameNameAfterBlock() {String z = "1";} String z = "2"; If the first definition is in an inner block, it is OK. OK
localVariableSameNameBeforeBlock() String z = "2"; {String z = "1";} However, an existing variable cannot be overridden in a block. ERROR
...

Do/While/For Loop Statement Validator Tests

In these test cases, the validation of different loop statements are tested.

Test case name Input code Description Expected output
forStatementSyntaxA_Literal() for(i : 5){} Tests that the ":" variant of the for cycle is properly validated OK
forStatementSyntaxB_Literal() for(i in 1 .. 5){} Tests that the "in" variant of the for cycle is properly validated OK
forStatementSyntaxA_LiteralReal() for(i : 1.1){} Only integer values can serve as iteration limits. ERROR
whileStatementNaturalLiteral() while(1){} Only boolean values can serve as while loop conditions OK
...

If Statement Validator Tests

In these test cases, the validation of if statements are tested. These test cases can be found here

Test case name Input code Description Expected output
ifStatementNaturalLiteral() if(1){} If statement condition should be boolean values. ERROR
ifStatementBooleanUnary() if(!true){} Boolean not expressions can serve as conditional operators. OK
...

Switch Statement Validator Tests

In these test cases, the validation of switch statements are tested. These test cases can be found here

Test case name Input code Description Expected output
switchStatementNaturalLiteralNoBreak() switch(1){case 1 : {}default : {}}; Break statements in switch cases are optional. OK
switchStatementRealLiteral() switch(1.1){case 1.1 : {}default : {}}; Real values are invalid switch parameters. ERROR
...

Parser Test Cases

The parser will be tested, once the language itself has reached a solid state. At such an early point in the development process as this, test cases, which check the contents of the parsed AST, would be immensely hard to maintain.

Clone this wiki locally