diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e4e5f6c8b2deb54bf38312dd9e2f53489b60d6a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..dbb728767c690b48128feae6e4626a3bffd39d74 --- /dev/null +++ b/Makefile @@ -0,0 +1,58 @@ +SRC=src/main.cpp +TESTS=tests +COV=coverage +OUT_T=test_output +OUT_E=expected_output +APP=program +CXX=g++ +BROWSER=google-chrome + +# Default +all: distclean compile test + +# Compiling section +compile: + $(CXX) -o $(APP) $(SRC) + +compile_coverage: + $(CXX) -o $(APP) --coverage $(SRC) + +# Testing section +test: simple_tests regression_test unit_tests + +simple_tests: + ./$(APP) 1 4 2 + ./$(APP) 2 4 2 + ./$(APP) 4 4 2 + +regression_test: + ./$(APP) 1 4 2 | tail -n 1 > $(OUT_T) + echo "c=6" > $(OUT_E) + diff $(OUT_T) $(OUT_E) + +unit_tests: + cd $(TESTS) && cmake CMakeLists.txt && make + ./$(TESTS)/runTests + +# Coverage section +coverage: clean clean_coverage compile_coverage simple_tests + lcov -t "result" -o $(APP).info -c -d . + genhtml -o $(COV) $(APP).info + +show_cov: coverage + $(BROWSER) $(COV)/index.html + +# Cleaning section +distclean: clean clean_tests clean_coverage + +.PHONY: clean +clean: + rm -f $(APP) + +clean_tests: + rm -rf $(TESTS)/CMakeFiles $(TESTS)/CMakeCache.txt $(TESTS)/cmake_install.cmake $(TESTS)/CMakeFiles $(TESTS)/runTests $(OUT_E) $(OUT_T) + +clean_coverage: + rm -rf *.gcno *.gcda $(APP).info $(COV) + + diff --git a/src/funcs.cpp b/src/funcs.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1eafd06a158bc04f03d1278e045c6b89e4f01d7e --- /dev/null +++ b/src/funcs.cpp @@ -0,0 +1,9 @@ +#include<iostream> +using namespace std; + +int add(int a,int b){return a+b;} +int sub(int a,int b){return a-b;} +int mul(int a,int b){return a*b;} +int dv(int a,int b){ + if (b!=0) return a/b; + else return 0;} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..702da9d3d1ba15eca83143727ab447ecae42122f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,21 @@ +#include "funcs.cpp" +using namespace std; + +int main(int argc, char *argv[]) +{ + int op=atoi(argv[1]); + int a= atoi(argv[2]); + int b= atoi(argv[3]); + printf("op=%d; a=%d; b=%d\n", op, a, b); + int c; + switch(op) + { + case 1: c=add(a,b); break; + case 2: c=sub(a,b); break; + case 3: c=mul(a,b); break; + case 4: c= dv(a,b); break; + default: c=0; + } + printf("c=%d\n", c); + return 0; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..92d4656105f4e4f7799deb2a5b76ddd2e33f30e5 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 2.6) + +# Locate GTest +find_package(GTest REQUIRED) +include_directories(${GTEST_INCLUDE_DIRS}) + +# Link runTests with what we want to test and the GTest and pthread library +add_executable(runTests test_funcs.cpp) +target_link_libraries(runTests ${GTEST_LIBRARIES} pthread) diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..3757ec24f42f871cb8210dc930dfe5f9d98ecea2 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,178 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.12 + +# Default target executed when no arguments are given to make. +default_target: all + +.PHONY : default_target + +# Allow only one "make -f Makefile2" at a time, but pass parallelism. +.NOTPARALLEL: + + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canonical targets will work. +.SUFFIXES: + + +# Remove some rules from gmake that .SUFFIXES does not remove. +SUFFIXES = + +.SUFFIXES: .hpux_make_needs_suffix_list + + +# Suppress display of executed commands. +$(VERBOSE).SILENT: + + +# A target that is always out of date. +cmake_force: + +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/bin/cmake + +# The command to remove a file. +RM = /usr/bin/cmake -E remove -f + +# Escaping for special characters. +EQUALS = = + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /home/sluka/Work/testing_example/tests + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /home/sluka/Work/testing_example/tests + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." + /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache + +.PHONY : rebuild_cache/fast + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..." + /usr/bin/ccmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache + +.PHONY : edit_cache/fast + +# The main all target +all: cmake_check_build_system + $(CMAKE_COMMAND) -E cmake_progress_start /home/sluka/Work/testing_example/tests/CMakeFiles /home/sluka/Work/testing_example/tests/CMakeFiles/progress.marks + $(MAKE) -f CMakeFiles/Makefile2 all + $(CMAKE_COMMAND) -E cmake_progress_start /home/sluka/Work/testing_example/tests/CMakeFiles 0 +.PHONY : all + +# The main clean target +clean: + $(MAKE) -f CMakeFiles/Makefile2 clean +.PHONY : clean + +# The main clean target +clean/fast: clean + +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +#============================================================================= +# Target rules for targets named runTests + +# Build rule for target. +runTests: cmake_check_build_system + $(MAKE) -f CMakeFiles/Makefile2 runTests +.PHONY : runTests + +# fast build rule for target. +runTests/fast: + $(MAKE) -f CMakeFiles/runTests.dir/build.make CMakeFiles/runTests.dir/build +.PHONY : runTests/fast + +test_funcs.o: test_funcs.cpp.o + +.PHONY : test_funcs.o + +# target to build an object file +test_funcs.cpp.o: + $(MAKE) -f CMakeFiles/runTests.dir/build.make CMakeFiles/runTests.dir/test_funcs.cpp.o +.PHONY : test_funcs.cpp.o + +test_funcs.i: test_funcs.cpp.i + +.PHONY : test_funcs.i + +# target to preprocess a source file +test_funcs.cpp.i: + $(MAKE) -f CMakeFiles/runTests.dir/build.make CMakeFiles/runTests.dir/test_funcs.cpp.i +.PHONY : test_funcs.cpp.i + +test_funcs.s: test_funcs.cpp.s + +.PHONY : test_funcs.s + +# target to generate assembly for a file +test_funcs.cpp.s: + $(MAKE) -f CMakeFiles/runTests.dir/build.make CMakeFiles/runTests.dir/test_funcs.cpp.s +.PHONY : test_funcs.cpp.s + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... rebuild_cache" + @echo "... runTests" + @echo "... edit_cache" + @echo "... test_funcs.o" + @echo "... test_funcs.i" + @echo "... test_funcs.s" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system + diff --git a/tests/test_funcs.cpp b/tests/test_funcs.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c13d2e87b2710220c95f0c54901cd66848f9f169 --- /dev/null +++ b/tests/test_funcs.cpp @@ -0,0 +1,21 @@ +#include "gtest/gtest.h" +#include "../src/funcs.cpp" + +TEST(FuncsTest,Add){ + EXPECT_EQ(6,add(4,2)); +}; +TEST(FuncsTest,Sub){ + EXPECT_EQ(2,sub(4,2)); +}; +TEST(FuncsTest,Mul){ + EXPECT_EQ(8,mul(4,2)); +}; +TEST(FuncsTest,Div){ + EXPECT_EQ(2,dv(4,2)); +}; + +int main(int argc,char**argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}