2023-11-09 15:48:51 -05:00
|
|
|
/*
|
|
|
|
|
|
|
|
9unit
|
|
|
|
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2023-11-09 18:24:48 -05:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
2023-11-09 15:48:51 -05:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2023-11-09 18:24:48 -05:00
|
|
|
Lesser General Public License for more details.
|
2023-11-09 15:48:51 -05:00
|
|
|
|
2023-11-09 18:24:48 -05:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this program. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>.
|
2023-11-09 15:48:51 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "../9unit.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
// Internal Prototypes
|
2023-11-09 15:48:51 -05:00
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
static void chk_TestState_eq_test(TestState *, void *, void *);
|
|
|
|
static TestResult chk_int_eq_test(TestState *, void *, void *);
|
|
|
|
static TestResult chk_ptr_eq_test(TestState *, void *, void *);
|
|
|
|
static TestResult chk_ptr_ne_test(TestState *, void *, void *);
|
|
|
|
static TestResult chk_str_eq_test(TestState *, void *, void *);
|
2023-11-14 18:53:43 -05:00
|
|
|
|
|
|
|
// discard report data
|
|
|
|
static void report(const char *);
|
|
|
|
|
2023-11-09 15:48:51 -05:00
|
|
|
// Public Functions
|
|
|
|
|
2023-11-10 15:48:40 -05:00
|
|
|
void
|
|
|
|
mk_sample_state(TestState *s)
|
|
|
|
{
|
|
|
|
memset(s, 0, sizeof(TestState));
|
|
|
|
s->passed = 1;
|
|
|
|
s->failed = 2;
|
|
|
|
s->pending = 3;
|
|
|
|
s->run = 6;
|
2023-11-14 18:53:43 -05:00
|
|
|
s->report = report;
|
2023-11-10 15:48:40 -05:00
|
|
|
}
|
|
|
|
|
2023-11-09 15:48:51 -05:00
|
|
|
void
|
2023-11-10 22:40:00 -05:00
|
|
|
chk_TestState_eq(
|
2023-11-09 15:48:51 -05:00
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
2023-11-10 22:40:00 -05:00
|
|
|
const TestState *actual,
|
|
|
|
const TestState *expected
|
2023-11-09 15:48:51 -05:00
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
test_context_compare(
|
|
|
|
s,
|
|
|
|
context,
|
|
|
|
chk_TestState_eq_test,
|
|
|
|
actual,
|
|
|
|
expected
|
|
|
|
);
|
2023-11-09 15:48:51 -05:00
|
|
|
}
|
|
|
|
|
2023-11-10 17:44:42 -05:00
|
|
|
void
|
2023-11-10 22:40:00 -05:00
|
|
|
chk_int_eq(
|
2023-11-10 17:44:42 -05:00
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
2023-11-10 22:40:00 -05:00
|
|
|
int actual,
|
|
|
|
int expected
|
2023-11-10 17:44:42 -05:00
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
single_test_context_compare(
|
|
|
|
s,
|
|
|
|
context,
|
|
|
|
chk_int_eq_test,
|
|
|
|
&actual,
|
|
|
|
&expected
|
|
|
|
);
|
2023-11-10 17:44:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-10 22:40:00 -05:00
|
|
|
chk_ptr_eq(
|
2023-11-10 17:44:42 -05:00
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
2023-11-21 00:12:41 -05:00
|
|
|
const void *actual,
|
|
|
|
const void *expected
|
2023-11-10 17:44:42 -05:00
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
single_test_context_compare(
|
|
|
|
s,
|
|
|
|
context,
|
|
|
|
chk_ptr_eq_test,
|
|
|
|
actual,
|
|
|
|
expected
|
|
|
|
);
|
2023-11-10 17:44:42 -05:00
|
|
|
}
|
|
|
|
|
2023-11-11 16:36:05 -05:00
|
|
|
void
|
|
|
|
chk_ptr_ne(
|
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
|
|
|
const void *actual,
|
|
|
|
const void *prohibited
|
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
single_test_context_compare(
|
|
|
|
s,
|
|
|
|
context,
|
|
|
|
chk_ptr_ne_test,
|
|
|
|
actual,
|
|
|
|
prohibited
|
|
|
|
);
|
2023-11-11 16:36:05 -05:00
|
|
|
}
|
|
|
|
|
2023-11-11 17:53:04 -05:00
|
|
|
void
|
|
|
|
chk_str_eq(
|
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
|
|
|
const char *actual,
|
|
|
|
const char *expected
|
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
single_test_context_compare(
|
|
|
|
s,
|
|
|
|
context,
|
|
|
|
chk_str_eq_test,
|
|
|
|
actual,
|
|
|
|
expected
|
|
|
|
);
|
2023-11-11 17:53:04 -05:00
|
|
|
}
|
|
|
|
|
2023-11-09 15:48:51 -05:00
|
|
|
// Local Functions
|
|
|
|
|
|
|
|
static void
|
2023-11-21 00:12:41 -05:00
|
|
|
chk_TestState_eq_test(
|
2023-11-09 16:12:21 -05:00
|
|
|
TestState *s,
|
2023-11-21 00:12:41 -05:00
|
|
|
void *aptr,
|
|
|
|
void *eptr
|
2023-11-09 18:04:45 -05:00
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
if (!eptr)
|
|
|
|
{
|
|
|
|
chk_ptr_eq(s, "is null", aptr, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestState
|
|
|
|
*actual = aptr,
|
|
|
|
*expected = eptr;
|
|
|
|
|
|
|
|
chk_int_eq(s, "run", actual->run, expected->run);
|
|
|
|
chk_int_eq(s, "passed", actual->passed, expected->passed);
|
|
|
|
chk_int_eq(s, "failed", actual->failed, expected->failed);
|
|
|
|
chk_int_eq(s, "pending", actual->pending, expected->pending);
|
|
|
|
chk_ptr_eq(s, "first_log", actual->first_log, expected->first_log);
|
|
|
|
chk_ptr_eq(s, "last_log", actual->last_log, expected->last_log);
|
|
|
|
chk_ptr_eq(s, "ptr", actual->ptr, expected->ptr);
|
|
|
|
chk_str_eq(s, "context", actual->context, expected->context);
|
|
|
|
chk_str_eq(s, "full_context", actual->full_context, expected->full_context);
|
|
|
|
chk_int_eq(s, "depth", actual->depth, expected->depth);
|
|
|
|
chk_ptr_eq(s, "report()", actual->report, expected->report);
|
2023-11-09 18:04:45 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
static TestResult
|
|
|
|
chk_int_eq_test(
|
2023-11-09 17:52:15 -05:00
|
|
|
TestState *s,
|
2023-11-21 00:12:41 -05:00
|
|
|
void *aptr,
|
|
|
|
void *eptr
|
2023-11-09 17:52:15 -05:00
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
int
|
|
|
|
*actual = aptr,
|
|
|
|
*expected = eptr;
|
2023-11-09 17:52:15 -05:00
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
if (*actual == *expected)
|
|
|
|
return test_success;
|
2023-11-14 18:08:19 -05:00
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
// log the error
|
|
|
|
char str[STR_BUF_SIZE];
|
|
|
|
log_test_context(s);
|
|
|
|
append_test_log(s, "ERROR:");
|
|
|
|
snprintf(str, STR_BUF_SIZE, "\texpected: %d", *expected);
|
|
|
|
append_test_log(s, str);
|
|
|
|
snprintf(str, STR_BUF_SIZE, "\tactual: %d", *actual);
|
|
|
|
append_test_log(s, str);
|
2023-11-14 18:08:19 -05:00
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
return test_failure;
|
2023-11-14 18:08:19 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
static TestResult
|
|
|
|
chk_ptr_eq_test(
|
2023-11-14 18:53:43 -05:00
|
|
|
TestState *s,
|
2023-11-21 00:12:41 -05:00
|
|
|
void *actual,
|
|
|
|
void *expected
|
2023-11-14 18:53:43 -05:00
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
if (actual == expected)
|
|
|
|
return test_success;
|
2023-11-14 18:53:43 -05:00
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
// log the error
|
2023-11-09 15:48:51 -05:00
|
|
|
char str[STR_BUF_SIZE];
|
2023-11-21 00:12:41 -05:00
|
|
|
log_test_context(s);
|
|
|
|
append_test_log(s, "ERROR:");
|
|
|
|
snprintf(str, STR_BUF_SIZE, "\texpected: 0x%x", expected);
|
2023-11-09 15:48:51 -05:00
|
|
|
append_test_log(s, str);
|
2023-11-21 00:12:41 -05:00
|
|
|
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", actual);
|
2023-11-09 15:48:51 -05:00
|
|
|
append_test_log(s, str);
|
|
|
|
|
2023-11-09 17:52:15 -05:00
|
|
|
return test_failure;
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
static TestResult
|
|
|
|
chk_ptr_ne_test(
|
|
|
|
TestState *s,
|
|
|
|
void *actual,
|
|
|
|
void *prohibited
|
|
|
|
)
|
2023-11-11 16:36:05 -05:00
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
if (actual != prohibited)
|
|
|
|
return test_success;
|
|
|
|
|
|
|
|
// log the error
|
2023-11-11 16:36:05 -05:00
|
|
|
char str[STR_BUF_SIZE];
|
2023-11-21 00:12:41 -05:00
|
|
|
log_test_context(s);
|
|
|
|
snprintf(
|
|
|
|
str,
|
|
|
|
STR_BUF_SIZE,
|
|
|
|
"ERROR: prohibited value: 0x%x",
|
|
|
|
actual
|
|
|
|
);
|
2023-11-11 16:36:05 -05:00
|
|
|
append_test_log(s, str);
|
2023-11-21 00:12:41 -05:00
|
|
|
|
2023-11-11 16:36:05 -05:00
|
|
|
return test_failure;
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
static TestResult
|
|
|
|
chk_str_eq_test(
|
|
|
|
TestState *s,
|
|
|
|
void *aptr,
|
|
|
|
void *eptr
|
|
|
|
)
|
2023-11-11 17:53:04 -05:00
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
if (!eptr)
|
|
|
|
{
|
|
|
|
if (!aptr) return test_success;
|
|
|
|
char str[STR_BUF_SIZE];
|
|
|
|
log_test_context(s);
|
|
|
|
append_test_log(s, "ERROR:");
|
|
|
|
append_test_log(s, "\texpected: 0x0");
|
|
|
|
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", aptr);
|
|
|
|
append_test_log(s, str);
|
|
|
|
return test_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char
|
|
|
|
*actual = aptr,
|
|
|
|
*expected = eptr;
|
|
|
|
|
|
|
|
if (!strcmp(actual, expected)) return;
|
|
|
|
|
|
|
|
// log the error
|
2023-11-11 17:53:04 -05:00
|
|
|
char str[STR_BUF_SIZE];
|
2023-11-21 00:12:41 -05:00
|
|
|
log_test_context(s);
|
|
|
|
append_test_log(s, "ERROR:");
|
|
|
|
snprintf(str, STR_BUF_SIZE, "\texpected: %s", expected);
|
2023-11-11 17:53:04 -05:00
|
|
|
append_test_log(s, str);
|
2023-11-21 00:12:41 -05:00
|
|
|
snprintf(str, STR_BUF_SIZE, "\tactual: %s", actual);
|
2023-11-11 17:53:04 -05:00
|
|
|
append_test_log(s, str);
|
2023-11-21 00:12:41 -05:00
|
|
|
|
2023-11-11 17:53:04 -05:00
|
|
|
return test_failure;
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:53:43 -05:00
|
|
|
static void
|
|
|
|
report(const char *)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-09 15:48:51 -05:00
|
|
|
//jl
|