Issue 87 execute tests with GitHub ci (#234)
* Enable lcov coverage in unit testing via cmake. * fix pipeline build error * add compile options for unit test to silence some warnings * remove all BAC_TEST unit tests in src/bacnet/ folder. They are now in test/bacnet/ folders using ztest. * removed key.c - only used for unit test. * produce XML test result output for parsing * produce junit XML test result output * change lint workflow to quality * update readme badge for quality results Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
@@ -1223,216 +1223,3 @@ bool octetstring_value_same(
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "ctest.h"
|
||||
|
||||
static void testBitString(Test *pTest)
|
||||
{
|
||||
uint8_t bit = 0;
|
||||
int max_bit;
|
||||
BACNET_BIT_STRING bit_string;
|
||||
BACNET_BIT_STRING bit_string2;
|
||||
BACNET_BIT_STRING bit_string3;
|
||||
|
||||
bitstring_init(&bit_string);
|
||||
/* verify initialization */
|
||||
ct_test(pTest, bitstring_bits_used(&bit_string) == 0);
|
||||
for (bit = 0; bit < (MAX_BITSTRING_BYTES * 8); bit++) {
|
||||
ct_test(pTest, bitstring_bit(&bit_string, bit) == false);
|
||||
}
|
||||
|
||||
/* test for true */
|
||||
for (bit = 0; bit < (MAX_BITSTRING_BYTES * 8); bit++) {
|
||||
bitstring_set_bit(&bit_string, bit, true);
|
||||
ct_test(pTest, bitstring_bits_used(&bit_string) == (bit + 1));
|
||||
ct_test(pTest, bitstring_bit(&bit_string, bit) == true);
|
||||
}
|
||||
/* test for false */
|
||||
bitstring_init(&bit_string);
|
||||
for (bit = 0; bit < (MAX_BITSTRING_BYTES * 8); bit++) {
|
||||
bitstring_set_bit(&bit_string, bit, false);
|
||||
ct_test(pTest, bitstring_bits_used(&bit_string) == (bit + 1));
|
||||
ct_test(pTest, bitstring_bit(&bit_string, bit) == false);
|
||||
}
|
||||
|
||||
/* test for compare equals */
|
||||
srand(time(NULL));
|
||||
for (max_bit = 0; max_bit < (MAX_BITSTRING_BYTES * 8); max_bit++) {
|
||||
bitstring_init(&bit_string);
|
||||
bitstring_init(&bit_string2);
|
||||
for (bit = 0; bit < max_bit; bit++) {
|
||||
bool bit_value = rand() % 2;
|
||||
bitstring_set_bit(&bit_string, bit, bit_value);
|
||||
bitstring_set_bit(&bit_string2, bit, bit_value);
|
||||
}
|
||||
ct_test(pTest, bitstring_same(&bit_string, &bit_string2));
|
||||
}
|
||||
/* test for compare not equals */
|
||||
for (max_bit = 1; max_bit < (MAX_BITSTRING_BYTES * 8); max_bit++) {
|
||||
bitstring_init(&bit_string);
|
||||
bitstring_init(&bit_string2);
|
||||
bitstring_init(&bit_string3);
|
||||
for (bit = 0; bit < max_bit; bit++) {
|
||||
bool bit_value = rand() % 2;
|
||||
bitstring_set_bit(&bit_string, bit, bit_value);
|
||||
bitstring_set_bit(&bit_string2, bit, bit_value);
|
||||
bitstring_set_bit(&bit_string3, bit, bit_value);
|
||||
}
|
||||
/* Set the first bit of bit_string2 and the last bit of bit_string3 to
|
||||
* be different */
|
||||
bitstring_set_bit(&bit_string2, 0, !bitstring_bit(&bit_string, 0));
|
||||
bitstring_set_bit(&bit_string3, max_bit - 1,
|
||||
!bitstring_bit(&bit_string, max_bit - 1));
|
||||
ct_test(pTest, !bitstring_same(&bit_string, &bit_string2));
|
||||
ct_test(pTest, !bitstring_same(&bit_string, &bit_string3));
|
||||
}
|
||||
}
|
||||
|
||||
static void testCharacterString(Test *pTest)
|
||||
{
|
||||
BACNET_CHARACTER_STRING bacnet_string;
|
||||
char *value = "Joshua,Mary,Anna,Christopher";
|
||||
char test_value[MAX_APDU] = "Patricia";
|
||||
char test_append_value[MAX_APDU] = " and the Kids";
|
||||
char test_append_string[MAX_APDU] = "";
|
||||
bool status = false;
|
||||
size_t length = 0;
|
||||
size_t test_length = 0;
|
||||
size_t i = 0;
|
||||
|
||||
/* verify initialization */
|
||||
status = characterstring_init(&bacnet_string, CHARACTER_ANSI_X34, NULL, 0);
|
||||
ct_test(pTest, status == true);
|
||||
ct_test(pTest, characterstring_length(&bacnet_string) == 0);
|
||||
ct_test(
|
||||
pTest, characterstring_encoding(&bacnet_string) == CHARACTER_ANSI_X34);
|
||||
/* bounds check */
|
||||
status = characterstring_init(&bacnet_string, CHARACTER_ANSI_X34, NULL,
|
||||
characterstring_capacity(&bacnet_string) + 1);
|
||||
ct_test(pTest, status == false);
|
||||
status = characterstring_truncate(
|
||||
&bacnet_string, characterstring_capacity(&bacnet_string) + 1);
|
||||
ct_test(pTest, status == false);
|
||||
status = characterstring_truncate(
|
||||
&bacnet_string, characterstring_capacity(&bacnet_string));
|
||||
ct_test(pTest, status == true);
|
||||
|
||||
test_length = strlen(test_value);
|
||||
status = characterstring_init(
|
||||
&bacnet_string, CHARACTER_ANSI_X34, &test_value[0], test_length);
|
||||
ct_test(pTest, status == true);
|
||||
value = characterstring_value(&bacnet_string);
|
||||
length = characterstring_length(&bacnet_string);
|
||||
ct_test(pTest, length == test_length);
|
||||
for (i = 0; i < test_length; i++) {
|
||||
ct_test(pTest, value[i] == test_value[i]);
|
||||
}
|
||||
test_length = strlen(test_append_value);
|
||||
status = characterstring_append(
|
||||
&bacnet_string, &test_append_value[0], test_length);
|
||||
strcat(test_append_string, test_value);
|
||||
strcat(test_append_string, test_append_value);
|
||||
test_length = strlen(test_append_string);
|
||||
ct_test(pTest, status == true);
|
||||
length = characterstring_length(&bacnet_string);
|
||||
value = characterstring_value(&bacnet_string);
|
||||
ct_test(pTest, length == test_length);
|
||||
for (i = 0; i < test_length; i++) {
|
||||
ct_test(pTest, value[i] == test_append_string[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void testOctetString(Test *pTest)
|
||||
{
|
||||
BACNET_OCTET_STRING bacnet_string;
|
||||
uint8_t *value = NULL;
|
||||
uint8_t test_value[MAX_APDU] = "Patricia";
|
||||
uint8_t test_append_value[MAX_APDU] = " and the Kids";
|
||||
uint8_t test_append_string[MAX_APDU] = "";
|
||||
bool status = false;
|
||||
size_t length = 0;
|
||||
size_t test_length = 0;
|
||||
size_t i = 0;
|
||||
|
||||
/* verify initialization */
|
||||
status = octetstring_init(&bacnet_string, NULL, 0);
|
||||
ct_test(pTest, status == true);
|
||||
ct_test(pTest, octetstring_length(&bacnet_string) == 0);
|
||||
value = octetstring_value(&bacnet_string);
|
||||
for (i = 0; i < octetstring_capacity(&bacnet_string); i++) {
|
||||
ct_test(pTest, value[i] == 0);
|
||||
}
|
||||
/* bounds check */
|
||||
status = octetstring_init(
|
||||
&bacnet_string, NULL, octetstring_capacity(&bacnet_string) + 1);
|
||||
ct_test(pTest, status == false);
|
||||
status = octetstring_init(
|
||||
&bacnet_string, NULL, octetstring_capacity(&bacnet_string));
|
||||
ct_test(pTest, status == true);
|
||||
status = octetstring_truncate(
|
||||
&bacnet_string, octetstring_capacity(&bacnet_string) + 1);
|
||||
ct_test(pTest, status == false);
|
||||
status = octetstring_truncate(
|
||||
&bacnet_string, octetstring_capacity(&bacnet_string));
|
||||
ct_test(pTest, status == true);
|
||||
|
||||
test_length = strlen((char *)test_value);
|
||||
status = octetstring_init(&bacnet_string, &test_value[0], test_length);
|
||||
ct_test(pTest, status == true);
|
||||
length = octetstring_length(&bacnet_string);
|
||||
value = octetstring_value(&bacnet_string);
|
||||
ct_test(pTest, length == test_length);
|
||||
for (i = 0; i < test_length; i++) {
|
||||
ct_test(pTest, value[i] == test_value[i]);
|
||||
}
|
||||
|
||||
test_length = strlen((char *)test_append_value);
|
||||
status =
|
||||
octetstring_append(&bacnet_string, &test_append_value[0], test_length);
|
||||
strcat((char *)test_append_string, (char *)test_value);
|
||||
strcat((char *)test_append_string, (char *)test_append_value);
|
||||
test_length = strlen((char *)test_append_string);
|
||||
ct_test(pTest, status == true);
|
||||
length = octetstring_length(&bacnet_string);
|
||||
value = octetstring_value(&bacnet_string);
|
||||
ct_test(pTest, length == test_length);
|
||||
for (i = 0; i < test_length; i++) {
|
||||
ct_test(pTest, value[i] == test_append_string[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void testBACnetStrings(Test *pTest)
|
||||
{
|
||||
bool rc;
|
||||
|
||||
/* add individual tests */
|
||||
rc = ct_addTestFunction(pTest, testBitString);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testCharacterString);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testOctetString);
|
||||
assert(rc);
|
||||
}
|
||||
|
||||
#ifdef TEST_BACSTR
|
||||
int main(void)
|
||||
{
|
||||
Test *pTest;
|
||||
|
||||
pTest = ct_create("BACnet Strings", NULL);
|
||||
testBACnetStrings(pTest);
|
||||
/* configure output */
|
||||
ct_setStream(pTest, stdout);
|
||||
ct_run(pTest);
|
||||
(void)ct_report(pTest);
|
||||
ct_destroy(pTest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* TEST_BACSTR */
|
||||
#endif /* BAC_TEST */
|
||||
|
||||
Reference in New Issue
Block a user