Inclusion unit tests

The unit tests for the functions have been added.

modified:   coupler/opc-ua-server/mod_io_i2c.h
        new file:   tests/unit_test/Makefile
        new file:   tests/unit_test/test_common.c
        new file:   tests/unit_test/test_modio_i2c.c
        new file:   tests/unit_test/unit_test.md

Test: Program is tested
parent bc0c17eb
......@@ -90,6 +90,12 @@ static int getDigitalInputState(int i2c_addr, char **digital_input)
*/
int file;
char filename[20];
if (I2C_VIRTUAL_MODE)
{
// we're in a virtual mode, likely on x86 platform or without I2C support
// simply do nothing
return 0;
}
// step 1: open device
file = open(I2C_BLOCK_DEVICE_NAME, O_RDWR);
......@@ -137,6 +143,12 @@ static int getAnalogInputStateAIN(int i2c_addr, int **analog_input, uint8_t read
*/
int file;
char filename[20];
if (I2C_VIRTUAL_MODE)
{
// we're in a virtual mode, likely on x86 platform or without I2C support
// simply do nothing
return 0;
}
// step 1: open device
file = open(I2C_BLOCK_DEVICE_NAME, O_RDWR);
......
CFLAGS= -Wall -Wno-missing-braces -ggdb `pkg-config --cflags criterion`
LDFLAGS= `pkg-config --libs criterion`
LIB_DIR=$(SRC_DIR)te
OUT_DIR=build/
all: test_common test_modio_i2c
test_common: test_common.o
@mkdir -p $(OUT_DIR)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
@mv $@ $(OUT_DIR)
test_modio_i2c: test_modio_i2c.o
@mkdir -p $(OUT_DIR)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
@mv $@ $(OUT_DIR)
run: all
@${OUT_DIR}/test_common --tap=${OUT_DIR}/test_common.tap
@${OUT_DIR}/test_modio_i2c --tap=${OUT_DIR}/test_modio_i2c.tap
clean:
@rm $(OUT_DIR)test_common 2>/dev/null || true
@rm $(OUT_DIR)test_common.tap 2>/dev/null || true
@rm $(OUT_DIR)test_modio_i2c 2>/dev/null || true
@rm $(OUT_DIR)test_modio_i2c.tap 2>/dev/null || true
@rm *.o 2>/dev/null || true
.PHONY: clean debug all
/* ================ Includes ===================== */
#include <criterion/criterion.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "../../coupler/opc-ua-server/common.h"
/* ================ Function Tests =============== */
// ############# get milliseconds since epoch ##############
Test(common, getMilliSecondsSinceEpoch) {
unsigned long int ms, retval;
struct timeval current_time;
gettimeofday(&current_time, NULL);
ms = current_time.tv_sec * 1000 + current_time.tv_usec / 1000;
retval = getMilliSecondsSinceEpoch();
cr_expect_eq(retval, ms);
}
// ############# load File parses the certificate file ##############
// ############# Generate a random String ##############
Test(common, randomString) {
char *result = "PKdht";
char *retval = randomString(5);
cr_expect_str_eq(retval, result);
}
// ############# Convert integer to String ##############
Test(common, convertInt2Str) {
char *result = "5";
char *retval = convertInt2Str(5);
cr_expect_str_eq(retval, result);
}
// ############# Convert a long integer to String ##############
Test(common, convertLongInt2Str) {
char *result = "5";
char *retval = convertLongInt2Str(5);
cr_expect_str_eq(retval, result);
}
// ############# A key/value dictionary system ##############
Test(common, dict) {
/* Create a dict */
dict_t **dict = dictAlloc();
char *result = "bar";
/* lets add foo to the dict */
addItem(dict, "foo", "bar");
/* and print their values */
char *retval = (char *)getItem(*dict, "foo");
cr_expect_str_eq(retval, result);
/* lets delete them */
delItem(dict, "foo");
/* see, their gone, there NULL */
retval = (char *)getItem(*dict, "foo");
cr_expect_null(retval);
/* add them again to proof it works */
addItem(dict, "foo", "bar");
/* see, here */
retval = (char *)getItem(*dict, "foo");
cr_expect_str_eq(retval, result);
delItem(dict, "foo");
dictDealloc(dict);
}
\ No newline at end of file
/* ================ Includes ===================== */
#include <criterion/criterion.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <string.h>
#include "../../coupler/opc-ua-server/mod_io_i2c.h"
/* ================ Function Tests =============== */
// ############# get I2C Slave List Length ##############
Test(modioi2c, getI2CSlaveListLength) {
int result, retval;
retval = getI2CSlaveListLength();
cr_expect_eq(retval, result);
}
// ############# Set Relay State (only virtual mode) ##############
Test(modioi2c, setRelayState) {
int result, retval, command = 0x00, i2c_addr = 0x58;
retval = setRelayState(command, i2c_addr);
cr_expect_eq(retval, result);
}
// ############# Get Digital Input State (only virtual mode) ##############
Test(modioi2c, getDigitalInputState) {
int result = 0, retval, i2c_addr = 0x58;
char *syscall_str = NULL;
retval = getDigitalInputState(i2c_addr, &syscall_str);
cr_expect_eq(retval, result);
}
// ############# Get Analog Input State AIN (only virtual mode) ##############
Test(modioi2c, getAnalogInputStateAIN) {
int result = 0, retval, i2c_addr = 0x58;
int *syscall_int = NULL;
uint8_t read_reg = 0x30;
retval = getAnalogInputStateAIN(i2c_addr, &syscall_int, read_reg);
cr_expect_eq(retval, result);
}
// ############# Safe Shutdown I2C Slave List ##############
Test(modioi2c, safeShutdownI2CSlaveList) {
int result = 0, retval;
safeShutdownI2CSlaveList();
cr_expect_eq(retval, result);
}
\ No newline at end of file
# Unit Tests
## Framework
Criterion: https://github.com/Snaipe/Criterion
## Criterion installation for Ubuntu (>=21.04)/Debian (>=11)
apt-get install libcriterion-dev
## Compile
```
make all
```
## Execute Tests
For `test_common` to execute:
```
./test_common
```
For `test_common` to execute and get a `tap` conform output execute:
```
./test_common --tap=test.tap
```
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment