Skip to content

Commit

Permalink
feat: add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SYaoJun committed Sep 3, 2023
1 parent af23c6c commit 7bf4823
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 04-cross-compie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 第四章:交叉编译

## 编译宏
- 在cmake时通过-DVIP=ABCDEDF传入对应的值
```
target_compile_definitions(main
INTERFACE
VIP="${VIP}"
)
```
## 编译参数
```
target_compile_features()
```
10 changes: 10 additions & 0 deletions 05-testing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.16)
project(main)
set(CMAKE_CXX_STANDARD 17)
include(CTest)
enable_testing()
add_subdirectory(src)

if(BUILD_TESTING)
add_subdirectory(test)
endif()
34 changes: 34 additions & 0 deletions 05-testing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 单元测试
```
include(CTest)
enable_testing()
add_test()
```
## 编译运行
```
ctest --test-dir build
# 或者只测试cs开头的数据
ctest --test-dir build -R "^cs."
```
## 宏macro
- ${ARGN}是所有传入的可变参数
```cmake
macro(my_add_test TEST_NAME)
add_executable(${TEST_NAME} ${ARGN})
add_test(NAME cs.${TEST_NAME} COMMAND ${TEST_NAME})
target_link_libraries(${TEST_NAME} PRIVATE gtest gtest_main)
endmacro()
```

## 函数function
```cmake
function(add_thirdparty)
cmake_parse_arguments(DORIS_THIRDPARTY
"NOTADD;LIB64"
"LIBNAME;WHOLELIBPATH"
""
${ARGN})
set(DORIS_THIRDPARTY_NAME ${DORIS_THIRDPARTY_UNPARSED_ARGUMENTS})
add_library(${DORIS_THIRDPARTY_NAME} STATIC IMPORTED)
endfunction()
```
1 change: 1 addition & 0 deletions 05-testing/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_executable(main main.cpp)
8 changes: 8 additions & 0 deletions 05-testing/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>

int main() {

std::cout<<"hello world!"<<std::endl;

return 0;
}
19 changes: 19 additions & 0 deletions 05-testing/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# google test
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.14.0
)
FetchContent_MakeAvailable(googletest)

macro(my_add_test TEST_NAME)
add_executable(${TEST_NAME} ${ARGN})
add_test(NAME cs.${TEST_NAME} COMMAND ${TEST_NAME})
target_link_libraries(${TEST_NAME} PRIVATE gtest gtest_main)
endmacro()

my_add_test(test01 gtest_test.cpp)
add_executable(test02 gtest_test2.cpp)
target_link_libraries(test02 PRIVATE gtest gtest_main)
add_test(NAME ee.test02 COMMAND test02)
14 changes: 14 additions & 0 deletions 05-testing/test/gtest_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "gtest/gtest.h"


int add(int a, int b) {
return a + b;
}

TEST(AddFunctionTest, PositiveNumbers) {
EXPECT_EQ(add(2, 3), 5);
EXPECT_EQ(add(0, 0), 0);
EXPECT_EQ(add(-1, 1), 0);
}


14 changes: 14 additions & 0 deletions 05-testing/test/gtest_test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "gtest/gtest.h"


int add(int a, int b) {
return a + b;
}

TEST(AddFunctionTest, PositiveNumbers) {
EXPECT_EQ(add(2, 3), 5);
EXPECT_EQ(add(0, 0), 0);
EXPECT_EQ(add(-1, 1), 0);
}


0 comments on commit 7bf4823

Please sign in to comment.