Build actual tests, convert byte_stuffer_tests

pull/690/head
Fred Sundvik 8 years ago
parent 8a9aac6e32
commit 0a11460175

@ -155,12 +155,8 @@ ifeq ($(strip $(TAP_DANCE_ENABLE)), yes)
endif endif
ifeq ($(strip $(SERIAL_LINK_ENABLE)), yes) ifeq ($(strip $(SERIAL_LINK_ENABLE)), yes)
SERIAL_DIR = $(QUANTUM_DIR)/serial_link
SERIAL_PATH = $(QUANTUM_PATH)/serial_link
SERIAL_SRC = $(wildcard $(SERIAL_PATH)/protocol/*.c)
SERIAL_SRC += $(wildcard $(SERIAL_PATH)/system/*.c)
SRC += $(patsubst $(QUANTUM_PATH)/%,%,$(SERIAL_SRC)) SRC += $(patsubst $(QUANTUM_PATH)/%,%,$(SERIAL_SRC))
OPT_DEFS += -DSERIAL_LINK_ENABLE OPT_DEFS += $(SERIAL_DEFS)
VAPTH += $(SERIAL_PATH) VAPTH += $(SERIAL_PATH)
endif endif

@ -10,7 +10,9 @@ TARGET=test/$(TEST)
GTEST_OUTPUT = $(BUILD_DIR)/gtest GTEST_OUTPUT = $(BUILD_DIR)/gtest
OUTPUTS := $(GTEST_OUTPUT) TEST_OBJ = $(BUILD_DIR)/test_obj
OUTPUTS := $(TEST_OBJ)/$(TEST) $(GTEST_OUTPUT)
GTEST_INC := \ GTEST_INC := \
$(LIB_PATH)/googletest/googletest/include\ $(LIB_PATH)/googletest/googletest/include\
@ -36,8 +38,16 @@ VPATH +=\
all: elf all: elf
include $(QUANTUM_PATH)/serial_link/tests/rules.mk
$(TEST_OBJ)/$(TEST)_SRC := $($(TEST)_SRC)
$(TEST_OBJ)/$(TEST)_INC := $($(TEST)_INC) $(VPATH) $(GTEST_INC)
$(TEST_OBJ)/$(TEST)_DEFS := $($(TEST)_DEFS)
include $(TMK_PATH)/native.mk include $(TMK_PATH)/native.mk
include $(TMK_PATH)/rules.mk include $(TMK_PATH)/rules.mk
$(shell mkdir $(BUILD_DIR)/test 2>/dev/null)
$(shell mkdir -p $(BUILD_DIR)/test 2>/dev/null)
$(shell mkdir -p $(TEST_OBJ) 2>/dev/null)

@ -9,4 +9,18 @@ LIB_PATH = $(TOP_DIR)/lib
QUANTUM_DIR = quantum QUANTUM_DIR = quantum
QUANTUM_PATH = $(TOP_DIR)/$(QUANTUM_DIR) QUANTUM_PATH = $(TOP_DIR)/$(QUANTUM_DIR)
BUILD_DIR := $(TOP_DIR)/.build BUILD_DIR := $(TOP_DIR)/.build
SERIAL_DIR := $(QUANTUM_DIR)/serial_link
SERIAL_PATH := $(QUANTUM_PATH)/serial_link
SERIAL_SRC := $(wildcard $(SERIAL_PATH)/protocol/*.c)
SERIAL_SRC += $(wildcard $(SERIAL_PATH)/system/*.c)
SERIAL_DEFS += -DSERIAL_LINK_ENABLE
VPATH += $(TOP_DIR)
VPATH += $(TMK_PATH)
VPATH += $(QUANTUM_PATH)
VPATH += $(QUANTUM_PATH)/keymap_extras
VPATH += $(QUANTUM_PATH)/audio
VPATH += $(QUANTUM_PATH)/process_keycode
VPATH += $(SERIAL_PATH)

@ -31,9 +31,6 @@ SOFTWARE.
// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
// http://www.stuartcheshire.org/papers/COBSforToN.pdf // http://www.stuartcheshire.org/papers/COBSforToN.pdf
#define MAX_FRAME_SIZE 1024
#define NUM_LINKS 2
typedef struct byte_stuffer_state { typedef struct byte_stuffer_state {
uint16_t next_zero; uint16_t next_zero;
uint16_t data_pos; uint16_t data_pos;

@ -27,6 +27,9 @@ SOFTWARE.
#include <stdint.h> #include <stdint.h>
#define MAX_FRAME_SIZE 1024
#define NUM_LINKS 2
void init_byte_stuffer(void); void init_byte_stuffer(void);
void byte_stuffer_recv_byte(uint8_t link, uint8_t data); void byte_stuffer_recv_byte(uint8_t link, uint8_t data);
void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size); void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size);

@ -22,70 +22,90 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <cgreen/cgreen.h> #include "gtest/gtest.h"
#include <cgreen/mocks.h> #include "gmock/gmock.h"
#include <vector>
#include <algorithm>
extern "C" {
#include "serial_link/protocol/byte_stuffer.h" #include "serial_link/protocol/byte_stuffer.h"
#include "serial_link/protocol/byte_stuffer.c"
#include "serial_link/protocol/frame_validator.h" #include "serial_link/protocol/frame_validator.h"
#include "serial_link/protocol/physical.h" #include "serial_link/protocol/physical.h"
}
static uint8_t sent_data[MAX_FRAME_SIZE*2]; using testing::_;
static uint16_t sent_data_size; using testing::ElementsAreArray;
using testing::Args;
Describe(ByteStuffer); class ByteStuffer : public ::testing::Test{
BeforeEach(ByteStuffer) { public:
init_byte_stuffer(); ByteStuffer() {
sent_data_size = 0; Instance = this;
} init_byte_stuffer();
AfterEach(ByteStuffer) {} }
void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { ~ByteStuffer() {
mock(data, size); 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;
void send_data(uint8_t link, const uint8_t* data, uint16_t size) { static ByteStuffer* Instance;
memcpy(sent_data + sent_data_size, data, size); };
sent_data_size += size;
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);
}
} }
Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { TEST_F(ByteStuffer, receives_no_frame_for_a_single_zero_byte) {
never_expect(validator_recv_frame); EXPECT_CALL(*this, validator_recv_frame(_, _, _))
.Times(0);
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { TEST_F(ByteStuffer, receives_no_frame_for_a_single_FF_byte) {
never_expect(validator_recv_frame); EXPECT_CALL(*this, validator_recv_frame(_, _, _))
.Times(0);
byte_stuffer_recv_byte(0, 0xFF); byte_stuffer_recv_byte(0, 0xFF);
} }
Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { TEST_F(ByteStuffer, receives_no_frame_for_a_single_random_byte) {
never_expect(validator_recv_frame); EXPECT_CALL(*this, validator_recv_frame(_, _, _))
.Times(0);
byte_stuffer_recv_byte(0, 0x4A); byte_stuffer_recv_byte(0, 0x4A);
} }
Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { TEST_F(ByteStuffer, receives_no_frame_for_a_zero_length_frame) {
never_expect(validator_recv_frame); EXPECT_CALL(*this, validator_recv_frame(_, _, _))
.Times(0);
byte_stuffer_recv_byte(0, 1); byte_stuffer_recv_byte(0, 1);
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_single_byte_valid_frame) { TEST_F(ByteStuffer, receives_single_byte_valid_frame) {
uint8_t expected[] = {0x37}; uint8_t expected[] = {0x37};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(1)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 1))
);
byte_stuffer_recv_byte(0, 2); byte_stuffer_recv_byte(0, 2);
byte_stuffer_recv_byte(0, 0x37); byte_stuffer_recv_byte(0, 0x37);
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
TEST_F(ByteStuffer, receives_three_bytes_valid_frame) {
Ensure(ByteStuffer, receives_three_bytes_valid_frame) {
uint8_t expected[] = {0x37, 0x99, 0xFF}; uint8_t expected[] = {0x37, 0x99, 0xFF};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(3)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 3))
);
byte_stuffer_recv_byte(0, 4); byte_stuffer_recv_byte(0, 4);
byte_stuffer_recv_byte(0, 0x37); byte_stuffer_recv_byte(0, 0x37);
byte_stuffer_recv_byte(0, 0x99); byte_stuffer_recv_byte(0, 0x99);
@ -93,23 +113,19 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) {
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_single_zero_valid_frame) { TEST_F(ByteStuffer, receives_single_zero_valid_frame) {
uint8_t expected[] = {0}; uint8_t expected[] = {0};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(1)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 1))
);
byte_stuffer_recv_byte(0, 1); byte_stuffer_recv_byte(0, 1);
byte_stuffer_recv_byte(0, 1); byte_stuffer_recv_byte(0, 1);
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { TEST_F(ByteStuffer, receives_valid_frame_with_zeroes) {
uint8_t expected[] = {5, 0, 3, 0}; uint8_t expected[] = {5, 0, 3, 0};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(4)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 4))
);
byte_stuffer_recv_byte(0, 2); byte_stuffer_recv_byte(0, 2);
byte_stuffer_recv_byte(0, 5); byte_stuffer_recv_byte(0, 5);
byte_stuffer_recv_byte(0, 2); byte_stuffer_recv_byte(0, 2);
@ -118,17 +134,14 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) {
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_two_valid_frames) {
TEST_F(ByteStuffer, receives_two_valid_frames) {
uint8_t expected1[] = {5, 0}; uint8_t expected1[] = {5, 0};
uint8_t expected2[] = {3}; uint8_t expected2[] = {3};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(2)), .With(Args<1, 2>(ElementsAreArray(expected1)));
when(data, is_equal_to_contents_of(expected1, 2)) EXPECT_CALL(*this, validator_recv_frame(_, _, _))
); .With(Args<1, 2>(ElementsAreArray(expected2)));
expect(validator_recv_frame,
when(size, is_equal_to(1)),
when(data, is_equal_to_contents_of(expected2, 1))
);
byte_stuffer_recv_byte(1, 2); byte_stuffer_recv_byte(1, 2);
byte_stuffer_recv_byte(1, 5); byte_stuffer_recv_byte(1, 5);
byte_stuffer_recv_byte(1, 1); byte_stuffer_recv_byte(1, 1);
@ -138,12 +151,10 @@ Ensure(ByteStuffer, receives_two_valid_frames) {
byte_stuffer_recv_byte(1, 0); byte_stuffer_recv_byte(1, 0);
} }
Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_zero) {
uint8_t expected[] = {5, 7}; uint8_t expected[] = {5, 7};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(2)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 2))
);
byte_stuffer_recv_byte(1, 3); byte_stuffer_recv_byte(1, 3);
byte_stuffer_recv_byte(1, 1); byte_stuffer_recv_byte(1, 1);
byte_stuffer_recv_byte(1, 0); byte_stuffer_recv_byte(1, 0);
@ -153,12 +164,10 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) {
byte_stuffer_recv_byte(1, 0); byte_stuffer_recv_byte(1, 0);
} }
Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) {
uint8_t expected[] = {5, 7}; uint8_t expected[] = {5, 7};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(2)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 2))
);
byte_stuffer_recv_byte(0, 2); byte_stuffer_recv_byte(0, 2);
byte_stuffer_recv_byte(0, 9); byte_stuffer_recv_byte(0, 9);
byte_stuffer_recv_byte(0, 4); // This should have been zero byte_stuffer_recv_byte(0, 4); // This should have been zero
@ -169,16 +178,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) {
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) {
uint8_t expected[254]; uint8_t expected[254];
int i; int i;
for (i=0;i<254;i++) { for (i=0;i<254;i++) {
expected[i] = i + 1; expected[i] = i + 1;
} }
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(254)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 254))
);
byte_stuffer_recv_byte(0, 0xFF); byte_stuffer_recv_byte(0, 0xFF);
for (i=0;i<254;i++) { for (i=0;i<254;i++) {
byte_stuffer_recv_byte(0, i+1); byte_stuffer_recv_byte(0, i+1);
@ -186,17 +193,15 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) {
uint8_t expected[255]; uint8_t expected[255];
int i; int i;
for (i=0;i<254;i++) { for (i=0;i<254;i++) {
expected[i] = i + 1; expected[i] = i + 1;
} }
expected[254] = 7; expected[254] = 7;
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(255)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 255))
);
byte_stuffer_recv_byte(0, 0xFF); byte_stuffer_recv_byte(0, 0xFF);
for (i=0;i<254;i++) { for (i=0;i<254;i++) {
byte_stuffer_recv_byte(0, i+1); byte_stuffer_recv_byte(0, i+1);
@ -206,17 +211,15 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) {
uint8_t expected[255]; uint8_t expected[255];
int i; int i;
for (i=0;i<254;i++) { for (i=0;i<254;i++) {
expected[i] = i + 1; expected[i] = i + 1;
} }
expected[254] = 0; expected[254] = 0;
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(255)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 255))
);
byte_stuffer_recv_byte(0, 0xFF); byte_stuffer_recv_byte(0, 0xFF);
for (i=0;i<254;i++) { for (i=0;i<254;i++) {
byte_stuffer_recv_byte(0, i+1); byte_stuffer_recv_byte(0, i+1);
@ -226,7 +229,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { TEST_F(ByteStuffer, receives_two_long_frames_and_some_more) {
uint8_t expected[515]; uint8_t expected[515];
int i; int i;
int j; int j;
@ -238,10 +241,8 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) {
for (i=0;i<7;i++) { for (i=0;i<7;i++) {
expected[254*2+i] = i + 1; expected[254*2+i] = i + 1;
} }
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(515)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 510))
);
byte_stuffer_recv_byte(0, 0xFF); byte_stuffer_recv_byte(0, 0xFF);
for (i=0;i<254;i++) { for (i=0;i<254;i++) {
byte_stuffer_recv_byte(0, i+1); byte_stuffer_recv_byte(0, i+1);
@ -261,12 +262,10 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) {
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { TEST_F(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
uint8_t expected[MAX_FRAME_SIZE] = {}; uint8_t expected[MAX_FRAME_SIZE] = {};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(MAX_FRAME_SIZE)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE))
);
int i; int i;
byte_stuffer_recv_byte(0, 1); byte_stuffer_recv_byte(0, 1);
for(i=0;i<MAX_FRAME_SIZE;i++) { for(i=0;i<MAX_FRAME_SIZE;i++) {
@ -275,9 +274,10 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) { TEST_F(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) {
uint8_t expected[1] = {0}; uint8_t expected[1] = {0};
never_expect(validator_recv_frame); EXPECT_CALL(*this, validator_recv_frame(_, _, _))
.Times(0);
int i; int i;
byte_stuffer_recv_byte(0, 1); byte_stuffer_recv_byte(0, 1);
for(i=0;i<MAX_FRAME_SIZE;i++) { for(i=0;i<MAX_FRAME_SIZE;i++) {
@ -287,12 +287,10 @@ Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) {
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) { TEST_F(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
uint8_t expected[1] = {1}; uint8_t expected[1] = {1};
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(1)), .With(Args<1, 2>(ElementsAreArray(expected)));
when(data, is_equal_to_contents_of(expected, 1))
);
int i; int i;
byte_stuffer_recv_byte(0, 1); byte_stuffer_recv_byte(0, 1);
for(i=0;i<MAX_FRAME_SIZE;i++) { for(i=0;i<MAX_FRAME_SIZE;i++) {
@ -303,76 +301,68 @@ Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
byte_stuffer_recv_byte(0, 0); byte_stuffer_recv_byte(0, 0);
} }
Ensure(ByteStuffer, does_nothing_when_sending_zero_size_frame) { TEST_F(ByteStuffer, does_nothing_when_sending_zero_size_frame) {
assert_that(sent_data_size, is_equal_to(0)); EXPECT_EQ(sent_data.size(), 0);
byte_stuffer_send_frame(0, NULL, 0); byte_stuffer_send_frame(0, NULL, 0);
} }
Ensure(ByteStuffer, send_one_byte_frame) { TEST_F(ByteStuffer, send_one_byte_frame) {
uint8_t data[] = {5}; uint8_t data[] = {5};
byte_stuffer_send_frame(1, data, 1); byte_stuffer_send_frame(1, data, 1);
uint8_t expected[] = {2, 5, 0}; uint8_t expected[] = {2, 5, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_two_byte_frame) { TEST_F(ByteStuffer, sends_two_byte_frame) {
uint8_t data[] = {5, 0x77}; uint8_t data[] = {5, 0x77};
byte_stuffer_send_frame(0, data, 2); byte_stuffer_send_frame(0, data, 2);
uint8_t expected[] = {3, 5, 0x77, 0}; uint8_t expected[] = {3, 5, 0x77, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_one_byte_frame_with_zero) { TEST_F(ByteStuffer, sends_one_byte_frame_with_zero) {
uint8_t data[] = {0}; uint8_t data[] = {0};
byte_stuffer_send_frame(0, data, 1); byte_stuffer_send_frame(0, data, 1);
uint8_t expected[] = {1, 1, 0}; uint8_t expected[] = {1, 1, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_two_byte_frame_starting_with_zero) { TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_zero) {
uint8_t data[] = {0, 9}; uint8_t data[] = {0, 9};
byte_stuffer_send_frame(1, data, 2); byte_stuffer_send_frame(1, data, 2);
uint8_t expected[] = {1, 2, 9, 0}; uint8_t expected[] = {1, 2, 9, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) { TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) {
uint8_t data[] = {9, 0}; uint8_t data[] = {9, 0};
byte_stuffer_send_frame(1, data, 2); byte_stuffer_send_frame(1, data, 2);
uint8_t expected[] = {2, 9, 1, 0}; uint8_t expected[] = {2, 9, 1, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) { TEST_F(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) {
uint8_t data[] = {9, 0, 0x68}; uint8_t data[] = {9, 0, 0x68};
byte_stuffer_send_frame(0, data, 3); byte_stuffer_send_frame(0, data, 3);
uint8_t expected[] = {2, 9, 2, 0x68, 0}; uint8_t expected[] = {2, 9, 2, 0x68, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_three_byte_frame_data_in_the_middle) { TEST_F(ByteStuffer, sends_three_byte_frame_data_in_the_middle) {
uint8_t data[] = {0, 0x55, 0}; uint8_t data[] = {0, 0x55, 0};
byte_stuffer_send_frame(0, data, 3); byte_stuffer_send_frame(0, data, 3);
uint8_t expected[] = {1, 2, 0x55, 1, 0}; uint8_t expected[] = {1, 2, 0x55, 1, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_three_byte_frame_with_all_zeroes) { TEST_F(ByteStuffer, sends_three_byte_frame_with_all_zeroes) {
uint8_t data[] = {0, 0, 0}; uint8_t data[] = {0, 0, 0};
byte_stuffer_send_frame(0, data, 3); byte_stuffer_send_frame(0, data, 3);
uint8_t expected[] = {1, 1, 1, 1, 0}; uint8_t expected[] = {1, 1, 1, 1, 0};
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_frame_with_254_non_zeroes) { TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes) {
uint8_t data[254]; uint8_t data[254];
int i; int i;
for(i=0;i<254;i++) { for(i=0;i<254;i++) {
@ -385,11 +375,10 @@ Ensure(ByteStuffer, sends_frame_with_254_non_zeroes) {
expected[i] = i; expected[i] = i;
} }
expected[255] = 0; expected[255] = 0;
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_frame_with_255_non_zeroes) { TEST_F(ByteStuffer, sends_frame_with_255_non_zeroes) {
uint8_t data[255]; uint8_t data[255];
int i; int i;
for(i=0;i<255;i++) { for(i=0;i<255;i++) {
@ -404,17 +393,16 @@ Ensure(ByteStuffer, sends_frame_with_255_non_zeroes) {
expected[255] = 2; expected[255] = 2;
expected[256] = 255; expected[256] = 255;
expected[257] = 0; expected[257] = 0;
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) { TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) {
uint8_t data[255]; uint8_t data[255];
int i; int i;
for(i=0;i<254;i++) { for(i=0;i<254;i++) {
data[i] = i + 1; data[i] = i + 1;
} }
data[255] = 0; data[254] = 0;
byte_stuffer_send_frame(0, data, 255); byte_stuffer_send_frame(0, data, 255);
uint8_t expected[258]; uint8_t expected[258];
expected[0] = 0xFF; expected[0] = 0xFF;
@ -424,53 +412,46 @@ Ensure(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) {
expected[255] = 1; expected[255] = 1;
expected[256] = 1; expected[256] = 1;
expected[257] = 0; expected[257] = 0;
assert_that(sent_data_size, is_equal_to(sizeof(expected))); EXPECT_THAT(sent_data, ElementsAreArray(expected));
assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
} }
Ensure(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) { TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) {
uint8_t original_data[] = { 1, 2, 3}; uint8_t original_data[] = { 1, 2, 3};
byte_stuffer_send_frame(0, original_data, sizeof(original_data)); byte_stuffer_send_frame(0, original_data, sizeof(original_data));
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(sizeof(original_data))), .With(Args<1, 2>(ElementsAreArray(original_data)));
when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
);
int i; int i;
for(i=0;i<sent_data_size;i++) { for(auto& d : sent_data) {
byte_stuffer_recv_byte(1, sent_data[i]); byte_stuffer_recv_byte(1, d);
} }
} }
Ensure(ByteStuffer, sends_and_receives_full_roundtrip_small_packet_with_zeros) { TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet_with_zeros) {
uint8_t original_data[] = { 1, 0, 3, 0, 0, 9}; uint8_t original_data[] = { 1, 0, 3, 0, 0, 9};
byte_stuffer_send_frame(1, original_data, sizeof(original_data)); byte_stuffer_send_frame(1, original_data, sizeof(original_data));
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(sizeof(original_data))), .With(Args<1, 2>(ElementsAreArray(original_data)));
when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
);
int i; int i;
for(i=0;i<sent_data_size;i++) { for(auto& d : sent_data) {
byte_stuffer_recv_byte(0, sent_data[i]); byte_stuffer_recv_byte(1, d);
} }
} }
Ensure(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes) { TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes) {
uint8_t original_data[254]; uint8_t original_data[254];
int i; int i;
for(i=0;i<254;i++) { for(i=0;i<254;i++) {
original_data[i] = i + 1; original_data[i] = i + 1;
} }
byte_stuffer_send_frame(0, original_data, sizeof(original_data)); byte_stuffer_send_frame(0, original_data, sizeof(original_data));
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(sizeof(original_data))), .With(Args<1, 2>(ElementsAreArray(original_data)));
when(data, is_equal_to_contents_of(original_data, sizeof(original_data))) for(auto& d : sent_data) {
); byte_stuffer_recv_byte(1, d);
for(i=0;i<sent_data_size;i++) {
byte_stuffer_recv_byte(1, sent_data[i]);
} }
} }
Ensure(ByteStuffer, sends_and_receives_full_roundtrip_256_bytes) { TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_256_bytes) {
uint8_t original_data[256]; uint8_t original_data[256];
int i; int i;
for(i=0;i<254;i++) { for(i=0;i<254;i++) {
@ -479,16 +460,14 @@ Ensure(ByteStuffer, sends_and_receives_full_roundtrip_256_bytes) {
original_data[254] = 22; original_data[254] = 22;
original_data[255] = 23; original_data[255] = 23;
byte_stuffer_send_frame(0, original_data, sizeof(original_data)); byte_stuffer_send_frame(0, original_data, sizeof(original_data));
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(sizeof(original_data))), .With(Args<1, 2>(ElementsAreArray(original_data)));
when(data, is_equal_to_contents_of(original_data, sizeof(original_data))) for(auto& d : sent_data) {
); byte_stuffer_recv_byte(1, d);
for(i=0;i<sent_data_size;i++) {
byte_stuffer_recv_byte(1, sent_data[i]);
} }
} }
Ensure(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes_and_then_zero) { TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes_and_then_zero) {
uint8_t original_data[255]; uint8_t original_data[255];
int i; int i;
for(i=0;i<254;i++) { for(i=0;i<254;i++) {
@ -496,11 +475,9 @@ Ensure(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes_and_then_zero) {
} }
original_data[254] = 0; original_data[254] = 0;
byte_stuffer_send_frame(0, original_data, sizeof(original_data)); byte_stuffer_send_frame(0, original_data, sizeof(original_data));
expect(validator_recv_frame, EXPECT_CALL(*this, validator_recv_frame(_, _, _))
when(size, is_equal_to(sizeof(original_data))), .With(Args<1, 2>(ElementsAreArray(original_data)));
when(data, is_equal_to_contents_of(original_data, sizeof(original_data))) for(auto& d : sent_data) {
); byte_stuffer_recv_byte(1, d);
for(i=0;i<sent_data_size;i++) {
byte_stuffer_recv_byte(1, sent_data[i]);
} }
} }

@ -0,0 +1,3 @@
serial_link_byte_stuffer_SRC :=\
$(SERIAL_PATH)/tests/byte_stuffer_tests.cpp \
$(SERIAL_PATH)/protocol/byte_stuffer.c
Loading…
Cancel
Save