it2s-itss-ldm/tests/test_metadata.c

210 lines
7.5 KiB
C

#include <metadata.h>
#include <stdlib.h>
#include <unity.h>
void setUp(void) {
// set stuff up here
}
void tearDown(void) {
// clean stuff up here
}
static void testMetadataSingle(void) {
ldm_metadata_storage_t storage;
ldm_metadata_storage_init(&storage);
ldm_metadata_t entry = {
.data_object_id = 1,
.reference_position.lat = 12345678,
.reference_position.lon = 12345678,
.reference_position.alt = 123,
.validity_timestamp = 1000,
};
ldm_metadata_storage_add(&storage, entry);
TEST_ASSERT_MESSAGE(storage.count == 1, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head != NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail != NULL, "List tail is not valid");
entry.validity_timestamp = 2000;
ldm_metadata_storage_update(&storage, entry);
TEST_ASSERT_MESSAGE(storage.count == 1, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head != NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail != NULL, "List tail is not valid");
TEST_ASSERT_MESSAGE(storage.head->metadata.validity_timestamp == 2000, "Entry was not updated correctly");
ldm_metadata_t peek;
int rv = ldm_metadata_storage_peek(&storage, &peek);
TEST_ASSERT_MESSAGE(rv == 0, "Entry was not peeked correctly");
TEST_ASSERT_MESSAGE(peek.validity_timestamp == 2000, "Entry was not peeked correctly");
ldm_metadata_storage_pop(&storage);
TEST_ASSERT_MESSAGE(storage.count == 0, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head == NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail == NULL, "List tail is not valid");
ldm_metadata_storage_delete(&storage, 1);
TEST_ASSERT_MESSAGE(storage.count == 0, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head == NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail == NULL, "List tail is not valid");
rv = ldm_metadata_storage_peek(&storage, &peek);
TEST_ASSERT_MESSAGE(rv == 1, "Entry was not peeked correctly");
ldm_metadata_storage_update(&storage, entry);
TEST_ASSERT_MESSAGE(storage.count == 0, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head == NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail == NULL, "List tail is not valid");
ldm_metadata_storage_pop(&storage);
TEST_ASSERT_MESSAGE(storage.count == 0, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head == NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail == NULL, "List tail is not valid");
}
static void testMetadataMultiple(void) {
ldm_metadata_storage_t storage;
ldm_metadata_storage_init(&storage);
ldm_metadata_t metadata = {
.data_object_id = 1,
.reference_position.lat = 12345678,
.reference_position.lon = 12345678,
.reference_position.alt = 123,
.validity_timestamp = 1000,
};
uint32_t ids[1000] = {0};
for (int i = 0; i < 1000; i++) {
metadata.data_object_id = i;
metadata.validity_timestamp = (rand() % 10000);
ids[i] = i;
ldm_metadata_storage_add(&storage, metadata);
}
TEST_ASSERT_MESSAGE(storage.count == 1000, "Entry count is not correct");
for (int i = 0; i < 30; i++) {
uint32_t rid = rand() % 1000;
while (ids[rid] == 0) {
++rid;
if (rid >= 1000) {
rid = 0;
}
}
metadata.data_object_id = ids[rid];
metadata.validity_timestamp = (rand() % 10000) + 5000;
ldm_metadata_storage_update(&storage, metadata);
ids[rid] = 0;
}
TEST_ASSERT_MESSAGE(storage.count == 1000, "Entry count is not correct");
for (int i = 0; i < 100; i++) {
uint32_t rid = rand() % 1000;
while (ids[rid] == 0) {
++rid;
if (rid >= 1000) {
rid = 0;
}
}
ldm_metadata_storage_delete(&storage, ids[rid]);
ids[rid] = 0;
}
TEST_ASSERT_MESSAGE(storage.count == 900, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head != NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail != NULL, "List tail is not valid");
for (int i = 0; i < 50; i++) {
ldm_metadata_t peek;
ldm_metadata_storage_peek(&storage, &peek);
for (int i = 0; i < 1000; i++) {
if (ids[i] == peek.data_object_id) {
ids[i] = 0;
break;
}
}
ldm_metadata_storage_pop(&storage);
}
TEST_ASSERT_MESSAGE(storage.count == 850, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head != NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail != NULL, "List tail is not valid");
for (int i = 0; i < 100; i++) {
uint32_t rid = rand() % 1000;
while (ids[rid] == 0) {
++rid;
if (rid >= 1000) {
rid = 0;
}
}
ldm_metadata_storage_delete(&storage, ids[rid]);
ids[rid] = 0;
}
TEST_ASSERT_MESSAGE(storage.count == 750, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head != NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail != NULL, "List tail is not valid");
for (int i = 0; i < 50; i++) {
ldm_metadata_t peek;
ldm_metadata_storage_peek(&storage, &peek);
for (int i = 0; i < 1000; i++) {
if (ids[i] == peek.data_object_id) {
ids[i] = 0;
break;
}
}
ldm_metadata_storage_pop(&storage);
}
TEST_ASSERT_MESSAGE(storage.count == 700, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head != NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail != NULL, "List tail is not valid");
ldm_metadata_storage_node_t* node = storage.head;
uint32_t count = 0;
while (node) {
if (node->next) {
TEST_ASSERT_MESSAGE(node->metadata.validity_timestamp <= node->next->metadata.validity_timestamp, "Entries are not sorted");
TEST_ASSERT_MESSAGE(count < 699, "Number of entries is not correct");
} else {
TEST_ASSERT_MESSAGE(count == 699, "Number of entries is not correct");
TEST_ASSERT_MESSAGE(node == storage.tail, "Tail is not correct");
}
count++;
node = node->next;
}
for (int i = 0; i < 700; i++) {
ldm_metadata_storage_pop(&storage);
}
TEST_ASSERT_MESSAGE(storage.count == 0, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head == NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail == NULL, "List tail is not valid");
// re-insert 100 objects and clean list with delete
for (int i = 0; i < 100; i++) {
metadata.data_object_id = i;
metadata.validity_timestamp = (rand() % 10000);
ids[i] = i;
ldm_metadata_storage_add(&storage, metadata);
}
TEST_ASSERT_MESSAGE(storage.count == 100, "Entry count is not correct");
for (int i = 0; i < 100; i++) {
ldm_metadata_storage_delete(&storage, i);
}
TEST_ASSERT_MESSAGE(storage.count == 0, "Entry count is not correct");
TEST_ASSERT_MESSAGE(storage.head == NULL, "List head is not valid");
TEST_ASSERT_MESSAGE(storage.tail == NULL, "List tail is not valid");
}
int main(void) {
// seed rand
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
UNITY_BEGIN();
RUN_TEST(testMetadataSingle);
RUN_TEST(testMetadataMultiple);
return UNITY_END();
}