1
0
Fork 0
forked from forks/qmk_firmware

Convert frame_validator_tests to GTest

This commit is contained in:
Fred Sundvik 2016-08-27 13:54:16 +03:00
parent cdd0913bcc
commit b3eba797af
2 changed files with 58 additions and 40 deletions

View file

@ -22,24 +22,47 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include "serial_link/protocol/frame_validator.c"
#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) {
mock(data, size);
FrameValidator::Instance->route_incoming_frame(link, data, size);
}
void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
mock(data, size);
FrameValidator::Instance->byte_stuffer_send_frame(link, data, size);
}
}
Describe(FrameValidator);
BeforeEach(FrameValidator) {}
AfterEach(FrameValidator) {}
Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) {
never_expect(route_incoming_frame);
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);
@ -47,55 +70,46 @@ Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) {
validator_recv_frame(0, data, 4);
}
Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) {
TEST_F(FrameValidator, validates_one_byte_frame_with_correct_crc) {
uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3};
expect(route_incoming_frame,
when(size, is_equal_to(1)),
when(data, is_equal_to_contents_of(data, 1))
);
EXPECT_CALL(*this, route_incoming_frame(_, _, _))
.With(Args<1, 2>(ElementsAreArray(data, 1)));
validator_recv_frame(0, data, 5);
}
Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) {
TEST_F(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) {
uint8_t data[] = {0x44, 0, 0, 0, 0};
never_expect(route_incoming_frame);
EXPECT_CALL(*this, route_incoming_frame(_, _, _))
.Times(0);
validator_recv_frame(1, data, 5);
}
Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) {
TEST_F(FrameValidator, validates_four_byte_frame_with_correct_crc) {
uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA};
expect(route_incoming_frame,
when(size, is_equal_to(4)),
when(data, is_equal_to_contents_of(data, 4))
);
EXPECT_CALL(*this, route_incoming_frame(_, _, _))
.With(Args<1, 2>(ElementsAreArray(data, 4)));
validator_recv_frame(1, data, 8);
}
Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) {
TEST_F(FrameValidator, validates_five_byte_frame_with_correct_crc) {
uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47};
expect(route_incoming_frame,
when(size, is_equal_to(5)),
when(data, is_equal_to_contents_of(data, 5))
);
EXPECT_CALL(*this, route_incoming_frame(_, _, _))
.With(Args<1, 2>(ElementsAreArray(data, 5)));
validator_recv_frame(0, data, 9);
}
Ensure(FrameValidator, sends_one_byte_with_correct_crc) {
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(byte_stuffer_send_frame,
when(size, is_equal_to(sizeof(expected))),
when(data, is_equal_to_contents_of(expected, sizeof(expected)))
);
EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _))
.With(Args<1, 2>(ElementsAreArray(expected)));
validator_send_frame(0, original, 1);
}
Ensure(FrameValidator, sends_five_bytes_with_correct_crc) {
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(byte_stuffer_send_frame,
when(size, is_equal_to(sizeof(expected))),
when(data, is_equal_to_contents_of(expected, sizeof(expected)))
);
EXPECT_CALL(*this, byte_stuffer_send_frame(_, _, _))
.With(Args<1, 2>(ElementsAreArray(expected)));
validator_send_frame(0, original, 5);
}

View file

@ -7,3 +7,7 @@ serial_link_frame_router_SRC := \
$(SERIAL_PATH)/protocol/byte_stuffer.c \
$(SERIAL_PATH)/protocol/frame_validator.c \
$(SERIAL_PATH)/protocol/frame_router.c
serial_link_frame_validator_SRC := \
$(SERIAL_PATH)/tests/frame_validator_tests.cpp \
$(SERIAL_PATH)/protocol/frame_validator.c