diff options
Diffstat (limited to 'quantum/serial_link/tests/triple_buffered_object_tests.c')
| -rw-r--r-- | quantum/serial_link/tests/triple_buffered_object_tests.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/quantum/serial_link/tests/triple_buffered_object_tests.c b/quantum/serial_link/tests/triple_buffered_object_tests.c new file mode 100644 index 000000000..6f7c82b46 --- /dev/null +++ b/quantum/serial_link/tests/triple_buffered_object_tests.c | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | /* | ||
| 2 | The MIT License (MIT) | ||
| 3 | |||
| 4 | Copyright (c) 2016 Fred Sundvik | ||
| 5 | |||
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 7 | of this software and associated documentation files (the "Software"), to deal | ||
| 8 | in the Software without restriction, including without limitation the rights | ||
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 10 | copies of the Software, and to permit persons to whom the Software is | ||
| 11 | furnished to do so, subject to the following conditions: | ||
| 12 | |||
| 13 | The above copyright notice and this permission notice shall be included in all | ||
| 14 | copies or substantial portions of the Software. | ||
| 15 | |||
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 22 | SOFTWARE. | ||
| 23 | */ | ||
| 24 | |||
| 25 | #include <cgreen/cgreen.h> | ||
| 26 | #include "serial_link/protocol/triple_buffered_object.c" | ||
| 27 | |||
| 28 | typedef struct { | ||
| 29 | uint8_t state; | ||
| 30 | uint32_t buffer[3]; | ||
| 31 | }test_object_t; | ||
| 32 | |||
| 33 | test_object_t test_object; | ||
| 34 | |||
| 35 | Describe(TripleBufferedObject); | ||
| 36 | BeforeEach(TripleBufferedObject) { | ||
| 37 | triple_buffer_init((triple_buffer_object_t*)&test_object); | ||
| 38 | } | ||
| 39 | AfterEach(TripleBufferedObject) {} | ||
| 40 | |||
| 41 | |||
| 42 | Ensure(TripleBufferedObject, writes_and_reads_object) { | ||
| 43 | *triple_buffer_begin_write(&test_object) = 0x3456ABCC; | ||
| 44 | triple_buffer_end_write(&test_object); | ||
| 45 | assert_that(*triple_buffer_read(&test_object), is_equal_to(0x3456ABCC)); | ||
| 46 | } | ||
| 47 | |||
| 48 | Ensure(TripleBufferedObject, does_not_read_empty) { | ||
| 49 | assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); | ||
| 50 | } | ||
| 51 | |||
| 52 | Ensure(TripleBufferedObject, writes_twice_and_reads_object) { | ||
| 53 | *triple_buffer_begin_write(&test_object) = 0x3456ABCC; | ||
| 54 | triple_buffer_end_write(&test_object); | ||
| 55 | *triple_buffer_begin_write(&test_object) = 0x44778899; | ||
| 56 | triple_buffer_end_write(&test_object); | ||
| 57 | assert_that(*triple_buffer_read(&test_object), is_equal_to(0x44778899)); | ||
| 58 | } | ||
| 59 | |||
| 60 | Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { | ||
| 61 | *triple_buffer_begin_write(&test_object) = 1; | ||
| 62 | triple_buffer_end_write(&test_object); | ||
| 63 | uint32_t* read = triple_buffer_read(&test_object); | ||
| 64 | *triple_buffer_begin_write(&test_object) = 2; | ||
| 65 | triple_buffer_end_write(&test_object); | ||
| 66 | assert_that(*read, is_equal_to(1)); | ||
| 67 | assert_that(*triple_buffer_read(&test_object), is_equal_to(2)); | ||
| 68 | assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); | ||
| 69 | } | ||
| 70 | |||
| 71 | Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { | ||
| 72 | *triple_buffer_begin_write(&test_object) = 1; | ||
| 73 | triple_buffer_end_write(&test_object); | ||
| 74 | uint32_t* read = triple_buffer_read(&test_object); | ||
| 75 | *triple_buffer_begin_write(&test_object) = 2; | ||
| 76 | triple_buffer_end_write(&test_object); | ||
| 77 | *triple_buffer_begin_write(&test_object) = 3; | ||
| 78 | triple_buffer_end_write(&test_object); | ||
| 79 | assert_that(*read, is_equal_to(1)); | ||
| 80 | assert_that(*triple_buffer_read(&test_object), is_equal_to(3)); | ||
| 81 | assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); | ||
| 82 | } | ||
