aboutsummaryrefslogtreecommitdiff
path: root/minimal-examples/api-tests
diff options
context:
space:
mode:
authorAndy Green <andy@warmcat.com>2018-10-14 06:12:27 +0800
committerAndy Green <andy@warmcat.com>2018-10-16 05:05:56 +0800
commita5b2248e727b64e781ffb5659e3c9d1499055d17 (patch)
tree424a314b4ba55ee62eb3f6eef974bb329b24faeb /minimal-examples/api-tests
parented00704566080327ea2b41e2a99fa5d88b0b533b (diff)
downloadlibwebsockets-a5b2248e727b64e781ffb5659e3c9d1499055d17.tar.gz
lwsac
Introduce an api for efficiently dealing with allocations for large, unknown amounts of objects.
Diffstat (limited to 'minimal-examples/api-tests')
-rw-r--r--minimal-examples/api-tests/README.md5
-rw-r--r--minimal-examples/api-tests/api-test-lwsac/CMakeLists.txt73
-rw-r--r--minimal-examples/api-tests/api-test-lwsac/README.md22
-rw-r--r--minimal-examples/api-tests/api-test-lwsac/main.c81
-rwxr-xr-xminimal-examples/api-tests/api-test-lwsac/selftest.sh24
5 files changed, 205 insertions, 0 deletions
diff --git a/minimal-examples/api-tests/README.md b/minimal-examples/api-tests/README.md
new file mode 100644
index 00000000..b047af52
--- /dev/null
+++ b/minimal-examples/api-tests/README.md
@@ -0,0 +1,5 @@
+|name|tests|
+---|---
+api-test-lwsac|LWS Allocated Chunks
+api-test-lws_tokenize|Generic secure string tokenizer
+
diff --git a/minimal-examples/api-tests/api-test-lwsac/CMakeLists.txt b/minimal-examples/api-tests/api-test-lwsac/CMakeLists.txt
new file mode 100644
index 00000000..a73c6807
--- /dev/null
+++ b/minimal-examples/api-tests/api-test-lwsac/CMakeLists.txt
@@ -0,0 +1,73 @@
+cmake_minimum_required(VERSION 2.8)
+include(CheckCSourceCompiles)
+
+set(SAMP lws-api-test-lwsac)
+set(SRCS main.c)
+
+# If we are being built as part of lws, confirm current build config supports
+# reqconfig, else skip building ourselves.
+#
+# If we are being built externally, confirm installed lws was configured to
+# support reqconfig, else error out with a helpful message about the problem.
+#
+MACRO(require_lws_config reqconfig _val result)
+
+ if (DEFINED ${reqconfig})
+ if (${reqconfig})
+ set (rq 1)
+ else()
+ set (rq 0)
+ endif()
+ else()
+ set(rq 0)
+ endif()
+
+ if (${_val} EQUAL ${rq})
+ set(SAME 1)
+ else()
+ set(SAME 0)
+ endif()
+
+ if (LWS_WITH_MINIMAL_EXAMPLES AND NOT ${SAME})
+ if (${_val})
+ message("${SAMP}: skipping as lws being built without ${reqconfig}")
+ else()
+ message("${SAMP}: skipping as lws built with ${reqconfig}")
+ endif()
+ set(${result} 0)
+ else()
+ if (LWS_WITH_MINIMAL_EXAMPLES)
+ set(MET ${SAME})
+ else()
+ CHECK_C_SOURCE_COMPILES("#include <libwebsockets.h>\nint main(void) {\n#if defined(${reqconfig})\n return 0;\n#else\n fail;\n#endif\n return 0;\n}\n" HAS_${reqconfig})
+ if (NOT DEFINED HAS_${reqconfig} OR NOT HAS_${reqconfig})
+ set(HAS_${reqconfig} 0)
+ else()
+ set(HAS_${reqconfig} 1)
+ endif()
+ if ((HAS_${reqconfig} AND ${_val}) OR (NOT HAS_${reqconfig} AND NOT ${_val}))
+ set(MET 1)
+ else()
+ set(MET 0)
+ endif()
+ endif()
+ if (NOT MET)
+ if (${_val})
+ message(FATAL_ERROR "This project requires lws must have been configured with ${reqconfig}")
+ else()
+ message(FATAL_ERROR "Lws configuration of ${reqconfig} is incompatible with this project")
+ endif()
+ endif()
+ endif()
+ENDMACRO()
+
+
+
+ add_executable(${SAMP} ${SRCS})
+
+ if (websockets_shared)
+ target_link_libraries(${SAMP} websockets_shared)
+ add_dependencies(${SAMP} websockets_shared)
+ else()
+ target_link_libraries(${SAMP} websockets)
+ endif()
diff --git a/minimal-examples/api-tests/api-test-lwsac/README.md b/minimal-examples/api-tests/api-test-lwsac/README.md
new file mode 100644
index 00000000..74034c79
--- /dev/null
+++ b/minimal-examples/api-tests/api-test-lwsac/README.md
@@ -0,0 +1,22 @@
+# lws api test lwsac
+
+Demonstrates how to use and performs selftests for lwsac
+
+## build
+
+```
+ $ cmake . && make
+```
+
+## usage
+
+Commandline option|Meaning
+---|---
+-d <loglevel>|Debug verbosity in decimal, eg, -d15
+
+```
+ $ ./lws-api-test-lwsac
+[2018/10/09 09:14:17:4834] USER: LWS API selftest: lwsac
+[2018/10/09 09:14:17:4835] USER: Completed: PASS
+```
+
diff --git a/minimal-examples/api-tests/api-test-lwsac/main.c b/minimal-examples/api-tests/api-test-lwsac/main.c
new file mode 100644
index 00000000..854e0adc
--- /dev/null
+++ b/minimal-examples/api-tests/api-test-lwsac/main.c
@@ -0,0 +1,81 @@
+/*
+ * lws-api-test-lwsac
+ *
+ * Copyright (C) 2018 Andy Green <andy@warmcat.com>
+ *
+ * This file is made available under the Creative Commons CC0 1.0
+ * Universal Public Domain Dedication.
+ */
+
+#include <libwebsockets.h>
+
+struct mytest {
+ int payload;
+ /* notice doesn't have to be at start of struct */
+ lws_list_ptr list_next;
+ /* a struct can appear on multiple lists too... */
+};
+
+/* converts a ptr to struct mytest .list_next to a ptr to struct mytest */
+#define list_to_mytest(p) lws_list_ptr_container(p, struct mytest, list_next)
+
+int main(int argc, const char **argv)
+{
+ int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE, acc;
+ lws_list_ptr list_head = NULL, iter;
+ struct lwsac *lwsac = NULL;
+ struct mytest *m;
+ const char *p;
+
+ if ((p = lws_cmdline_option(argc, argv, "-d")))
+ logs = atoi(p);
+
+ lws_set_log_level(logs, NULL);
+ lwsl_user("LWS API selftest: lwsac\n");
+
+ /*
+ * 1) allocate and create 1000 struct mytest in a linked-list
+ */
+
+ for (n = 0; n < 1000; n++) {
+ m = lwsac_use(&lwsac, sizeof(*m), 0);
+ m->payload = n;
+
+ lws_list_ptr_insert(&list_head, &m->list_next, NULL);
+ }
+
+ /*
+ * 2) report some debug info about the lwsac state... those 1000
+ * allocations actually only required 4 mallocs
+ */
+
+ lwsac_info(lwsac);
+
+ /* 3) iterate the list, accumulating the payloads */
+
+ acc = 0;
+ iter = list_head;
+ while (iter) {
+ m = list_to_mytest(iter);
+ acc += m->payload;
+
+ lws_list_ptr_advance(iter);
+ }
+
+ if (acc != 499500) {
+ lwsl_err("%s: FAIL acc %d\n", __func__, acc);
+
+ return 1;
+ }
+
+ /*
+ * 4) deallocate everything (lwsac is also set to NULL). It just
+ * deallocates the 4 mallocs, everything in there is gone accordingly
+ */
+
+ lwsac_free(&lwsac);
+
+ lwsl_user("Completed: PASS\n");
+
+ return 0;
+}
diff --git a/minimal-examples/api-tests/api-test-lwsac/selftest.sh b/minimal-examples/api-tests/api-test-lwsac/selftest.sh
new file mode 100755
index 00000000..16d1e2e8
--- /dev/null
+++ b/minimal-examples/api-tests/api-test-lwsac/selftest.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+#
+# $1: path to minimal example binaries...
+# if lws is built with -DLWS_WITH_MINIMAL_EXAMPLES=1
+# that will be ./bin from your build dir
+#
+# $2: path for logs and results. The results will go
+# in a subdir named after the directory this script
+# is in
+#
+# $3: offset for test index count
+#
+# $4: total test count
+#
+# $5: path to ./minimal-examples dir in lws
+#
+# Test return code 0: OK, 254: timed out, other: error indication
+
+. $5/selftests-library.sh
+
+COUNT_TESTS=1
+
+dotest $1 $2 apiselftest
+exit $FAILS