diff --git a/modules/nikitin_k_lab2/CMakeLists.txt b/modules/nikitin_k_lab2/CMakeLists.txt new file mode 100644 index 00000000..6dfc3cf4 --- /dev/null +++ b/modules/nikitin_k_lab2/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.28.1) +# Declare variables for binaries' names +get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +set(MODULE "${DIR_NAME}") +set(LIBRARY "lib_${MODULE}") +set(TESTS "test_${MODULE}") + +# Include directory with public headers +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +# Add all submodules +add_subdirectory(src) +add_subdirectory(test) diff --git a/modules/nikitin_k_lab2/include/Triangle.h b/modules/nikitin_k_lab2/include/Triangle.h new file mode 100644 index 00000000..000b02ec --- /dev/null +++ b/modules/nikitin_k_lab2/include/Triangle.h @@ -0,0 +1,20 @@ +// Copyright 2024 Nikitin Kirill +#ifndef TRIANGLE_H +#define TRIANGLE_H + +#include + +class Triangle { + public: + Triangle(double x1, double y1, double x2, double y2, double x3, double y3); + + double sideLength(double x1, double y1, double x2, double y2) const; + double perimeter() const; + double area() const; + + private: + double x1, y1, x2, y2, x3, y3; +}; + +#endif // TRIANGLE_H +#pragma once diff --git a/modules/nikitin_k_lab2/src/CMakeLists.txt b/modules/nikitin_k_lab2/src/CMakeLists.txt new file mode 100644 index 00000000..b1dc57b0 --- /dev/null +++ b/modules/nikitin_k_lab2/src/CMakeLists.txt @@ -0,0 +1,18 @@ +set(target ${LIBRARY}) + +file(GLOB srcs "*.cpp") +file(GLOB hdrs "../include/*.h") +set_source_files_properties(${srcs} ${hdrs} PROPERTIES + LABELS "${MODULE};Library") + +add_library(${target} STATIC ${srcs} ${hdrs}) +set_target_properties(${target} PROPERTIES + OUTPUT_NAME ${MODULE} + LABELS "${MODULE};Library") + +if (UNIX) + target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT}) +endif (UNIX) +target_link_libraries(${target} ${LIBRARY_DEPS}) + +set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE) diff --git a/modules/nikitin_k_lab2/src/Triangle.cpp b/modules/nikitin_k_lab2/src/Triangle.cpp new file mode 100644 index 00000000..f2c44364 --- /dev/null +++ b/modules/nikitin_k_lab2/src/Triangle.cpp @@ -0,0 +1,27 @@ +// Copyright 2024 Nikitin Kirill +#include "include/Triangle.h" + +#include + +Triangle::Triangle(double x1, double y1, double x2, double y2, double x3, + double y3) + : x1(x1), y1(y1), x2(x2), y2(y2), x3(x3), y3(y3) {} + +double Triangle::sideLength(double x1, double y1, double x2, double y2) const { + return std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2)); +} + +double Triangle::perimeter() const { + double a = sideLength(x1, y1, x2, y2); + double b = sideLength(x2, y2, x3, y3); + double c = sideLength(x3, y3, x1, y1); + return a + b + c; +} + +double Triangle::area() const { + double a = sideLength(x1, y1, x2, y2); + double b = sideLength(x2, y2, x3, y3); + double c = sideLength(x3, y3, x1, y1); + double s = (a + b + c) / 2; + return std::sqrt(s * (s - a) * (s - b) * (s - c)); +} diff --git a/modules/nikitin_k_lab2/test/CMakeLists.txt b/modules/nikitin_k_lab2/test/CMakeLists.txt new file mode 100644 index 00000000..cd06c57b --- /dev/null +++ b/modules/nikitin_k_lab2/test/CMakeLists.txt @@ -0,0 +1,27 @@ +set(target ${TESTS}) + +file(GLOB srcs "*.cpp") +set_source_files_properties(${srcs} PROPERTIES + LABELS "${MODULE};Test") + +add_executable(${target} ${srcs} ${hdrs}) +set_target_properties(${target} PROPERTIES + LABELS "${MODULE};Test") + +if (UNIX) + target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread) +endif (UNIX) +target_link_libraries(${target} gtest ${LIBRARY}) + +# VS2012 doesn't support correctly the tuples yet, +# see http://code.google.com/p/googletest/issues/detail?id=412 +if(MSVC) + target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10) +endif() + +add_test( + NAME ${MODULE}_gtest + COMMAND ${target} +) +set_tests_properties (${MODULE}_gtest PROPERTIES + LABELS "${MODULE}") diff --git a/modules/nikitin_k_lab2/test/tests.cpp b/modules/nikitin_k_lab2/test/tests.cpp new file mode 100644 index 00000000..a6d3cdd3 --- /dev/null +++ b/modules/nikitin_k_lab2/test/tests.cpp @@ -0,0 +1,27 @@ +// Copyright 2024 Nikitin Kirill +#include + +#include "include/Triangle.h" + +TEST(TriangleTest, SideLengthCalculation) { + Triangle triangle(0, 0, 3, 4, 0, 0); + EXPECT_DOUBLE_EQ(triangle.sideLength(0, 0, 3, 4), 5); + EXPECT_DOUBLE_EQ(triangle.sideLength(3, 4, 6, 8), 5); +} + +TEST(TriangleTest, PerimeterCalculation) { + Triangle triangle(0, 0, 3, 4, 6, 0); + EXPECT_NEAR(triangle.perimeter(), 16.0, 0.0001); +} + +TEST(TriangleTest, AreaCalculation) { + Triangle triangle1(0, 0, 3, 0, 3, 4); + Triangle triangle2(0, 0, 3, 4, 6, 0); + EXPECT_NEAR(triangle1.area(), 6, 0.0001); + EXPECT_NEAR(triangle2.area(), 12, 0.0001); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}