diff options
Diffstat (limited to 'tests/unit/test_negentropy_adapter.c')
| -rw-r--r-- | tests/unit/test_negentropy_adapter.c | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/tests/unit/test_negentropy_adapter.c b/tests/unit/test_negentropy_adapter.c new file mode 100644 index 0000000..1693ca6 --- /dev/null +++ b/tests/unit/test_negentropy_adapter.c | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | |||
| 7 | typedef struct { | ||
| 8 | uint64_t created_at; | ||
| 9 | uint8_t id[32]; | ||
| 10 | } negentropy_item_t; | ||
| 11 | |||
| 12 | typedef struct negentropy_adapter { | ||
| 13 | void *storage; | ||
| 14 | negentropy_item_t *items; | ||
| 15 | size_t count; | ||
| 16 | size_t capacity; | ||
| 17 | } negentropy_adapter_t; | ||
| 18 | |||
| 19 | static negentropy_adapter_t *negentropy_adapter_from_storage(void *storage_engine) | ||
| 20 | { | ||
| 21 | if (!storage_engine) return NULL; | ||
| 22 | negentropy_adapter_t *adapter = calloc(1, sizeof(negentropy_adapter_t)); | ||
| 23 | if (!adapter) return NULL; | ||
| 24 | adapter->storage = storage_engine; | ||
| 25 | return adapter; | ||
| 26 | } | ||
| 27 | |||
| 28 | static void negentropy_adapter_destroy(negentropy_adapter_t *adapter) | ||
| 29 | { | ||
| 30 | if (!adapter) return; | ||
| 31 | if (adapter->items) free(adapter->items); | ||
| 32 | free(adapter); | ||
| 33 | } | ||
| 34 | |||
| 35 | static int adapter_insert(negentropy_adapter_t *adapter, uint64_t created_at, const uint8_t *id) | ||
| 36 | { | ||
| 37 | if (!adapter || !id) return -1; | ||
| 38 | if (adapter->count >= adapter->capacity) { | ||
| 39 | size_t new_cap = adapter->capacity == 0 ? 64 : adapter->capacity * 2; | ||
| 40 | negentropy_item_t *new_items = realloc(adapter->items, new_cap * sizeof(negentropy_item_t)); | ||
| 41 | if (!new_items) return -2; | ||
| 42 | adapter->items = new_items; | ||
| 43 | adapter->capacity = new_cap; | ||
| 44 | } | ||
| 45 | negentropy_item_t *item = &adapter->items[adapter->count]; | ||
| 46 | item->created_at = created_at; | ||
| 47 | memcpy(item->id, id, 32); | ||
| 48 | adapter->count++; | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | |||
| 52 | static int test_adapter_create(void) { | ||
| 53 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 54 | ASSERT(a != NULL, "adapter created from storage"); | ||
| 55 | ASSERT(a->storage == (void*)0x1, "storage pointer set"); | ||
| 56 | ASSERT(a->count == 0, "initial count is 0"); | ||
| 57 | ASSERT(a->items == NULL, "initial items is NULL"); | ||
| 58 | negentropy_adapter_destroy(a); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | static int test_adapter_null_storage(void) { | ||
| 63 | negentropy_adapter_t *a = negentropy_adapter_from_storage(NULL); | ||
| 64 | ASSERT(a == NULL, "NULL storage returns NULL adapter"); | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | static int test_adapter_insert(void) { | ||
| 69 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 70 | uint8_t id[32]; | ||
| 71 | memset(id, 0xAA, 32); | ||
| 72 | int rc = adapter_insert(a, 1700000000, id); | ||
| 73 | ASSERT(rc == 0, "insert succeeds"); | ||
| 74 | ASSERT(a->count == 1, "count is 1 after insert"); | ||
| 75 | ASSERT(a->items[0].created_at == 1700000000, "created_at stored"); | ||
| 76 | ASSERT(memcmp(a->items[0].id, id, 32) == 0, "id stored correctly"); | ||
| 77 | negentropy_adapter_destroy(a); | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | static int test_adapter_insert_multiple(void) { | ||
| 82 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 83 | uint8_t id1[32]; memset(id1, 0x11, 32); | ||
| 84 | uint8_t id2[32]; memset(id2, 0x22, 32); | ||
| 85 | uint8_t id3[32]; memset(id3, 0x33, 32); | ||
| 86 | |||
| 87 | adapter_insert(a, 100, id1); | ||
| 88 | adapter_insert(a, 200, id2); | ||
| 89 | adapter_insert(a, 300, id3); | ||
| 90 | |||
| 91 | ASSERT(a->count == 3, "count is 3 after 3 inserts"); | ||
| 92 | ASSERT(a->items[0].created_at == 100, "item 0 created_at"); | ||
| 93 | ASSERT(a->items[1].created_at == 200, "item 1 created_at"); | ||
| 94 | ASSERT(a->items[2].created_at == 300, "item 2 created_at"); | ||
| 95 | ASSERT(memcmp(a->items[0].id, id1, 32) == 0, "item 0 id"); | ||
| 96 | ASSERT(memcmp(a->items[1].id, id2, 32) == 0, "item 1 id"); | ||
| 97 | ASSERT(memcmp(a->items[2].id, id3, 32) == 0, "item 2 id"); | ||
| 98 | |||
| 99 | negentropy_adapter_destroy(a); | ||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | static int test_adapter_grow(void) { | ||
| 104 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 105 | uint8_t id[32]; | ||
| 106 | for (int i = 0; i < 100; i++) { | ||
| 107 | memset(id, i, 32); | ||
| 108 | int rc = adapter_insert(a, i, id); | ||
| 109 | ASSERT(rc == 0, "insert succeeds"); | ||
| 110 | } | ||
| 111 | ASSERT(a->count == 100, "count is 100"); | ||
| 112 | ASSERT(a->capacity >= 100, "capacity >= 100"); | ||
| 113 | negentropy_adapter_destroy(a); | ||
| 114 | return 0; | ||
| 115 | } | ||
| 116 | |||
| 117 | static int test_adapter_destroy_null(void) { | ||
| 118 | negentropy_adapter_destroy(NULL); | ||
| 119 | ASSERT(1, "destroy NULL does not crash"); | ||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | |||
| 123 | int main(void) { | ||
| 124 | int failed = 0; | ||
| 125 | failed += test_adapter_create(); | ||
| 126 | failed += test_adapter_null_storage(); | ||
| 127 | failed += test_adapter_insert(); | ||
| 128 | failed += test_adapter_insert_multiple(); | ||
| 129 | failed += test_adapter_grow(); | ||
| 130 | failed += test_adapter_destroy_null(); | ||
| 131 | |||
| 132 | if (failed == 0) { | ||
| 133 | printf("\n=== ALL NEGENTROPY ADAPTER TESTS PASSED ===\n"); | ||
| 134 | } | ||
| 135 | return failed; | ||
| 136 | } | ||