Skip to content

Commit

Permalink
重构项目结构,添加依赖管理,更新格式化规则,增加Fibonacci函数及其测试
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzd123456 committed Jan 7, 2025
1 parent 0f5ed9d commit 987b00b
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ColumnLimit: 80
IndentWidth: 4
IndentWidth: 4
AllowShortFunctionsOnASingleLine: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ build/
dist/
lib/
bin/
result/test.json
7 changes: 5 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "3rd/googletest"]
path = 3rd/googletest
[submodule "deps/gtest"]
path = deps/gtest
url = https://github.com/google/googletest.git
[submodule "deps/cmocka"]
path = deps/cmocka
url = https://github.com/clibs/cmocka.git
Empty file removed 3rd/.gitkeep
Empty file.
1 change: 0 additions & 1 deletion 3rd/CMakeLists.txt

This file was deleted.

1 change: 0 additions & 1 deletion 3rd/README.md

This file was deleted.

1 change: 0 additions & 1 deletion 3rd/googletest
Submodule googletest deleted from 58d77f
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ file(GLOB_RECURSE SOURCE
add_executable(${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME})

add_subdirectory(3rd)
add_subdirectory(test)
add_subdirectory(test)
add_subdirectory(deps/gtest)
add_subdirectory(deps/cmocka)
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# CPP-Engineering-Scaffolding
CPP Engineering Scaffolding

# Usage
## clone depends

git submodule update --recursive --init

## Usage

```shell
cmake .
mkdir build
cd build
cmake ..
make
```

Expand Down
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

mkdir build
cd build && cmake .. && make
3 changes: 3 additions & 0 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

rm -rf build
2 changes: 2 additions & 0 deletions coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
lcov ./result/coverage.info './*'
1 change: 1 addition & 0 deletions deps/cmocka
Submodule cmocka added at ec387a
1 change: 1 addition & 0 deletions deps/gtest
Submodule gtest added at e54519
6 changes: 6 additions & 0 deletions src/fib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int fib(int n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
1 change: 1 addition & 0 deletions src/fib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int fib(int n);
13 changes: 10 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include <iostream>

#include "fib.h"

#ifndef TEST
int main(int args, char** argv) {
// TODO
std::cout << "hello world!" << std::endl;
int main() {
int n = 0;
printf("Enter a number: ");
scanf("%d", &n);

int ret = fib(n);

printf("Sum of Fibonacci series up to %d is %d\n", n, ret);
return 0;
}
#endif
2 changes: 2 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./bin/test --gtest_output="json:./result/test.json"
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ file(GLOB_RECURSE SOURCE

add_definitions(-DTEST)

# Now simply link against gtest or gtest_main as needed. Eg
SET(CMAKE_CXX_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
SET(CMAKE_C_FLAGS "-g -O0 -Wall -W -fprofile-arcs -ftest-coverage")

add_executable(${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME} gtest_main gtest gmock)
add_test(NAME all_test COMMAND ${PROJECT_NAME})
28 changes: 28 additions & 0 deletions test/fib_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <gtest/gtest.h>

#include "fib.h"

class FibTestFixture : public ::testing::Test {
protected:
void SetUp() override {
}

void TearDown() override {
}
};

TEST_F(FibTestFixture, HandlesZeroInput) {
EXPECT_EQ(fib(0), 0);
}

TEST_F(FibTestFixture, HandlesOneInput) {
EXPECT_EQ(fib(1), 1);
}

TEST_F(FibTestFixture, HandlesPositiveInput) {
EXPECT_EQ(fib(2), 1);
EXPECT_EQ(fib(3), 2);
EXPECT_EQ(fib(4), 3);
EXPECT_EQ(fib(5), 5);
EXPECT_EQ(fib(10), 55);
}
6 changes: 6 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <gtest/gtest.h>

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Empty file removed test/test.cpp
Empty file.
Binary file removed test/unit_test
Binary file not shown.

0 comments on commit 987b00b

Please sign in to comment.