logger, implementation and test

This commit is contained in:
2020-10-29 14:30:55 +01:00
parent 08c4907526
commit e5a77ccc00
6 changed files with 224 additions and 45 deletions

View File

@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <ringbuffer.h>
#include <logger.h>
// #define DEBUG
@ -815,6 +816,134 @@ void testRingbuffer99() {
CU_ASSERT(rb.bufferReadIdx == 0);
}
int init_suite_logger(void) {
logInit();
return 0;
}
int clean_suite_logger(void) {
logFree();
return 0;
}
void testLogger0() {
#ifdef DEBUG
printf("\nLog a message\n");
#endif
char goldValue1[] = "Wolfgang\n";
logMsg(goldValue1);
#ifdef DEBUG
printf("\nRead all chars from logging\n");
#endif
char buffer[128];
int c;
uint8_t i = 0;
memset(buffer, 0, 128);
i = 0;
while (0 < (c = logExecute())) {
buffer[i] = (uint8_t) c;
i++;
}
CU_ASSERT(strcmp(goldValue1, buffer) == 0);
}
void testLogger1() {
#ifdef DEBUG
printf("\nLog some messages\n");
#endif
char goldValueFull[128];
memset(goldValueFull, 0, 128);
char goldValue1[] = "Wolfgang\n"; // 9
strcat(goldValueFull, goldValue1);
logMsg(goldValue1);
char goldValue2[] = "Andreas\n"; // +8 = 17
strcat(goldValueFull, goldValue2);
logMsg(goldValue2);
char goldValue3[] = "Frank\n"; // +6 = 23
strcat(goldValueFull, goldValue3);
logMsg(goldValue3);
char goldValue4[] = "Thomas\n"; // +7 = 30
strcat(goldValueFull, goldValue4);
logMsg(goldValue4);
#ifdef DEBUG
printf("\nRead all chars from logging\n");
#endif
char buffer[128];
int c;
uint8_t i = 0;
memset(buffer, 0, 128);
i = 0;
while (0 < (c = logExecute())) {
buffer[i] = (uint8_t) c;
i++;
}
// printf("Buffer: %s\n", buffer);
CU_ASSERT(strcmp(goldValueFull, buffer) == 0);
}
void testLogger2() {
#ifdef DEBUG
printf("\nLog some messages, a bit more than space in buffer\n");
#endif
char goldValueFullNotOk[128];
memset(goldValueFullNotOk, 0, 128);
char goldValueFullOk[128];
memset(goldValueFullOk, 0, 128);
char goldValue1[] = "Wolfgang\n"; // 9
strcat(goldValueFullNotOk, goldValue1);
strcat(goldValueFullOk, goldValue1);
int r = logMsg(goldValue1);
CU_ASSERT(r == 0);
char goldValue2[] = "Andreas\n"; // +8 = 17
strcat(goldValueFullNotOk, goldValue2);
strcat(goldValueFullOk, goldValue2);
r = logMsg(goldValue2);
CU_ASSERT(r == 0);
char goldValue3[] = "Frank\n"; // +6 = 23
strcat(goldValueFullNotOk, goldValue3);
strcat(goldValueFullOk, goldValue3);
r = logMsg(goldValue3);
CU_ASSERT(r == 0);
char goldValue4[] = "Thomas\n"; // +7 = 30
strcat(goldValueFullNotOk, goldValue4);
strcat(goldValueFullOk, goldValue4);
r = logMsg(goldValue4);
CU_ASSERT(r == 0);
char goldValue5[] = "Barbara\n"; // +8 = 38, too much
strcat(goldValueFullNotOk, goldValue5);
r = logMsg(goldValue5);
CU_ASSERT(r == -1);
#ifdef DEBUG
printf("\nRead all chars from logging\n");
#endif
char buffer[128];
int c;
uint8_t i = 0;
memset(buffer, 0, 128);
i = 0;
while (0 < (c = logExecute())) {
buffer[i] = (uint8_t) c;
i++;
}
// printf("Buffer: %s\n", buffer);
CU_ASSERT(strcmp(goldValueFullNotOk, buffer) != 0);
CU_ASSERT(strcmp(goldValueFullOk, buffer) == 0);
}
int main() {
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
@ -848,6 +977,22 @@ int main() {
return CU_get_error();
}
CU_pSuite loggerSuite = CU_add_suite("Suite_Logger", init_suite_logger, clean_suite_logger);
if (NULL == loggerSuite) {
CU_cleanup_registry();
return CU_get_error();
}
if (
(NULL == CU_add_test(loggerSuite, "test 0 of logger", testLogger0)) ||
(NULL == CU_add_test(loggerSuite, "test 1 of logger", testLogger1)) ||
(NULL == CU_add_test(loggerSuite, "test 2 of logger", testLogger2)) ||
0 ) {
CU_cleanup_registry();
return CU_get_error();
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();