summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2016-08-27 17:06:45 -0400
committerGitHub <noreply@github.com>2016-08-27 17:06:45 -0400
commit4fd5ac8326f491a347b8c049f32a3353a633ebf5 (patch)
treec82d8b5325ea82a7544489022afe00bf909495c3
parentf815b4871ebc5cc2f8d1a306eceec68c0a973a4c (diff)
parent922c4ea3bc46f6246d9f67ead11bcf53ff947ef3 (diff)
Merge pull request #690 from fredizzimo/unit_test
Add Unit Testing support
-rw-r--r--.gitmodules3
-rw-r--r--.travis.yml10
-rw-r--r--Makefile71
-rw-r--r--build_keyboard.mk52
-rw-r--r--build_test.mk57
-rw-r--r--common.mk26
m---------lib/googletest0
-rw-r--r--message.mk8
-rw-r--r--quantum/serial_link/protocol/byte_stuffer.c3
-rw-r--r--quantum/serial_link/protocol/byte_stuffer.h3
-rw-r--r--quantum/serial_link/protocol/transport.c4
-rw-r--r--quantum/serial_link/protocol/transport.h7
-rw-r--r--quantum/serial_link/tests/byte_stuffer_tests.cpp (renamed from quantum/serial_link/tests/byte_stuffer_tests.c)313
-rw-r--r--quantum/serial_link/tests/frame_router_tests.c231
-rw-r--r--quantum/serial_link/tests/frame_router_tests.cpp229
-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.mk22
-rw-r--r--quantum/serial_link/tests/testlist.mk6
-rw-r--r--quantum/serial_link/tests/transport_tests.c168
-rw-r--r--quantum/serial_link/tests/transport_tests.cpp188
-rw-r--r--quantum/serial_link/tests/triple_buffered_object_tests.cpp (renamed from quantum/serial_link/tests/triple_buffered_object_tests.c)52
-rw-r--r--readme.md51
-rw-r--r--testlist.mk13
-rw-r--r--tmk_core/common.mk71
-rw-r--r--tmk_core/native.mk24
-rw-r--r--tmk_core/rules.mk29
26 files changed, 1007 insertions, 724 deletions
diff --git a/.gitmodules b/.gitmodules
index 7e3c92d031..12b7cb918f 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -7,3 +7,6 @@
[submodule "lib/ugfx"]
path = lib/ugfx
url = https://bitbucket.org/Tectu/ugfx
+[submodule "lib/googletest"]
+ path = lib/googletest
+ url = https://github.com/google/googletest
diff --git a/.travis.yml b/.travis.yml
index 138a2c5532..f4c918a5ed 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,7 +10,10 @@ env:
global:
- secure: vBTSL34BDPxDilKUuTXqU4CJ26Pv5hogD2nghatkxSQkI1/jbdnLj/DQdPUrMJFDIY6TK3AltsBx72MaMsLQ1JO/Ou24IeHINHXzUC1FlS9yQa48cpxnhX5kzXNyGs3oa0qaFbvnr7RgYRWtmD52n4bIZuSuW+xpBv05x2OCizdT2ZonH33nATaHGFasxROm4qYZ241VfzcUv766V6RVHgL4x9V08warugs+RENVkfzxxwhk3NmkrISabze0gSVJLHBPHxroZC6EUcf/ocobcuDrCwFqtEt90i7pNIAFUE7gZsN2uE75LmpzAWin21G7lLPcPL2k4FJVd8an1HiP2WmscJU6U89fOfMb2viObnKcCzebozBCmKGtHEuXZo9FcReOx49AnQSpmESJGs+q2dL/FApkTjQiyT4J6O5dJpoww0/r57Wx0cmmqjETKBb5rSgXM51Etk3wO09mvcPHsEwrT7qH8r9XWdyCDoEn7FCLX3/LYnf/D4SmZ633YPl5gv3v9XEwxR5+04akjgnvWDSNIaDbWBdxHNb7l4pMc+WR1bwCyMyA7KXj0RrftEGOrm9ZRLe6BkbT4cycA+j77nbPOMcyZChliV9pPQos+4TOJoTzcK2L8yWVoY409aDNVuAjdP6Yum0R2maBGl/etLmIMpJC35C5/lZ+dUNjJAM=
script:
-- make all-keyboards AUTOGEN=true
+- make $TARGET AUTOGEN=$AUTOGEN
+env:
+- TARGET=all-keyboards AUTOGEN=true
+- TARGET=test AUTOGEN=false
addons:
apt:
packages:
@@ -22,4 +25,7 @@ addons:
- binutils-arm-none-eabi
- libnewlib-arm-none-eabi
- diffutils
-after_success: bash util/travis_compiled_push.sh
+after_success:
+ if [ "$AUTOGEN" == "true" ]; then
+ bash util/travis_compiled_push.sh;
+ fi
diff --git a/Makefile b/Makefile
index f78cca6c6f..0aaf585786 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,10 @@ ifndef VERBOSE
.SILENT:
endif
+# Never run this makefile in parallel, as it could screw things up
+# It won't affect the submakes, so you still get the speedup from specifying -jx
+.NOTPARALLEL:
+
# Allow the silent with lower caps to work the same way as upper caps
ifdef silent
SILENT = $(silent)
@@ -28,6 +32,7 @@ ABS_ROOT_MAKEFILE := $(abspath $(ROOT_MAKEFILE))
ABS_STARTING_DIR := $(dir $(ABS_STARTING_MAKEFILE))
ABS_ROOT_DIR := $(dir $(ABS_ROOT_MAKEFILE))
STARTING_DIR := $(subst $(ABS_ROOT_DIR),,$(ABS_STARTING_DIR))
+TEST_DIR := $(ROOT_DIR)/.build/test
MAKEFILE_INCLUDED=yes
@@ -224,6 +229,8 @@ define PARSE_RULE
# PARSE_ALL_KEYBOARDS
ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
$$(eval $$(call PARSE_ALL_KEYBOARDS))
+ else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,test),true)
+ $$(eval $$(call PARSE_TEST))
# If the rule starts with the name of a known keyboard, then continue
# the parsing from PARSE_KEYBOARD
else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
@@ -356,7 +363,6 @@ define PARSE_KEYMAP
MAKE_TARGET := $$(patsubst -%,%,$$(RULE))
# We need to generate an unique indentifer to append to the COMMANDS list
COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
- COMMANDS += $$(COMMAND)
# If we are compiling a keyboard without a subproject, we want to display just the name
# of the keyboard, otherwise keyboard/subproject
ifeq ($$(CURRENT_SP),)
@@ -368,13 +374,18 @@ define PARSE_KEYMAP
KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
# Specify the variables that we are passing forward to submake
MAKE_VARS := KEYBOARD=$$(CURRENT_KB) SUBPROJECT=$$(CURRENT_SP) KEYMAP=$$(CURRENT_KM)
- MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
# And the first part of the make command
MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_keyboard.mk $$(MAKE_TARGET)
# The message to display
MAKE_MSG := $$(MSG_MAKE_KB)
# We run the command differently, depending on if we want more output or not
# The true version for silent output and the false version otherwise
+ $$(eval $$(call BUILD))
+endef
+
+define BUILD
+ MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
+ COMMANDS += $$(COMMAND)
COMMAND_true_$$(COMMAND) := \
printf "$$(MAKE_MSG)" | \
$$(MAKE_MSG_FORMAT); \
@@ -388,7 +399,10 @@ define PARSE_KEYMAP
fi;
COMMAND_false_$$(COMMAND) := \
printf "$$(MAKE_MSG)\n\n"; \
- $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false;
+ $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false; \
+ if [ $$$$? -gt 0 ]; \
+ then error_occured=1; \
+ fi;
endef
# Just parse all the keymaps for a specifc keyboard
@@ -396,6 +410,41 @@ define PARSE_ALL_KEYMAPS
$$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
endef
+define BUILD_TEST
+ TEST_NAME := $1
+ MAKE_TARGET := $2
+ COMMAND := $1
+ MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
+ MAKE_VARS := TEST=$$(TEST_NAME)
+ MAKE_MSG := $$(MSG_MAKE_TEST)
+ $$(eval $$(call BUILD))
+ ifneq ($$(MAKE_TARGET),clean)
+ TEST_EXECUTABLE := $$(TEST_DIR)/$$(TEST_NAME).elf
+ TESTS += $$(TEST_NAME)
+ TEST_MSG := $$(MSG_TEST)
+ $$(TEST_NAME)_COMMAND := \
+ printf "$$(TEST_MSG)\n"; \
+ $$(TEST_EXECUTABLE); \
+ if [ $$$$? -gt 0 ]; \
+ then error_occured=1; \
+ fi; \
+ printf "\n";
+ endif
+endef
+
+define PARSE_TEST
+ TESTS :=
+ TEST_NAME := $$(firstword $$(subst -, ,$$(RULE)))
+ TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME)-,,$$(RULE)))
+ ifeq ($$(TEST_NAME),all)
+ MATCHED_TESTS := $$(TEST_LIST)
+ else
+ MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
+ endif
+ $$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
+endef
+
+
# Set the silent mode depending on if we are trying to compile multiple keyboards or not
# By default it's on in that case, but it can be overriden by specifying silent=false
# from the command line
@@ -440,12 +489,13 @@ $(SUBPROJECTS): %: %-allkm
# But we return the error code at the end, to trigger travis failures
+error_occured=0; \
$(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND)) \
- if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi
-
+ if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi;\
+ $(foreach TEST,$(TESTS),$($(TEST)_COMMAND)) \
+ if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi;\
# All should compile everything
.PHONY: all
-all: all-keyboards
+all: all-keyboards test-all
# Define some shortcuts, mostly for compability with the old syntax
.PHONY: all-keyboards
@@ -454,9 +504,16 @@ all-keyboards: allkb-allsp-allkm
.PHONY: all-keyboards-defaults
all-keyboards-defaults: allkb-allsp-default
+.PHONY: test
+test: test-all
+
+.PHONY: test-clean
+test-clean: test-all-clean
# Generate the version.h file
GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
$(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
-$(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h) \ No newline at end of file
+$(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
+
+include $(ROOT_DIR)/testlist.mk \ No newline at end of file
diff --git a/build_keyboard.mk b/build_keyboard.mk
index f843572f66..04632c1c94 100644
--- a/build_keyboard.mk
+++ b/build_keyboard.mk
@@ -4,19 +4,7 @@ endif
.DEFAULT_GOAL := all
-include message.mk
-
-
-# Directory common source filess exist
-TOP_DIR = .
-TMK_DIR = tmk_core
-TMK_PATH = $(TOP_DIR)/$(TMK_DIR)
-LIB_PATH = $(TOP_DIR)/lib
-
-QUANTUM_DIR = quantum
-QUANTUM_PATH = $(TOP_DIR)/$(QUANTUM_DIR)
-
-BUILD_DIR := $(TOP_DIR)/.build
+include common.mk
ifneq ($(SUBPROJECT),)
TARGET ?= $(KEYBOARD)_$(SUBPROJECT)_$(KEYMAP)
@@ -35,6 +23,16 @@ ifdef master
MASTER = $(master)
endif
+ifeq ($(MASTER),right)
+ OPT_DEFS += -DMASTER_IS_ON_RIGHT
+else
+ ifneq ($(MASTER),left)
+$(error MASTER does not have a valid value(left/right))
+ endif
+endif
+
+
+
KEYBOARD_PATH := keyboards/$(KEYBOARD)
KEYBOARD_C := $(KEYBOARD_PATH)/$(KEYBOARD).c
@@ -167,12 +165,8 @@ ifeq ($(strip $(TAP_DANCE_ENABLE)), yes)
endif
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))
- OPT_DEFS += -DSERIAL_LINK_ENABLE
+ OPT_DEFS += $(SERIAL_DEFS)
VAPTH += $(SERIAL_PATH)
endif
@@ -185,15 +179,14 @@ ifneq ($(SUBPROJECT),)
VPATH += $(SUBPROJECT_PATH)
endif
VPATH += $(KEYBOARD_PATH)
-VPATH += $(TOP_DIR)
-VPATH += $(TMK_PATH)
-VPATH += $(QUANTUM_PATH)
-VPATH += $(QUANTUM_PATH)/keymap_extras
-VPATH += $(QUANTUM_PATH)/audio
-VPATH += $(QUANTUM_PATH)/process_keycode
+VPATH += $(COMMON_VPATH)
include $(TMK_PATH)/common.mk
+SRC += $(TMK_COMMON_SRC)
+OPT_DEFS += $(TMK_COMMON_DEFS)
+EXTRALDFLAGS += $(TMK_COMMON_LDFLAGS)
+
ifeq ($(PLATFORM),AVR)
include $(TMK_PATH)/protocol/lufa.mk
include $(TMK_PATH)/avr.mk
@@ -205,17 +198,24 @@ ifeq ($(strip $(VISUALIZER_ENABLE)), yes)
include $(VISUALIZER_PATH)/visualizer.mk
endif
-
OUTPUTS := $(KEYMAP_OUTPUT) $(KEYBOARD_OUTPUT)
$(KEYMAP_OUTPUT)_SRC := $(SRC)
$(KEYMAP_OUTPUT)_DEFS := $(OPT_DEFS) -DQMK_KEYBOARD=\"$(KEYBOARD)\" -DQMK_KEYMAP=\"$(KEYMAP)\"
-$(KEYMAP_OUTPUT)_INC := $(EXTRAINCDIRS) $(VPATH)
+$(KEYMAP_OUTPUT)_INC := $(VPATH) $(EXTRAINCDIRS)
$(KEYMAP_OUTPUT)_CONFIG := $(CONFIG_H)
$(KEYBOARD_OUTPUT)_SRC := $(CHIBISRC)
$(KEYBOARD_OUTPUT)_DEFS := $(PROJECT_DEFS)
$(KEYBOARD_OUTPUT)_INC := $(PROJECT_INC)
$(KEYBOARD_OUTPUT)_CONFIG := $(PROJECT_CONFIG)
+# Default target.
+all: build sizeafter
+
+# Change the build target to build a HEX file or a library.
+build: elf hex
+#build: elf hex eep lss sym
+#build: lib
+
include $(TMK_PATH)/rules.mk
diff --git a/build_test.mk b/build_test.mk
new file mode 100644
index 0000000000..ee15a45726
--- /dev/null
+++ b/build_test.mk
@@ -0,0 +1,57 @@
+ifndef VERBOSE
+.SILENT:
+endif
+
+.DEFAULT_GOAL := all
+
+include common.mk
+
+TARGET=test/$(TEST)
+
+GTEST_OUTPUT = $(BUILD_DIR)/gtest
+
+TEST_OBJ = $(BUILD_DIR)/test_obj
+
+OUTPUTS := $(TEST_OBJ)/$(TEST) $(GTEST_OUTPUT)
+
+GTEST_INC := \
+ $(LIB_PATH)/googletest/googletest/include\
+ $(LIB_PATH)/googletest/googlemock/include\
+
+GTEST_INTERNAL_INC :=\
+ $(LIB_PATH)/googletest/googletest\
+ $(LIB_PATH)/googletest/googlemock
+
+$(GTEST_OUTPUT)_SRC :=\
+ googletest/src/gtest-all.cc\
+ googletest/src/gtest_main.cc\
+ googlemock/src/gmock-all.cc
+
+$(GTEST_OUTPUT)_DEFS :=
+$(GTEST_OUTPUT)_INC := $(GTEST_INC) $(GTEST_INTERNAL_INC)
+
+LDFLAGS += -lstdc++ -lpthread -shared-libgcc
+CREATE_MAP := no
+
+VPATH +=\
+ $(LIB_PATH)/googletest\
+ $(LIB_PATH)/googlemock
+
+all: elf
+
+VPATH += $(COMMON_VPATH)
+
+include $(TMK_PATH)/common.mk
+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)/rules.mk
+
+
+$(shell mkdir -p $(BUILD_DIR)/test 2>/dev/null)
+$(shell mkdir -p $(TEST_OBJ) 2>/dev/null)
+
diff --git a/common.mk b/common.mk
new file mode 100644
index 0000000000..18751cd5ac
--- /dev/null
+++ b/common.mk
@@ -0,0 +1,26 @@
+include message.mk
+
+# Directory common source files exist
+TOP_DIR = .
+TMK_DIR = tmk_core
+TMK_PATH = $(TOP_DIR)/$(TMK_DIR)
+LIB_PATH = $(TOP_DIR)/lib
+
+QUANTUM_DIR = quantum
+QUANTUM_PATH = $(TOP_DIR)/$(QUANTUM_DIR)
+
+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
+
+COMMON_VPATH := $(TOP_DIR)
+COMMON_VPATH += $(TMK_PATH)
+COMMON_VPATH += $(QUANTUM_PATH)
+COMMON_VPATH += $(QUANTUM_PATH)/keymap_extras
+COMMON_VPATH += $(QUANTUM_PATH)/audio
+COMMON_VPATH += $(QUANTUM_PATH)/process_keycode
+COMMON_VPATH += $(SERIAL_PATH) \ No newline at end of file
diff --git a/lib/googletest b/lib/googletest
new file mode 160000
+Subproject ec44c6c1675c25b9827aacd08c02433cccde778
diff --git a/message.mk b/message.mk
index fb1a7a1f9a..fa051fd712 100644
--- a/message.mk
+++ b/message.mk
@@ -69,3 +69,11 @@ define GENERATE_MSG_MAKE_KB
endif
endef
MSG_MAKE_KB = $(eval $(call GENERATE_MSG_MAKE_KB))$(MSG_MAKE_KB_ACTUAL)
+define GENERATE_MSG_MAKE_TEST
+ MSG_MAKE_TEST_ACTUAL := Making test $(BOLD)$(TEST_NAME)$(NO_COLOR)
+ ifneq ($$(MAKE_TARGET),)
+ MSG_MAKE_TEST_ACTUAL += with target $(BOLD)$$(MAKE_TARGET)$(NO_COLOR)
+ endif
+endef
+MSG_MAKE_TEST = $(eval $(call GENERATE_MSG_MAKE_TEST))$(MSG_MAKE_TEST_ACTUAL)
+MSG_TEST = Testing $(BOLD)$(TEST_NAME)$(NO_COLOR)
diff --git a/quantum/serial_link/protocol/byte_stuffer.c b/quantum/serial_link/protocol/byte_stuffer.c
index fb4c45a8dc..2c87d64c29 100644
--- a/quantum/serial_link/protocol/byte_stuffer.c
+++ b/quantum/serial_link/protocol/byte_stuffer.c
@@ -31,9 +31,6 @@ SOFTWARE.
// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
// http://www.stuartcheshire.org/papers/COBSforToN.pdf
-#define MAX_FRAME_SIZE 1024
-#define NUM_LINKS 2
-
typedef struct byte_stuffer_state {
uint16_t next_zero;
uint16_t data_pos;
diff --git a/quantum/serial_link/protocol/byte_stuffer.h b/quantum/serial_link/protocol/byte_stuffer.h
index 2cc88beb42..97e8968564 100644
--- a/quantum/serial_link/protocol/byte_stuffer.h
+++ b/quantum/serial_link/protocol/byte_stuffer.h
@@ -27,6 +27,9 @@ SOFTWARE.
#include <stdint.h>
+#define MAX_FRAME_SIZE 1024
+#define NUM_LINKS 2
+
void init_byte_stuffer(void);
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);
diff --git a/quantum/serial_link/protocol/transport.c b/quantum/serial_link/protocol/transport.c
index f418d11ceb..ff795fe201 100644
--- a/quantum/serial_link/protocol/transport.c
+++ b/quantum/serial_link/protocol/transport.c
@@ -31,6 +31,10 @@ SOFTWARE.
static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS];
static uint32_t num_remote_objects = 0;
+void reinitialize_serial_link_transport(void) {
+ num_remote_objects = 0;
+}
+
void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
unsigned int i;
for(i=0;i<_num_remote_objects;i++) {
diff --git a/quantum/serial_link/protocol/transport.h b/quantum/serial_link/protocol/transport.h
index 9a052d8809..2c5d890b21 100644
--- a/quantum/serial_link/protocol/transport.h
+++ b/quantum/serial_link/protocol/transport.h
@@ -82,7 +82,7 @@ typedef struct { \
remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\
triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
- return triple_buffer_read_internal(obj->object_size, tb); \
+ return (type*)triple_buffer_read_internal(obj->object_size, tb); \
}
#define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \
@@ -112,7 +112,7 @@ typedef struct { \
remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
uint8_t* start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);\
triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
- return triple_buffer_read_internal(obj->object_size, tb); \
+ return (type*)triple_buffer_read_internal(obj->object_size, tb); \
}
#define SLAVE_TO_MASTER_OBJECT(name, type) \
@@ -139,12 +139,13 @@ typedef struct { \
uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\
start+=slave * REMOTE_OBJECT_SIZE(obj->object_size); \
triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \
- return triple_buffer_read_internal(obj->object_size, tb); \
+ return (type*)triple_buffer_read_internal(obj->object_size, tb); \
}
#define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name
void add_remote_objects(remote_object_t** remote_objects, uint32_t num_remote_objects);
+void reinitialize_serial_link_transport(void);
void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size);
void update_transport(void);
diff --git a/quantum/serial_link/tests/byte_stuffer_tests.c b/quantum/serial_link/tests/byte_stuffer_tests.cpp
index 64b170e8c1..ff49d727bb 100644
--- a/quantum/serial_link/tests/byte_stuffer_tests.c
+++ b/quantum/serial_link/tests/byte_stuffer_tests.cpp
@@ -22,70 +22,90 @@ 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 "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include <vector>
+#include <algorithm>
+extern "C" {
#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/physical.h"
+}
-static uint8_t sent_data[MAX_FRAME_SIZE*2];
-static uint16_t sent_data_size;
+using testing::_;
+using testing::ElementsAreArray;
+using testing::Args;
-Describe(ByteStuffer);
-BeforeEach(ByteStuffer) {
- init_byte_stuffer();
- sent_data_size = 0;
-}
-AfterEach(ByteStuffer) {}
+class ByteStuffer : public ::testing::Test{
+public:
+ ByteStuffer() {
+ Instance = this;
+ init_byte_stuffer();
+ }
-void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) {
- mock(data, size);
-}
+ ~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;
-void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
- memcpy(sent_data + sent_data_size, data, size);
- sent_data_size += size;
+ 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);
+ }
}
-Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) {
- never_expect(validator_recv_frame);
+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);
}
-Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) {
- never_expect(validator_recv_frame);
+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);
}
-Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) {
- never_expect(validator_recv_frame);
+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);
}
-Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) {
- never_expect(validator_recv_frame);
+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);
}
-Ensure(ByteStuffer, receives_single_byte_valid_frame) {
+TEST_F(ByteStuffer, receives_single_byte_valid_frame) {
uint8_t expected[] = {0x37};
- expect(validator_recv_frame,
- when(size, is_equal_to(1)),
- when(data, is_equal_to_contents_of(expected, 1))
- );
+ 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);
}
-
-Ensure(ByteStuffer, receives_three_bytes_valid_frame) {
+TEST_F(ByteStuffer, receives_three_bytes_valid_frame) {
uint8_t expected[] = {0x37, 0x99, 0xFF};
- expect(validator_recv_frame,
- when(size, is_equal_to(3)),
- when(data, is_equal_to_contents_of(expected, 3))
- );
+ 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);
@@ -93,23 +113,19 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) {
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};
- expect(validator_recv_frame,
- when(size, is_equal_to(1)),
- when(data, is_equal_to_contents_of(expected, 1))
- );
+ 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);
}
-Ensure(ByteStuffer, receives_valid_frame_with_zeroes) {
+TEST_F(ByteStuffer, receives_valid_frame_with_zeroes) {
uint8_t expected[] = {5, 0, 3, 0};
- expect(validator_recv_frame,
- when(size, is_equal_to(4)),
- when(data, is_equal_to_contents_of(expected, 4))
- );
+ 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);
@@ -118,17 +134,14 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) {
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 expected2[] = {3};
- expect(validator_recv_frame,
- when(size, is_equal_to(2)),
- when(data, is_equal_to_contents_of(expected1, 2))
- );
- expect(validator_recv_frame,
- when(size, is_equal_to(1)),
- when(data, is_equal_to_contents_of(expected2, 1))
- );
+ 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);
@@ -138,12 +151,10 @@ Ensure(ByteStuffer, receives_two_valid_frames) {
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};
- expect(validator_recv_frame,
- when(size, is_equal_to(2)),
- when(data, is_equal_to_contents_of(expected, 2))
- );
+ 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);
@@ -153,12 +164,10 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) {
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};
- expect(validator_recv_frame,
- when(size, is_equal_to(2)),
- when(data, is_equal_to_contents_of(expected, 2))
- );
+ 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
@@ -169,16 +178,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) {
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];
int i;
for (i=0;i<254;i++) {
expected[i] = i + 1;
}
- expect(validator_recv_frame,
- when(size, is_equal_to(254)),
- when(data, is_equal_to_contents_of(expected, 254))
- );
+ 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);
@@ -186,17 +193,15 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_
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];
int i;
for (i=0;i<254;i++) {
expected[i] = i + 1;
}
expected[254] = 7;
- expect(validator_recv_frame,
- when(size, is_equal_to(255)),
- when(data, is_equal_to_contents_of(expected, 255))
- );
+ 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);
@@ -206,17 +211,15 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_
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];
int i;
for (i=0;i<254;i++) {
expected[i] = i + 1;
}
expected[254] = 0;
- expect(validator_recv_frame,
- when(size, is_equal_to(255)),
- when(data, is_equal_to_contents_of(expected, 255))
- );
+ 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);
@@ -226,7 +229,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_
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];
int i;
int j;
@@ -238,10 +241,8 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) {
for (i=0;i<7;i++) {
expected[254*2+i] = i + 1;
}
- expect(validator_recv_frame,
- when(size, is_equal_to(515)),
- when(data, is_equal_to_contents_of(expected, 510))
- );
+ 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);
@@ -261,12 +262,10 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) {
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] = {};
- expect(validator_recv_frame,
- when(size, is_equal_to(MAX_FRAME_SIZE)),
- when(data, is_equal_to_contents_of(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++) {
@@ -275,9 +274,10 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
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};
- never_expect(validator_recv_frame);
+ EXPECT_CALL(*this, validator_recv_frame(_, _, _))
+ .Times(0);
int i;
byte_stuffer_recv_byte(0, 1);
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);
}
-Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
+TEST_F(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
uint8_t expected