From b0a5cd8a28bf1d1883317ceac6cb8967d840d6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0tetiar?= Date: Tue, 19 Nov 2019 14:31:44 +0100 Subject: add cram based unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For improved QA etc. For the start with initial test cases for avl, base64, jshn and list components. Moved runqueue and blobmsg from examples to tests. Converted just a few first test cases from json-script example into the new cram based unit test, more to come. Signed-off-by: Petr Štetiar --- examples/CMakeLists.txt | 6 -- examples/blobmsg-example.c | 145 -------------------------------------------- examples/runqueue-example.c | 112 ---------------------------------- 3 files changed, 263 deletions(-) delete mode 100644 examples/blobmsg-example.c delete mode 100644 examples/runqueue-example.c (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a635535..04f18b0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -9,15 +9,9 @@ IF (BUILD_EXAMPLES) FIND_LIBRARY(json NAMES json-c json) - ADD_EXECUTABLE(blobmsg-example blobmsg-example.c) - TARGET_LINK_LIBRARIES(blobmsg-example ubox blobmsg_json ${json}) - ADD_EXECUTABLE(ustream-example ustream-example.c) TARGET_LINK_LIBRARIES(ustream-example ubox) - ADD_EXECUTABLE(runqueue-example runqueue-example.c) - TARGET_LINK_LIBRARIES(runqueue-example ubox) - ADD_EXECUTABLE(json_script-example json_script-example.c) TARGET_LINK_LIBRARIES(json_script-example ubox blobmsg_json json_script ${json}) ENDIF() diff --git a/examples/blobmsg-example.c b/examples/blobmsg-example.c deleted file mode 100644 index 56cac27..0000000 --- a/examples/blobmsg-example.c +++ /dev/null @@ -1,145 +0,0 @@ -#include -#include - -#include "blobmsg.h" -#include "blobmsg_json.h" - -static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t"; - -#define indent_printf(indent, ...) do { \ - if (indent > 0) \ - fwrite(indent_str, indent, 1, stderr); \ - fprintf(stderr, __VA_ARGS__); \ -} while(0) - -static void dump_attr_data(struct blob_attr *data, int indent, int next_indent); - -static void -dump_table(struct blob_attr *head, size_t len, int indent, bool array) -{ - struct blob_attr *attr; - struct blobmsg_hdr *hdr; - - indent_printf(indent, "{\n"); - __blob_for_each_attr(attr, head, len) { - hdr = blob_data(attr); - if (!array) - indent_printf(indent + 1, "%s : ", hdr->name); - dump_attr_data(attr, 0, indent + 1); - } - indent_printf(indent, "}\n"); -} - -static void dump_attr_data(struct blob_attr *data, int indent, int next_indent) -{ - int type = blobmsg_type(data); - switch(type) { - case BLOBMSG_TYPE_STRING: - indent_printf(indent, "%s\n", blobmsg_get_string(data)); - break; - case BLOBMSG_TYPE_INT8: - indent_printf(indent, "%d\n", blobmsg_get_u8(data)); - break; - case BLOBMSG_TYPE_INT16: - indent_printf(indent, "%d\n", blobmsg_get_u16(data)); - break; - case BLOBMSG_TYPE_INT32: - indent_printf(indent, "%d\n", blobmsg_get_u32(data)); - break; - case BLOBMSG_TYPE_INT64: - indent_printf(indent, "%"PRIu64"\n", blobmsg_get_u64(data)); - break; - case BLOBMSG_TYPE_DOUBLE: - indent_printf(indent, "%lf\n", blobmsg_get_double(data)); - break; - case BLOBMSG_TYPE_TABLE: - case BLOBMSG_TYPE_ARRAY: - if (!indent) - indent_printf(indent, "\n"); - dump_table(blobmsg_data(data), blobmsg_data_len(data), - next_indent, type == BLOBMSG_TYPE_ARRAY); - break; - } -} - -enum { - FOO_MESSAGE, - FOO_LIST, - FOO_TESTDATA -}; - -static const struct blobmsg_policy pol[] = { - [FOO_MESSAGE] = { - .name = "message", - .type = BLOBMSG_TYPE_STRING, - }, - [FOO_LIST] = { - .name = "list", - .type = BLOBMSG_TYPE_ARRAY, - }, - [FOO_TESTDATA] = { - .name = "testdata", - .type = BLOBMSG_TYPE_TABLE, - }, -}; - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif - -static void dump_message(struct blob_buf *buf) -{ - struct blob_attr *tb[ARRAY_SIZE(pol)]; - - if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) { - fprintf(stderr, "Parse failed\n"); - return; - } - if (tb[FOO_MESSAGE]) - fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE])); - - if (tb[FOO_LIST]) { - fprintf(stderr, "List: "); - dump_table(blobmsg_data(tb[FOO_LIST]), blobmsg_data_len(tb[FOO_LIST]), 0, true); - } - if (tb[FOO_TESTDATA]) { - fprintf(stderr, "Testdata: "); - dump_table(blobmsg_data(tb[FOO_TESTDATA]), blobmsg_data_len(tb[FOO_TESTDATA]), 0, false); - } -} - -static void -fill_message(struct blob_buf *buf) -{ - void *tbl; - - blobmsg_add_string(buf, "message", "Hello, world!"); - - tbl = blobmsg_open_table(buf, "testdata"); - blobmsg_add_double(buf, "double", 1.337e2); - blobmsg_add_u32(buf, "hello", 1); - blobmsg_add_string(buf, "world", "2"); - blobmsg_close_table(buf, tbl); - - tbl = blobmsg_open_array(buf, "list"); - blobmsg_add_u32(buf, NULL, 0); - blobmsg_add_u32(buf, NULL, 1); - blobmsg_add_u32(buf, NULL, 2); - blobmsg_add_double(buf, "double", 1.337e2); - blobmsg_close_table(buf, tbl); -} - -int main(int argc, char **argv) -{ - static struct blob_buf buf; - - blobmsg_buf_init(&buf); - fill_message(&buf); - dump_message(&buf); - fprintf(stderr, "json: %s\n", blobmsg_format_json(buf.head, true)); - - if (buf.buf) - free(buf.buf); - - return 0; -} diff --git a/examples/runqueue-example.c b/examples/runqueue-example.c deleted file mode 100644 index 13ab864..0000000 --- a/examples/runqueue-example.c +++ /dev/null @@ -1,112 +0,0 @@ -/* - * runqueue-example.c - * - * Copyright (C) 2013 Felix Fietkau - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#include "uloop.h" -#include "runqueue.h" - -static struct runqueue q; - -struct sleeper { - struct runqueue_process proc; - int val; -}; - -static void q_empty(struct runqueue *q) -{ - fprintf(stderr, "All done!\n"); - uloop_end(); -} - -static void q_sleep_run(struct runqueue *q, struct runqueue_task *t) -{ - struct sleeper *s = container_of(t, struct sleeper, proc.task); - char str[32]; - pid_t pid; - - fprintf(stderr, "[%d/%d] start 'sleep %d'\n", q->running_tasks, q->max_running_tasks, s->val); - - pid = fork(); - if (pid < 0) - return; - - if (pid) { - runqueue_process_add(q, &s->proc, pid); - return; - } - - sprintf(str, "%d", s->val); - execlp("sleep", "sleep", str, NULL); - exit(1); -} - -static void q_sleep_cancel(struct runqueue *q, struct runqueue_task *t, int type) -{ - struct sleeper *s = container_of(t, struct sleeper, proc.task); - - fprintf(stderr, "[%d/%d] cancel 'sleep %d'\n", q->running_tasks, q->max_running_tasks, s->val); - runqueue_process_cancel_cb(q, t, type); -} - -static void q_sleep_complete(struct runqueue *q, struct runqueue_task *p) -{ - struct sleeper *s = container_of(p, struct sleeper, proc.task); - - fprintf(stderr, "[%d/%d] finish 'sleep %d'\n", q->running_tasks, q->max_running_tasks, s->val); - free(s); -} - -static void add_sleeper(int val) -{ - static const struct runqueue_task_type sleeper_type = { - .run = q_sleep_run, - .cancel = q_sleep_cancel, - .kill = runqueue_process_kill_cb, - }; - struct sleeper *s; - - s = calloc(1, sizeof(*s)); - s->proc.task.type = &sleeper_type; - s->proc.task.run_timeout = 500; - s->proc.task.complete = q_sleep_complete; - s->val = val; - runqueue_task_add(&q, &s->proc.task, false); -} - -int main(int argc, char **argv) -{ - uloop_init(); - - runqueue_init(&q); - q.empty_cb = q_empty; - q.max_running_tasks = 1; - - if (argc > 1) - q.max_running_tasks = atoi(argv[1]); - - add_sleeper(1); - add_sleeper(1); - add_sleeper(1); - uloop_run(); - uloop_done(); - - return 0; -} -- cgit v1.2.3