Commit | Line | Data |
---|---|---|
50edb3d9 FS |
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 | ||
ffb0a126 FS |
25 | #include "gtest/gtest.h" |
26 | extern "C" { | |
27 | #include "serial_link/protocol/triple_buffered_object.h" | |
28 | } | |
50edb3d9 | 29 | |
ffb0a126 | 30 | struct test_object{ |
50edb3d9 FS |
31 | uint8_t state; |
32 | uint32_t buffer[3]; | |
ffb0a126 | 33 | }; |
4bb5733c | 34 | |
ffb0a126 | 35 | test_object test_object; |
4bb5733c | 36 | |
ffb0a126 FS |
37 | class TripleBufferedObject : public testing::Test { |
38 | public: | |
39 | TripleBufferedObject() { | |
40 | triple_buffer_init((triple_buffer_object_t*)&test_object); | |
41 | } | |
42 | }; | |
4bb5733c | 43 | |
ffb0a126 | 44 | TEST_F(TripleBufferedObject, writes_and_reads_object) { |
2710361c FS |
45 | *triple_buffer_begin_write(&test_object) = 0x3456ABCC; |
46 | triple_buffer_end_write(&test_object); | |
ffb0a126 | 47 | EXPECT_EQ(*triple_buffer_read(&test_object), 0x3456ABCC); |
4bb5733c FS |
48 | } |
49 | ||
ffb0a126 FS |
50 | TEST_F(TripleBufferedObject, does_not_read_empty) { |
51 | EXPECT_EQ(triple_buffer_read(&test_object), nullptr); | |
4bb5733c FS |
52 | } |
53 | ||
ffb0a126 | 54 | TEST_F(TripleBufferedObject, writes_twice_and_reads_object) { |
2710361c FS |
55 | *triple_buffer_begin_write(&test_object) = 0x3456ABCC; |
56 | triple_buffer_end_write(&test_object); | |
57 | *triple_buffer_begin_write(&test_object) = 0x44778899; | |
58 | triple_buffer_end_write(&test_object); | |
ffb0a126 | 59 | EXPECT_EQ(*triple_buffer_read(&test_object), 0x44778899); |
50edb3d9 | 60 | } |
4bb5733c | 61 | |
ffb0a126 | 62 | TEST_F(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { |
2710361c FS |
63 | *triple_buffer_begin_write(&test_object) = 1; |
64 | triple_buffer_end_write(&test_object); | |
65 | uint32_t* read = triple_buffer_read(&test_object); | |
66 | *triple_buffer_begin_write(&test_object) = 2; | |
67 | triple_buffer_end_write(&test_object); | |
ffb0a126 FS |
68 | EXPECT_EQ(*read, 1); |
69 | EXPECT_EQ(*triple_buffer_read(&test_object), 2); | |
70 | EXPECT_EQ(triple_buffer_read(&test_object), nullptr); | |
8e2d70d1 FS |
71 | } |
72 | ||
ffb0a126 | 73 | TEST_F(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { |
2710361c FS |
74 | *triple_buffer_begin_write(&test_object) = 1; |
75 | triple_buffer_end_write(&test_object); | |
76 | uint32_t* read = triple_buffer_read(&test_object); | |
77 | *triple_buffer_begin_write(&test_object) = 2; | |
78 | triple_buffer_end_write(&test_object); | |
79 | *triple_buffer_begin_write(&test_object) = 3; | |
80 | triple_buffer_end_write(&test_object); | |
ffb0a126 FS |
81 | EXPECT_EQ(*read, 1); |
82 | EXPECT_EQ(*triple_buffer_read(&test_object), 3); | |
83 | EXPECT_EQ(triple_buffer_read(&test_object), nullptr); | |
4bb5733c | 84 | } |