2017-12-29 06:13:28 +01:00
|
|
|
#ifdef _MSC_VER
|
2016-05-02 19:22:57 +02:00
|
|
|
#include <platform.h>
|
2017-12-29 06:13:28 +01:00
|
|
|
#endif
|
2016-05-01 13:36:00 +02:00
|
|
|
|
|
|
|
#include "log.h"
|
2021-02-14 18:27:24 +01:00
|
|
|
#include "stats.h"
|
2017-12-29 06:13:28 +01:00
|
|
|
#include "macros.h"
|
2016-05-01 13:36:00 +02:00
|
|
|
|
2018-10-21 19:22:30 +02:00
|
|
|
#include <CuTest.h>
|
|
|
|
#include <tests.h>
|
|
|
|
|
2016-05-01 13:36:00 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
void log_string(void *data, int level, const char *module, const char *format, va_list args) {
|
|
|
|
char *str = (char *)data;
|
|
|
|
const char *arg = va_arg(args, const char *);
|
2017-01-10 16:31:05 +01:00
|
|
|
UNUSED_ARG(format);
|
|
|
|
UNUSED_ARG(module);
|
|
|
|
UNUSED_ARG(level);
|
2017-01-06 21:24:31 +01:00
|
|
|
strcpy(str, arg);
|
2016-05-01 13:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_logging(CuTest * tc)
|
|
|
|
{
|
|
|
|
char str1[32];
|
|
|
|
char str2[32];
|
2016-05-01 13:47:30 +02:00
|
|
|
struct log_t * id1 = log_create(LOG_CPWARNING, str1, log_string);
|
|
|
|
struct log_t * id2 = log_create(LOG_CPWARNING, str2, log_string);
|
2016-05-01 13:36:00 +02:00
|
|
|
CuAssertTrue(tc, id1!=id2);
|
|
|
|
log_warning("Hello %s", "World");
|
|
|
|
log_destroy(id1);
|
|
|
|
log_destroy(id2);
|
2016-06-11 13:47:38 +02:00
|
|
|
CuAssertStrEquals(tc, "World", str1);
|
|
|
|
CuAssertStrEquals(tc, "World", str2);
|
2016-05-01 13:36:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-21 19:22:30 +02:00
|
|
|
static int stats_cb(const char *stat, int num, void *udata) {
|
|
|
|
int *counter = (int *)udata;
|
|
|
|
if (counter) {
|
|
|
|
*counter += num;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_stats(CuTest * tc)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
test_setup();
|
|
|
|
CuAssertIntEquals(tc, 1, stats_count("foobar", 1));
|
|
|
|
CuAssertIntEquals(tc, 2, stats_count("test.one", 2));
|
|
|
|
CuAssertIntEquals(tc, 1, stats_count("test.two", 1));
|
|
|
|
CuAssertIntEquals(tc, 4, stats_count("test.one", 2));
|
|
|
|
CuAssertIntEquals(tc, 1, stats_count("test.two", 0));
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
CuAssertIntEquals(tc, 0, stats_walk("", stats_cb, &n));
|
|
|
|
CuAssertIntEquals(tc, 6, n);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
CuAssertIntEquals(tc, 0, stats_walk("test", stats_cb, &n));
|
|
|
|
CuAssertIntEquals(tc, 5, n);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
CuAssertIntEquals(tc, 0, stats_walk("test.one", stats_cb, &n));
|
|
|
|
CuAssertIntEquals(tc, 4, n);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
CuAssertIntEquals(tc, 0, stats_walk("foobar", stats_cb, &n));
|
|
|
|
CuAssertIntEquals(tc, 1, n);
|
|
|
|
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2016-05-01 13:36:00 +02:00
|
|
|
CuSuite *get_log_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_logging);
|
2018-10-21 19:22:30 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_stats);
|
2016-05-01 13:36:00 +02:00
|
|
|
return suite;
|
|
|
|
}
|