init commit
This commit is contained in:
commit
32f10f1ccb
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
build
|
||||
|
||||
40
CMakeLists.txt
Normal file
40
CMakeLists.txt
Normal file
@ -0,0 +1,40 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
|
||||
# Define the project
|
||||
project("CMake test")
|
||||
|
||||
# Create a compile_commands.json in the build directory
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# Setup some CPP stuff
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
|
||||
set(CMAKE_CXX_EXTENSIONS TRUE)
|
||||
|
||||
# Compile flags
|
||||
set(CMAKE_CXX_FLAGS "\
|
||||
-Wall \
|
||||
-Werror")
|
||||
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "\
|
||||
-g3 \
|
||||
-O")
|
||||
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "\
|
||||
-g0 \
|
||||
-O3")
|
||||
|
||||
# Call the CMakeLists.txt in src
|
||||
add_subdirectory(src)
|
||||
|
||||
if (BUILD_TESTING)
|
||||
include (FetchContent)
|
||||
FetchContent_Declare(
|
||||
googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG v1.17.0)
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
enable_testing()
|
||||
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
12
include/shapes/Rectangle.hpp
Normal file
12
include/shapes/Rectangle.hpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "Shape.hpp"
|
||||
|
||||
struct Rectangle : public Shape
|
||||
{
|
||||
Rectangle(const double sideLen);
|
||||
|
||||
double area() override;
|
||||
|
||||
private:
|
||||
double sideLen{};
|
||||
};
|
||||
|
||||
15
include/shapes/Shape.hpp
Normal file
15
include/shapes/Shape.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
struct Shape
|
||||
{
|
||||
virtual double area() = 0;
|
||||
|
||||
double getArea();
|
||||
|
||||
protected:
|
||||
Shape() = delete;
|
||||
Shape(const size_t numSides);
|
||||
|
||||
size_t numSides{};
|
||||
};
|
||||
11
include/shapes/Triangle.hpp
Normal file
11
include/shapes/Triangle.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include "Shape.hpp"
|
||||
|
||||
struct Triangle : public Shape
|
||||
{
|
||||
Triangle(const double sideLen);
|
||||
|
||||
double area() override;
|
||||
|
||||
private:
|
||||
double sideLen{};
|
||||
};
|
||||
16
src/CMakeLists.txt
Normal file
16
src/CMakeLists.txt
Normal file
@ -0,0 +1,16 @@
|
||||
add_library(BaseShape STATIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/shapes/Shape.cpp
|
||||
)
|
||||
|
||||
target_include_directories(BaseShape PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/../include/shapes
|
||||
)
|
||||
|
||||
add_library(Shapes STATIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/shapes/Rectangle.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/shapes/Triangle.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(Shapes PUBLIC
|
||||
BaseShape
|
||||
)
|
||||
5
src/shapes/Rectangle.cpp
Normal file
5
src/shapes/Rectangle.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include "Rectangle.hpp"
|
||||
|
||||
Rectangle::Rectangle(const double sideLen) : sideLen(sideLen) {}
|
||||
|
||||
Rectangle::area() { return sideLen * sideLen; }
|
||||
8
src/shapes/Shape.cpp
Normal file
8
src/shapes/Shape.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "Shape.hpp"
|
||||
|
||||
Shape::Shape(const size_t numSides) : numSides(numSides)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
double Shape::getArea(){ return area(); }
|
||||
5
src/shapes/Triangle.cpp
Normal file
5
src/shapes/Triangle.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include "Triangle.hpp"
|
||||
|
||||
Triangle::Triangle(const double sideLen) : sideLen(sideLen) {}
|
||||
|
||||
Triangle::area() { return 0.5 * sideLen * sideLen; }
|
||||
13
test/CMakeLists.txt
Normal file
13
test/CMakeLists.txt
Normal file
@ -0,0 +1,13 @@
|
||||
add_executable(unittest
|
||||
${CMAKE_CURRENT_LIST_DIR}/TestShape.cpp
|
||||
)
|
||||
|
||||
message("${CMAKE_CURRENT_LIST_DIR}/TestShape.cpp")
|
||||
|
||||
target_link_libraries(unittest PUBLIC
|
||||
Shapes
|
||||
gtest_main
|
||||
)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(unittest)
|
||||
16
test/TestShape.cpp
Normal file
16
test/TestShape.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include <gtest/gtest.hpp>
|
||||
|
||||
#include "Rectangle.hpp"
|
||||
#include "Triangle.hpp"
|
||||
|
||||
TEST(TestRectangle, area)
|
||||
{
|
||||
const Rectangle r{5.0};
|
||||
EXPECT_DOUBLE_EQ(r.getArea(), 25.0);
|
||||
}
|
||||
|
||||
TEST(TestTriangle, area)
|
||||
{
|
||||
const Triangle t{10.0};
|
||||
EXPECT_DOUBLE_EQ(t.getArea(), 50.0);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user