1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

unittests: Add XFA tests

This commit is contained in:
Joakim Nohlgård 2018-05-14 22:27:10 +02:00 committed by Kaspar Schleiser
parent 6adeec09e9
commit 12a2243485
6 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2018 Eistec AB
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @addtogroup unittests
* @{
*
* @file
* @brief Data elements for the core/xfa unit test
*
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
*/
#include "xfa.h"
#include "tests-core-xfa.h"
XFA(xfatest, 0) xfatest_t _xfatest1 = { .val = 12345, .text = "xfatest1" };
XFA_CONST(xfatest_const, 0) xfatest_t _xfatest_const1 = { .val = 0xcafe, .text = "xfatest_const1" };
XFA_INIT(xfatest_t, xfatest_use);
XFA_INIT_CONST(xfatest_t, xfatest_use_const);
XFA(xfatest_use, 0) xfatest_t _xfatest_use1 = { .val = 3333, .text = "xfatest_use1" };
XFA(xfatest_use, 0) xfatest_t _xfatest_use_again = { .val = 555, .text = "xfatest use again" };
XFA_CONST(xfatest_use_const, 0) xfatest_t _xfatest_use_const1 = { .val = 4444, .text = "xfatest_use_const1" };
int hack1;
/** @} */

View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2018 Eistec AB
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @addtogroup unittests
* @{
*
* @file
* @brief Data elements for the core/xfa unit test
*
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
*/
#include "xfa.h"
#include "tests-core-xfa.h"
XFA(xfatest, 0) xfatest_t _xfatest2 = { .val = 0xbeef, .text = "another test string" };
XFA_CONST(xfatest_const, 0) xfatest_t _xfatest_const2 = { .val = 32444, .text = "const string xfa 2" };
XFA(xfatest_use, 0) xfatest_t _xfatest_use2 = { .val = 11111, .text = "xfatest_use2" };
XFA_CONST(xfatest_use_const, 0) xfatest_t _xfatest_use_const2 = { .val = 22222, .text = "xfatest_use_const2" };
/** @} */

View File

@ -0,0 +1,144 @@
/*
* Copyright (C) 2018 Eistec AB
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
#include <stdint.h>
#include "bitarithm.h"
#include "xfa.h"
#include "embUnit.h"
#include "tests-core.h"
#include "tests-core-xfa.h"
XFA_INIT(xfatest_t, xfatest);
XFA_INIT_CONST(xfatest_t, xfatest_const);
XFA_USE(xfatest_t, xfatest_use);
XFA_USE_CONST(xfatest_t, xfatest_use_const);
/* Verifying that cross file array linking is correct by iterating over an external array */
static void test_xfa_data(void)
{
unsigned n = XFA_LEN(xfatest_t, xfatest);
TEST_ASSERT_EQUAL_INT(2, n);
unsigned found = 0;
for (unsigned k = 0; k < n; ++k) {
/* we do not want to enforce the order of the data elements */
switch (xfatest[k].val) {
case 12345:
/* tests-core-xfa-data1.c */
TEST_ASSERT_EQUAL_STRING("xfatest1", xfatest[k].text);
TEST_ASSERT(!(found & BIT0));
found |= BIT0;
break;
case 0xbeef:
/* tests-core-xfa-data2.c */
TEST_ASSERT_EQUAL_STRING("another test string", xfatest[k].text);
TEST_ASSERT(!(found & BIT1));
found |= BIT1;
break;
default:
break;
}
}
TEST_ASSERT_EQUAL_INT((1U << n) - 1, found);
}
static void test_xfa_const_data(void)
{
unsigned n = XFA_LEN(xfatest_t, xfatest_const);
TEST_ASSERT_EQUAL_INT(2, n);
unsigned found = 0;
for (unsigned k = 0; k < n; ++k) {
/* we do not want to enforce the order of the data elements */
switch (xfatest_const[k].val) {
case 0xcafe:
/* tests-core-xfa-data1.c */
TEST_ASSERT_EQUAL_STRING("xfatest_const1", xfatest_const[k].text);
++found;
break;
case 32444:
/* tests-core-xfa-data2.c */
TEST_ASSERT_EQUAL_STRING("const string xfa 2", xfatest_const[k].text);
++found;
break;
default:
break;
}
}
TEST_ASSERT_EQUAL_INT(n, found);
}
static void test_xfa_use_data(void)
{
unsigned n = XFA_LEN(xfatest_t, xfatest_use);
TEST_ASSERT_EQUAL_INT(3, n);
unsigned found = 0;
for (unsigned k = 0; k < n; ++k) {
/* we do not want to enforce the order of the data elements */
switch (xfatest_use[k].val) {
case 3333:
/* tests-core-xfa-data1.c */
TEST_ASSERT_EQUAL_STRING("xfatest_use1", xfatest_use[k].text);
++found;
break;
case 555:
/* tests-core-xfa-data1.c */
TEST_ASSERT_EQUAL_STRING("xfatest use again", xfatest_use[k].text);
++found;
break;
case 11111:
/* tests-core-xfa-data2.c */
TEST_ASSERT_EQUAL_STRING("xfatest_use2", xfatest_use[k].text);
++found;
break;
default:
break;
}
}
TEST_ASSERT_EQUAL_INT(n, found);
}
static void test_xfa_use_const_data(void)
{
unsigned n = XFA_LEN(xfatest_t, xfatest_use_const);
TEST_ASSERT_EQUAL_INT(2, n);
unsigned found = 0;
for (unsigned k = 0; k < n; ++k) {
/* we do not want to enforce the order of the data elements */
switch (xfatest_use_const[k].val) {
case 4444:
/* tests-core-xfa-data1.c */
TEST_ASSERT_EQUAL_STRING("xfatest_use_const1", xfatest_use_const[k].text);
++found;
break;
case 22222:
/* tests-core-xfa-data2.c */
TEST_ASSERT_EQUAL_STRING("xfatest_use_const2", xfatest_use_const[k].text);
++found;
break;
default:
break;
}
}
TEST_ASSERT_EQUAL_INT(n, found);
}
Test *tests_core_xfa_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_xfa_data),
new_TestFixture(test_xfa_const_data),
new_TestFixture(test_xfa_use_data),
new_TestFixture(test_xfa_use_const_data),
};
EMB_UNIT_TESTCALLER(core_xfa_tests, NULL, NULL,
fixtures);
return (Test *)&core_xfa_tests;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2018 Eistec AB
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @addtogroup unittests
* @{
*
* @file
* @brief Declarations for the core/xfa unit test
*
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
*/
#ifndef TESTS_CORE_XFA_H
#define TESTS_CORE_XFA_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
unsigned val;
const char *text;
} xfatest_t;
#ifdef __cplusplus
}
#endif
#endif /* TESTS_CORE_XFA_H */
/** @} */

View File

@ -19,4 +19,5 @@ void tests_core(void)
TESTS_RUN(tests_core_priority_queue_tests());
TESTS_RUN(tests_core_byteorder_tests());
TESTS_RUN(tests_core_ringbuffer_tests());
TESTS_RUN(tests_core_xfa_tests());
}

View File

@ -92,6 +92,13 @@ Test *tests_core_byteorder_tests(void);
*/
Test *tests_core_ringbuffer_tests(void);
/**
* @brief Generates tests for xfa.h
*
* @return embUnit tests if successful, NULL if not.
*/
Test *tests_core_xfa_tests(void);
#ifdef __cplusplus
}
#endif