Compare commits

...
4 Commits
Author SHA1 Message Date
paul db29cbb20c Improve tests.h just a little
continuous-integration/drone/push Build is passing
2022-10-26 20:56:33 +02:00
paul afe0276961 Move tests.h to header
continuous-integration/drone/push Build is passing
2022-10-25 17:18:17 +02:00
paul 92ddbac37c Add basic unit tests framework
continuous-integration/drone/push Build is passing
2022-10-25 17:09:21 +02:00
paul d868534516 Move algo2 stuff into its own pipeline
continuous-integration/drone Build is passing
2022-09-15 01:58:41 +02:00
4 changed files with 129 additions and 20 deletions
+7 -3
View File
@@ -2,7 +2,6 @@
kind: pipeline kind: pipeline
type: docker type: docker
name: default name: default
steps: steps:
- name: hello world - name: hello world
image: alpine:3.16.2 image: alpine:3.16.2
@@ -13,12 +12,17 @@ steps:
image: python:3.11-rc-alpine image: python:3.11-rc-alpine
commands: commands:
- python3 -c "print(6 * 7)" - python3 -c "print(6 * 7)"
---
kind: pipeline
type: docker
name: algo2
steps:
- name: pairingheap - name: pairingheap
image: gcc:12.2.0 image: gcc:12.2.0
commands: commands:
- cd algo2/pairingheap - cd algo2/pairingheap
- gcc -Wall -Werror -o main pairingheap.c - gcc -Wall -Werror -o tests pairingheap.c
- ./main - ./tests
- name: radixheap - name: radixheap
image: gcc:12.2.0 image: gcc:12.2.0
commands: commands:
+1 -1
View File
@@ -2,4 +2,4 @@
# CI Playground # CI Playground
This is where is play around with [Drone](https://ci.pbrinkmeier.de). This is where I play around with [Drone](https://ci.pbrinkmeier.de).
+49 -6
View File
@@ -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;
@@ -160,23 +162,64 @@ struct PHeapNode *ph_deletemin(struct PHeap *ph) {
return m; return m;
} }
void ph_deinit_free(struct PHeap *ph) {
while (ph->min != NULL) {
struct PHeapNode *h = ph_deletemin(ph);
free(h);
}
}
int cmp_int(const void *a, const void *b) {
return *((int *) a) - *((int *) b);
}
int main() { int main() {
TEST("consecutive deleteMin calls yield increasing results", {
srand(2309); srand(2309);
struct PHeap ph; struct PHeap ph;
ph_init(&ph); ph_init(&ph);
for (int i = 0; i < 15; i++) { for (int i = 0; i < 1000; i++) {
ph_insert_malloc(&ph, rand() % 100); ph_insert_malloc(&ph, rand());
} }
ph_print(&ph); int max = INT_MIN;
while (ph.min != NULL) { while (ph.min != NULL) {
struct PHeapNode *h = ph_deletemin(&ph); struct PHeapNode *h = ph_deletemin(&ph);
printf("deleteMin: %d\n", h->key); ASSERT(h->key >= max);
max = h->key;
free(h); free(h);
} }
ph_print(&ph); 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
} }
+62
View File
@@ -0,0 +1,62 @@
// 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.
struct tests_state {
int passed_tests;
int run_tests;
int total_tests;
char *current_label;
};
struct tests_state tests_state = { 0, 0, 0, NULL };
/* Basic idea:
TEST("...", {
ASSERT(...)
ASSERT(...)
...
})
TEST("...", {
ASSERT(...)
ASSERT(...)
...
})
DONE
*/
#define TEST(LABEL, BLOCK) \
tests_state.current_label = LABEL; \
tests_state.run_tests++; \
tests_state.total_tests = __COUNTER__; \
BLOCK; \
fprintf(stderr, "[%3d] \x1b[32m%s ✓\x1b[0m\n", tests_state.run_tests, tests_state.current_label); \
tests_state.passed_tests++;
#define ASSERT(EXPR) \
if (!(EXPR)) { \
fprintf(stderr, "[%3d] \x1b[31m%s ❌\x1b[0m\n", tests_state.run_tests, tests_state.current_label); \
fprintf(stderr, " Assertion failed in %s:%d:\n", __FILE__, __LINE__); \
fprintf(stderr, " " #EXPR "\n"); \
goto tests_bail; \
}
#define DONE \
tests_bail: { \
tests_state.total_tests = __COUNTER__; \
fprintf(stderr, "[\x1b[%dm***\x1b[0m] %d/%d tests passed", tests_state.passed_tests == tests_state.total_tests ? 32 : 31, tests_state.passed_tests, tests_state.total_tests); \
if (tests_state.total_tests > tests_state.run_tests) { \
fprintf(stderr, ", %d skipped", tests_state.total_tests - tests_state.run_tests); \
} \
fprintf(stderr, ".\n"); \
if (tests_state.passed_tests != tests_state.total_tests) exit(1); \
}