Convert tirple_buffered_object_tests to GTest
[jackhill/qmk/firmware.git] / quantum / serial_link / tests / triple_buffered_object_tests.cpp
CommitLineData
50edb3d9
FS
1/*
2The MIT License (MIT)
3
4Copyright (c) 2016 Fred Sundvik
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
ffb0a126
FS
25#include "gtest/gtest.h"
26extern "C" {
27#include "serial_link/protocol/triple_buffered_object.h"
28}
50edb3d9 29
ffb0a126 30struct test_object{
50edb3d9
FS
31 uint8_t state;
32 uint32_t buffer[3];
ffb0a126 33};
4bb5733c 34
ffb0a126 35test_object test_object;
4bb5733c 36
ffb0a126
FS
37class TripleBufferedObject : public testing::Test {
38public:
39 TripleBufferedObject() {
40 triple_buffer_init((triple_buffer_object_t*)&test_object);
41 }
42};
4bb5733c 43
ffb0a126 44TEST_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
50TEST_F(TripleBufferedObject, does_not_read_empty) {
51 EXPECT_EQ(triple_buffer_read(&test_object), nullptr);
4bb5733c
FS
52}
53
ffb0a126 54TEST_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 62TEST_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 73TEST_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}