aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dynarray_struct.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/dynarray_struct.c')
-rw-r--r--tests/dynarray_struct.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/dynarray_struct.c b/tests/dynarray_struct.c
new file mode 100644
index 0000000..6cce97c
--- /dev/null
+++ b/tests/dynarray_struct.c
@@ -0,0 +1,40 @@
+
+#define CONTAINER_IMPLEMENTATION
+#include "../include/container.h"
+
+#include <stdio.h>
+
+struct point {
+ float x, y, z, w;
+};
+
+void print_point(struct point* p) {
+ printf("{ .x=%.2f, .y=%.2f, .z=%.2f, .w=%.2f };\n", p->x, p->y, p->z, p->w);
+}
+
+void print_array(struct point* p) {
+ for (size_t i = 0; i < array_size(p); ++i) {
+ printf("%zu: ", i);
+ print_point(p+i);
+ }
+}
+
+int point_abs(const void* a, const void* b) {
+ struct point* c = (struct point*) a;
+ struct point* d = (struct point*) b;
+ float absa = c->x * c->x + c->y * c->y + c->z * c->z + c->w * c->w;
+ float absb = d->x * d->x + d->y * d->y + d->z * d->z + d->w * d->w;
+ return absa-absb;
+}
+
+int main()
+{
+ struct point *points = array_new(struct point, 4);
+ for (size_t i = 0; i < array_size(points); ++i) points[i] = (struct point){.x = i, .y = 2 * i, .z = 4 * i, .w = 8 * i};
+ array_push(points, ((struct point){.0f, 2.5f, 3.0, 4.0}) );
+ array_insert(points, ((struct point){0}), 3);
+ array_qsort(points, point_abs);
+ print_array(points);
+ array_free(points);
+ return 0;
+}