Skip to content

Commit

Permalink
Lab2 added (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitinKU authored Dec 2, 2024
1 parent 76b2a8b commit 10aee89
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
13 changes: 13 additions & 0 deletions modules/nikitin_k_lab2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 20 additions & 0 deletions modules/nikitin_k_lab2/include/Triangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 Nikitin Kirill
#ifndef TRIANGLE_H
#define TRIANGLE_H

#include <cmath>

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
18 changes: 18 additions & 0 deletions modules/nikitin_k_lab2/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions modules/nikitin_k_lab2/src/Triangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Nikitin Kirill
#include "include/Triangle.h"

#include <iostream>

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));
}
27 changes: 27 additions & 0 deletions modules/nikitin_k_lab2/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
27 changes: 27 additions & 0 deletions modules/nikitin_k_lab2/test/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Nikitin Kirill
#include <gtest/gtest.h>

#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();
}

0 comments on commit 10aee89

Please sign in to comment.