diff options
author | Fred Sundvik <fsundvik@gmail.com> | 2016-08-27 13:54:16 +0300 |
---|---|---|
committer | Fred Sundvik <fsundvik@gmail.com> | 2016-08-27 21:57:49 +0300 |
commit | b3eba797af74ace19b9f2e762bdd33d9449e3f94 (patch) | |
tree | e45e127534e60318d034a2b1c06f80b479088b64 | |
parent | cdd0913bcc63334fa20f1a7bd46bdce4d4f2843b (diff) | |
download | qmk_firmware-b3eba797af74ace19b9f2e762bdd33d9449e3f94.tar.gz qmk_firmware-b3eba797af74ace19b9f2e762bdd33d9449e3f94.zip |
Convert frame_validator_tests to GTest
-rw-r--r-- | quantum/serial_link/tests/frame_validator_tests.cpp (renamed from quantum/serial_link/tests/frame_validator_tests.c) | 90 | ||||
-rw-r--r-- | quantum/serial_link/tests/rules.mk | 8 |
2 files changed, 58 insertions, 40 deletions
diff --git a/quantum/serial_link/tests/frame_validator_tests.c b/quantum/serial_link/tests/frame_validator_tests.cpp index d20947e2c..9223af83b 100644 --- a/quantum/serial_link/tests/frame_validator_tests.c +++ b/quantum/serial_link/tests/frame_validator_tests.cpp | |||
@@ -22,24 +22,47 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |||
22 | SOFTWARE. | 22 | SOFTWARE. |
23 | */ | 23 | */ |
24 | 24 | ||
25 | #include <cgreen/cgreen.h> | 25 | #include "gtest/gtest.h" |
26 | #include <cgreen/mocks.h> | 26 | #include "gmock/gmock.h" |
27 | #include "serial_link/protocol/frame_validator.c" | 27 | extern "C" { |
28 | #include "serial_link/protocol/frame_validator.h" | ||
29 | } | ||
30 | |||
31 | using testing::_; | ||
32 | using testing::ElementsAreArray; | ||
33 | using testing::Args; | ||
34 | |||
35 | class FrameValidator : public testing::Test { | ||
36 | public: | ||
37 | FrameValidator() { | ||
38 | Instance = this; | ||
39 | } | ||
40 | |||
41 | ~FrameValidator() { | ||
42 | Instance = nullptr; | ||
43 | } | ||
44 | |||
45 | MOCK_METHOD3(route_incoming_frame, void (uint8_t link, uint8_t* data, uint16_t size)); | ||
46 | MOCK_METHOD3(byte_stuffer_send_frame, void (uint8_t link, uint8_t* data, uint16_t size)); | ||
28 | 47 | ||
48 | static FrameValidator* Instance; | ||
49 | }; | ||
50 | |||
51 | FrameValidator* FrameValidator::Instance = nullptr; | ||
52 | |||
53 | extern "C" { | ||
29 | void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { | 54 | void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { |
30 | mock(data, size); | 55 | FrameValidator::Instance->route_incoming_frame(link, data, size); |
31 | } | 56 | } |
32 | 57 | ||
33 | void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) { | 58 | void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) { |
34 | mock(data, size); | 59 | FrameValidator::Instance->byte_stuffer_send_frame(link, data, size); |
60 | } | ||
35 | } | 61 | } |
36 | 62 | ||
37 | Describe(FrameValidator); | 63 | TEST_F(FrameValidator, doesnt_validate_frames_under_5_bytes) { |
38 | BeforeEach(FrameValidator) {} | 64 | EXPECT_CALL(*this, route_incoming_frame(_, _, _)) |
39 | AfterEach(FrameValidator) {} | 65 | .Times(0); |
40 | |||
41 | Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { | ||
42 | never_expect(route_incoming_frame); | ||
43 | uint8_t data[] = {1, 2}; | 66 | uint8_t data[] = {1, 2}; |
44 | validator_recv_frame(0, 0, 1); | 67 | validator_recv_frame(0, 0, 1); |
45 | validator_recv_frame(0, data, 2); | 68 | validator_recv_frame(0, data, 2); |
@@ -47,55 +70,46 @@ Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { | |||
47 | validator_recv_frame(0, data, 4); | 70 | validator_recv_frame(0, data, 4); |
48 | } | 71 | } |
49 | 72 | ||
50 | Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { | 73 | TEST_F(FrameValidator, validates_one_byte_frame_with_correct_crc) { |
51 | uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; | 74 | uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; |
52 | expect(route_incoming_frame, | 75 | EXPECT_CALL(*this, route_incoming_frame(_, _, _)) |
53 | when(size, is_equal_to(1)), | 76 | .With(Args<1, 2>(ElementsAreArray(data, 1))); |
54 | when(data, is_equal_to_contents_of(data, 1)) | ||
55 | ); | ||
56 | validator_recv_frame(0, data, 5); | 77 | validator_recv_frame(0, data, 5); |
57 | } | 78 | } |
58 | 79 | ||
59 | Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { | 80 | TEST_F(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { |
60 | uint8_t data[] = {0x44, 0, 0, 0, 0}; | 81 | uint8_t data[] = {0x44, 0, 0, 0, 0}; |
61 | never_expect(route_incoming_frame); | 82 | EXPECT_CALL(*this, route_incoming_frame(_, _, _)) |
83 | .Times(0); | ||
62 | validator_recv_frame(1, data, 5); | 84 | validator_recv_frame(1, data, 5); |
63 | } | 85 | } |
64 | 86 | ||
65 | Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { | 87 | TEST_F(FrameValidator, validates_four_byte_frame_with_correct_crc) { |
66 | uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA}; | 88 | uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA}; |
67 | expect(route_incoming_frame, | 89 | EXPECT_CALL(*this, route_incoming_frame(_, _, _)) |
68 | when(size, is_equal_to(4)), | 90 | .With(Args<1, 2>(ElementsAreArray(data, 4))); |
69 | when(data, is_equal_to_contents_of(data, 4)) | ||
70 | ); | ||
71 | validator_recv_frame(1, data, 8); | 91 | validator_recv_frame(1, data, 8); |
72 | } | 92 | } |
73 | 93 | ||
74 | Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { | 94 | TEST_F(FrameValidator, validates_five_byte_frame_with_correct_crc) { |
75 | uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; | 95 | uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; |
76 | expect(route_incoming_frame, | 96 | EXPECT_CALL(*this, route_incoming_frame(_, _, _)) |
77 | when(size, is_equal_to(5)), | 97 | .With(Args<1, 2>(ElementsAreArray(data, 5))); |
78 | when(data, is_equal_to_contents_of(data, 5)) | ||
79 | ); | ||
80 | validator_recv_frame(0, data, 9); | 98 | validator_recv_frame(0, data, 9); |
81 | } | 99 | } |
82 | 100 | ||
83 | Ensure(FrameValidator, sends_one_byte_with_correct_crc) { | 101 | TEST_F(FrameValidator, sends_one_byte_with_correct_crc) { |
84 | uint8_t original[] = {0x44, 0, 0, 0, 0}; | 102 | uint8_t original[] = {0x44, 0, 0, 0, 0}; |
85 | uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; | 103 | uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; |
86 | expect(byte_stuffer_send_frame, | 104 | EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _)) |
87 | when(size, is_equal_to(sizeof(expected))), | 105 | .With(Args<1, 2>(ElementsAreArray(expected))); |
88 | when(data, is_equal_to_contents_of(expected, sizeof(expected))) | ||
89 | ); | ||
90 | validator_send_frame(0, original, 1); | 106 | validator_send_frame(0, original, 1); |
91 | } | 107 | } |
92 | 108 | ||
93 | Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { | 109 | TEST_F(FrameValidator, sends_five_bytes_with_correct_crc) { |
94 | uint8_t original[] = {1, 2, 3, 4, 5, 0, 0, 0, 0}; | 110 | uint8_t original[] = {1, 2, 3, 4, 5, 0, 0, 0, 0}; |
95 | uint8_t expected[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; | 111 | uint8_t expected[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; |
96 | expect(byte_stuffer_send_frame, | 112 | EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _)) |
97 | when(size, is_equal_to(sizeof(expected))), | 113 | .With(Args<1, 2>(ElementsAreArray(expected))); |
98 | when(data, is_equal_to_contents_of(expected, sizeof(expected))) | ||
99 | ); | ||
100 | validator_send_frame(0, original, 5); | 114 | validator_send_frame(0, original, 5); |
101 | } | 115 | } |
diff --git a/quantum/serial_link/tests/rules.mk b/quantum/serial_link/tests/rules.mk index 7d0d6c0d0..7f2a8f457 100644 --- a/quantum/serial_link/tests/rules.mk +++ b/quantum/serial_link/tests/rules.mk | |||
@@ -5,5 +5,9 @@ serial_link_byte_stuffer_SRC :=\ | |||
5 | serial_link_frame_router_SRC := \ | 5 | serial_link_frame_router_SRC := \ |
6 | $(SERIAL_PATH)/tests/frame_router_tests.cpp \ | 6 | $(SERIAL_PATH)/tests/frame_router_tests.cpp \ |
7 | $(SERIAL_PATH)/protocol/byte_stuffer.c \ | 7 | $(SERIAL_PATH)/protocol/byte_stuffer.c \ |
8 | $(SERIAL_PATH)/protocol/frame_validator.c \ | 8 | $(SERIAL_PATH)/protocol/frame_validator.c \ |
9 | $(SERIAL_PATH)/protocol/frame_router.c \ No newline at end of file | 9 | $(SERIAL_PATH)/protocol/frame_router.c |
10 | |||
11 | serial_link_frame_validator_SRC := \ | ||
12 | $(SERIAL_PATH)/tests/frame_validator_tests.cpp \ | ||
13 | $(SERIAL_PATH)/protocol/frame_validator.c \ No newline at end of file | ||