The NAVFoundation.Assert library provides a comprehensive set of assertion functions for testing, debugging, and verifying code behavior in AMX NetLinx applications. These functions help ensure that expected conditions are met during development and testing phases, making it easier to identify and fix issues.
The library is particularly useful for:
- Unit testing NetLinx code
- Verifying function behavior during development
- Debugging complex applications
- Ensuring correct implementation of algorithms and business logic
The Assert library offers a wide range of assertion functions for different data types and comparison operations:
define_function char NAVAssertCharEqual(char testName[], char expected, char actual)Description: Tests if two char values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected char valueactual- Actual char value
Returns: true if equal, false otherwise
define_function char NAVAssertWideCharEqual(char testName[], widechar expected, widechar actual)Description: Tests if two widechar values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected widechar valueactual- Actual widechar value
Returns: true if equal, false otherwise
define_function char NAVAssertIntegerEqual(char testName[], integer expected, integer actual)Description: Tests if two integer values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected integer valueactual- Actual integer value
Returns: true if equal, false otherwise
define_function char NAVAssertSignedIntegerEqual(char testName[], sinteger expected, sinteger actual)Description: Tests if two signed integer values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected signed integer valueactual- Actual signed integer value
Returns: true if equal, false otherwise
define_function char NAVAssertLongEqual(char testName[], long expected, long actual)Description: Tests if two long values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected long valueactual- Actual long value
Returns: true if equal, false otherwise
define_function char NAVAssertSignedLongEqual(char testName[], slong expected, slong actual)Description: Tests if two signed long values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected signed long valueactual- Actual signed long value
Returns: true if equal, false otherwise
define_function char NAVAssertFloatEqual(char testName[], float expected, float actual)Description: Tests if two float values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected float valueactual- Actual float value
Returns: true if equal, false otherwise
define_function char NAVAssertDoubleEqual(char testName[], double expected, double actual)Description: Tests if two double values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected double valueactual- Actual double value
Returns: true if equal, false otherwise
define_function char NAVAssertStringEqual(char testName[], char expected[], char actual[])Description: Tests if two string values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected stringactual- Actual string
Returns: true if equal, false otherwise
define_function char NAVAssertInt64Equal(char testName[], _NAVInt64 expected, _NAVInt64 actual)Description: Tests if two Int64 values are equal and logs the result.
Parameters:
testName- Name of the testexpected- Expected Int64 valueactual- Actual Int64 value
Returns: true if equal, false otherwise
define_function char NAVAssertStringNotEqual(char testName[], char expected[], char actual[])Description: Tests if two string values are not equal and logs the result.
Parameters:
testName- Name of the testexpected- Value that should not matchactual- Actual value
Returns: true if not equal (test passed), false otherwise
define_function char NAVAssertIntegerNotEqual(char testName[], integer expected, integer actual)Description: Tests if two integer values are not equal and logs the result.
Parameters:
testName- Name of the testexpected- Value that should not matchactual- Actual value
Returns: true if not equal (test passed), false otherwise
define_function char NAVAssertCharNotEqual(char testName[], char expected, char actual)Description: Tests if two char values are not equal and logs the result.
Parameters:
testName- Name of the testexpected- Value that should not matchactual- Actual value
Returns: true if not equal (test passed), false otherwise
define_function char NAVAssertWideCharNotEqual(char testName[], widechar expected, widechar actual)Description: Tests if two widechar values are not equal and logs the result.
Parameters:
testName- Name of the testexpected- Value that should not matchactual- Actual value
Returns: true if not equal (test passed), false otherwise
define_function char NAVAssertSignedIntegerNotEqual(char testName[], sinteger expected, sinteger actual)Description: Tests if two signed integer values are not equal and logs the result.
Parameters:
testName- Name of the testexpected- Value that should not matchactual- Actual value
Returns: true if not equal (test passed), false otherwise
define_function char NAVAssertFloatNotEqual(char testName[], float expected, float actual)Description: Tests if two float values are not equal and logs the result.
Parameters:
testName- Name of the testexpected- Value that should not matchactual- Actual value
Returns: true if not equal (test passed), false otherwise
define_function char NAVAssertInt64NotEqual(char testName[], _NAVInt64 expected, _NAVInt64 actual)Description: Tests if two Int64 values are not equal and logs the result.
Parameters:
testName- Name of the testexpected- Value that should not matchactual- Actual value
Returns: true if not equal (test passed), false otherwise
define_function char NAVAssertIntegerGreaterThan(char testName[], integer expected, integer actual)Description: Tests if actual integer value is greater than expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be greater thanactual- Actual value
Returns: true if actual > expected (test passed), false otherwise
define_function char NAVAssertIntegerLessThan(char testName[], integer expected, integer actual)Description: Tests if actual integer value is less than expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be less thanactual- Actual value
Returns: true if actual < expected (test passed), false otherwise
define_function char NAVAssertIntegerGreaterThanOrEqual(char testName[], integer expected, integer actual)Description: Tests if actual integer value is greater than or equal to expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be greater than or equal toactual- Actual value
Returns: true if actual >= expected (test passed), false otherwise
define_function char NAVAssertIntegerLessThanOrEqual(char testName[], integer expected, integer actual)Description: Tests if actual integer value is less than or equal to expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be less than or equal toactual- Actual value
Returns: true if actual <= expected (test passed), false otherwise
define_function char NAVAssertFloatGreaterThan(char testName[], float expected, float actual)Description: Tests if actual float value is greater than expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be greater thanactual- Actual value
Returns: true if actual > expected (test passed), false otherwise
define_function char NAVAssertFloatLessThan(char testName[], float expected, float actual)Description: Tests if actual float value is less than expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be less thanactual- Actual value
Returns: true if actual < expected (test passed), false otherwise
define_function char NAVAssertFloatGreaterThanOrEqual(char testName[], float expected, float actual)Description: Tests if actual float value is greater than or equal to expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be greater than or equal toactual- Actual value
Returns: true if actual >= expected (test passed), false otherwise
define_function char NAVAssertFloatLessThanOrEqual(char testName[], float expected, float actual)Description: Tests if actual float value is less than or equal to expected value and logs the result.
Parameters:
testName- Name of the testexpected- Value that actual should be less than or equal toactual- Actual value
Returns: true if actual <= expected (test passed), false otherwise
define_function char NAVAssertTrue(char testName[], char condition)Description: Tests if a condition is true.
Parameters:
testName- Name of the testcondition- Condition to test
Returns: true if condition is true, false otherwise
define_function char NAVAssertFalse(char testName[], char condition)Description: Tests if a condition is false.
Parameters:
testName- Name of the testcondition- Condition to test
Returns: true if condition is false, false otherwise
define_function char NAVAssertStringContains(char testName[], char searchString[], char stringToSearch[])Description: Tests if a string contains a substring.
Parameters:
testName- Name of the testsearchString- String to findstringToSearch- String to search in
Returns: true if stringToSearch contains searchString, false otherwise
define_function char NAVAssertStringStartsWith(char testName[], char prefix[], char str[])Description: Tests if a string starts with the specified prefix.
Parameters:
testName- Name of the testprefix- Expected prefixstr- String to test
Returns: true if str starts with prefix, false otherwise
define_function char NAVAssertStringEndsWith(char testName[], char suffix[], char str[])Description: Tests if a string ends with the specified suffix.
Parameters:
testName- Name of the testsuffix- Expected suffixstr- String to test
Returns: true if str ends with suffix, false otherwise
define_function char NAVAssertFloatAlmostEqual(char testName[], float expected, float actual, float epsilon)Description: Tests if two float values are almost equal within a given epsilon.
Parameters:
testName- Name of the testexpected- Expected valueactual- Actual valueepsilon- Maximum allowed difference
Returns: true if |expected-actual| <= epsilon, false otherwise
define_function char NAVAssertStringArrayEqual(char testName[], char expected[][], char actual[][])Description: Tests if two string arrays are equal (same length and all elements match).
Parameters:
testName- Name of the testexpected- Expected string arrayactual- Actual string array
Returns: true if arrays are equal, false otherwise
define_function char NAVAssertStringArrayNotEqual(char testName[], char expected[][], char actual[][])Description: Tests if two string arrays are not equal (different length or any elements differ).
Parameters:
testName- Name of the testexpected- Expected string arrayactual- Actual string array
Returns: true if arrays are not equal, false otherwise
define_function char NAVAssertStringArrayContains(char testName[], char searchString[], char array[][])Description: Tests if a string array contains a specific string.
Parameters:
testName- Name of the testsearchString- String to search forarray- String array to search in
Returns: true if array contains searchString, false otherwise
define_function char NAVAssertStringArrayNotContains(char testName[], char searchString[], char array[][])Description: Tests if a string array does not contain a specific string.
Parameters:
testName- Name of the testsearchString- String to search forarray- String array to search in
Returns: true if array does not contain searchString, false otherwise
define_function char NAVAssertStringArrayLengthEqual(char testName[], integer expectedLength, char array[][])Description: Tests if a string array has the expected length.
Parameters:
testName- Name of the testexpectedLength- Expected array lengtharray- String array to check
Returns: true if array length matches expectedLength, false otherwise
define_function char NAVAssertStringArrayLengthNotEqual(char testName[], integer expectedLength, char array[][])Description: Tests if a string array does not have the specified length.
Parameters:
testName- Name of the testexpectedLength- Length that array should not havearray- String array to check
Returns: true if array length does not match expectedLength, false otherwise
define_function char NAVAssertIntegerArrayEqual(char testName[], integer expected[], integer actual[])Description: Tests if two integer arrays are equal (same length and all elements match).
Parameters:
testName- Name of the testexpected- Expected integer arrayactual- Actual integer array
Returns: true if arrays are equal, false otherwise
define_function char NAVAssertIntegerArrayNotEqual(char testName[], integer expected[], integer actual[])Description: Tests if two integer arrays are not equal (different length or any elements differ).
Parameters:
testName- Name of the testexpected- Expected integer arrayactual- Actual integer array
Returns: true if arrays are not equal, false otherwise
define_function char NAVAssertIntegerArrayLengthEqual(char testName[], integer expectedLength, integer array[])Description: Tests if an integer array has the expected length.
Parameters:
testName- Name of the testexpectedLength- Expected array lengtharray- Integer array to check
Returns: true if array length matches expectedLength, false otherwise
define_function char NAVAssertIntegerArrayLengthNotEqual(char testName[], integer expectedLength, integer array[])Description: Tests if an integer array does not have the specified length.
Parameters:
testName- Name of the testexpectedLength- Length that array should not havearray- Integer array to check
Returns: true if array length does not match expectedLength, false otherwise
define_function char NAVAssertInteger2DArrayEqual(char testName[], integer expected[][], integer actual[][])Description: Tests if two 2D integer arrays are equal (same dimensions and all elements match).
Parameters:
testName- Name of the testexpected- Expected 2D integer arrayactual- Actual 2D integer array
Returns: true if arrays are equal, false otherwise
define_function char NAVAssertInteger2DArrayNotEqual(char testName[], integer expected[][], integer actual[][])Description: Tests if two 2D integer arrays are not equal (different dimensions or any elements differ).
Parameters:
testName- Name of the testexpected- Expected 2D integer arrayactual- Actual 2D integer array
Returns: true if arrays are not equal, false otherwise
define_function char NAVAssertInteger2DArrayDimensionsEqual(char testName[], integer expectedRows, integer expectedCols, integer array[][])Description: Tests if a 2D integer array has the expected dimensions.
Parameters:
testName- Name of the testexpectedRows- Expected number of rowsexpectedCols- Expected number of columnsarray- 2D integer array to check
Returns: true if array dimensions match expected values, false otherwise
define_function char NAVAssertInteger2DArrayDimensionsNotEqual(char testName[], integer expectedRows, integer expectedCols, integer array[][])Description: Tests if a 2D integer array does not have the specified dimensions.
Parameters:
testName- Name of the testexpectedRows- Row count that array should not haveexpectedCols- Column count that array should not havearray- 2D integer array to check
Returns: true if array dimensions do not match expected values, false otherwise
// Import the Assert module
#include 'NAVFoundation.Assert.axi'
// Example function to test
define_function integer Add(integer a, integer b)
{
return a + b
}
// Test function
define_function TestAddFunction()
{
stack_var integer result
// Test case 1: Basic addition
result = Add(2, 3)
NAVAssertIntegerEqual('Add function - positive integers', 5, result)
// Test case 2: Addition with zero
result = Add(10, 0)
NAVAssertIntegerEqual('Add function - zero as operand', 10, result)
// Test case 3: Addition with negative numbers
result = Add(-5, 3)
NAVAssertIntegerEqual('Add function - negative integer', -2, result)
}// Import the Assert module
#include 'NAVFoundation.Assert.axi'
// Example string function to test
define_function char[100] FormatName(char firstName[], char lastName[])
{
stack_var char formattedName[100]
if (length_array(firstName) == 0 && length_array(lastName) == 0)
{
return 'Unknown'
}
if (length_array(firstName) == 0)
{
return lastName
}
if (length_array(lastName) == 0)
{
return firstName
}
formattedName = "lastName, ', ', firstName"
return formattedName
}
// Test function
define_function TestFormatNameFunction()
{
stack_var char result[100]
// Test case 1: Normal case
result = FormatName('John', 'Doe')
NAVAssertStringEqual('FormatName - full name', 'Doe, John', result)
// Test case 2: Missing first name
result = FormatName('', 'Doe')
NAVAssertStringEqual('FormatName - missing first name', 'Doe', result)
// Test case 3: Missing last name
result = FormatName('John', '')
NAVAssertStringEqual('FormatName - missing last name', 'John', result)
// Test case 4: Both names missing
result = FormatName('', '')
NAVAssertStringEqual('FormatName - both names missing', 'Unknown', result)
// String pattern assertions
NAVAssertStringContains('FormatName - contains comma', ',', FormatName('John', 'Doe'))
NAVAssertStringStartsWith('FormatName - starts with last name', 'Doe', FormatName('John', 'Doe'))
}// Import the Assert module
#include 'NAVFoundation.Assert.axi'
// Example function to test
define_function float CalculateCircleArea(float radius)
{
stack_var float PI
PI = 3.14159
return PI * radius * radius
}
// Test function
define_function TestCircleAreaCalculation()
{
stack_var float result
stack_var float epsilon
// Small value for floating point comparison tolerance
epsilon = 0.0001
// Test case 1: Unit circle (r = 1)
result = CalculateCircleArea(1.0)
NAVAssertFloatAlmostEqual('Circle area - unit circle', 3.14159, result, epsilon)
// Test case 2: Circle with radius 2
result = CalculateCircleArea(2.0)
NAVAssertFloatAlmostEqual('Circle area - radius 2', 12.56636, result, epsilon)
// Test case 3: Zero radius
result = CalculateCircleArea(0.0)
NAVAssertFloatEqual('Circle area - zero radius', 0.0, result)
// Test case 4: Comparison tests
result = CalculateCircleArea(5.0)
NAVAssertFloatGreaterThan('Circle area - larger than 50', 50.0, result)
NAVAssertFloatLessThan('Circle area - smaller than 100', 100.0, result)
}// Import the Assert module
#include 'NAVFoundation.Assert.axi'
// Run all tests
define_function RunAllTests()
{
stack_var integer passCount
stack_var integer totalTests
passCount = 0
totalTests = 0
// String tests
totalTests++
if (NAVAssertStringEqual('String equality test', 'Hello World', 'Hello World'))
{
passCount++
}
totalTests++
if (NAVAssertStringNotEqual('String inequality test', 'Hello', 'World'))
{
passCount++
}
totalTests++
if (NAVAssertStringContains('String contains test', 'World', 'Hello World'))
{
passCount++
}
totalTests++
if (NAVAssertStringStartsWith('String starts with test', 'Hello', 'Hello World'))
{
passCount++
}
totalTests++
if (NAVAssertStringEndsWith('String ends with test', 'World', 'Hello World'))
{
passCount++
}
// Integer tests
totalTests++
if (NAVAssertIntegerEqual('Integer equality test', 42, 42))
{
passCount++
}
totalTests++
if (NAVAssertIntegerNotEqual('Integer inequality test', 42, 43))
{
passCount++
}
totalTests++
if (NAVAssertIntegerGreaterThan('Integer greater than test', 10, 20))
{
passCount++
}
totalTests++
if (NAVAssertIntegerLessThan('Integer less than test', 30, 20))
{
passCount++
}
// Float tests
totalTests++
if (NAVAssertFloatAlmostEqual('Float almost equal test', 3.14159, 3.14158, 0.0001))
{
passCount++
}
// Boolean condition tests
totalTests++
if (NAVAssertTrue('Boolean true test', 1 == 1))
{
passCount++
}
totalTests++
if (NAVAssertFalse('Boolean false test', 1 == 2))
{
passCount++
}
// Print test summary
send_string 0, "'\n--- Test Summary ---'"
send_string 0, "'Tests passed: ', itoa(passCount), '/', itoa(totalTests)"
send_string 0, "'Success rate: ', ftoa((passCount * 100.0) / totalTests), '%'"
}- All assertion functions log test information using the NAVErrorLog system at NAV_LOG_LEVEL_DEBUG
- Test names are optional but recommended for clear identification of failing tests
- When an assertion fails, it logs both the expected and actual values
- The functions return a boolean value indicating success or failure, allowing them to be used in conditional logic
- For floating-point comparisons, consider using NAVAssertFloatAlmostEqual with a suitable epsilon value
- The assertion library does not abort execution on failure, allowing multiple tests to run in sequence
-
Use Descriptive Test Names: Include meaningful test names that clearly identify what is being tested and what the expected outcome is.
-
Test Edge Cases: Include tests for boundary conditions, zero values, empty strings, and other edge cases.
-
Group Related Tests: Organize tests for related functionality together in test suite functions.
-
Keep Tests Independent: Each test should be independent of others and not rely on state changes from previous tests.
-
Test Failure Conditions: Assert that functions fail appropriately when given invalid inputs.
-
Use Appropriate Assertion Types: Choose the most appropriate assertion type for each test (e.g., use NAVAssertFloatAlmostEqual for floating-point comparisons).
-
Run Tests Regularly: Integrate testing into your development workflow and run tests after code changes.
-
Track Test Coverage: Ensure that you have assertions that cover all critical paths in your code.
// Import the Assert module
#include 'NAVFoundation.Assert.axi'
// Test device control functions
define_function TestDeviceControlModule()
{
stack_var char commandString[100]
// Test command string generation
commandString = BuildDeviceCommand('POWER', 'ON')
NAVAssertStringEqual('Power on command', 'PWR1', commandString)
commandString = BuildDeviceCommand('VOLUME', '50')
NAVAssertStringEqual('Volume command', 'VOL50', commandString)
// Test command validation
NAVAssertTrue('Valid command check', IsValidCommand('PWR1'))
NAVAssertFalse('Invalid command check', IsValidCommand('XYZ123'))
// Test response parsing
NAVAssertStringEqual('Response parsing', 'ON', ParseDeviceResponse('PWR=ON'))
NAVAssertStringEqual('Error response parsing', 'ERROR', ParseDeviceResponse('ERR'))
}// Import the Assert module
#include 'NAVFoundation.Assert.axi'
// Test UI state logic
define_function TestUIStateLogic()
{
// Test button state calculations based on system state
NAVAssertIntegerEqual('Home button active', BUTTON_STATE_ACTIVE,
CalculateButtonState(BUTTON_HOME, SYSTEM_STATE_NORMAL))
NAVAssertIntegerEqual('Settings button disabled', BUTTON_STATE_DISABLED,
CalculateButtonState(BUTTON_SETTINGS, SYSTEM_STATE_LOCKED))
// Test visibility logic
NAVAssertTrue('Page visibility check - home page',
ShouldShowPage(PAGE_HOME, ACCESS_LEVEL_USER))
NAVAssertFalse('Page visibility check - admin page',
ShouldShowPage(PAGE_ADMIN, ACCESS_LEVEL_USER))
NAVAssertTrue('Page visibility check - admin access',
ShouldShowPage(PAGE_ADMIN, ACCESS_LEVEL_ADMIN))
}// Import the Assert module
#include 'NAVFoundation.Assert.axi'
// Test data processing functions
define_function TestDataProcessing()
{
stack_var char testJson[500]
stack_var char extractedValue[100]
stack_var integer arrayCount
// Test JSON parsing
testJson = '{"name":"Test Device","ip":"192.168.1.100","port":23}'
extractedValue = ExtractJsonValue(testJson, 'name')
NAVAssertStringEqual('JSON extract name', 'Test Device', extractedValue)
extractedValue = ExtractJsonValue(testJson, 'ip')
NAVAssertStringEqual('JSON extract IP', '192.168.1.100', extractedValue)
// Test array processing
arrayCount = CountArrayItems('apple,orange,banana', ',')
NAVAssertIntegerEqual('Array item count', 3, arrayCount)
extractedValue = GetArrayItem('apple,orange,banana', ',', 2)
NAVAssertStringEqual('Array item extraction', 'orange', extractedValue)
}- The assertion library relies on the NAVErrorLog system for output
- There's no automatic test discovery or runner; tests must be explicitly called
- Complex assertions requiring multiple conditions must be broken down into multiple assertion calls
- No built-in support for setup/teardown operations before and after tests
- Limited support for testing asynchronous operations; additional custom handling is required