diff options
Diffstat (limited to 'mqtt_websockets/c-rbuf')
-rw-r--r-- | mqtt_websockets/c-rbuf/.github/workflows/codeql-analysis.yml | 67 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/.github/workflows/run-tests.yaml | 13 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/.gitignore | 8 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/LICENSE | 165 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/Makefile | 24 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/README.md | 20 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/build/.keep | 0 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/include/ringbuffer.h | 53 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/src/ringbuffer.c | 209 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/src/ringbuffer_internal.h | 43 | ||||
-rw-r--r-- | mqtt_websockets/c-rbuf/tests/ringbuffer_test.c | 491 |
11 files changed, 0 insertions, 1093 deletions
diff --git a/mqtt_websockets/c-rbuf/.github/workflows/codeql-analysis.yml b/mqtt_websockets/c-rbuf/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 876f7171..00000000 --- a/mqtt_websockets/c-rbuf/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,67 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL" - -on: - push: - branches: [ master ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ master ] - schedule: - - cron: '19 3 * * 1' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - language: [ 'cpp' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] - # Learn more: - # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 - - # ℹ️ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 diff --git a/mqtt_websockets/c-rbuf/.github/workflows/run-tests.yaml b/mqtt_websockets/c-rbuf/.github/workflows/run-tests.yaml deleted file mode 100644 index 700671ec..00000000 --- a/mqtt_websockets/c-rbuf/.github/workflows/run-tests.yaml +++ /dev/null @@ -1,13 +0,0 @@ -name: run-tests -on: - push: - schedule: - - cron: '5 3 * * 0' - pull_request: -jobs: - run-tests: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: make - - run: ./test diff --git a/mqtt_websockets/c-rbuf/.gitignore b/mqtt_websockets/c-rbuf/.gitignore deleted file mode 100644 index 8c15b268..00000000 --- a/mqtt_websockets/c-rbuf/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -build/* -!build/.keep -libringbuffer.* -test -.vscode -*.o -.deps -.dirstamp diff --git a/mqtt_websockets/c-rbuf/LICENSE b/mqtt_websockets/c-rbuf/LICENSE deleted file mode 100644 index 0a041280..00000000 --- a/mqtt_websockets/c-rbuf/LICENSE +++ /dev/null @@ -1,165 +0,0 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. - - 0. Additional Definitions. - - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. - - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. - - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. diff --git a/mqtt_websockets/c-rbuf/Makefile b/mqtt_websockets/c-rbuf/Makefile deleted file mode 100644 index 259f3b13..00000000 --- a/mqtt_websockets/c-rbuf/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -CFLAGS = -Wall -Wextra -std=c99 - -all: libringbuffer.a libringbuffer.so build/ringbuffer_test.o - gcc -o test build/ringbuffer_test.o libringbuffer.a $(CFLAGS) - -libringbuffer.so: build/ringbuffer.o - gcc -shared -o libringbuffer.so build/ringbuffer.o - -libringbuffer.a: build/ringbuffer.o - ar rcs libringbuffer.a build/ringbuffer.o - -build/ringbuffer.o: src/ringbuffer.c src/ringbuffer_internal.h include/ringbuffer.h - gcc -o build/ringbuffer.o -c src/ringbuffer.c -Iinclude -fPIC $(CFLAGS) - -build/ringbuffer_test.o: libringbuffer.a tests/ringbuffer_test.c - gcc -o build/ringbuffer_test.o -c tests/ringbuffer_test.c -Iinclude $(CFLAGS) - -clean: - rm -f build/* - rm -f libringbuffer.a - rm -f libringbuffer.so - rm -f test - -distclean: clean diff --git a/mqtt_websockets/c-rbuf/README.md b/mqtt_websockets/c-rbuf/README.md deleted file mode 100644 index a6f59408..00000000 --- a/mqtt_websockets/c-rbuf/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# c-rbuf [![run-tests](https://github.com/underhood/c-rbuf/workflows/run-tests/badge.svg)](https://github.com/underhood/c-rbuf/actions) - -Simple C ringbuffer implementation with API allowing usage without intermediate buffer. Meant to be as simple as possible to use and integrate. You can copy the files into your project, use it as a static lib, or shared lib depending on what is most convenient for you. - -Usage without intermediate buffer means when reading from file descriptor/socket you can insert data directly into the ringbuffer: -```C -char *ptr; -size_t bytes; -rbuf_t buff = rbuf_create(12345); -ptr = rbuf_get_linear_insert_range(buff, &ret); -read(fd, ptr, bytes); -``` - -**Multiple tail support:** Only single tail is supported but multiple tail support might be implemented in the future. - -**Thread Safety:** Currently you will have to ensure only a single thread accesses the buffer at a time yourself. - -## License - -The Project is released under LGPL v3 license. See [License](LICENSE) diff --git a/mqtt_websockets/c-rbuf/build/.keep b/mqtt_websockets/c-rbuf/build/.keep deleted file mode 100644 index e69de29b..00000000 --- a/mqtt_websockets/c-rbuf/build/.keep +++ /dev/null diff --git a/mqtt_websockets/c-rbuf/include/ringbuffer.h b/mqtt_websockets/c-rbuf/include/ringbuffer.h deleted file mode 100644 index cc8d334c..00000000 --- a/mqtt_websockets/c-rbuf/include/ringbuffer.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * - * Copyright: SPDX-License-Identifier: LGPL-3.0-only - * - * Author: Timotej Šiškovič <timotejs@gmail.com> - * - */ - -#ifndef RINGBUFFER_H -#define RINGBUFFER_H - -#include <stddef.h> - -typedef struct rbuf_t *rbuf_t; - -rbuf_t rbuf_create(size_t size); -void rbuf_free(rbuf_t buffer); -void rbuf_flush(rbuf_t buffer); - -/* /param bytes how much bytes can be copied into pointer returned - * /return pointer where data can be copied to or NULL if buffer full - */ -char *rbuf_get_linear_insert_range(rbuf_t buffer, size_t *bytes); -char *rbuf_get_linear_read_range(rbuf_t buffer, size_t *bytes); - -int rbuf_bump_head(rbuf_t buffer, size_t bytes); -int rbuf_bump_tail(rbuf_t buffer, size_t bytes); - -/* @param buffer related buffer instance - * @returns total capacity of buffer in bytes (not free/used) - */ -size_t rbuf_get_capacity(rbuf_t buffer); - -/* @param buffer related buffer instance - * @returns count of bytes stored in the buffer - */ -size_t rbuf_bytes_available(rbuf_t buffer); - -/* @param buffer related buffer instance - * @returns count of bytes available/free in the buffer (how many more bytes you can store in this buffer) - */ -size_t rbuf_bytes_free(rbuf_t buffer); - -/* writes as many bytes from `data` into the `buffer` as possible - * but maximum `len` bytes - */ -size_t rbuf_push(rbuf_t buffer, const char *data, size_t len); -size_t rbuf_pop(rbuf_t buffer, char *data, size_t len); - -char *rbuf_find_bytes(rbuf_t buffer, const char *needle, size_t needle_bytes, int *found_idx); -int rbuf_memcmp_n(rbuf_t buffer, const char *to_cmp, size_t to_cmp_bytes); - -#endif diff --git a/mqtt_websockets/c-rbuf/src/ringbuffer.c b/mqtt_websockets/c-rbuf/src/ringbuffer.c deleted file mode 100644 index 0dec1365..00000000 --- a/mqtt_websockets/c-rbuf/src/ringbuffer.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * - * Copyright: SPDX-License-Identifier: LGPL-3.0-only - * - * Author: Timotej Šiškovič <timotejs@gmail.com> - * - */ - -#include "ringbuffer.h" -#include "ringbuffer_internal.h" - -#include <stdlib.h> -#include <assert.h> -#include <string.h> - -#define MIN(a,b) (((a)<(b))?(a):(b)) -#define MAX(a,b) (((a)>(b))?(a):(b)) - -// this allows user to use their own -// custom memory allocation functions -#ifdef RBUF_CUSTOM_MALLOC -#include "ringbuffer_pal.h" -#else -#define crbuf_malloc(...) malloc(__VA_ARGS__) -#define crbuf_free(...) free(__VA_ARGS__) -#endif - -rbuf_t rbuf_create(size_t size) -{ - rbuf_t buffer = crbuf_malloc(sizeof(struct rbuf_t) + size); - if (!buffer) - return NULL; - - memset(buffer, 0, sizeof(struct rbuf_t)); - - buffer->data = ((char*)buffer) + sizeof(struct rbuf_t); - - buffer->head = buffer->data; - buffer->tail = buffer->data; - buffer->size = size; - buffer->end = buffer->data + size; - - return buffer; -} - -void rbuf_free(rbuf_t buffer) -{ - crbuf_free(buffer); -} - -void rbuf_flush(rbuf_t buffer) -{ - buffer->head = buffer->data; - buffer->tail = buffer->data; - buffer->size_data = 0; -} - -char *rbuf_get_linear_insert_range(rbuf_t buffer, size_t *bytes) -{ - *bytes = 0; - if (buffer->head == buffer->tail && buffer->size_data) - return NULL; - - *bytes = ((buffer->head >= buffer->tail) ? buffer->end : buffer->tail) - buffer->head; - return buffer->head; -} - -char *rbuf_get_linear_read_range(rbuf_t buffer, size_t *bytes) -{ - *bytes = 0; - if(buffer->head == buffer->tail && !buffer->size_data) - return NULL; - - *bytes = ((buffer->tail >= buffer->head) ? buffer->end : buffer->head) - buffer->tail; - - return buffer->tail; -} - -int rbuf_bump_head(rbuf_t buffer, size_t bytes) -{ - size_t free_bytes = rbuf_bytes_free(buffer); - if (bytes > free_bytes) - return 0; - int i = buffer->head - buffer->data; - buffer->head = &buffer->data[(i + bytes) % buffer->size]; - buffer->size_data += bytes; - return 1; -} - -int rbuf_bump_tail(rbuf_t buffer, size_t bytes) -{ - if(!rbuf_bump_tail_noopt(buffer, bytes)) - return 0; - - // if tail catched up with head - // start writing buffer from beggining - // this is not necessary (rbuf must work well without it) - // but helps to optimize big writes as rbuf_get_linear_insert_range - // will return bigger continuous region - if(buffer->tail == buffer->head) { - assert(buffer->size_data == 0); - rbuf_flush(buffer); - } - - return 1; -} - -size_t rbuf_get_capacity(rbuf_t buffer) -{ - return buffer->size; -} - -size_t rbuf_bytes_available(rbuf_t buffer) -{ - return buffer->size_data; -} - -size_t rbuf_bytes_free(rbuf_t buffer) -{ - return buffer->size - buffer->size_data; -} - -size_t rbuf_push(rbuf_t buffer, const char *data, size_t len) -{ - size_t to_cpy; - char *w_ptr = rbuf_get_linear_insert_range(buffer, &to_cpy); - if(!to_cpy) - return to_cpy; - - to_cpy = MIN(to_cpy, len); - memcpy(w_ptr, data, to_cpy); - rbuf_bump_head(buffer, to_cpy); - if(to_cpy < len) - to_cpy += rbuf_push(buffer, &data[to_cpy], len - to_cpy); - return to_cpy; -} - -size_t rbuf_pop(rbuf_t buffer, char *data, size_t len) -{ - size_t to_cpy; - const char *r_ptr = rbuf_get_linear_read_range(buffer, &to_cpy); - if(!to_cpy) - return to_cpy; - - to_cpy = MIN(to_cpy, len); - memcpy(data, r_ptr, to_cpy); - rbuf_bump_tail(buffer, to_cpy); - if(to_cpy < len) - to_cpy += rbuf_pop(buffer, &data[to_cpy], len - to_cpy); - return to_cpy; -} - -static inline void rbuf_ptr_inc(rbuf_t buffer, const char **ptr) -{ - (*ptr)++; - if(*ptr >= buffer->end) - *ptr = buffer->data; -} - -int rbuf_memcmp(rbuf_t buffer, const char *haystack, const char *needle, size_t needle_bytes) -{ - const char *end = needle + needle_bytes; - - // as head==tail can mean 2 things here - if (haystack == buffer->head && buffer->size_data) { - if (*haystack != *needle) - return (*haystack - *needle); - rbuf_ptr_inc(buffer, &haystack); - needle++; - } - - while (haystack != buffer->head && needle != end) { - if (*haystack != *needle) - return (*haystack - *needle); - rbuf_ptr_inc(buffer, &haystack); - needle++; - } - return 0; -} - -int rbuf_memcmp_n(rbuf_t buffer, const char *to_cmp, size_t to_cmp_bytes) -{ - return rbuf_memcmp(buffer, buffer->tail, to_cmp, to_cmp_bytes); -} - -char *rbuf_find_bytes(rbuf_t buffer, const char *needle, size_t needle_bytes, int *found_idx) -{ - const char *ptr = buffer->tail; - *found_idx = 0; - - if (!rbuf_bytes_available(buffer)) - return NULL; - - if (buffer->head == buffer->tail && buffer->size_data) { - if(!rbuf_memcmp(buffer, ptr, needle, needle_bytes)) - return (char *)ptr; - rbuf_ptr_inc(buffer, &ptr); - (*found_idx)++; - } - - while (ptr != buffer->head) - { - if(!rbuf_memcmp(buffer, ptr, needle, needle_bytes)) - return (char *)ptr; - rbuf_ptr_inc(buffer, &ptr); - (*found_idx)++; - } - return NULL; -} diff --git a/mqtt_websockets/c-rbuf/src/ringbuffer_internal.h b/mqtt_websockets/c-rbuf/src/ringbuffer_internal.h deleted file mode 100644 index f7bdf815..00000000 --- a/mqtt_websockets/c-rbuf/src/ringbuffer_internal.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * Copyright: SPDX-License-Identifier: LGPL-3.0-only - * - * Author: Timotej Šiškovič <timotejs@gmail.com> - * - */ - -#ifndef RINGBUFFER_INTERNAL_H -#define RINGBUFFER_INTERNAL_H - -struct rbuf_t { - char *data; - - // points to next byte where we can write - char *head; - // points to oldest (next to be poped) readable byte - char *tail; - - // to avoid calculating data + size - // all the time - char *end; - - size_t size; - size_t size_data; -}; - -/* this exists so that it can be tested by unit tests - * without optimization that resets head and tail to - * beginning if buffer empty - */ -inline static int rbuf_bump_tail_noopt(rbuf_t buffer, size_t bytes) -{ - if (bytes > buffer->size_data) - return 0; - int i = buffer->tail - buffer->data; - buffer->tail = &buffer->data[(i + bytes) % buffer->size]; - buffer->size_data -= bytes; - - return 1; -} - -#endif diff --git a/mqtt_websockets/c-rbuf/tests/ringbuffer_test.c b/mqtt_websockets/c-rbuf/tests/ringbuffer_test.c deleted file mode 100644 index d810ea5a..00000000 --- a/mqtt_websockets/c-rbuf/tests/ringbuffer_test.c +++ /dev/null @@ -1,491 +0,0 @@ -/* - * - * Copyright: SPDX-License-Identifier: LGPL-3.0-only - * - * Author: Timotej Šiškovič <timotejs@gmail.com> - * - */ - -#include "ringbuffer.h" - -// to be able to access internals -// never do this from app -#include "../src/ringbuffer_internal.h" - -#include <stdio.h> -#include <string.h> - -#define KNRM "\x1B[0m" -#define KRED "\x1B[31m" -#define KGRN "\x1B[32m" -#define KYEL "\x1B[33m" -#define KBLU "\x1B[34m" -#define KMAG "\x1B[35m" -#define KCYN "\x1B[36m" -#define KWHT "\x1B[37m" - -#define UNUSED(x) (void)(x) - -int total_fails = 0; -int total_tests = 0; -int total_checks = 0; - -#define CHECK_EQ_RESULT(x, y) \ - while (s_len--) \ - putchar('.'); \ - printf("%s%s " KNRM "\n", (((x) == (y)) ? KGRN : KRED), (((x) == (y)) ? " PASS " : " FAIL ")); \ - if ((x) != (y)) \ - total_fails++; \ - total_checks++; - -#define CHECK_EQ_PREFIX(x, y, prefix, subtest_name, ...) \ - { \ - int s_len = \ - 100 - \ - printf(("Checking: " KWHT "%s %s%2d " subtest_name " " KNRM), __func__, prefix, subtest_no, ##__VA_ARGS__); \ - CHECK_EQ_RESULT(x, y) \ - } - -#define CHECK_EQ(x, y, subtest_name, ...) \ - { \ - int s_len = \ - 100 - printf(("Checking: " KWHT "%s %2d " subtest_name " " KNRM), __func__, subtest_no, ##__VA_ARGS__); \ - CHECK_EQ_RESULT(x, y) \ - } - -#define TEST_DECL() \ - int subtest_no = 0; \ - printf(KYEL "TEST SUITE: %s\n" KNRM, __func__); \ - total_tests++; - -static void test_rbuf_get_linear_insert_range() -{ - TEST_DECL(); - - // check empty buffer behaviour - rbuf_t buff = rbuf_create(5); - char *to_write; - size_t ret; - to_write = rbuf_get_linear_insert_range(buff, &ret); - CHECK_EQ(ret, 5, "empty size"); - CHECK_EQ(to_write, buff->head, "empty write ptr"); - rbuf_free(buff); - - // check full buffer behaviour - subtest_no++; - buff = rbuf_create(5); - ret = rbuf_bump_head(buff, 5); - CHECK_EQ(ret, 1, "ret"); - to_write = rbuf_get_linear_insert_range(buff, &ret); - CHECK_EQ(to_write, NULL, "writable NULL"); - CHECK_EQ(ret, 0, "writable count = 0"); - - // check buffer flush - subtest_no++; - rbuf_flush(buff); - CHECK_EQ(rbuf_bytes_free(buff), 5, "size_free"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check behaviour head > tail - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 3); - to_write = rbuf_get_linear_insert_range(buff, &ret); - CHECK_EQ(to_write, buff->head, "write location"); - CHECK_EQ(ret, 2, "availible to linear write"); - - // check behaviour tail > head - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 5); - rbuf_bump_tail(buff, 3); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data + 3, "tail_ptr"); - to_write = rbuf_get_linear_insert_range(buff, &ret); - CHECK_EQ(to_write, buff->head, "write location"); - CHECK_EQ(ret, 3, "availible to linear write"); - -/* // check behaviour tail and head at last element - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 4); - rbuf_bump_tail(buff, 4); - CHECK_EQ(buff->head, buff->end - 1, "head_ptr"); - CHECK_EQ(buff->tail, buff->end - 1, "tail_ptr"); - to_write = rbuf_get_linear_insert_range(buff, &ret); - CHECK_EQ(to_write, buff->head, "write location"); - CHECK_EQ(ret, 1, "availible to linear write");*/ - - // check behaviour tail and head at last element - // after rbuf_bump_tail optimisation that restarts buffer - // in case tail catches up with head - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 4); - rbuf_bump_tail(buff, 4); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - to_write = rbuf_get_linear_insert_range(buff, &ret); - CHECK_EQ(to_write, buff->head, "write location"); - CHECK_EQ(ret, 5, "availible to linear write"); -} - -#define _CHECK_EQ(x, y, subtest_name, ...) CHECK_EQ_PREFIX(x, y, prefix, subtest_name, ##__VA_ARGS__) -#define _PREFX "(size = %5zu) " -static void test_rbuf_bump_head_bsize(size_t size) -{ - char prefix[16]; - snprintf(prefix, 16, _PREFX, size); - int subtest_no = 0; - rbuf_t buff = rbuf_create(size); - _CHECK_EQ(rbuf_bytes_free(buff), size, "size_free"); - - subtest_no++; - int ret = rbuf_bump_head(buff, size); - _CHECK_EQ(buff->data, buff->head, "loc"); - _CHECK_EQ(ret, 1, "ret"); - _CHECK_EQ(buff->size_data, buff->size, "size"); - _CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free"); - - subtest_no++; - ret = rbuf_bump_head(buff, 1); - _CHECK_EQ(buff->data, buff->head, "loc no move"); - _CHECK_EQ(ret, 0, "ret error"); - _CHECK_EQ(buff->size_data, buff->size, "size"); - _CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free"); - rbuf_free(buff); - - subtest_no++; - buff = rbuf_create(size); - ret = rbuf_bump_head(buff, size - 1); - _CHECK_EQ(buff->head, buff->end-1, "loc end"); - rbuf_free(buff); -} -#undef _CHECK_EQ - -static void test_rbuf_bump_head() -{ - TEST_DECL(); - UNUSED(subtest_no); - - size_t test_sizes[] = { 1, 2, 3, 5, 6, 7, 8, 100, 99999, 0 }; - for (int i = 0; test_sizes[i]; i++) - test_rbuf_bump_head_bsize(test_sizes[i]); -} - -static void test_rbuf_bump_tail_noopt(int subtest_no) -{ - rbuf_t buff = rbuf_create(10); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - - subtest_no++; - int ret = rbuf_bump_head(buff, 5); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_free(buff), 5, "size_free"); - CHECK_EQ(rbuf_bytes_available(buff), 5, "size_avail"); - CHECK_EQ(buff->head, buff->data + 5, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail_noopt(buff, 2); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 3, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 7, "size_free"); - CHECK_EQ(buff->head, buff->data + 5, "head_ptr"); - CHECK_EQ(buff->tail, buff->data + 2, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail_noopt(buff, 3); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(buff->head, buff->data + 5, "head_ptr"); - CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail_noopt(buff, 1); - CHECK_EQ(ret, 0, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(buff->head, buff->data + 5, "head_ptr"); - CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_head(buff, 7); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 7, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 3, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail_noopt(buff, 5); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check tail can't overrun head - subtest_no++; - ret = rbuf_bump_tail_noopt(buff, 3); - CHECK_EQ(ret, 0, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check head can't overrun tail - subtest_no++; - ret = rbuf_bump_head(buff, 9); - CHECK_EQ(ret, 0, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check head can fill the buffer - subtest_no++; - ret = rbuf_bump_head(buff, 8); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 10, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free"); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check can empty the buffer - subtest_no++; - ret = rbuf_bump_tail_noopt(buff, 10); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); -} - -static void test_rbuf_bump_tail_opt(int subtest_no) -{ - subtest_no++; - rbuf_t buff = rbuf_create(10); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - - subtest_no++; - int ret = rbuf_bump_head(buff, 5); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_free(buff), 5, "size_free"); - CHECK_EQ(rbuf_bytes_available(buff), 5, "size_avail"); - CHECK_EQ(buff->head, buff->data + 5, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail(buff, 2); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 3, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 7, "size_free"); - CHECK_EQ(buff->head, buff->data + 5, "head_ptr"); - CHECK_EQ(buff->tail, buff->data + 2, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail(buff, 3); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail_noopt(buff, 1); - CHECK_EQ(ret, 0, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_head(buff, 6); - ret = rbuf_bump_tail(buff, 5); - ret = rbuf_bump_head(buff, 6); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 7, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 3, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr"); - - subtest_no++; - ret = rbuf_bump_tail(buff, 5); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check tail can't overrun head - subtest_no++; - ret = rbuf_bump_tail(buff, 3); - CHECK_EQ(ret, 0, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check head can't overrun tail - subtest_no++; - ret = rbuf_bump_head(buff, 9); - CHECK_EQ(ret, 0, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free"); - CHECK_EQ(buff->head, buff->data + 2, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check head can fill the buffer - subtest_no++; - ret = rbuf_bump_head(buff, 8); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 10, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free"); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - - // check can empty the buffer - subtest_no++; - ret = rbuf_bump_tail(buff, 10); - CHECK_EQ(ret, 1, "ret"); - CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail"); - CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free"); - CHECK_EQ(buff->head, buff->data, "head_ptr"); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); -} - -static void test_rbuf_bump_tail() -{ - TEST_DECL(); - test_rbuf_bump_tail_noopt(subtest_no); - test_rbuf_bump_tail_opt(subtest_no); -} - -#define ASCII_A 0x61 -#define ASCII_Z 0x7A -#define TEST_DATA_SIZE ASCII_Z-ASCII_A+1 -static void test_rbuf_push() -{ - TEST_DECL(); - rbuf_t buff = rbuf_create(10); - int i; - char test_data[TEST_DATA_SIZE]; - - for (int i = 0; i <= TEST_DATA_SIZE; i++) - test_data[i] = i + ASCII_A; - - int ret = rbuf_push(buff, test_data, 10); - CHECK_EQ(ret, 10, "written 10 bytes"); - CHECK_EQ(rbuf_bytes_free(buff), 0, "empty size == 0"); - for (i = 0; i < 10; i++) - CHECK_EQ(buff->data[i], i + ASCII_A, "Check data"); - - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 5); - rbuf_bump_tail_noopt(buff, 5); //to not reset both pointers to beginning - ret = rbuf_push(buff, test_data, 10); - CHECK_EQ(ret, 10, "written 10 bytes"); - for (i = 0; i < 10; i++) - CHECK_EQ(buff->data[i], ((i+5)%10) + ASCII_A, "Check Data"); - - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 9); - rbuf_bump_tail_noopt(buff, 9); - ret = rbuf_push(buff, test_data, 10); - CHECK_EQ(ret, 10, "written 10 bytes"); - for (i = 0; i < 10; i++) - CHECK_EQ(buff->data[i], ((i + 1) % 10) + ASCII_A, "Check data"); - - // let tail > head - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 9); - rbuf_bump_tail_noopt(buff, 9); - rbuf_bump_head(buff, 1); - ret = rbuf_push(buff, test_data, 9); - CHECK_EQ(ret, 9, "written 9 bytes"); - CHECK_EQ(buff->head, buff->end - 1, "head_ptr"); - CHECK_EQ(buff->tail, buff->head, "tail_ptr"); - rbuf_bump_tail(buff, 1); - //TODO push byte can be usefull optimisation - ret = rbuf_push(buff, &test_data[9], 1); - CHECK_EQ(ret, 1, "written 1 byte"); - CHECK_EQ(rbuf_bytes_free(buff), 0, "empty size == 0"); - for (i = 0; i < 10; i++) - CHECK_EQ(buff->data[i], i + ASCII_A, "Check data"); - - subtest_no++; - rbuf_flush(buff); - rbuf_bump_head(buff, 9); - rbuf_bump_tail_noopt(buff, 7); - rbuf_bump_head(buff, 1); - ret = rbuf_push(buff, test_data, 7); - CHECK_EQ(ret, 7, "written 7 bytes"); - CHECK_EQ(buff->head, buff->data + 7, "head_ptr"); - CHECK_EQ(buff->tail, buff->head, "tail_ptr"); - rbuf_bump_tail(buff, 3); - CHECK_EQ(buff->tail, buff->data, "tail_ptr"); - //TODO push byte can be usefull optimisation - ret = rbuf_push(buff, &test_data[7], 3); - CHECK_EQ(ret, 3, "written 3 bytes"); - CHECK_EQ(rbuf_bytes_free(buff), 0, "empty size == 0"); - for (i = 0; i < 10; i++) - CHECK_EQ(buff->data[i], i + ASCII_A, "Check data"); - - // test can't overfill the buffer - subtest_no++; - rbuf_flush(buff); - rbuf_push(buff, test_data, TEST_DATA_SIZE); - CHECK_EQ(ret, 3, "written 10 bytes"); - for (i = 0; i < 10; i++) - CHECK_EQ(buff->data[i], i + ASCII_A, "Check data"); -} - -#define TEST_RBUF_FIND_BYTES_SIZE 10 -void test_rbuf_find_bytes() -{ - TEST_DECL(); - rbuf_t buff = rbuf_create(TEST_RBUF_FIND_BYTES_SIZE); - char *filler_3 = " "; - char *needle = "needle"; - int idx; - char *ptr; - - // make sure needle is wrapped aroung in the buffer - // to test we still can find it - // target "edle ne" - rbuf_bump_head(buff, TEST_RBUF_FIND_BYTES_SIZE / 2); - rbuf_push(buff, filler_3, strlen(filler_3)); - rbuf_bump_tail(buff, TEST_RBUF_FIND_BYTES_SIZE / 2); - rbuf_push(buff, needle, strlen(needle)); - ptr = rbuf_find_bytes(buff, needle, strlen(needle), &idx); - CHECK_EQ(ptr, buff->data + (TEST_RBUF_FIND_BYTES_SIZE / 2) + strlen(filler_3), "Pointer to needle correct"); - CHECK_EQ(idx, ptr - buff->tail, "Check needle index"); -} - -int main() -{ - test_rbuf_bump_head(); - test_rbuf_bump_tail(); - test_rbuf_get_linear_insert_range(); - test_rbuf_push(); - test_rbuf_find_bytes(); - - printf( - KNRM "Total Tests %d, Total Checks %d, Successful Checks %d, Failed Checks %d\n", - total_tests, total_checks, total_checks - total_fails, total_fails); - if (total_fails) - printf(KRED "!!!Some test(s) Failed!!!\n"); - else - printf(KGRN "ALL TESTS PASSED\n"); - - return total_fails; -} |