Move tests.h to header
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
92ddbac37c
commit
afe0276961
@ -21,7 +21,7 @@ steps:
|
|||||||
image: gcc:12.2.0
|
image: gcc:12.2.0
|
||||||
commands:
|
commands:
|
||||||
- cd algo2/pairingheap
|
- cd algo2/pairingheap
|
||||||
- gcc -Wall -Werror -o tests tests.c
|
- gcc -Wall -Werror -o tests pairingheap.c
|
||||||
- ./tests
|
- ./tests
|
||||||
- name: radixheap
|
- name: radixheap
|
||||||
image: gcc:12.2.0
|
image: gcc:12.2.0
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "tests.h"
|
||||||
|
|
||||||
struct PHeapNode {
|
struct PHeapNode {
|
||||||
int key;
|
int key;
|
||||||
struct PHeapNode *parent;
|
struct PHeapNode *parent;
|
||||||
@ -166,3 +168,58 @@ void ph_deinit_free(struct PHeap *ph) {
|
|||||||
free(h);
|
free(h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int cmp_int(const void *a, const void *b) {
|
||||||
|
return *((int *) a) - *((int *) b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
TEST("consecutive deleteMin calls yield increasing results", {
|
||||||
|
srand(2309);
|
||||||
|
|
||||||
|
struct PHeap ph;
|
||||||
|
ph_init(&ph);
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
ph_insert_malloc(&ph, rand());
|
||||||
|
}
|
||||||
|
|
||||||
|
int max = INT_MIN;
|
||||||
|
while (ph.min != NULL) {
|
||||||
|
struct PHeapNode *h = ph_deletemin(&ph);
|
||||||
|
ASSERT(h->key >= max);
|
||||||
|
max = h->key;
|
||||||
|
free(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
ph_deinit_free(&ph);
|
||||||
|
})
|
||||||
|
|
||||||
|
TEST("deleteMin yields the same order as qsort", {
|
||||||
|
srand(2309);
|
||||||
|
|
||||||
|
struct PHeap ph;
|
||||||
|
ph_init(&ph);
|
||||||
|
|
||||||
|
int n = 1000;
|
||||||
|
int xs[n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
xs[i] = rand();
|
||||||
|
ph_insert_malloc(&ph, xs[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
qsort(xs, n, sizeof(int), cmp_int);
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
struct PHeapNode *h = ph_deletemin(&ph);
|
||||||
|
ASSERT(h->key == xs[i]);
|
||||||
|
free(h);
|
||||||
|
}
|
||||||
|
ASSERT(ph.min == NULL);
|
||||||
|
|
||||||
|
ph_deinit_free(&ph);
|
||||||
|
})
|
||||||
|
|
||||||
|
DONE
|
||||||
|
}
|
||||||
|
@ -1,86 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#include "pairingheap.c"
|
|
||||||
|
|
||||||
int tests_passed = 0;
|
|
||||||
char *current_label;
|
|
||||||
int current_num;
|
|
||||||
|
|
||||||
#define TEST(LABEL, BLOCK) \
|
|
||||||
current_label = LABEL; \
|
|
||||||
current_num = __COUNTER__ + 1; \
|
|
||||||
BLOCK; \
|
|
||||||
fprintf(stderr, "[%3d] \x1b[32m%s ✓\x1b[0m\n", current_num, current_label); \
|
|
||||||
tests_passed++;
|
|
||||||
|
|
||||||
#define ASSERT(EXPR) \
|
|
||||||
if (!(EXPR)) { \
|
|
||||||
fprintf(stderr, "[%3d] \x1b[31m%s ❌\x1b[0m\n", current_num, current_label); \
|
|
||||||
fprintf(stderr, " Assertion failed in %s:%d:\n", __FILE__, __LINE__); \
|
|
||||||
fprintf(stderr, " " #EXPR "\n"); \
|
|
||||||
goto after_tests; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define DONE \
|
|
||||||
after_tests: { \
|
|
||||||
int tests_total = __COUNTER__; \
|
|
||||||
fprintf(stderr, "[\x1b[%dm***\x1b[0m] %d/%d tests passed.\n", tests_passed == tests_total ? 32 : 31, tests_passed, tests_total); \
|
|
||||||
if (tests_passed != tests_total) exit(1); \
|
|
||||||
}
|
|
||||||
|
|
||||||
int cmp_int(const void *a, const void *b) {
|
|
||||||
return *((int *) a) - *((int *) b);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
TEST("consecutive deleteMin calls yield increasing results", {
|
|
||||||
srand(2309);
|
|
||||||
|
|
||||||
struct PHeap ph;
|
|
||||||
ph_init(&ph);
|
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
|
||||||
ph_insert_malloc(&ph, rand());
|
|
||||||
}
|
|
||||||
|
|
||||||
int max = INT_MIN;
|
|
||||||
while (ph.min != NULL) {
|
|
||||||
struct PHeapNode *h = ph_deletemin(&ph);
|
|
||||||
ASSERT(h->key >= max);
|
|
||||||
max = h->key;
|
|
||||||
free(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
ph_deinit_free(&ph);
|
|
||||||
})
|
|
||||||
|
|
||||||
TEST("deleteMin yields the same order as qsort", {
|
|
||||||
srand(2309);
|
|
||||||
|
|
||||||
struct PHeap ph;
|
|
||||||
ph_init(&ph);
|
|
||||||
|
|
||||||
int n = 1000;
|
|
||||||
int xs[n];
|
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
xs[i] = rand();
|
|
||||||
ph_insert_malloc(&ph, xs[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
qsort(xs, n, sizeof(int), cmp_int);
|
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
struct PHeapNode *h = ph_deletemin(&ph);
|
|
||||||
ASSERT(h->key == xs[i]);
|
|
||||||
free(h);
|
|
||||||
}
|
|
||||||
ASSERT(ph.min == NULL);
|
|
||||||
|
|
||||||
ph_deinit_free(&ph);
|
|
||||||
})
|
|
||||||
|
|
||||||
DONE
|
|
||||||
}
|
|
52
algo2/pairingheap/tests.h
Normal file
52
algo2/pairingheap/tests.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// single-header unit test framework.
|
||||||
|
// if you want to keep your sanity, do not use this.
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
// globals.
|
||||||
|
int tests_passed = 0;
|
||||||
|
char *current_label;
|
||||||
|
int current_num;
|
||||||
|
|
||||||
|
/* Basic idea:
|
||||||
|
|
||||||
|
TEST("...", {
|
||||||
|
ASSERT(...)
|
||||||
|
ASSERT(...)
|
||||||
|
...
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
TEST("...", {
|
||||||
|
ASSERT(...)
|
||||||
|
ASSERT(...)
|
||||||
|
...
|
||||||
|
})
|
||||||
|
|
||||||
|
DONE
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define TEST(LABEL, BLOCK) \
|
||||||
|
current_label = LABEL; \
|
||||||
|
current_num = __COUNTER__ + 1; \
|
||||||
|
BLOCK; \
|
||||||
|
fprintf(stderr, "[%3d] \x1b[32m%s ✓\x1b[0m\n", current_num, current_label); \
|
||||||
|
tests_passed++;
|
||||||
|
|
||||||
|
#define ASSERT(EXPR) \
|
||||||
|
if (!(EXPR)) { \
|
||||||
|
fprintf(stderr, "[%3d] \x1b[31m%s ❌\x1b[0m\n", current_num, current_label); \
|
||||||
|
fprintf(stderr, " Assertion failed in %s:%d:\n", __FILE__, __LINE__); \
|
||||||
|
fprintf(stderr, " " #EXPR "\n"); \
|
||||||
|
goto after_tests; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DONE \
|
||||||
|
after_tests: { \
|
||||||
|
int tests_total = __COUNTER__; \
|
||||||
|
fprintf(stderr, "[\x1b[%dm***\x1b[0m] %d/%d tests passed.\n", tests_passed == tests_total ? 32 : 31, tests_passed, tests_total); \
|
||||||
|
if (tests_passed != tests_total) exit(1); \
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user