From 2c9367a4a7fc159c7622f6f8a026c5eed70f3e65 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 28 Feb 2019 19:18:50 -0500 Subject: add test for distinct values --- test.c | 16 +++++++++++++++- test.h | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/test.c b/test.c index 2750ec3..51ce05f 100644 --- a/test.c +++ b/test.c @@ -41,7 +41,7 @@ static void print_result(int success, const char *format, ...) vprintf(format, ap); va_end(ap); - printf("%c", success ? '\r' : '\n'); + printf("%c", success ? '\n' : '\n'); set_output_color(DEFAULT); } @@ -97,3 +97,17 @@ void test_string_imp(const char *expression, const char *totest, const char *toc } print_result(success, "%s == \"%s\"", expression, tocompare); } + +void test_distinct_imp(const char *arrayname, int *array, size_t nelements) +{ + int success = 1; + size_t i, j; + for (i = 0; i < nelements; i++) { + for (j = i + 1; j < nelements; j++) { + if (array[i] == array[j]) { + success = 0; + } + } + } + print_result(success, "Elements in %s are%s distinct", arrayname, success ? "" : " not"); +} diff --git a/test.h b/test.h index 4f763b8..38b50ae 100644 --- a/test.h +++ b/test.h @@ -1,3 +1,5 @@ +#include + void testing_header(const char *); void testing_comment(const char*); void testing_end(void); @@ -15,6 +17,10 @@ void test_bool_imp(const char *, int, int); void test_string_imp(const char*, const char*, const char*); #define test_string(expression, string) test_string_imp(#expression, expression, string) +void test_distinct_imp(const char*, int *, size_t); +#define test_distinct(array) test_distinct_imp(#array, array, sizeof(array) / sizeof(array[0])) + void test_assert(void); void test_ctype(void); void test_locale(void); +void test_errno(void); -- cgit v1.2.1