-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |