From eabea8f03c31b1f78965f6f3c44c0cdc79c3be8b Mon Sep 17 00:00:00 2001 From: Eric Paniagua Date: Fri, 4 Sep 2020 11:19:02 -0700 Subject: [PATCH] [WIP]: Test in testing/kaleidoscope builds, links, and segfaults. Signed-off-by: Eric Paniagua --- testing/Makefile | 2 +- testing/common/KeyboardReport.cpp | 2 +- testing/common/Makefile | 20 +++++----- testing/common/Simulator.cpp | 11 ++++++ testing/common/State.cpp | 6 ++- testing/common/State.h | 12 +----- testing/common/VirtualDeviceTest.cpp | 3 ++ .../kaleidoscope/.kaleidoscope-builder.conf | 4 ++ testing/kaleidoscope/Makefile | 38 +++++++++++++------ .../{ => test}/simulator_test.cpp | 11 ++++++ 10 files changed, 73 insertions(+), 36 deletions(-) create mode 100644 testing/kaleidoscope/.kaleidoscope-builder.conf rename testing/kaleidoscope/{ => test}/simulator_test.cpp (81%) diff --git a/testing/Makefile b/testing/Makefile index 47fb4d9b..2907e55c 100644 --- a/testing/Makefile +++ b/testing/Makefile @@ -13,7 +13,7 @@ googletest: FORCE common: FORCE cd common && env LIB_DIR="${LIB_DIR}" $(MAKE) -%: FORCE +%: common googltest FORCE if [ -f "$@/Makefile" ]; then cd "$@"; $(MAKE); fi .PHONY: FORCE googletest build-libs diff --git a/testing/common/KeyboardReport.cpp b/testing/common/KeyboardReport.cpp index 6974f43f..ff93176c 100644 --- a/testing/common/KeyboardReport.cpp +++ b/testing/common/KeyboardReport.cpp @@ -29,7 +29,7 @@ KeyboardReport::KeyboardReport(const void* data) { std::vector KeyboardReport::ActiveKeycodes() const { std::vector active_keys; - for (uint8_t i = 0; i <= HID_LAST_KEY; ++i) { + for (uint8_t i = 0; i < HID_LAST_KEY; ++i) { uint8_t bit = 1 << (uint8_t(i) % 8); uint8_t key_code = report_data_.keys[i/8] & bit; if (key_code) active_keys.push_back(i); diff --git a/testing/common/Makefile b/testing/common/Makefile index bbe9e4cd..eed75aa7 100644 --- a/testing/common/Makefile +++ b/testing/common/Makefile @@ -3,27 +3,27 @@ OBJ_DIR ?= ${PWD}/obj CXX_FILES=$(wildcard *.cpp) -LIB_FILE=common.a +LIB_FILE=libcommon.a OBJ_FILES=$(patsubst %.cpp,${OBJ_DIR}/%.o,$(CXX_FILES)) -Makefile: ${LIB_DIR}/${LIB_FILE} +Makefile: ${OBJ_FILES} ${LIB_DIR}/${LIB_FILE} @: ${LIB_DIR}/${LIB_FILE}: ${OBJ_FILES} mkdir -p "${LIB_DIR}" ar rcs "${LIB_DIR}/${LIB_FILE}" ${OBJ_FILES} -${OBJ_DIR}/%.o: %.cpp +${OBJ_DIR}/%.o: %.cpp $(wildcard *.h) @echo "compile $@" mkdir -p "${OBJ_DIR}" g++ -o "$@" -c \ - -I../.. \ - -I../../src \ - -I../../../../../virtual/cores/arduino \ - -I../../../Kaleidoscope-HIDAdaptor-KeyboardioHID/src \ - -I../../../KeyboardioHID/src \ - -I../../testing/googletest/googlemock/include \ - -I../../testing/googletest/googletest/include \ + -I${PWD}/../.. \ + -I${PWD}/../../src \ + -I${PWD}/../../../../../virtual/cores/arduino \ + -I${PWD}/../../../Kaleidoscope-HIDAdaptor-KeyboardioHID/src \ + -I${PWD}/../../../KeyboardioHID/src \ + -I${PWD}/../../testing/googletest/googlemock/include \ + -I${PWD}/../../testing/googletest/googletest/include \ -DARDUINO=10607 \ -DARDUINO_AVR_MODEL01 \ '-DKALEIDOSCOPE_HARDWARE_H="Kaleidoscope-Hardware-Model01.h"' \ diff --git a/testing/common/Simulator.cpp b/testing/common/Simulator.cpp index 725219f4..ece065a5 100644 --- a/testing/common/Simulator.cpp +++ b/testing/common/Simulator.cpp @@ -17,11 +17,16 @@ #include "Kaleidoscope.h" #include "testing/common/Simulator.h" +#include "testing/common/fix-macros.h" +#include + namespace kaleidoscope { namespace testing { void Simulator::RunCycle() { + std::cout << "epan 2.2.1" << std::endl; Kaleidoscope.loop(); + std::cout << "epan 2.2.2" << std::endl; } void Simulator::RunCycles(size_t n) { @@ -34,5 +39,11 @@ void Simulator::Press(uint8_t row, uint8_t col) { kaleidoscope::Device::Props::KeyScanner::KeyState::Pressed); } +void Simulator::Release(uint8_t row, uint8_t col) { + Kaleidoscope.device().keyScanner().setKeystate( + KeyAddr{row, col}, + kaleidoscope::Device::Props::KeyScanner::KeyState::NotPressed); +} + } // namespace testing } // namespace kaleidoscope diff --git a/testing/common/State.cpp b/testing/common/State.cpp index 8ead9cb5..f4cbe69d 100644 --- a/testing/common/State.cpp +++ b/testing/common/State.cpp @@ -59,7 +59,7 @@ void State::ProcessHidReport( break; } case HID_REPORTID_NKRO_KEYBOARD: { - ProcessReport(KeyboardReport{data}); + ProcessKeyboardReport(KeyboardReport{data}); break; } default: @@ -75,5 +75,9 @@ const KeyboardReport& State::KeyboardReports(size_t i) const { return keyboard_reports_.at(i); } +void State::ProcessKeyboardReport(const KeyboardReport& report) { + keyboard_reports_.push_back(report); +} + } // namespace testing } // namespace kaleidoscope diff --git a/testing/common/State.h b/testing/common/State.h index 7376544b..2581f044 100644 --- a/testing/common/State.h +++ b/testing/common/State.h @@ -39,20 +39,10 @@ class State { const KeyboardReport& KeyboardReports(size_t i) const; private: - template - void ProcessReport(const R& report); + void ProcessKeyboardReport(const KeyboardReport& report); std::vector keyboard_reports_; }; -/* - * Implementation - */ - -template <> -void State::ProcessReport(const KeyboardReport& report) { - keyboard_reports_.push_back(report); -} - } // namespace testing } // namespace kaleidoscope diff --git a/testing/common/VirtualDeviceTest.cpp b/testing/common/VirtualDeviceTest.cpp index 267ebf7d..bce40c5e 100644 --- a/testing/common/VirtualDeviceTest.cpp +++ b/testing/common/VirtualDeviceTest.cpp @@ -20,8 +20,11 @@ namespace kaleidoscope { namespace testing { void VirtualDeviceTest::RunCycle() { + std::cout << "epan 2.1" << std::endl; state_.Clear(); + std::cout << "epan 2.2" << std::endl; sim_.RunCycle(); + std::cout << "epan 2.3" << std::endl; } void VirtualDeviceTest::RunCycles(size_t n) { diff --git a/testing/kaleidoscope/.kaleidoscope-builder.conf b/testing/kaleidoscope/.kaleidoscope-builder.conf new file mode 100644 index 00000000..d6c5ef1e --- /dev/null +++ b/testing/kaleidoscope/.kaleidoscope-builder.conf @@ -0,0 +1,4 @@ +# -*- mode: sh -*- + +DEFAULT_SKETCH=sketch +ARCH=virtual diff --git a/testing/kaleidoscope/Makefile b/testing/kaleidoscope/Makefile index d77a2fe9..2dcbbb85 100644 --- a/testing/kaleidoscope/Makefile +++ b/testing/kaleidoscope/Makefile @@ -1,13 +1,14 @@ BIN_DIR=bin LIB_DIR=lib OBJ_DIR=obj +SRC_DIR=test SKETCH_FILE=$(wildcard *.ino) BIN_FILE=$(subst .ino,,$(SKETCH_FILE)) LIB_FILE=${BIN_FILE}-latest.a -TEST_FILES=$(wildcard *_test.cpp) -TEST_OBJS=$(patsubst %.cpp,${OBJ_DIR}/%.o,$(TEST_FILES)) +TEST_FILES=$(wildcard $(SRC_DIR)/*_test.cpp) +TEST_OBJS=$(patsubst $(SRC_DIR)/%.cpp,${OBJ_DIR}/%.o,$(TEST_FILES)) run: ${BIN_DIR}/${BIN_FILE} @echo "run" @@ -17,19 +18,32 @@ ${BIN_DIR}/${BIN_FILE}: ${TEST_OBJS} FORCE @echo "link" mkdir -p "${BIN_DIR}" "${LIB_DIR}" env LIBONLY=yes LOCAL_CFLAGS='"-I$(PWD)"' OUTPUT_PATH="$(PWD)/$(LIB_DIR)" VERBOSE=1 $(MAKE) -f delegate.mk - g++ -o "${BIN_DIR}/${BIN_FILE}" -lpthread -g -w -lm -lXtst -lX11 ${TEST_OBJS} "${LIB_DIR}/${LIB_FILE}" -L"$(PWD)/../googletest/lib" -lgtest -lgmock - -${OBJ_DIR}/%.o: %.cpp + g++ -o "${BIN_DIR}/${BIN_FILE}" \ + -lpthread \ + -g \ + -w \ + ${TEST_OBJS} \ + "${LIB_DIR}/${LIB_FILE}" \ + -L"$(PWD)/../googletest/lib" \ + -L"${PWD}/../lib" \ + -lcommon \ + -lgtest \ + -lgmock \ + -lm \ + -lXtst \ + -lX11 + +${OBJ_DIR}/%.o: ${SRC_DIR}/%.cpp @echo "compile $@" mkdir -p "${OBJ_DIR}" g++ -o "$@" -c \ - -I../.. \ - -I../../src \ - -I../../../../../virtual/cores/arduino \ - -I../../../Kaleidoscope-HIDAdaptor-KeyboardioHID/src \ - -I../../../KeyboardioHID/src \ - -I../../testing/googletest/googlemock/include \ - -I../../testing/googletest/googletest/include \ + -I${PWD}/../.. \ + -I${PWD}/../../src \ + -I${PWD}/../../../../../virtual/cores/arduino \ + -I${PWD}/../../../Kaleidoscope-HIDAdaptor-KeyboardioHID/src \ + -I${PWD}/../../../KeyboardioHID/src \ + -I${PWD}/../../testing/googletest/googlemock/include \ + -I${PWD}/../../testing/googletest/googletest/include \ -DARDUINO=10607 \ -DARDUINO_AVR_MODEL01 \ '-DKALEIDOSCOPE_HARDWARE_H="Kaleidoscope-Hardware-Model01.h"' \ diff --git a/testing/kaleidoscope/simulator_test.cpp b/testing/kaleidoscope/test/simulator_test.cpp similarity index 81% rename from testing/kaleidoscope/simulator_test.cpp rename to testing/kaleidoscope/test/simulator_test.cpp index 71b1c076..bffbe165 100644 --- a/testing/kaleidoscope/simulator_test.cpp +++ b/testing/kaleidoscope/test/simulator_test.cpp @@ -22,6 +22,8 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include + #include "testing/common/matchers.h" #include "testing/common/VirtualDeviceTest.h" @@ -36,18 +38,27 @@ using ::testing::IsEmpty; class KeyboardReports : public VirtualDeviceTest {}; TEST_F(KeyboardReports, KeysActiveWhenPressed) { + std::cout << "epan 1" << std::endl; sim_.Press(2, 1); // A + std::cout << "epan 2" << std::endl; RunCycle(); + std::cout << "epan 3" << std::endl; EXPECT_EQ(Result().KeyboardReports().size(), 1); + std::cout << "epan 4" << std::endl; EXPECT_THAT( Result().KeyboardReports(0).ActiveKeycodes(), ContainsKey(Key_A)); + std::cout << "epan 5" << std::endl; sim_.Release(2, 1); // A + std::cout << "epan 6" << std::endl; RunCycles(2); + std::cout << "epan 7" << std::endl; + std::cout << "epan 8" << std::endl; EXPECT_THAT(Result().KeyboardReports(), IsEmpty()); + std::cout << "epan 9" << std::endl; } } // namespace