#include "cx/test.h"
#include <limits.h>
#include <float.h>
#include "cx/compare.h"
#define test_compare_gen_subroutine(
T, max_number, signed_type) \
static CX_TEST_SUBROUTINE( \
test_sub_cmp_##T, \
cx_compare_func fnc \
) { \
T m = max_number /
512; \
T x, y; \
x = (signed_type ?
-3 :
3) * m; \
y =
5 * m; \
CX_TEST_ASSERT(fnc(&x, &y) <
0); \
CX_TEST_ASSERT(fnc(&y, &x) >
0); \
x =
120 * m; \
y =
348 * m; \
CX_TEST_ASSERT(fnc(&x, &y) <
0); \
CX_TEST_ASSERT(fnc(&y, &x) >
0); \
if (signed_type) { \
x =
-120 * m; \
y =
-348 * m; \
CX_TEST_ASSERT(fnc(&x, &y) >
0); \
CX_TEST_ASSERT(fnc(&y, &x) <
0); \
} \
x = y; \
CX_TEST_ASSERT(fnc(&x, &y) ==
0); \
CX_TEST_ASSERT(fnc(&y, &x) ==
0); \
}
typedef long long cx_longlong;
typedef unsigned long cx_ulong;
typedef unsigned long long cx_ulonglong;
test_compare_gen_subroutine(
int,
INT_MAX, true)
test_compare_gen_subroutine(
long,
LONG_MAX, true)
test_compare_gen_subroutine(cx_longlong,
LLONG_MAX, true)
test_compare_gen_subroutine(
int16_t,
INT16_MAX, true)
test_compare_gen_subroutine(
int32_t,
INT32_MAX, true)
test_compare_gen_subroutine(
int64_t,
INT64_MAX, true)
test_compare_gen_subroutine(
unsigned,
UINT_MAX, false)
test_compare_gen_subroutine(cx_ulong,
ULONG_MAX, false)
test_compare_gen_subroutine(cx_ulonglong,
ULLONG_MAX, false)
test_compare_gen_subroutine(
size_t,
SIZE_MAX, false)
test_compare_gen_subroutine(
uint16_t,
UINT16_MAX, false)
test_compare_gen_subroutine(
uint32_t,
UINT32_MAX, false)
test_compare_gen_subroutine(
uint64_t,
UINT64_MAX, false)
test_compare_gen_subroutine(
float,
FLT_MAX, true)
test_compare_gen_subroutine(
double,
DBL_MAX, true)
test_compare_gen_subroutine(
intptr_t,
INTPTR_MAX, true)
test_compare_gen_subroutine(
uintptr_t,
UINTPTR_MAX, false)
CX_TEST(test_compare_int) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int);
}
CX_TEST(test_compare_long) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint);
}
CX_TEST(test_compare_longlong) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong);
}
CX_TEST(
test_compare_int16_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_int16_t, cx_cmp_int16);
}
CX_TEST(
test_compare_int32_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_int32_t, cx_cmp_int32);
}
CX_TEST(
test_compare_int64_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_int64_t, cx_cmp_int64);
}
CX_TEST(test_compare_unsigned) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint);
}
CX_TEST(test_compare_ulong) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint);
}
CX_TEST(test_compare_ulonglong) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong);
}
CX_TEST(
test_compare_size_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_size_t, cx_cmp_size);
}
CX_TEST(
test_compare_uint16_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uint16_t, cx_cmp_uint16);
}
CX_TEST(
test_compare_uint32_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uint32_t, cx_cmp_uint32);
}
CX_TEST(
test_compare_uint64_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uint64_t, cx_cmp_uint64);
}
CX_TEST(test_compare_float) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float);
}
CX_TEST(test_compare_double) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double);
}
CX_TEST(
test_compare_intptr_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_intptr_t, cx_cmp_intptr);
}
CX_TEST(
test_compare_uintptr_t) {
CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uintptr_t, cx_cmp_uintptr);
}
CX_TEST(test_compare_ptr) {
int data[
3];
CX_TEST_DO {
CX_TEST_ASSERT(
0 == cx_cmp_ptr(data, data));
CX_TEST_ASSERT(
-1 == cx_cmp_ptr(&data[
0], &data[
1]));
CX_TEST_ASSERT(
-1 == cx_cmp_ptr(&data[
1], &data[
2]));
CX_TEST_ASSERT(
1 == cx_cmp_ptr(&data[
2], &data[
1]));
CX_TEST_ASSERT(
1 == cx_cmp_ptr(&data[
1], data));
CX_TEST_ASSERT(
0 == cx_cmp_ptr(&data[
1], &data[
1]));
}
}
CxTestSuite *cx_test_suite_compare(
void) {
CxTestSuite *suite = cx_test_suite_new(
"compare");
cx_test_register(suite, test_compare_int);
cx_test_register(suite, test_compare_long);
cx_test_register(suite, test_compare_longlong);
cx_test_register(suite,
test_compare_int16_t);
cx_test_register(suite,
test_compare_int32_t);
cx_test_register(suite,
test_compare_int64_t);
cx_test_register(suite, test_compare_unsigned);
cx_test_register(suite, test_compare_ulong);
cx_test_register(suite, test_compare_ulonglong);
cx_test_register(suite,
test_compare_size_t);
cx_test_register(suite,
test_compare_uint16_t);
cx_test_register(suite,
test_compare_uint32_t);
cx_test_register(suite,
test_compare_uint64_t);
cx_test_register(suite, test_compare_float);
cx_test_register(suite, test_compare_double);
cx_test_register(suite,
test_compare_intptr_t);
cx_test_register(suite,
test_compare_uintptr_t);
cx_test_register(suite, test_compare_ptr);
return suite;
}