diff options
author | Didier Loiseau <didierloiseau+git@gmail.com> | 2016-09-11 01:26:47 +0200 |
---|---|---|
committer | Didier Loiseau <didierloiseau+git@gmail.com> | 2016-09-11 01:26:47 +0200 |
commit | b9014c757599ca288be07629ad1d00ef66c033f1 (patch) | |
tree | ba056e704e76327c597e25d1be200c4c991efeb8 /quantum/serial_link/tests | |
parent | 60a826923d5a6b0dbe416b0837608149ab82e470 (diff) | |
parent | 5010df3d8b68a53c382b853c7c2e45922975b631 (diff) |
Merge remote-tracking branch 'remotes/jackhumbert/master' into bépo
Diffstat (limited to 'quantum/serial_link/tests')
-rw-r--r-- | quantum/serial_link/tests/Makefile | 61 | ||||
-rw-r--r-- | quantum/serial_link/tests/byte_stuffer_tests.cpp | 483 | ||||
-rw-r--r-- | quantum/serial_link/tests/frame_router_tests.cpp | 229 | ||||
-rw-r--r-- | quantum/serial_link/tests/frame_validator_tests.cpp | 115 | ||||
-rw-r--r-- | quantum/serial_link/tests/rules.mk | 22 | ||||
-rw-r--r-- | quantum/serial_link/tests/testlist.mk | 6 | ||||
-rw-r--r-- | quantum/serial_link/tests/transport_tests.cpp | 188 | ||||
-rw-r--r-- | quantum/serial_link/tests/triple_buffered_object_tests.cpp | 84 |
8 files changed, 1188 insertions, 0 deletions
diff --git a/quantum/serial_link/tests/Makefile b/quantum/serial_link/tests/Makefile new file mode 100644 index 0000000000..1b072c6f1d --- /dev/null +++ b/quantum/serial_link/tests/Makefile @@ -0,0 +1,61 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# 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. + +CC = gcc +CFLAGS = +INCLUDES = -I. -I../../ +LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared +LDLIBS = -lcgreen +UNITOBJ = $(BUILDDIR)/serialtest/unitobj +DEPDIR = $(BUILDDIR)/serialtest/unit.d +UNITTESTS = $(BUILDDIR)/serialtest/unittests +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +EXT = .so +UNAME := $(shell uname) +ifneq (, $(findstring MINGW, $(UNAME))) + EXT = .dll +endif +ifneq (, $(findstring CYGWIN, $(UNAME))) + EXT = .dll +endif + +SRC = $(wildcard *.c) +TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC)) +$(shell mkdir -p $(DEPDIR) >/dev/null) + +test: $(TESTFILES) + @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES) + +$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o + @mkdir -p $(UNITTESTS) + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +$(UNITOBJ)/%.o : %.c +$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d + @mkdir -p $(UNITOBJ) + $(CC) $(CFLAGS) $(DEPFLAGS) $(INCLUDES) -c $< -o $@ + @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(DEPDIR)/%.d: ; +.PRECIOUS: $(DEPDIR)/%.d + +-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC)))
\ No newline at end of file diff --git a/quantum/serial_link/tests/byte_stuffer_tests.cpp b/quantum/serial_link/tests/byte_stuffer_tests.cpp new file mode 100644 index 0000000000..ff49d727bb --- /dev/null +++ b/quantum/serial_link/tests/byte_stuffer_tests.cpp @@ -0,0 +1,483 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +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. +*/ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include <vector> +#include <algorithm> +extern "C" { +#include "serial_link/protocol/byte_stuffer.h" +#include "serial_link/protocol/frame_validator.h" +#include "serial_link/protocol/physical.h" +} + +using testing::_; +using testing::ElementsAreArray; +using testing::Args; + +class ByteStuffer : public ::testing::Test{ +public: + ByteStuffer() { + Instance = this; + init_byte_stuffer(); + } + + ~ByteStuffer() { + Instance = nullptr; + } + + MOCK_METHOD3(validator_recv_frame, void (uint8_t link, uint8_t* data, uint16_t size)); + + void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + std::copy(data, data + size, std::back_inserter(sent_data)); + } + std::vector<uint8_t> sent_data; + + static ByteStuffer* Instance; +}; + +ByteStuffer* ByteStuffer::Instance = nullptr; + +extern "C" { + void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { + ByteStuffer::Instance->validator_recv_frame(link, data, size); + } + + void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + ByteStuffer::Instance->send_data(link, data, size); + } +} + +TEST_F(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .Times(0); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .Times(0); + byte_stuffer_recv_byte(0, 0xFF); +} + +TEST_F(ByteStuffer, receives_no_frame_for_a_single_random_byte) { + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .Times(0); + byte_stuffer_recv_byte(0, 0x4A); +} + +TEST_F(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .Times(0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_single_byte_valid_frame) { + uint8_t expected[] = {0x37}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_recv_byte(0, 0); +} +TEST_F(ByteStuffer, receives_three_bytes_valid_frame) { + uint8_t expected[] = {0x37, 0x99, 0xFF}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_recv_byte(0, 0x99); + byte_stuffer_recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_single_zero_valid_frame) { + uint8_t expected[] = {0}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_valid_frame_with_zeroes) { + uint8_t expected[] = {5, 0, 3, 0}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); +} + + +TEST_F(ByteStuffer, receives_two_valid_frames) { + uint8_t expected1[] = {5, 0}; + uint8_t expected2[] = {3}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected1))); + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected2))); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 0); +} + +TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_zero) { + uint8_t expected[] = {5, 7}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 7); + byte_stuffer_recv_byte(1, 0); +} + +TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { + uint8_t expected[] = {5, 7}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 9); + byte_stuffer_recv_byte(0, 4); // This should have been zero + byte_stuffer_recv_byte(0, 0); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { + uint8_t expected[254]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 0xFF); + for (i=0;i<254;i++) { + byte_stuffer_recv_byte(0, i+1); + } + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { + uint8_t expected[255]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expected[254] = 7; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 0xFF); + for (i=0;i<254;i++) { + byte_stuffer_recv_byte(0, i+1); + } + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { + uint8_t expected[255]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expected[254] = 0; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 0xFF); + for (i=0;i<254;i++) { + byte_stuffer_recv_byte(0, i+1); + } + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_two_long_frames_and_some_more) { + uint8_t expected[515]; + int i; + int j; + for (j=0;j<2;j++) { + for (i=0;i<254;i++) { + expected[i+254*j] = i + 1; + } + } + for (i=0;i<7;i++) { + expected[254*2+i] = i + 1; + } + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + byte_stuffer_recv_byte(0, 0xFF); + for (i=0;i<254;i++) { + byte_stuffer_recv_byte(0, i+1); + } + byte_stuffer_recv_byte(0, 0xFF); + for (i=0;i<254;i++) { + byte_stuffer_recv_byte(0, i+1); + } + byte_stuffer_recv_byte(0, 8); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 6); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { + uint8_t expected[MAX_FRAME_SIZE] = {}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + int i; + byte_stuffer_recv_byte(0, 1); + for(i=0;i<MAX_FRAME_SIZE;i++) { + byte_stuffer_recv_byte(0, 1); + } + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) { + uint8_t expected[1] = {0}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .Times(0); + int i; + byte_stuffer_recv_byte(0, 1); + for(i=0;i<MAX_FRAME_SIZE;i++) { + byte_stuffer_recv_byte(0, 1); + } + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, received_frame_is_aborted_when_its_too_long) { + uint8_t expected[1] = {1}; + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + int i; + byte_stuffer_recv_byte(0, 1); + for(i=0;i<MAX_FRAME_SIZE;i++) { + byte_stuffer_recv_byte(0, 1); + } + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); +} + +TEST_F(ByteStuffer, does_nothing_when_sending_zero_size_frame) { + EXPECT_EQ(sent_data.size(), 0); + byte_stuffer_send_frame(0, NULL, 0); +} + +TEST_F(ByteStuffer, send_one_byte_frame) { + uint8_t data[] = {5}; + byte_stuffer_send_frame(1, data, 1); + uint8_t expected[] = {2, 5, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_two_byte_frame) { + uint8_t data[] = {5, 0x77}; + byte_stuffer_send_frame(0, data, 2); + uint8_t expected[] = {3, 5, 0x77, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_one_byte_frame_with_zero) { + uint8_t data[] = {0}; + byte_stuffer_send_frame(0, data, 1); + uint8_t expected[] = {1, 1, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_zero) { + uint8_t data[] = {0, 9}; + byte_stuffer_send_frame(1, data, 2); + uint8_t expected[] = {1, 2, 9, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) { + uint8_t data[] = {9, 0}; + byte_stuffer_send_frame(1, data, 2); + uint8_t expected[] = {2, 9, 1, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) { + uint8_t data[] = {9, 0, 0x68}; + byte_stuffer_send_frame(0, data, 3); + uint8_t expected[] = {2, 9, 2, 0x68, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_three_byte_frame_data_in_the_middle) { + uint8_t data[] = {0, 0x55, 0}; + byte_stuffer_send_frame(0, data, 3); + uint8_t expected[] = {1, 2, 0x55, 1, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_three_byte_frame_with_all_zeroes) { + uint8_t data[] = {0, 0, 0}; + byte_stuffer_send_frame(0, data, 3); + uint8_t expected[] = {1, 1, 1, 1, 0}; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes) { + uint8_t data[254]; + int i; + for(i=0;i<254;i++) { + data[i] = i + 1; + } + byte_stuffer_send_frame(0, data, 254); + uint8_t expected[256]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 0; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_frame_with_255_non_zeroes) { + uint8_t data[255]; + int i; + for(i=0;i<255;i++) { + data[i] = i + 1; + } + byte_stuffer_send_frame(0, data, 255); + uint8_t expected[258]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 2; + expected[256] = 255; + expected[257] = 0; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) { + uint8_t data[255]; + int i; + for(i=0;i<254;i++) { + data[i] = i + 1; + } + data[254] = 0; + byte_stuffer_send_frame(0, data, 255); + uint8_t expected[258]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 1; + expected[256] = 1; + expected[257] = 0; + EXPECT_THAT(sent_data, ElementsAreArray(expected)); +} + +TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) { + uint8_t original_data[] = { 1, 2, 3}; + byte_stuffer_send_frame(0, original_data, sizeof(original_data)); + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(original_data))); + int i; + for(auto& d : sent_data) { + byte_stuffer_recv_byte(1, d); + } +} + +TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet_with_zeros) { + uint8_t original_data[] = { 1, 0, 3, 0, 0, 9}; + byte_stuffer_send_frame(1, original_data, sizeof(original_data)); + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(original_data))); + int i; + for(auto& d : sent_data) { + byte_stuffer_recv_byte(1, d); + } +} + +TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes) { + uint8_t original_data[254]; + int i; + for(i=0;i<254;i++) { + original_data[i] = i + 1; + } + byte_stuffer_send_frame(0, original_data, sizeof(original_data)); + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(original_data))); + for(auto& d : sent_data) { + byte_stuffer_recv_byte(1, d); + } +} + +TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_256_bytes) { + uint8_t original_data[256]; + int i; + for(i=0;i<254;i++) { + original_data[i] = i + 1; + } + original_data[254] = 22; + original_data[255] = 23; + byte_stuffer_send_frame(0, original_data, sizeof(original_data)); + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(original_data))); + for(auto& d : sent_data) { + byte_stuffer_recv_byte(1, d); + } +} + +TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes_and_then_zero) { + uint8_t original_data[255]; + int i; + for(i=0;i<254;i++) { + original_data[i] = i + 1; + } + original_data[254] = 0; + byte_stuffer_send_frame(0, original_data, sizeof(original_data)); + EXPECT_CALL(*this, validator_recv_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(original_data))); + for(auto& d : sent_data) { + byte_stuffer_recv_byte(1, d); + } +} diff --git a/quantum/serial_link/tests/frame_router_tests.cpp b/quantum/serial_link/tests/frame_router_tests.cpp new file mode 100644 index 0000000000..2bd5bf830d --- /dev/null +++ b/quantum/serial_link/tests/frame_router_tests.cpp @@ -0,0 +1,229 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +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. +*/ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include <array> +extern "C" { + #include "serial_link/protocol/transport.h" + #include "serial_link/protocol/byte_stuffer.h" + #include "serial_link/protocol/frame_router.h" +} + +using testing::_; +using testing::ElementsAreArray; +using testing::Args; + +class FrameRouter : public testing::Test { +public: + FrameRouter() : + current_router_buffer(nullptr) + { + Instance = this; + init_byte_stuffer(); + } + + ~FrameRouter() { + Instance = nullptr; + } + + void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + auto& buffer = current_router_buffer->send_buffers[link]; + std::copy(data, data + size, std::back_inserter(buffer)); + } + + void receive_data(uint8_t link, uint8_t* data, uint16_t size) { + int i; + for(i=0;i<size;i++) { + byte_stuffer_recv_byte(link, data[i]); + } + } + + void activate_router(uint8_t num) { + current_router_buffer = router_buffers + num; + router_set_master(num==0); + } + + void simulate_transport(uint8_t from, uint8_t to) { + activate_router(to); + if (from > to) { + receive_data(DOWN_LINK, + router_buffers[from].send_buffers[UP_LINK].data(), + router_buffers[from].send_buffers[UP_LINK].size()); + } + else if(to > from) { + receive_data(UP_LINK, + router_buffers[from].send_buffers[DOWN_LINK].data(), + router_buffers[from].send_buffers[DOWN_LINK].size()); + } + } + + MOCK_METHOD3(transport_recv_frame, void (uint8_t from, uint8_t* data, uint16_t size)); + + std::vector<uint8_t> received_data; + + struct router_buffer { + std::vector<uint8_t> send_buffers[2]; + }; + + router_buffer router_buffers[8]; + router_buffer* current_router_buffer; + + static FrameRouter* Instance; +}; + +FrameRouter* FrameRouter::Instance = nullptr; + + +typedef struct { + std::array<uint8_t, 4> data; + uint8_t extra[16]; +} frame_buffer_t; + + +extern "C" { + void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + FrameRouter::Instance->send_data(link, data, size); + } + + + void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { + FrameRouter::Instance->transport_recv_frame(from, data, size); + } +} + +TEST_F(FrameRouter, master_broadcast_is_received_by_everyone) { + frame_buffer_t data; + data.data = {0xAB, 0x70, 0x55, 0xBB}; + activate_router(0); + router_send_frame(0xFF, (uint8_t*)&data, 4); + EXPECT_GT(router_buffers[0].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0); + EXPECT_CALL(*this, transport_recv_frame(0, _, _)) + .With(Args<1, 2>(ElementsAreArray(data.data))); + simulate_transport(0, 1); + EXPECT_GT(router_buffers[1].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[1].send_buffers[UP_LINK].size(), 0); + + EXPECT_CALL(*this, transport_recv_frame(0, _, _)) + .With(Args<1, 2>(ElementsAreArray(data.data))); + simulate_transport(1, 2); + EXPECT_GT(router_buffers[2].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[2].send_buffers[UP_LINK].size(), 0); +} + +TEST_F(FrameRouter, master_send_is_received_by_targets) { + frame_buffer_t data; + data.data = {0xAB, 0x70, 0x55, 0xBB}; + activate_router(0); + router_send_frame((1 << 1) | (1 << 2), (uint8_t*)&data, 4); + EXPECT_GT(router_buffers[0].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0); + + simulate_transport(0, 1); + EXPECT_GT(router_buffers[1].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[1].send_buffers[UP_LINK].size(), 0); + + EXPECT_CALL(*this, transport_recv_frame(0, _, _)) + .With(Args<1, 2>(ElementsAreArray(data.data))); + simulate_transport(1, 2); + EXPECT_GT(router_buffers[2].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[2].send_buffers[UP_LINK].size(), 0); + + EXPECT_CALL(*this, transport_recv_frame(0, _, _)) + .With(Args<1, 2>(ElementsAreArray(data.data))); + simulate_transport(2, 3); + EXPECT_GT(router_buffers[3].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[3].send_buffers[UP_LINK].size(), 0); +} + +TEST_F(FrameRouter, first_link_sends_to_master) { + frame_buffer_t data; + data.data = {0xAB, 0x70, 0x55, 0xBB}; + activate_router(1); + router_send_frame(0, (uint8_t*)&data, 4); + EXPECT_GT(router_buffers[1].send_buffers[UP_LINK].size(), 0); + EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0); + + EXPECT_CALL(*this, transport_recv_frame(1, _, _)) + .With(Args<1, 2>(ElementsAreArray(data.data))); + simulate_transport(1, 0); + EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0); +} + +TEST_F(FrameRouter, second_link_sends_to_master) { + frame_buffer_t data; + data.data = {0xAB, 0x70, 0x55, 0xBB}; + activate_router(2); + router_send_frame(0, (uint8_t*)&data, 4); + EXPECT_GT(router_buffers[2].send_buffers[UP_LINK].size(), 0); + EXPECT_EQ(router_buffers[2].send_buffers[DOWN_LINK].size(), 0); + + simulate_transport(2, 1); + EXPECT_GT(router_buffers[1].send_buffers[UP_LINK].size(), 0); + EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0); + + EXPECT_CALL(*this, transport_recv_frame(2, _, _)) + .With(Args<1, 2>(ElementsAreArray(data.data))); + simulate_transport(1, 0); + EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0); + EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0); +} + +TEST_F(FrameRouter, master_sends_to_master_does_nothing) { + frame_buffer_t data; + data.data = {0xAB, 0x70, 0x55, 0xBB}; + activate_router(0); + router_send_frame(0, (uint8_t*)&data, 4); + EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0); + EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0); +} + +TEST_F(FrameRouter, link_sends_to_other_link_does_nothing) { + frame_buffer_t data; + data.data = {0xAB, 0x70, 0x55, 0xBB}; + activate_router(1); + router_send_frame(2, (uint8_t*)&data, 4); + EXPECT_EQ(router_buffers[1].send_buffers[UP_LINK].size(), 0); + EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0); +} + +TEST_F(FrameRouter, master_receives_on_uplink_does_nothing) { + frame_buffer_t data; + data.data = {0xAB, 0x70, 0x55, 0xBB}; + activate_router(1); + router_send_frame(0, (uint8_t*)&data, 4); + EXPECT_GT(router_buffers[1].send_buffers[UP_LINK].size(), 0); + EXPECT_EQ(router_buffers[1].send_buffers[DOWN_LINK].size(), 0); + + EXPECT_CALL(*this, transport_recv_frame(_, _, _)) + .Times(0); + activate_router(0); + receive_data(UP_LINK, + router_buffers[1].send_buffers[UP_LINK].data(), + router_buffers[1].send_buffers[UP_LINK].size()); + EXPECT_EQ(router_buffers[0].send_buffers[UP_LINK].size(), 0); + EXPECT_EQ(router_buffers[0].send_buffers[DOWN_LINK].size(), 0); +} diff --git a/quantum/serial_link/tests/frame_validator_tests.cpp b/quantum/serial_link/tests/frame_validator_tests.cpp new file mode 100644 index 0000000000..9223af83b0 --- /dev/null +++ b/quantum/serial_link/tests/frame_validator_tests.cpp @@ -0,0 +1,115 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +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. +*/ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" +extern "C" { +#include "serial_link/protocol/frame_validator.h" +} + +using testing::_; +using testing::ElementsAreArray; +using testing::Args; + +class FrameValidator : public testing::Test { +public: + FrameValidator() { + Instance = this; + } + + ~FrameValidator() { + Instance = nullptr; + } + + MOCK_METHOD3(route_incoming_frame, void (uint8_t link, uint8_t* data, uint16_t size)); + MOCK_METHOD3(byte_stuffer_send_frame, void (uint8_t link, uint8_t* data, uint16_t size)); + + static FrameValidator* Instance; +}; + +FrameValidator* FrameValidator::Instance = nullptr; + +extern "C" { +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { + FrameValidator::Instance->route_incoming_frame(link, data, size); +} + +void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) { + FrameValidator::Instance->byte_stuffer_send_frame(link, data, size); +} +} + +TEST_F(FrameValidator, doesnt_validate_frames_under_5_bytes) { + EXPECT_CALL(*this, route_incoming_frame(_, _, _)) + .Times(0); + uint8_t data[] = {1, 2}; + validator_recv_frame(0, 0, 1); + validator_recv_frame(0, data, 2); + validator_recv_frame(0, data, 3); + validator_recv_frame(0, data, 4); +} + +TEST_F(FrameValidator, validates_one_byte_frame_with_correct_crc) { + uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; + EXPECT_CALL(*this, route_incoming_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(data, 1))); + validator_recv_frame(0, data, 5); +} + +TEST_F(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { + uint8_t data[] = {0x44, 0, 0, 0, 0}; + EXPECT_CALL(*this, route_incoming_frame(_, _, _)) + .Times(0); + validator_recv_frame(1, data, 5); +} + +TEST_F(FrameValidator, validates_four_byte_frame_with_correct_crc) { + uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA}; + EXPECT_CALL(*this, route_incoming_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(data, 4))); + validator_recv_frame(1, data, 8); +} + +TEST_F(FrameValidator, validates_five_byte_frame_with_correct_crc) { + uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; + EXPECT_CALL(*this, route_incoming_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(data, 5))); + validator_recv_frame(0, data, 9); +} + +TEST_F(FrameValidator, sends_one_byte_with_correct_crc) { + uint8_t original[] = {0x44, 0, 0, 0, 0}; + uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; + EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + validator_send_frame(0, original, 1); +} + +TEST_F(FrameValidator, sends_five_bytes_with_correct_crc) { + uint8_t original[] = {1, 2, 3, 4, 5, 0, 0, 0, 0}; + uint8_t expected[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; + EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _)) + .With(Args<1, 2>(ElementsAreArray(expected))); + validator_send_frame(0, original, 5); +} diff --git a/quantum/serial_link/tests/rules.mk b/quantum/serial_link/tests/rules.mk new file mode 100644 index 0000000000..b81515bc55 --- /dev/null +++ b/quantum/serial_link/tests/rules.mk @@ -0,0 +1,22 @@ +serial_link_byte_stuffer_SRC :=\ + $(SERIAL_PATH)/tests/byte_stuffer_tests.cpp \ + $(SERIAL_PATH)/protocol/byte_stuffer.c + +serial_link_frame_validator_SRC := \ + $(SERIAL_PATH)/tests/frame_validator_tests.cpp \ + $(SERIAL_PATH)/protocol/frame_validator.c + +serial_link_frame_router_SRC := \ + $(SERIAL_PATH)/tests/frame_router_tests.cpp \ + $(SERIAL_PATH)/protocol/byte_stuffer.c \ + $(SERIAL_PATH)/protocol/frame_validator.c \ + $(SERIAL_PATH)/protocol/frame_router.c + +serial_link_triple_buffered_object_SRC := \ + $(SERIAL_PATH)/tests/triple_buffered_object_tests.cpp \ + $(SERIAL_PATH)/protocol/triple_buffered_object.c + +serial_link_transport_SRC := \ + $(SERIAL_PATH)/tests/transport_tests.cpp \ + $(SERIAL_PATH)/protocol/transport.c \ + $(SERIAL_PATH)/protocol/triple_buffered_object.c diff --git a/quantum/serial_link/tests/testlist.mk b/quantum/serial_link/tests/testlist.mk new file mode 100644 index 0000000000..a80e888849 --- /dev/null +++ b/quantum/serial_link/tests/testlist.mk @@ -0,0 +1,6 @@ +TEST_LIST +=\ + serial_link_byte_stuffer\ + serial_link_frame_validator\ + serial_link_frame_router\ + serial_link_triple_buffered_object\ + serial_link_transport
\ No newline at end of file diff --git a/quantum/serial_link/tests/transport_tests.cpp b/quantum/serial_link/tests/transport_tests.cpp new file mode 100644 index 0000000000..21b7b165f6 --- /dev/null +++ b/quantum/serial_link/tests/transport_tests.cpp @@ -0,0 +1,188 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +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. +*/ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" + +using testing::_; +using testing::ElementsAreArray; +using testing::Args; + +extern "C" { +#include "serial_link/protocol/transport.h" +} + +struct test_object1 { + uint32_t test; +}; + +struct test_object2 { + uint32_t test1; + uint32_t test2; +}; + +MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1); +MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1); +SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1); + +static remote_object_t* test_remote_objects[] = { + REMOTE_OBJECT(master_to_slave), + REMOTE_OBJECT(master_to_single_slave), + REMOTE_OBJECT(slave_to_master), +}; + +class Transport : public testing::Test { +public: + Transport() { + Instance = this; + add_remote_objects(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); + } + + ~Transport() { + Instance = nullptr; + reinitialize_serial_link_transport(); + } + + MOCK_METHOD0(signal_data_written, void ()); + MOCK_METHOD1(router_send_frame, void (uint8_t destination)); + + void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + router_send_frame(destination); + std::copy(data, data + size, std::back_inserter(sent_data)); + } + + static Transport* Instance; + + std::vector<uint8_t> sent_data; +}; + +Transport* Transport::Instance = nullptr; + +extern "C" { +void signal_data_written(void) { + Transport::Instance->signal_data_written(); +} + +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + Transport::Instance->router_send_frame(destination, data, size); +} +} + +TEST_F(Transport, write_to_local_signals_an_event) { + begin_write_master_to_slave(); + EXPECT_CALL(*this, signal_data_written()); + end_write_master_to_slave(); + begin_write_slave_to_master(); + EXPECT_CALL(*this, signal_data_written()); + end_write_slave_to_master(); + begin_write_master_to_single_slave(1); + EXPECT_CALL(*this, signal_data_written()); + end_write_master_to_single_slave(1); +} + +TEST_F(Transport, writes_from_master_to_all_slaves) { + update_transport(); + test_object1* obj = begin_write_master_to_slave(); + obj->test = 5; + EXPECT_CALL(*this, signal_data_written()); + end_write_master_to_slave(); + EXPECT_CALL(*this, router_send_frame(0xFF)); + update_transport(); + transport_recv_frame(0, sent_data.data(), sent_data.size()); + test_object1* obj2 = read_master_to_slave(); + EXPECT_NE(obj2, nullptr); + EXPECT_EQ(obj2->test, 5); +} + +TEST_F(Transport, writes_from_slave_to_master) { + update_transport(); + test_object1* obj = begin_write_slave_to_master(); + obj->test = 7; + EXPECT_CALL(*this, signal_data_written()); + end_write_slave_to_master(); + EXPECT_CALL(*this, router_send_frame(0)); + update_transport(); + transport_recv_frame(3, sent_data.data(), sent_data.size()); + test_object1* obj2 = read_slave_to_master(2); + EXPECT_EQ(read_slave_to_master(0), nullptr); + EXPECT_NE(obj2, nullptr); + EXPECT_EQ(obj2->test, 7); +} + +TEST_F(Transport, writes_from_master_to_single_slave) { + update_transport(); + test_object1* obj = begin_write_master_to_single_slave(3); + obj->test = 7; + EXPECT_CALL(*this, signal_data_written()); + end_write_master_to_single_slave(3); + EXPECT_CALL(*this, router_send_frame(4)); + update_transport(); + transport_recv_frame(0, sent_data.data(), sent_data.size()); + test_object1* obj2 = read_master_to_single_slave(); + EXPECT_NE(obj2, nullptr); + EXPECT_EQ(obj2->test, 7); +} + +TEST_F(Transport, ignores_object_with_invalid_id) { + update_transport(); + test_object1* obj = begin_write_master_to_single_slave(3); + obj->test = 7; + EXPECT_CALL(*this, signal_data_written()); + end_write_master_to_single_slave(3); + EXPECT_CALL(*this, router_send_frame(4)); + update_transport(); + sent_data[sent_data.size() - 1] = 44; + transport_recv_frame(0, sent_data.data(), sent_data.size()); + test_object1* obj2 = read_master_to_single_slave(); + EXPECT_EQ(obj2, nullptr); +} + +TEST_F(Transport, ignores_object_with_size_too_small) { + update_transport(); + test_object1* obj = begin_write_master_to_slave(); + obj->test = 7; + EXPECT_CALL(*this, signal_data_written()); + end_write_master_to_slave(); + EXPECT_CALL(*this, router_send_frame(_)); + update_transport(); + sent_data[sent_data.size() - 2] = 0; + transport_recv_frame(0, sent_data.data(), sent_data.size() - 1); + test_object1* obj2 = read_master_to_slave(); + EXPECT_EQ(obj2, nullptr); +} + +TEST_F(Transport, ignores_object_with_size_too_big) { + update_transport(); + test_object1* obj = begin_write_master_to_slave(); + obj->test = 7; + EXPECT_CALL(*this, signal_data_written()); + end_write_master_to_slave(); + EXPECT_CALL(*this, router_send_frame(_)); + update_transport(); + sent_data.resize(sent_data.size() + 22); + sent_data[sent_data.size() - 1] = 0; + transport_recv_frame(0, sent_data.data(), sent_data.size()); + test_object1* obj2 = read_master_to_slave(); + EXPECT_EQ(obj2, nullptr); +} diff --git a/quantum/serial_link/tests/triple_buffered_object_tests.cpp b/quantum/serial_link/tests/triple_buffered_object_tests.cpp new file mode 100644 index 0000000000..7724bbee9c --- /dev/null +++ b/quantum/serial_link/tests/triple_buffered_object_tests.cpp @@ -0,0 +1,84 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +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. +*/ + +#include "gtest/gtest.h" +extern "C" { +#include "serial_link/protocol/triple_buffered_object.h" +} + +struct test_object{ + uint8_t state; + uint32_t buffer[3]; +}; + +test_object test_object; + +class TripleBufferedObject : public testing::Test { +public: + TripleBufferedObject() { + triple_buffer_init((triple_buffer_object_t*)&test_object); + } +}; + +TEST_F(TripleBufferedObject, writes_and_reads_object) { + *triple_buffer_begin_write(&test_object) = 0x3456ABCC; + triple_buffer_end_write(&test_object); + EXPECT_EQ(*triple_buffer_read(&test_object), 0x3456ABCC); +} + +TEST_F(TripleBufferedObject, does_not_read_empty) { + EXPECT_EQ(triple_buffer_read(&test_object), nullptr); +} + +TEST_F(TripleBufferedObject, writes_twice_and_reads_object) { + *triple_buffer_begin_write(&test_object) = 0x3456ABCC; + triple_buffer_end_write(&test_object); + *triple_buffer_begin_write(&test_object) = 0x44778899; + triple_buffer_end_write(&test_object); + EXPECT_EQ(*triple_buffer_read(&test_object), 0x44778899); +} + +TEST_F(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { + *triple_buffer_begin_write(&test_object) = 1; + triple_buffer_end_write(&test_object); + uint32_t* read = triple_buffer_read(&test_object); + *triple_buffer_begin_write(&test_object) = 2; + triple_buffer_end_write(&test_object); + EXPECT_EQ(*read, 1); + EXPECT_EQ(*triple_buffer_read(&test_object), 2); + EXPECT_EQ(triple_buffer_read(&test_object), nullptr); +} + +TEST_F(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { + *triple_buffer_begin_write(&test_object) = 1; + triple_buffer_end_write(&test_object); + uint32_t* read = triple_buffer_read(&test_object); + *triple_buffer_begin_write(&test_object) = 2; + triple_buffer_end_write(&test_object); + *triple_buffer_begin_write(&test_object) = 3; + triple_buffer_end_write(&test_object); + EXPECT_EQ(*read, 1); + EXPECT_EQ(*triple_buffer_read(&test_object), 3); + EXPECT_EQ(triple_buffer_read(&test_object), nullptr); +} |