add files
This commit is contained in:
22
CMakeLists.txt
Normal file
22
CMakeLists.txt
Normal file
@@ -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)
|
||||||
9
include/calculator.h
Normal file
9
include/calculator.h
Normal file
@@ -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
|
||||||
21
src/calculator.c
Normal file
21
src/calculator.c
Normal file
@@ -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
|
||||||
42
tests/test_calculator.c
Normal file
42
tests/test_calculator.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user