Skip to content

Commit

Permalink
use cmake for build process and add better test (#3)
Browse files Browse the repository at this point in the history
* use cmake for build process and add better test

* fix test

* fix test 2

* use cmake for tests
  • Loading branch information
InnocentBug authored May 9, 2023
1 parent 6294fa7 commit dfe8dd6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/test_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ on:
branches:
- main
pull_request:
branches: -main
branches:
- main

jobs:
install:
Expand All @@ -14,8 +15,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: install dependencies
run: sudo apt install g++
run: sudo apt install g++ cmake
- name: compile
run: g++ src/main.cpp
run: |
cmake src
make
- name: test
run: ./a.out 10 31
run: ctest --verbose
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,6 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.trunk
.trunk
build
install
12 changes: 12 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.16)

project(main)

add_executable(main main.cpp)

install(TARGETS main)

enable_testing()
# Configure the test script for the current build
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/test_sum.sh.in ${CMAKE_CURRENT_BINARY_DIR}/test_sum.sh)
add_test(NAME test-summation COMMAND bash test_sum.sh)
56 changes: 56 additions & 0 deletions src/tests/test_sum.sh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash



# Let's test that execution without arguments should fail
`@CMAKE_CURRENT_BINARY_DIR@/main`
# Test the return code of the execution to not be 0
if [ $? -eq 0 ]; then
exit -1
fi

# Test some basic summation
result=`@CMAKE_CURRENT_BINARY_DIR@/main 3.14`
echo $result

# Check that return code is 0
if [ ! $? -eq 0 ]; then exit -1; fi
if [ ! "$result" = "3.14" ]; then
exit -2
fi

result=`@CMAKE_CURRENT_BINARY_DIR@/main 0.1 0.5`
echo $result

# Check that return code is 0
if [ ! $? -eq 0 ]; then exit -1; fi
if [ ! "$result" = "0.6" ]; then
exit -2
fi

result=`@CMAKE_CURRENT_BINARY_DIR@/main 0.1 -0.1`
echo $result

# Check that return code is 0
if [ ! $? -eq 0 ]; then exit -1; fi
if [ ! "$result" = "0" ]; then
exit -2
fi

result=`@CMAKE_CURRENT_BINARY_DIR@/main 0.1 -0.15 0.5`
echo $result

# Check that return code is 0
if [ ! $? -eq 0 ]; then exit -1; fi
if [ ! "$result" = "0.45" ]; then
exit -2
fi

result=`@CMAKE_CURRENT_BINARY_DIR@/main -0.1 -0.15 0.05`
echo $result

# Check that return code is 0
if [ ! $? -eq 0 ]; then exit -1; fi
if [ ! "$result" = "-0.2" ]; then
exit -2
fi

0 comments on commit dfe8dd6

Please sign in to comment.