summaryrefslogtreecommitdiffstats
path: root/example/bin.c
diff options
context:
space:
mode:
authorEthan Werner <mail@ethanwerner.com>2024-04-17 16:00:00 -0700
committerEthan Werner <mail@ethanwerner.com>2026-06-08 16:01:49 +0400
commitf6a144987cc403de8ad6bddd055f756120f8d405 (patch)
tree3b4cfc8c76db7adba8504dc0e079466e2a0d336c /example/bin.c
archival commit
Diffstat (limited to 'example/bin.c')
-rwxr-xr-xexample/bin.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/example/bin.c b/example/bin.c
new file mode 100755
index 0000000..40a7f77
--- /dev/null
+++ b/example/bin.c
@@ -0,0 +1,47 @@
+#include <stdint.h>
+#include <stdio.h>
+
+typedef int32_t bin_key_t;
+
+#define TEST_LENGTH 10
+#define TEST_START -2
+#define TEST_STEP 0.5;
+
+struct sample_t {
+ bin_key_t time;
+ double data;
+};
+
+#define BIN_IMPLEMENTATION
+#include "../include/bin.h"
+
+int main() {
+ char const *path = "./test.bin";
+ FILE *bin = bin_init(path, sizeof(struct sample_t));
+
+ bin_key_t half = (bin_key_t)((TEST_START + TEST_LENGTH) / 2.0);
+
+ // Insert half of the values using bin_insert()
+ for (bin_key_t i = half - 1; i >= TEST_START; i--) {
+ bin_insert(bin, 0, &(struct sample_t){.time = i, .data = 2.0 * i}, 1);
+ }
+
+ // Append half of the values to the end
+ for (bin_key_t i = half; i < TEST_START + TEST_LENGTH; i++) {
+ bin_append(bin, &(struct sample_t){.time = i, .data = 2.0 * i}, 1);
+ }
+
+ uint64_t l = bin_length(bin);
+ uint64_t bs = bin_block_size(bin);
+
+ struct sample_t x[l];
+ bin_read(bin, 0, x, l);
+
+ // Test that all values are correct
+ for (int32_t i = 0; i < TEST_LENGTH; i++) {
+ bin_key_t key = TEST_START + i;
+ assert(x[i].time == key && x[i].data == 2.0 * key);
+ }
+
+ bin_close(bin);
+} \ No newline at end of file