9unit/test/util.c

339 lines
6.7 KiB
C
Raw Normal View History

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
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
Lesser General Public License for more details.
2023-11-09 15:48:51 -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"
// Local Types
typedef struct CompareInts CompareInts;
typedef struct ComparePtrs ComparePtrs;
2023-11-09 15:48:51 -05:00
struct CompareInts
{
const char *context;
int expected;
int actual;
};
struct ComparePtrs
{
const char *context;
const void *expected;
const void *actual;
};
2023-11-09 15:48:51 -05:00
// Local Prototypes
// compare the run value of two states
static void compare_run(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the passed value of two states
static void compare_passed(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the failed value of two states
static void compare_failed(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the pending value of two states
static void compare_pending(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the first_log value of two states
static void compare_first_log(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the last_log value of two states
static void compare_last_log(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the ptr value of two states
static void compare_ptr(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
2023-11-09 15:48:51 -05:00
// compare two ints
static void compare_ints(
TestState *,
const char *,
int,
int
);
// compare two pointers
static void compare_ptrs(
TestState *,
const char *,
void *,
void *
);
2023-11-09 15:48:51 -05:00
decl_test(compare_ints_test);
decl_test(compare_ptrs_test);
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-09 15:48:51 -05:00
void
compare_states(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
compare_run(s, prefix, context, expected, actual);
compare_passed(s, prefix, context, expected, actual);
compare_failed(s, prefix, context, expected, actual);
compare_pending(s, prefix, context, expected, actual);
compare_first_log(s, prefix, context, expected, actual);
compare_last_log(s, prefix, context, expected, actual);
compare_ptr(s, prefix, context, expected, actual);
2023-11-09 15:48:51 -05:00
}
// Local Functions
static void
compare_run(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("run\n");
snprintf(full_context, STR_BUF_SIZE, "%s run:", context);
compare_ints(s, full_context, expected->run, actual->run);
}
static void
compare_passed(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("passed\n");
snprintf(full_context, STR_BUF_SIZE, "%s passed:", context);
compare_ints(s, full_context, expected->passed, actual->passed);
}
static void
compare_failed(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("failed\n");
snprintf(full_context, STR_BUF_SIZE, "%s failed:", context);
compare_ints(s, full_context, expected->failed, actual->failed);
}
static void
compare_pending(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("pending\n");
snprintf(full_context, STR_BUF_SIZE, "%s pending:", context);
compare_ints(s, full_context, expected->pending, actual->pending);
}
static void
compare_first_log(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("first_log\n");
snprintf(full_context, STR_BUF_SIZE, "%s first_log:", context);
compare_ptrs(s, full_context, expected->first_log, actual->first_log);
}
static void
compare_last_log(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("last_log\n");
snprintf(full_context, STR_BUF_SIZE, "%s last_log:", context);
compare_ptrs(s, full_context, expected->last_log, actual->last_log);
}
static void
compare_ptr(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("ptr\n");
snprintf(full_context, STR_BUF_SIZE, "%s ptr:", context);
compare_ptrs(s, full_context, expected->ptr, actual->ptr);
}
2023-11-09 15:48:51 -05:00
static void
compare_ints(
TestState *s,
const char *context,
int expected,
int actual
)
{
void *old_ptr = s->ptr;
CompareInts ci;
ci.context = context;
ci.expected = expected;
ci.actual = actual;
s->ptr = &ci;
run_test(s, compare_ints_test);
s->ptr = old_ptr;
}
static void
compare_ptrs(
TestState *s,
const char *context,
void *expected,
void *actual
)
{
void *old_ptr = s->ptr;
ComparePtrs cp;
cp.context = context;
cp.expected = expected;
cp.actual = actual;
s->ptr = &cp;
run_test(s, compare_ptrs_test);
s->ptr = old_ptr;
}
2023-11-09 15:48:51 -05:00
def_test(compare_ints_test, s)
{
const CompareInts *ci = s->ptr;
if (ci->actual == ci->expected) return test_success;
char str[STR_BUF_SIZE];
append_test_log(s, ci->context);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->actual);
append_test_log(s, str);
return test_failure;
}
def_test(compare_ptrs_test, s)
{
const ComparePtrs *cp = s->ptr;
if (cp->actual == cp->expected) return test_success;
char str[STR_BUF_SIZE];
append_test_log(s, cp->context);
snprintf(str, STR_BUF_SIZE, "\texpected: 0x%x", cp->expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", cp->actual);
append_test_log(s, str);
return test_failure;
}
2023-11-09 15:48:51 -05:00
//jl