diff --git a/LICENSE b/LICENSE index 261eeb9..38ce70c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,201 +1,24 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. +Copyright (c) 2021 Quantum Leaps (https:://www.state-machine.com) + +SPDX-License-Identifier: MIT + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index d146672..aa7e3c5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ of a ring buffer (a.k.a. circular buffer) in C, specifically suitable for embedded systems. ---------------------------------------------------------------------- + # Lock-Free Restrictions The ring buffer does not require any "locking" (mutual exclusion mechanism) as long as the following restrictions are met: @@ -15,32 +15,43 @@ In other words, a given LFRB can be used only by one pair of producer/ consumer threads/interrupts. Fortunately, this is the most frequently encountered scenario. ---------------------------------------------------------------------- +There are also some additional assumptions about the "atomicity" of the +LFRB counters, which are spelled out in the [ring_buf.h](src/ring_buf.h) +header file. + # Code Structure The ring buffer implementation consists of two files located in the src directory: -- ring_buf.h - contains the interface -- ring_buf.c - contains the implementation +- [ring_buf.h](src/ring_buf.h) - contains the interface +- [ring_buf.c](src/ring_buf.c) - contains the implementation The ring buffer holds elements of they type RingBufElement, which -can be customized (typically uint8_t, uint16_t, uint32_t, float, etc.) +can be customized (typically `uint8_t`, `uint16_t`, `uint32_t`, `float`, +`void*` (pointers), etc.) + ---------------------------------------------------------------------- # Test/Example of Use -The directory test contains test/example of using the ring buffer. -Specifically the file test_ring_buf.c tests the whole interface. +The directory `test` contains a very "Simple Unit Testing (SUTEST)" framework +for unit testing, consisting of the files: + +- [sutest.h](test/sutest.h) - SUTEST interface +- [sutest.c](test/sutest.c) - SUTEST implementation + +The SUTEST framework is used to test the LFRB. Specifically, the tests +and examples of use of LFRB are located in the file: + +- [test_ring_buf.c](test/test_ring_buf.c) - example of use and tests the LFRB. + ---------------------------------------------------------------------- # Licensing -LFRB is [licensed](LICENSE.txt) under the MIT open source license. +The LFRB is [licensed](LICENSE.txt) under the MIT open source license. + ---------------------------------------------------------------------- # Comments/Discussion If you'd like to discuss LFRB or related subjects, plese use the ["Issues" tab](https://github.com/QuantumLeaps/lock-free-ring-buffer/issues). ---------------------------------------------------------------------- # Contact Information [state-machine.com](https://www.state-machine.com) diff --git a/src/ring_buf.c b/src/ring_buf.c index dbf1d32..baa23cc 100644 --- a/src/ring_buf.c +++ b/src/ring_buf.c @@ -1,9 +1,4 @@ -/** -* @file -* @brief Lock-free ring buffer -*/ -/***************************************************************************** -* Last updated on 2021-03-02 +/*============================================================================ * * Q u a n t u m L e a P s * ------------------------ @@ -11,30 +6,19 @@ * * Copyright (C) 2021 Quantum Leaps, LLC. All rights reserved. * -* This software is licensed under the following open source MIT license: -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -* DEALINGS IN THE SOFTWARE. +* SPDX-License-Identifier: MIT * * Contact information: * * +============================================================================*/ +/*! +* @date Last updated on: 2022-06-09 +* +* @file +* @brief Lock-free ring buffer */ + #include #include @@ -55,7 +39,7 @@ bool RingBuf_put(RingBuf * const me, RingBufElement const el) { head = 0U; } if (head != me->tail) { /* buffer NOT full? */ - me->buf[me->head] = el; + me->buf[me->head] = el; /* copy the element into the buffer */ me->head = head; /* update the head to a *valid* index */ return true; /* element placed in the buffer */ } @@ -80,18 +64,6 @@ bool RingBuf_get(RingBuf * const me, RingBufElement *pel) { } } /*..........................................................................*/ -void RingBuf_process_all(RingBuf * const me, RingBufHandler handler) { - RingBufCtr tail = me->tail; - while (me->head != tail) { /* ring buffer NOT empty? */ - (*handler)(me->buf[tail]); - ++tail; - if (tail == me->end) { - tail = 0U; - } - me->tail = tail; /* update the tail to a *valid* index */ - } -} -/*..........................................................................*/ RingBufCtr RingBuf_num_free(RingBuf * const me) { RingBufCtr head = me->head; RingBufCtr tail = me->tail; @@ -105,3 +77,16 @@ RingBufCtr RingBuf_num_free(RingBuf * const me) { return (RingBufCtr)(me->end + tail - head - 1U); } } + +/*..........................................................................*/ +void RingBuf_process_all(RingBuf * const me, RingBufHandler handler) { + RingBufCtr tail = me->tail; + while (me->head != tail) { /* ring buffer NOT empty? */ + (*handler)(me->buf[tail]); + ++tail; + if (tail == me->end) { + tail = 0U; + } + me->tail = tail; /* update the tail to a *valid* index */ + } +} diff --git a/src/ring_buf.h b/src/ring_buf.h index 39749b3..6cfaeb8 100644 --- a/src/ring_buf.h +++ b/src/ring_buf.h @@ -1,9 +1,4 @@ -/** -* @file -* @brief Lock-free ring buffer -*/ -/***************************************************************************** -* Last updated on 2022-02-22 +/*============================================================================ * * Q u a n t u m L e a P s * ------------------------ @@ -11,35 +6,24 @@ * * Copyright (C) 2021 Quantum Leaps, LLC. All rights reserved. * -* This software is licensed under the following open source MIT license: -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -* DEALINGS IN THE SOFTWARE. +* SPDX-License-Identifier: MIT * * Contact information: * * +============================================================================*/ +/*! +* @date Last updated on: 2022-06-09 +* +* @file +* @brief Lock-free ring buffer */ #ifndef RING_BUF_H #define RING_BUF_H -/* -* ASSUMPTION: +/*! Ring buffer counter/index +* +* @attention * The following RingBufCtr type is assumed to be "atomic" in a target CPU, * meaning that the CPU needs to write the whole RingBufCtr in one machine * instruction. An example of violating this assumption would be an 8-bit @@ -56,22 +40,40 @@ */ typedef uint16_t RingBufCtr; +/*! Ring buffer element type +* +* @details +* The type of the ring buffer elements is not critical for the lock-free +* operation and does not need to be "atomic". For example, it can be +* an integer type (uint16_t, uint32_t, uint64_t), a pointer type, +* or even a struct type. However, the bigger the type the more RAM is +* needed for the ring buffer and more CPU cycles are needed to copy it +* into and out of the buffer memory. +*/ typedef uint8_t RingBufElement; +/*! Ring buffer struct */ typedef struct { RingBufElement *buf; /*!< pointer to the start of the ring buffer */ RingBufCtr end; /*!< offset of the end of the ring buffer */ RingBufCtr volatile head; /*!< offset to where next el. will be inserted */ - RingBufCtr volatile tail; /*!< offset of where next el. will be extracted */ + RingBufCtr volatile tail; /*!< offset of where next el. will be removed */ } RingBuf; -typedef void (*RingBufHandler)(RingBufElement const el); - void RingBuf_ctor(RingBuf * const me, RingBufElement sto[], RingBufCtr sto_len); +RingBufCtr RingBuf_num_free(RingBuf * const me); bool RingBuf_put(RingBuf * const me, RingBufElement const el); bool RingBuf_get(RingBuf * const me, RingBufElement *pel); + +/*! Ring buffer callback function for RingBuf_process_all() +* +* @details +* The callback processes one element and runs in the context of +* RingBuf_process_all(). +*/ +typedef void (*RingBufHandler)(RingBufElement const el); + void RingBuf_process_all(RingBuf * const me, RingBufHandler handler); -RingBufCtr RingBuf_num_free(RingBuf * const me); -#endif /* RING_BUF */ +#endif /* RING_BUF_H */ diff --git a/test/sutest.c b/test/sutest.c index cf80769..ad9e590 100644 --- a/test/sutest.c +++ b/test/sutest.c @@ -1,9 +1,4 @@ -/** -* @file -* @brief Simple Unit Testing ("SUTEST") framework -*/ -/***************************************************************************** -* Last updated on 2022-06-08 +/*============================================================================ * * Q u a n t u m L e a P s * ------------------------ @@ -11,42 +6,32 @@ * * Copyright (C) 2021 Quantum Leaps, LLC. All rights reserved. * -* This software is licensed under the following open source MIT license: -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -* DEALINGS IN THE SOFTWARE. +* SPDX-License-Identifier: MIT * * Contact information: * * +============================================================================*/ +/*! +* @date Last updated on: 2022-06-09 +* +* @file +* @brief Simple Unit Testing ("SUTEST") framework */ + #include "sutest.h" #include /* for printf() */ #include /* for exit() */ +/* is this Windows OS? */ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #define WIN32_LEAN_AND_MEAN #include /* for DebugBreak() */ #define DBG_BREAK() DebugBreak() -#else +#else /* not Windows */ #define DBG_BREAK() ((void)0) @@ -58,17 +43,20 @@ static int l_test_count; /*..........................................................................*/ void TEST(char const *title) { if (l_test_count > 0) { + tearDown(); printf(" PASSED\n"); } - printf("test: \"%s\" ...", title); + printf("test[%2d] | \"%s\"", l_test_count, title); + setUp(); + printf("%s", "..."); ++l_test_count; } /*..........................................................................*/ void TEST_fail_(char const *cond, char const *file, int line) { printf(" FAILED in %s:%d\n" - "%s\n" - "---------------------------------------------\n" + "expected : \"%s\"\n" + "-------------------------------------------------------\n" "%d test(s)\n" "FAILED\n", file, line, cond, l_test_count); @@ -79,14 +67,17 @@ void TEST_fail_(char const *cond, char const *file, int line) { /*..........................................................................*/ int main(void) { - printf("\n%s\n", "Simple Unit Internal Testing -- SUTEST"); + printf("\n%s\n%s\n", + "\"SUTEST\": Simple Unit Testing (github.com/QuantumLeaps)", + "-------------------------------------------------------"); TEST_onRun(); if (l_test_count > 0) { + tearDown(); printf("%s\n", " PASSED"); } - printf("---------------------------------------------\n" + printf("-------------------------------------------------------\n" "%d test(s)\nOK\n", l_test_count); return 0; /* success */ diff --git a/test/sutest.h b/test/sutest.h index 8941d20..c1a68f3 100644 --- a/test/sutest.h +++ b/test/sutest.h @@ -1,9 +1,4 @@ -/** -* @file -* @brief Simple Unit Testing ("SUTEST") framework -*/ -/***************************************************************************** -* Last updated on 2021-02-12 +/*============================================================================ * * Q u a n t u m L e a P s * ------------------------ @@ -11,29 +6,17 @@ * * Copyright (C) 2021 Quantum Leaps, LLC. All rights reserved. * -* This software is licensed under the following open source MIT license: -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -* DEALINGS IN THE SOFTWARE. +* SPDX-License-Identifier: MIT * * Contact information: * * +============================================================================*/ +/*! +* @date Last updated on: 2022-06-09 +* +* @file +* @brief Simple Unit Testing ("SUTEST") framework */ #ifndef SUTEST_H #define SUTEST_H @@ -51,6 +34,9 @@ void TEST(char const *title); void TEST_fail_(char const *cond, char const *module, int line); void TEST_onRun(void); /* user-defined callback to run the tests */ +void setUp(void); +void tearDown(void); + #ifdef __cplusplus } #endif diff --git a/test/test_ring_buf.c b/test/test_ring_buf.c index d2cc19a..c471878 100644 --- a/test/test_ring_buf.c +++ b/test/test_ring_buf.c @@ -1,15 +1,27 @@ +/*============================================================================ +* +* Q u a n t u m L e a P s +* ------------------------ +* Modern Embedded Software +* +* Copyright (C) 2021 Quantum Leaps, LLC. All rights reserved. +* +* SPDX-License-Identifier: MIT +* +* Contact information: +* +* +============================================================================*/ #include #include #include "ring_buf.h" #include "sutest.h" -#include - - RingBufElement buf[8]; RingBuf rb; +/* ring-buffer "handler" function for RingBuf_process_all() */ static void rb_handler(RingBufElement const el); static RingBufElement test_data[] = { @@ -20,37 +32,50 @@ static RingBufElement test_data[] = { }; static RingBufCtr test_idx; +void setUp(void) { +} + +void tearDown(void) { +} + void TEST_onRun(void) { - RingBufElement el; - RingBufCtr i; RingBuf_ctor(&rb, buf, ARRAY_LEN(buf)); TEST("RingBuf_num_free"); - EXPECT(RingBuf_num_free(&rb) == ARRAY_LEN(buf) - 1U); + { + EXPECT(RingBuf_num_free(&rb) == ARRAY_LEN(buf) - 1U); + } TEST("RingBuf_put 3"); - RingBuf_put(&rb, 0xAAU); - RingBuf_put(&rb, 0xBBU); - RingBuf_put(&rb, 0xCCU); - EXPECT(RingBuf_num_free(&rb) == ARRAY_LEN(buf) - 1U - 3U); + { + RingBuf_put(&rb, 0xAAU); + RingBuf_put(&rb, 0xBBU); + RingBuf_put(&rb, 0xCCU); + EXPECT(RingBuf_num_free(&rb) == ARRAY_LEN(buf) - 1U - 3U); + } TEST("RingBuf_get"); - EXPECT(true == RingBuf_get(&rb, &el)); - EXPECT(0xAAU == el); - EXPECT(true == RingBuf_get(&rb, &el)); - EXPECT(0xBBU == el); - EXPECT(true == RingBuf_get(&rb, &el)); - EXPECT(0xCCU == el); - EXPECT(false == RingBuf_get(&rb, &el)); + { + RingBufElement el; + EXPECT(true == RingBuf_get(&rb, &el)); + EXPECT(0xAAU == el); + EXPECT(true == RingBuf_get(&rb, &el)); + EXPECT(0xBBU == el); + EXPECT(true == RingBuf_get(&rb, &el)); + EXPECT(0xCCU == el); + EXPECT(false == RingBuf_get(&rb, &el)); + } TEST("RingBuf_process_all test_data"); - for (i = 0U; i < ARRAY_LEN(test_data); ++i) { - RingBuf_put(&rb, test_data[i]); + { + for (RingBufCtr i = 0U; i < ARRAY_LEN(test_data); ++i) { + RingBuf_put(&rb, test_data[i]); + } + test_idx = 0U; + RingBuf_process_all(&rb, &rb_handler); + EXPECT(RingBuf_num_free(&rb) == ARRAY_LEN(buf) - 1U); } - test_idx = 0U; - RingBuf_process_all(&rb, &rb_handler); - EXPECT(RingBuf_num_free(&rb) == ARRAY_LEN(buf) - 1U); } static void rb_handler(RingBufElement const el) {