From 44b0694e9d6bb4dd853987be17181ebd77526a91 Mon Sep 17 00:00:00 2001 From: asaky Date: Sat, 16 May 2026 19:04:23 +0300 Subject: [PATCH] add files --- CMakeLists.txt | 22 +++++++++++++++++++++ include/calculator.h | 9 +++++++++ src/calculator.c | 21 +++++++++++++++++++++ tests/test_calculator.c | 42 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 include/calculator.h create mode 100644 src/calculator.c create mode 100644 tests/test_calculator.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2a0014b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.10) +project(Calculator VERSION 1.0 LANGUAGES C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +enable_testing() + +add_library(calculator src/calculator.c) +target_include_directories(calculator PUBLIC include) + +add_executable(main src/calculator.c) +target_include_directories(main PUBLIC include) + +add_executable(test_calculator tests/test_calculator.c) +target_link_libraries(test_calculator calculator m) +target_include_directories(test_calculator PRIVATE include) + +add_test(NAME CalculatorTests COMMAND test_calculator) + +install(TARGETS calculator main DESTINATION bin) +install(FILES include/calculator.h DESTINATION include) diff --git a/include/calculator.h b/include/calculator.h new file mode 100644 index 0000000..6a8ce7d --- /dev/null +++ b/include/calculator.h @@ -0,0 +1,9 @@ +#ifndef CALCULATOR_H +#define CALCULATOR_H + +int add(int a, int b); +int subtract(int a, int b); +int multiply(int a, int b); +double divide(int a, int b); + +#endif diff --git a/src/calculator.c b/src/calculator.c new file mode 100644 index 0000000..d1a59e1 --- /dev/null +++ b/src/calculator.c @@ -0,0 +1,21 @@ +#include "calculator.h" + +int add(int a, int b) { + return a + b; +} + +int subtract(int a, int b) { + return a - b; +} + +int multiply(int a, int b) { + return a * b; +} + +double divide(int a, int b) { + if (b == 0) { + return 0.0; + } + return (double)a / b; +} +EOF diff --git a/tests/test_calculator.c b/tests/test_calculator.c new file mode 100644 index 0000000..8170959 --- /dev/null +++ b/tests/test_calculator.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include "calculator.h" + +void test_add() { + assert(add(2, 3) == 5); + assert(add(-1, 1) == 0); + assert(add(0, 0) == 0); + printf("test_add passed\n"); +} + +void test_subtract() { + assert(subtract(5, 3) == 2); + assert(subtract(1, 1) == 0); + assert(subtract(0, 5) == -5); + printf("test_subtract passed\n"); +} + +void test_multiply() { + assert(multiply(3, 4) == 12); + assert(multiply(0, 5) == 0); + assert(multiply(-2, 3) == -6); + printf("test_multiply passed\n"); +} + +void test_divide() { + assert(fabs(divide(10, 2) - 5.0) < 0.001); + assert(fabs(divide(7, 2) - 3.5) < 0.001); + assert(divide(5, 0) == 0.0); + printf("test_divide passed\n"); +} + +int main() { + printf("Running calculator tests...\n\n"); + test_add(); + test_subtract(); + test_multiply(); + test_divide(); + printf("\nAll tests passed!\n"); + return 0; +}