Skip to content

Commit

Permalink
example: add kv unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Aug 16, 2023
1 parent 89e9b95 commit 9cc38b4
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/tests/kv/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

ELF2TAB_ARGS += --write_id 17767 --read_ids 17767 --access_ids 17767

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
28 changes: 28 additions & 0 deletions examples/tests/kv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
KV Unit Test App
================

Runs a series of unit tests for the KV store.

Needs to have the `examples/services/unit_test_supervisor` app installed as
well.

Output
------

Should get expected output like:

```
1.000: exists [✓]
1.001: set_get [✓]
1.002: set_get_too_long [✓]
1.003: key_too_long [✓]
1.004: set_value_too_long [✓]
1.005: get_not_found [✓]
1.006: add [✓]
1.007: add_add [✓]
1.008: update [✓]
1.009: update_no_exist [✓]
1.010: delete [✓]
1.011: delete_delete [✓]
Summary 1: [12/12] Passed, [0/12] Failed, [0/12] Incomplete
```
262 changes: 262 additions & 0 deletions examples/tests/kv/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <kv_system.h>
#include <unit_test.h>

#define KEY_LEN 200
#define DATA_LEN 3000

uint8_t key_buf[KEY_LEN];
uint8_t data_buf[DATA_LEN];
uint8_t value_buf[DATA_LEN];


static bool test_exists(void) {
int ret = kv_system_check_status();
CHECK(ret == RETURNCODE_SUCCESS);
return true;
}

static bool test_set_get(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 45;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_get_sync(key_buf, strlen(key), data_buf, DATA_LEN, &value_len);
CHECK(ret == RETURNCODE_SUCCESS);
CHECK(value_len == 45);
for (uint32_t i=0;i<value_len;i++) {
CHECK(value_buf[i] == (uint8_t) i);
}

return true;
}

static bool test_set_get_too_long(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 10;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_get_sync(key_buf, strlen(key), data_buf, 2, &value_len);
CHECK(ret == RETURNCODE_ESIZE);
CHECK(value_len == 10);
for (uint32_t i=0;i<2;i++) {
CHECK(value_buf[i] == (uint8_t) i);
}

return true;
}

static bool test_key_too_long(void) {
int ret;

uint32_t value_len = 0;
ret = kv_system_get_sync(key_buf, KEY_LEN, value_buf, 50,&value_len);
CHECK(ret == RETURNCODE_ESIZE);

value_buf[0] = 'V';
ret = kv_system_set_sync(key_buf, KEY_LEN, value_buf, 1);
CHECK(ret == RETURNCODE_ESIZE);
ret = kv_system_add_sync(key_buf, KEY_LEN, value_buf, 1);
CHECK(ret == RETURNCODE_ESIZE);
ret = kv_system_update_sync(key_buf, KEY_LEN, value_buf, 1);
CHECK(ret == RETURNCODE_ESIZE);

ret = kv_system_delete_sync(key_buf, KEY_LEN);
CHECK(ret == RETURNCODE_ESIZE);

return true;
}

static bool test_set_value_too_long(void) {
int ret;

key_buf[0] = 'K';
ret = kv_system_set_sync(key_buf, 1, value_buf, DATA_LEN);
CHECK(ret == RETURNCODE_ESIZE);

return true;
}

static bool test_get_not_found(void) {
int ret;
char random_key[] = "UOR_knaOI#@GUab09578anqOOI$%";
strcpy((char*) key_buf, random_key);

uint32_t value_len = 0;
ret = kv_system_get_sync(key_buf, strlen(random_key), data_buf, DATA_LEN, &value_len);
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

static bool test_add(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

ret = kv_system_delete_sync(key_buf, strlen(key));
CHECK(ret == RETURNCODE_SUCCESS || ret == RETURNCODE_ENOSUPPORT);

uint32_t value_len = 10;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_add_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

return true;
}

static bool test_add_add(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

ret = kv_system_delete_sync(key_buf, strlen(key));
CHECK(ret == RETURNCODE_SUCCESS || ret == RETURNCODE_ENOSUPPORT);

uint32_t value_len = 10;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_add_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_add_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

static bool test_update(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 10;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) (10-i);
}

ret = kv_system_update_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_get_sync(key_buf, strlen(key), data_buf, 100, &value_len);
CHECK(ret == RETURNCODE_SUCCESS);
CHECK(value_len == 10);
for (uint32_t i=0;i<10;i++) {
CHECK(value_buf[i] == (uint8_t) (10-i));
}

return true;
}

static bool test_update_no_exist(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

ret = kv_system_delete_sync(key_buf, strlen(key));
CHECK(ret == RETURNCODE_SUCCESS || ret == RETURNCODE_ENOSUPPORT);

uint32_t value_len = 10;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_update_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

static bool test_delete(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 10;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_delete_sync(key_buf, strlen(key));
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_get_sync(key_buf, strlen(key), data_buf, 100, &value_len);
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

static bool test_delete_delete(void) {
int ret;
char key[] = "kvtestapp";
strcpy((char*) key_buf, key);

uint32_t value_len = 10;
for (uint32_t i=0;i<value_len;i++) {
value_buf[i] = (uint8_t) i;
}

ret = kv_system_set_sync(key_buf, strlen(key), value_buf, value_len);
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_delete_sync(key_buf, strlen(key));
CHECK(ret == RETURNCODE_SUCCESS);

ret = kv_system_delete_sync(key_buf, strlen(key));
CHECK(ret == RETURNCODE_ENOSUPPORT);

return true;
}

int main(void) {
unit_test_fun tests[] = {
TEST(exists),
TEST(set_get),
TEST(set_get_too_long),
TEST(key_too_long),
TEST(set_value_too_long),
TEST(get_not_found),
TEST(add),
TEST(add_add),
TEST(update),
TEST(update_no_exist),
TEST(delete),
TEST(delete_delete),
};
unit_test_runner(tests, sizeof(tests) / sizeof(unit_test_fun), 2000, "org.tockos.unit_test");
return 0;
}

0 comments on commit 9cc38b4

Please sign in to comment.