From 32f10f1ccbcfb2b72414fd1ad35046ead487e302 Mon Sep 17 00:00:00 2001 From: ajet Date: Tue, 23 Dec 2025 11:06:30 -1000 Subject: [PATCH] init commit --- .gitignore | 2 ++ CMakeLists.txt | 40 ++++++++++++++++++++++++++++++++++++ include/shapes/Rectangle.hpp | 12 +++++++++++ include/shapes/Shape.hpp | 15 ++++++++++++++ include/shapes/Triangle.hpp | 11 ++++++++++ src/CMakeLists.txt | 16 +++++++++++++++ src/shapes/Rectangle.cpp | 5 +++++ src/shapes/Shape.cpp | 8 ++++++++ src/shapes/Triangle.cpp | 5 +++++ test/CMakeLists.txt | 13 ++++++++++++ test/TestShape.cpp | 16 +++++++++++++++ 11 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 include/shapes/Rectangle.hpp create mode 100644 include/shapes/Shape.hpp create mode 100644 include/shapes/Triangle.hpp create mode 100644 src/CMakeLists.txt create mode 100644 src/shapes/Rectangle.cpp create mode 100644 src/shapes/Shape.cpp create mode 100644 src/shapes/Triangle.cpp create mode 100644 test/CMakeLists.txt create mode 100644 test/TestShape.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ef9604 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..32c7511 --- /dev/null +++ b/CMakeLists.txt @@ -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() diff --git a/include/shapes/Rectangle.hpp b/include/shapes/Rectangle.hpp new file mode 100644 index 0000000..64434bd --- /dev/null +++ b/include/shapes/Rectangle.hpp @@ -0,0 +1,12 @@ +#include "Shape.hpp" + +struct Rectangle : public Shape +{ + Rectangle(const double sideLen); + + double area() override; + +private: + double sideLen{}; +}; + diff --git a/include/shapes/Shape.hpp b/include/shapes/Shape.hpp new file mode 100644 index 0000000..dfb94f9 --- /dev/null +++ b/include/shapes/Shape.hpp @@ -0,0 +1,15 @@ +#include +#include + +struct Shape +{ + virtual double area() = 0; + + double getArea(); + +protected: + Shape() = delete; + Shape(const size_t numSides); + + size_t numSides{}; +}; diff --git a/include/shapes/Triangle.hpp b/include/shapes/Triangle.hpp new file mode 100644 index 0000000..79a1dd2 --- /dev/null +++ b/include/shapes/Triangle.hpp @@ -0,0 +1,11 @@ +#include "Shape.hpp" + +struct Triangle : public Shape +{ + Triangle(const double sideLen); + + double area() override; + +private: + double sideLen{}; +}; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..f331071 --- /dev/null +++ b/src/CMakeLists.txt @@ -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 +) diff --git a/src/shapes/Rectangle.cpp b/src/shapes/Rectangle.cpp new file mode 100644 index 0000000..a126755 --- /dev/null +++ b/src/shapes/Rectangle.cpp @@ -0,0 +1,5 @@ +#include "Rectangle.hpp" + +Rectangle::Rectangle(const double sideLen) : sideLen(sideLen) {} + +Rectangle::area() { return sideLen * sideLen; } diff --git a/src/shapes/Shape.cpp b/src/shapes/Shape.cpp new file mode 100644 index 0000000..faa7096 --- /dev/null +++ b/src/shapes/Shape.cpp @@ -0,0 +1,8 @@ +#include "Shape.hpp" + +Shape::Shape(const size_t numSides) : numSides(numSides) +{ + +} + +double Shape::getArea(){ return area(); } diff --git a/src/shapes/Triangle.cpp b/src/shapes/Triangle.cpp new file mode 100644 index 0000000..13087af --- /dev/null +++ b/src/shapes/Triangle.cpp @@ -0,0 +1,5 @@ +#include "Triangle.hpp" + +Triangle::Triangle(const double sideLen) : sideLen(sideLen) {} + +Triangle::area() { return 0.5 * sideLen * sideLen; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..4cbf914 --- /dev/null +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/TestShape.cpp b/test/TestShape.cpp new file mode 100644 index 0000000..3801d5b --- /dev/null +++ b/test/TestShape.cpp @@ -0,0 +1,16 @@ +#include + +#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); +}