Skip to content

Commit

Permalink
tests: update test design like cglm library and drop cmocka
Browse files Browse the repository at this point in the history
  • Loading branch information
recp committed Oct 20, 2019
1 parent dc15b8c commit 2215255
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 223 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 🔗 Data Structures
# 🔗 Data Structures and Algorithms

[![Build Status](https://travis-ci.org/recp/ds.svg?branch=master)](https://travis-ci.org/recp/libds)
[![Build status](https://ci.appveyor.com/api/projects/status/yqpyll64woh39a23/branch/master?svg=true)](https://ci.appveyor.com/project/recp/libds/branch/master)
Expand Down Expand Up @@ -58,14 +58,12 @@ main(int argc, const char * argv[]) {
### Unix (Autotools)
```text
$ sh ./build-deps.sh # run only once (dependencies)
$
```bash
$ sh autogen.sh
$ ./configure
$ make
$ make install
$ [sudo] make install
$ make check # [Optional]
$ [sudo] make install # [Optional]
```

### Windows (MSBuild)
Expand Down
30 changes: 0 additions & 30 deletions build-deps.sh

This file was deleted.

2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

AC_PREREQ([2.69])
AC_INIT([ds], [0.2.1], [[email protected]])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects serial-tests])

AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR([src/])
Expand Down
9 changes: 2 additions & 7 deletions makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ lib_LTLIBRARIES = libds.la
libds_la_LDFLAGS = -no-undefined -version-info 0:1:0

checkLDFLAGS = -L./.libs \
-L./test/lib/cmocka/build/src \
-lcmocka \
-lm \
-lds
checkCFLAGS = -I./test/lib/cmocka/include \
-I./include
checkCFLAGS = -I./include

check_PROGRAMS = test/tests
TESTS = $(check_PROGRAMS)
Expand Down Expand Up @@ -56,11 +53,9 @@ libds_la_SOURCES=\
src/sort/sort.c

test_tests_SOURCES=\
test/runner.c \
test/src/test_common.c \
test/src/test_main.c \
test/src/test_rb.c \
test/src/test_flist.c \
test/src/test_flist_sep.c \
test/src/test_htable.c
all-local:
sh ./post-build.sh
19 changes: 0 additions & 19 deletions post-build.sh

This file was deleted.

115 changes: 115 additions & 0 deletions test/include/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/

#ifndef tests_common_h
#define tests_common_h

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct test_status_t {
const char *msg;
int status;
} test_status_t;

typedef test_status_t (*fntest)(void);

typedef struct test_entry_t {
char *name;
fntest entry;
int ret;
int show_output;
} test_entry_t;

#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */

#define TEST_DECLARE(FUN) test_status_t test_ ## FUN(void);
#define TEST_ENTRY(FUN) { #FUN, test_ ## FUN, 0, 0 },
#define TEST_LIST static test_entry_t tests[] =

/* __VA_ARGS__ workaround for MSVC: https://stackoverflow.com/a/5134656 */
#define EXPAND(x) x

#define TEST_OK 1
#define TEST_SUCCESS return (test_status_t){NULL, TEST_OK};

#define TEST_IMPL_ARG1(FUN) \
test_status_t test_ ## FUN (void); \
test_status_t test_ ## FUN()

#define TEST_IMPL_ARG2(PREFIX, FUN) TEST_IMPL_ARG1(PREFIX ## FUN)
#define TEST_IMPL_ARG3(arg1, arg2, arg3, ...) arg3

#define TEST_IMPL_CHOOSER(...) \
EXPAND(TEST_IMPL_ARG3(__VA_ARGS__, TEST_IMPL_ARG2, TEST_IMPL_ARG1))

#define TEST_IMPL(...) EXPAND(TEST_IMPL_CHOOSER(__VA_ARGS__)(__VA_ARGS__))

#define ASSERT_EXT(expr, msg) \
if (!(expr)) { \
fprintf(stderr, \
RED " assert fail" RESET \
" in " BOLDCYAN "%s " RESET \
"on " BOLDMAGENTA "line %d" RESET \
" : " BOLDWHITE " ASSERT(%s)\n" RESET, \
__FILE__, \
__LINE__, \
#expr); \
return (test_status_t){msg, 0}; \
}

#define ASSERT_ARG1(expr) ASSERT_EXT(expr, NULL)
#define ASSERT_ARG2(expr, msg) ASSERT_EXT(expr, msg)
#define ASSERT_ARG3(arg1, arg2, arg3, ...) arg3

#define ASSERT_CHOOSER(...) ASSERT_ARG3(__VA_ARGS__, ASSERT_ARG2, ASSERT_ARG1)
#define ASSERT(...) do { ASSERT_CHOOSER(__VA_ARGS__)(__VA_ARGS__) } while(0);
#define ASSERTIFY(expr) do { \
test_status_t ts; \
ts = expr; \
if (ts.status != TEST_OK) { \
fprintf(stderr, \
RED " assert fail" RESET \
" in " BOLDCYAN "%s " RESET \
"on " BOLDMAGENTA "line %d" RESET \
" : " BOLDWHITE " ASSERTIFY(%s)\n" RESET, \
__FILE__, \
__LINE__, \
#expr); \
return (test_status_t){ts.msg, 0}; \
} \
} while(0);

#if defined(_WIN32)
# define drand48() ((float)(rand() / (RAND_MAX + 1.0)))
# define OK_TEXT "ok:"
# define FAIL_TEXT "fail:"
# define FINAL_TEXT "^_^"
#else
# define OK_TEXT "✔︎"
# define FAIL_TEXT "𐄂"
# define FINAL_TEXT "🎉"
#endif

#endif /* common_h */
100 changes: 100 additions & 0 deletions test/runner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/

#include "include/common.h"
#include "tests.h"

#include <stdlib.h>
#include <time.h>
#include <string.h>

#define TARGET_NAME "libds"

int
main(int argc, const char * argv[]) {
test_entry_t *entry;
test_status_t st;
int32_t i, count, passed, failed, maxlen;
double start, end, elapsed, total;

passed = failed = maxlen = 0;
total = 0.0;
count = sizeof(tests) / sizeof(tests[0]);

fprintf(stderr, CYAN "\nWelcome to %s tests\n\n" RESET, TARGET_NAME);

for (i = 0; i < count; i++) {
int32_t len;

entry = tests + i;
len = (int32_t)strlen(entry->name);

if (len > maxlen)
maxlen = len;
}

maxlen += 5;

fprintf(stderr,
BOLDWHITE " %-*s %-*s\n",
maxlen, "Test Name", maxlen, "Elapsed Time");

for (i = 0; i < count; i++) {
entry = tests + i;
start = clock();
st = entry->entry();
end = clock();
elapsed = (end - start) / CLOCKS_PER_SEC;
total += elapsed;

if (!st.status) {
fprintf(stderr,
BOLDRED " " FAIL_TEXT BOLDWHITE " %s " RESET, entry->name);
if (st.msg) {
fprintf(stderr,
YELLOW "- %s" RESET,
st.msg);
}

fprintf(stderr, "\n");

failed++;
} else {
fprintf(stderr, GREEN " " OK_TEXT RESET " %-*s ", maxlen, entry->name);

if (elapsed > 0.01)
fprintf(stderr, YELLOW "%.2fs", elapsed);
else
fprintf(stderr, "0");

fprintf(stderr, "\n" RESET);
passed++;
}
}

if (failed == 0) {
fprintf(stderr,
BOLDGREEN "\n All tests are passed " FINAL_TEXT "\n" RESET);
}

fprintf(stderr,
CYAN "\n%s test results (%0.2fs):\n" RESET
"--------------------------\n"

MAGENTA "%d" RESET " tests are runned, "
GREEN "%d" RESET " %s passed, "
RED "%d" RESET " %s failed\n\n" RESET,
TARGET_NAME,
total,
count,
passed,
passed > 1 ? "are" : "is",
failed,
failed > 1 ? "are" : "is");

return failed;
}
3 changes: 1 addition & 2 deletions test/src/test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
*/

#include "test_common.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>

void
rand_str(char *dest, size_t length) {
Expand Down
3 changes: 2 additions & 1 deletion test/src/test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#ifndef test_common_h
#define test_common_h

#include "../include/common.h"

#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <time.h>

#include <stdlib.h>
Expand Down
Loading

0 comments on commit 2215255

Please sign in to comment.