From 48cf2bf9cb81f9db449ce618500964a140399365 Mon Sep 17 00:00:00 2001 From: John Zhao Date: Tue, 3 Apr 2018 10:27:28 +0800 Subject: [PATCH] Add types & test --- .gitmodules | 14 ++-- CMakeLists.txt | 2 + Makefile | 11 ++- include/mynteye/types.h | 157 ++++++++++++++++++++++++++++++++++++++ src/public/types.cc | 90 ++++++++++++++++++++++ test/CMakeLists.txt | 88 +++++++++++++++++++++ test/gtest | 1 + test/gtest_main.cc | 34 +++++++++ test/public/types_test.cc | 57 ++++++++++++++ tests/gtest | 1 - 10 files changed, 444 insertions(+), 11 deletions(-) create mode 100644 include/mynteye/types.h create mode 100644 src/public/types.cc create mode 100644 test/CMakeLists.txt create mode 160000 test/gtest create mode 100644 test/gtest_main.cc create mode 100644 test/public/types_test.cc delete mode 160000 tests/gtest diff --git a/.gitmodules b/.gitmodules index edd1f4f..d40f165 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,10 +1,10 @@ -[submodule "tests/gtest"] - path = tests/gtest +[submodule "test/gtest"] + path = test/gtest url = https://github.com/google/googletest.git [submodule "third_party/glog"] - path = third_party/glog - url = https://github.com/google/glog.git + path = third_party/glog + url = https://github.com/google/glog.git [submodule "tools/linter"] - path = tools/linter - url = https://github.com/slightech/linter.git - branch = slightech + path = tools/linter + url = https://github.com/slightech/linter.git + branch = slightech diff --git a/CMakeLists.txt b/CMakeLists.txt index 0542be6..0ddbeeb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,7 @@ set(MYNTEYE_NAME ${PROJECT_NAME}) set(MYNTEYE_PUBLIC_H ${CMAKE_CURRENT_SOURCE_DIR}/include/mynteye/global.h ${CMAKE_CURRENT_BINARY_DIR}/include/mynteye/mynteye.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/mynteye/types.h ) if(OS_WIN) @@ -90,6 +91,7 @@ endif() set(MYNTEYE_SRCS ${UVC_SRC} + src/public/types.cc ) set(MYNTEYE_LINKLIBS diff --git a/Makefile b/Makefile index d7f7452..ec61304 100644 --- a/Makefile +++ b/Makefile @@ -62,10 +62,13 @@ build: third_party # test -test: submodules +test: install @$(call echo,Make $@) @$(call echo,Make gtest,33) - @$(call cmake_build,./tests/gtest/_build) + @$(call cmake_build,./test/gtest/_build) + @$(call echo,Make test,33) + @$(call cmake_build,./test/_build) + @./test/_output/bin/mynteye_test .PHONY: test @@ -94,11 +97,13 @@ clean: @$(call rm,./_install/) @$(call rm,./samples/_build/) @$(call rm,./samples/_output/) + @$(call rm,./test/_build/) + @$(call rm,./test/_output/) @$(MAKE) cleanlog cleanall: clean @$(call rm,./doc/_output/) - @$(call rm,./tests/gtest/_build/) + @$(call rm,./test/gtest/_build/) @$(call rm,./third_party/glog/_build/) @$(FIND) . -type f -name ".DS_Store" -print0 | xargs -0 rm -f diff --git a/include/mynteye/types.h b/include/mynteye/types.h new file mode 100644 index 0000000..7cbdfa1 --- /dev/null +++ b/include/mynteye/types.h @@ -0,0 +1,157 @@ +#ifndef MYNTEYE_TYPES_H_ // NOLINT +#define MYNTEYE_TYPES_H_ +#pragma once + +#include + +#include +#include + +#include "mynteye/mynteye.h" + +MYNTEYE_BEGIN_NAMESPACE + +/** + * @ingroup enumerations + * @brief Streams define different type of data. + */ +enum class Stream : std::uint8_t { + /** Left stream */ + LEFT, + /** Right stream */ + RIGHT, + /** Left stream, rectified */ + LEFT_RECTIFIED, + /** Right stream, rectified */ + RIGHT_RECTIFIED, + /** Disparity stream */ + DISPARITY, + /** Disparity stream, normalized */ + DISPARITY_NORMALIZED, + /** Depth stream */ + DEPTH, + /** Point cloud stream */ + POINTS, + /** Last guard */ + LAST +}; + +/** + * @ingroup enumerations + * @brief Capabilities define the full set of functionality that the device + * might provide. + */ +enum class Capabilities : std::uint8_t { + /** Provices stereo stream */ + STEREO, + /** Provices color stream */ + COLOR, + /** Provices depth stream */ + DEPTH, + /** Provices point cloud stream */ + POINTS, + /** Provices fisheye stream */ + FISHEYE, + /** Provices IMU (accelerometer, gyroscope) data */ + IMU, + /** Last guard */ + LAST +}; + +/** + * @ingroup enumerations + * @brief Camera info fields are read-only strings that can be queried from the + * device. + */ +enum class Info : std::uint8_t { + /** Device name */ + DEVICE_NAME, + /** Serial number */ + SERIAL_NUMBER, + /** Firmware version */ + FIRMWARE_VERSION, + /** Hardware version */ + HARDWARE_VERSION, + /** Spec version */ + SPEC_VERSION, + /** Lens type */ + LENS_TYPE, + /** IMU type */ + IMU_TYPE, + /** Nominal baseline */ + NOMINAL_BASELINE, + /** Last guard */ + LAST, +}; + +/** + * @ingroup enumerations + * Camera control options define general configuration controls. + */ +enum class Option : std::uint8_t { + /** Image gain, setting it if manual-exposure */ + GAIN, + /** Image brightness, setting it if manual-exposure */ + BRIGHTNESS, + /** Image contrast */ + CONTRAST, + /** Image frame rate */ + FRAME_RATE, + /** IMU frequency */ + IMU_FREQUENCY, + /** + * Exposure mode + * + * 0: enable auto-exposure + * 1: disable auto-exposure (manual-exposure) + */ + EXPOSURE_MODE, + /** Max gain, setting it if auto-exposure */ + MAX_GAIN, + /** Max exposure time, setting it if auto-exposure */ + MAX_EXPOSURE_TIME, + /** Desired brightness */ + DESIRED_BRIGHTNESS, + /** IR control */ + IR_CONTROL, + /** + * HDR mode + * + * 0: 10-bit + * 1: 12-bit + */ + HDR_MODE, + /** Zero drift calibration */ + ZERO_DRIFT_CALIBRATION, + /** Erase chip */ + ERASE_CHIP, + /** Last guard */ + LAST, +}; + +#define MYNTEYE_ENUM_HELPERS(TYPE) \ + const char *to_string(const TYPE &value); \ + inline bool is_valid(const TYPE &value) { \ + using utype = std::underlying_type::type; \ + utype val = static_cast(value); \ + utype max = static_cast(TYPE::LAST); \ + return /*val >= 0 &&*/ val < max; \ + } \ + inline std::ostream &operator<<(std::ostream &os, const TYPE &value) { \ + using utype = std::underlying_type::type; \ + if (is_valid(value)) \ + return os << to_string(value); \ + else \ + return os << static_cast(value); \ + } + +MYNTEYE_ENUM_HELPERS(Stream) +MYNTEYE_ENUM_HELPERS(Capabilities) +MYNTEYE_ENUM_HELPERS(Info) +MYNTEYE_ENUM_HELPERS(Option) + +#undef MYNTEYE_ENUM_HELPERS + +MYNTEYE_END_NAMESPACE + +#endif // MYNTEYE_TYPES_H_ NOLINT diff --git a/src/public/types.cc b/src/public/types.cc new file mode 100644 index 0000000..08964db --- /dev/null +++ b/src/public/types.cc @@ -0,0 +1,90 @@ +#include "mynteye/types.h" + +#include + +MYNTEYE_BEGIN_NAMESPACE + +const char *to_string(const Stream &value) { +#define CASE(X) \ + case Stream::X: \ + return "Stream::" #X; + switch (value) { + CASE(LEFT) + CASE(RIGHT) + CASE(LEFT_RECTIFIED) + CASE(RIGHT_RECTIFIED) + CASE(DISPARITY) + CASE(DISPARITY_NORMALIZED) + CASE(DEPTH) + CASE(POINTS) + default: + CHECK(is_valid(value)); + return "Stream::UNKNOWN"; + } +#undef CASE +} + +const char *to_string(const Capabilities &value) { +#define CASE(X) \ + case Capabilities::X: \ + return "Capabilities::" #X; + switch (value) { + CASE(STEREO) + CASE(COLOR) + CASE(DEPTH) + CASE(POINTS) + CASE(FISHEYE) + CASE(IMU) + default: + CHECK(is_valid(value)); + return "Capabilities::UNKNOWN"; + } +#undef CASE +} + +const char *to_string(const Info &value) { +#define CASE(X) \ + case Info::X: \ + return "Info::" #X; + switch (value) { + CASE(DEVICE_NAME) + CASE(SERIAL_NUMBER) + CASE(FIRMWARE_VERSION) + CASE(HARDWARE_VERSION) + CASE(SPEC_VERSION) + CASE(LENS_TYPE) + CASE(IMU_TYPE) + CASE(NOMINAL_BASELINE) + default: + CHECK(is_valid(value)); + return "Info::UNKNOWN"; + } +#undef CASE +} + +const char *to_string(const Option &value) { +#define CASE(X) \ + case Option::X: \ + return "Option::" #X; + switch (value) { + CASE(GAIN) + CASE(BRIGHTNESS) + CASE(CONTRAST) + CASE(FRAME_RATE) + CASE(IMU_FREQUENCY) + CASE(EXPOSURE_MODE) + CASE(MAX_GAIN) + CASE(MAX_EXPOSURE_TIME) + CASE(DESIRED_BRIGHTNESS) + CASE(IR_CONTROL) + CASE(HDR_MODE) + CASE(ZERO_DRIFT_CALIBRATION) + CASE(ERASE_CHIP) + default: + CHECK(is_valid(value)); + return "Option::UNKNOWN"; + } +#undef CASE +} + +MYNTEYE_END_NAMESPACE diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..72c8a67 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,88 @@ +cmake_minimum_required(VERSION 3.0) + +project(mynteye_test VERSION 2.0.0 LANGUAGES C CXX) + +get_filename_component(PRO_DIR ${PROJECT_SOURCE_DIR} DIRECTORY) + +# flags + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") + +include(${PRO_DIR}/cmake/DetectCXX11.cmake) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") + +string(STRIP "${CMAKE_C_FLAGS}" CMAKE_C_FLAGS) +string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS) +message(STATUS "C_FLAGS: ${CMAKE_C_FLAGS}") +message(STATUS "CXX_FLAGS: ${CMAKE_CXX_FLAGS}") + +# packages + +LIST(APPEND CMAKE_PREFIX_PATH ${PRO_DIR}/third_party/glog/_build) +find_package(glog REQUIRED) +message(STATUS "Found glog: ${glog_VERSION}") + +LIST(APPEND CMAKE_PREFIX_PATH ${PRO_DIR}/_install/lib/cmake) +find_package(mynteye REQUIRED) +message(STATUS "Found mynteye: ${mynteye_VERSION}") + +LIST(APPEND CMAKE_MODULE_PATH ${PRO_DIR}/cmake) + +## gtest + +set(TEST_DIR ${PROJECT_SOURCE_DIR}) +set(GMOCK_DIR ${TEST_DIR}/gtest/googlemock) +set(GTEST_DIR ${TEST_DIR}/gtest/googletest) + +set(GMOCK_LIB_DIR ${TEST_DIR}/gtest/_build/googlemock) +set(GTEST_LIB_DIR ${GMOCK_LIB_DIR}/gtest) +if(OS_WIN AND NOT MINGW) + set(GTEST_LIB_DIR ${GTEST_LIB_DIR}/Release) +endif() + +set(GTEST_LIBS + ${GTEST_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX} + #${GTEST_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest_main${CMAKE_STATIC_LIBRARY_SUFFIX} +) +message(STATUS "Found gtest libs: ${GTEST_LIBS}") + +# targets + +include(${PRO_DIR}/cmake/Common.cmake) + +set(OUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/_output") + +set_outdir( + "${OUT_DIR}/lib" + "${OUT_DIR}/lib" + "${OUT_DIR}/bin" +) + +include_directories( + ${GTEST_DIR}/include +) + +file(GLOB TEST_PUBLIC_SRC "public/*.cc") + +add_executable(${PROJECT_NAME} + gtest_main.cc + ${TEST_PUBLIC_SRC} +) +target_link_libraries(${PROJECT_NAME} ${GTEST_LIBS} mynteye) + +if(OS_WIN) + target_compile_definitions(${PROJECT_NAME} + PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES + ) +endif() + +#find_package(Threads REQUIRED) +#if(THREADS_HAVE_PTHREAD_ARG) +# target_compile_options(PUBLIC ${PROJECT_NAME} "-pthread") +#endif() +#if(CMAKE_THREAD_LIBS_INIT) +# target_link_libraries(${PROJECT_NAME} "${CMAKE_THREAD_LIBS_INIT}") +#endif() diff --git a/test/gtest b/test/gtest new file mode 160000 index 0000000..7888184 --- /dev/null +++ b/test/gtest @@ -0,0 +1 @@ +Subproject commit 7888184f28509dba839e3683409443e0b5bb8948 diff --git a/test/gtest_main.cc b/test/gtest_main.cc new file mode 100644 index 0000000..74df380 --- /dev/null +++ b/test/gtest_main.cc @@ -0,0 +1,34 @@ +#include "gtest/gtest.h" +#include +#include "glog/logging.h" + +struct glog_init { + glog_init(int /*argc*/, char *argv[]) { + // FLAGS_logtostderr = true; + FLAGS_alsologtostderr = true; + FLAGS_colorlogtostderr = true; + + FLAGS_log_dir = "."; + FLAGS_max_log_size = 1024; + FLAGS_stop_logging_if_full_disk = true; + + // FLAGS_v = 2; + + google::InitGoogleLogging(argv[0]); + + VLOG(2) << __func__; + } + + ~glog_init() { + VLOG(2) << __func__; + google::ShutdownGoogleLogging(); + } +}; + +GTEST_API_ int main(int argc, char **argv) { + glog_init _(argc, argv); + + printf("Running main() from gtest_main.cc\n"); + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/public/types_test.cc b/test/public/types_test.cc new file mode 100644 index 0000000..e77fecb --- /dev/null +++ b/test/public/types_test.cc @@ -0,0 +1,57 @@ +#include "gtest/gtest.h" + +#include "mynteye/types.h" + +MYNTEYE_USE_NAMESPACE + +TEST(Stream, VerifyToString) { + EXPECT_STREQ("Stream::LEFT", to_string(Stream::LEFT)); + EXPECT_STREQ("Stream::RIGHT", to_string(Stream::RIGHT)); + EXPECT_STREQ("Stream::LEFT_RECTIFIED", to_string(Stream::LEFT_RECTIFIED)); + EXPECT_STREQ("Stream::RIGHT_RECTIFIED", to_string(Stream::RIGHT_RECTIFIED)); + EXPECT_STREQ("Stream::DISPARITY", to_string(Stream::DISPARITY)); + EXPECT_STREQ( + "Stream::DISPARITY_NORMALIZED", to_string(Stream::DISPARITY_NORMALIZED)); + EXPECT_STREQ("Stream::DEPTH", to_string(Stream::DEPTH)); + EXPECT_STREQ("Stream::POINTS", to_string(Stream::POINTS)); +} + +TEST(Capabilities, VerifyToString) { + EXPECT_STREQ("Capabilities::STEREO", to_string(Capabilities::STEREO)); + EXPECT_STREQ("Capabilities::COLOR", to_string(Capabilities::COLOR)); + EXPECT_STREQ("Capabilities::DEPTH", to_string(Capabilities::DEPTH)); + EXPECT_STREQ("Capabilities::POINTS", to_string(Capabilities::POINTS)); + EXPECT_STREQ("Capabilities::FISHEYE", to_string(Capabilities::FISHEYE)); + EXPECT_STREQ("Capabilities::IMU", to_string(Capabilities::IMU)); +} + +TEST(Info, VerifyToString) { + EXPECT_STREQ("Info::DEVICE_NAME", to_string(Info::DEVICE_NAME)); + EXPECT_STREQ("Info::SERIAL_NUMBER", to_string(Info::SERIAL_NUMBER)); + EXPECT_STREQ("Info::FIRMWARE_VERSION", to_string(Info::FIRMWARE_VERSION)); + EXPECT_STREQ("Info::HARDWARE_VERSION", to_string(Info::HARDWARE_VERSION)); + EXPECT_STREQ("Info::SPEC_VERSION", to_string(Info::SPEC_VERSION)); + EXPECT_STREQ("Info::LENS_TYPE", to_string(Info::LENS_TYPE)); + EXPECT_STREQ("Info::IMU_TYPE", to_string(Info::IMU_TYPE)); + EXPECT_STREQ("Info::NOMINAL_BASELINE", to_string(Info::NOMINAL_BASELINE)); +} + +TEST(Option, VerifyToString) { + EXPECT_STREQ("Option::GAIN", to_string(Option::GAIN)); + EXPECT_STREQ("Option::BRIGHTNESS", to_string(Option::BRIGHTNESS)); + EXPECT_STREQ("Option::CONTRAST", to_string(Option::CONTRAST)); + EXPECT_STREQ("Option::FRAME_RATE", to_string(Option::FRAME_RATE)); + EXPECT_STREQ("Option::IMU_FREQUENCY", to_string(Option::IMU_FREQUENCY)); + EXPECT_STREQ("Option::EXPOSURE_MODE", to_string(Option::EXPOSURE_MODE)); + EXPECT_STREQ("Option::MAX_GAIN", to_string(Option::MAX_GAIN)); + EXPECT_STREQ( + "Option::MAX_EXPOSURE_TIME", to_string(Option::MAX_EXPOSURE_TIME)); + EXPECT_STREQ( + "Option::DESIRED_BRIGHTNESS", to_string(Option::DESIRED_BRIGHTNESS)); + EXPECT_STREQ("Option::IR_CONTROL", to_string(Option::IR_CONTROL)); + EXPECT_STREQ("Option::HDR_MODE", to_string(Option::HDR_MODE)); + EXPECT_STREQ( + "Option::ZERO_DRIFT_CALIBRATION", + to_string(Option::ZERO_DRIFT_CALIBRATION)); + EXPECT_STREQ("Option::ERASE_CHIP", to_string(Option::ERASE_CHIP)); +} diff --git a/tests/gtest b/tests/gtest deleted file mode 160000 index 9bda90b..0000000 --- a/tests/gtest +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9bda90b7e5e08c4c37a832d0cea218aed6af6470