check ptr when comparing two TestState values

This commit is contained in:
jlamothe 2023-11-09 22:52:15 +00:00
parent e69a2d0375
commit 0665b8be3e

View File

@ -28,6 +28,7 @@
// Local Types
typedef struct CompareInts CompareInts;
typedef struct ComparePtrs ComparePtrs;
struct CompareInts
{
@ -36,6 +37,13 @@ struct CompareInts
int actual;
};
struct ComparePtrs
{
const char *context;
const void *expected;
const void *actual;
};
// Local Prototypes
// compare the run value of two states
@ -74,6 +82,15 @@ static void compare_pending(
const TestState *
);
// compare the ptr value of two states
static void compare_ptr(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare two ints
static void compare_ints(
TestState *,
@ -82,7 +99,16 @@ static void compare_ints(
int
);
// compare two pointers
static void compare_ptrs(
TestState *,
const char *,
void *,
void *
);
decl_test(compare_ints_test);
decl_test(compare_ptrs_test);
// Public Functions
@ -99,6 +125,7 @@ compare_states(
compare_passed(s, prefix, context, expected, actual);
compare_failed(s, prefix, context, expected, actual);
compare_pending(s, prefix, context, expected, actual);
compare_ptr(s, prefix, context, expected, actual);
}
// Local Functions
@ -167,6 +194,22 @@ compare_pending(
compare_ints(s, full_context, expected->pending, actual->pending);
}
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);
}
static void
compare_ints(
TestState *s,
@ -185,6 +228,24 @@ compare_ints(
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;
}
def_test(compare_ints_test, s)
{
const CompareInts *ci = s->ptr;
@ -198,4 +259,17 @@ def_test(compare_ints_test, s)
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;
}
//jl