diff --git a/.clang-format b/.clang-format index 6755fbb..7c5b0b4 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,6 @@ Language: Cpp BasedOnStyle: LLVM -AccessModifierOffset: 0 +AccessModifierOffset: -4 AlignAfterOpenBracket: DontAlign AlignEscapedNewlines: DontAlign AlignOperands: false diff --git a/include/box_nesting/Box.hpp b/include/box_nesting/Box.hpp new file mode 100644 index 0000000..ecd3fa4 --- /dev/null +++ b/include/box_nesting/Box.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include + +/** + * @brief Contains code for the boxnesting problem + */ +namespace BoxNesting +{ +/** + * @brief Class representing a box + */ +class Box +{ +public: + /** + * @brief Deleted default constructor + */ + Box() = delete; + + /** + * Default virtual destructor. + */ + virtual ~Box() = default; + + /** + * Default copy constructor. + * \param b + */ + Box(const Box& b) = default; + + /** + * Default move constructor. + * \param b + */ + Box(Box&& b) = default; + + /** + * Default copy assignment operator. + * \param b + * \return + */ + Box& operator=(const Box& b) = default; + + /** + * Default move assignment operator. + * \param b + * \return + */ + Box& operator=(Box&& b) = default; + + /** + * @brief Construct a new Box object + * + * @param x The length of the box in the x-direction + * @param y The length of the box in the y-direction + * @param z The length of the box in the z-direction + * @return A new Box object + */ + Box(double x, double y, double z); + + /** + * @brief Check if this box can nest inside other box + * + * @param b The box to check if this box can nest inside. + * @return True if this box can nest inside the passed box. False otherwise + */ + bool isNestable(const Box& b) const; + +private: + /** + * @brief The lengths of each side ordered from smallest to longest + */ + std::array sideLengths; +}; +} diff --git a/include/box_nesting/base.hpp b/include/box_nesting/base.hpp deleted file mode 100644 index f99a891..0000000 --- a/include/box_nesting/base.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once - -/** - * An abstract base class. - */ -class base -{ - public: - /** - * Default constructor. - */ - base() = default; - - /** - * Default virtual destructor. - */ - virtual ~base() = default; - - /** - * Default copy constructor. - * \param b - */ - base(const base& b) = default; - - /** - * Default move constructor. - * \param b - */ - base(base&& b) = default; - - /** - * Default copy assignment operator. - * \param b - * \return - */ - base& operator=(const base& b) = default; - - /** - * Default move assignment operator. - * \param b - * \return - */ - base& operator=(base&& b) = default; - - /** - * Pure virtual member function. - */ - virtual void init() = 0; -}; diff --git a/include/box_nesting/derived.hpp b/include/box_nesting/derived.hpp deleted file mode 100644 index 4b0d0dd..0000000 --- a/include/box_nesting/derived.hpp +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include "base.hpp" - -#include -#include - -/** - * Defines some known class types. - */ -enum class class_type -{ - /** - * Has no implementation. - */ - INTERFACE, - - /** - * Has some implementation. - */ - ABSTRACT, - - /** - * Has only implementation. - */ - NORMAL -}; - -/** - * struct containing a min and a max. - */ -struct range -{ - /** - * The minimum value. - */ - std::uint8_t min; - - /** - * The maximum value. - */ - std::uint8_t max; -}; - -/** - * Convert class_type to a string representation. - * \param t The type to translate. - * \return The string variant. - */ -std::string class_types_to_string(class_type t); - -/** - * Class that derives from an abstract base - */ -class derived : public base -{ - public: - /** - * Default constructor. - */ - derived() = default; - - ~derived() override = default; - - /** - * Default copy constructor - * \param d - */ - derived(const derived& d) = default; - - /** - * Default move constructor. - * \param d - */ - derived(derived&& d) = default; - - /** - * Default copy assignment operator. - * \param d - * \return - */ - derived& operator=(const derived& d) = default; - - /** - * Default move assignment operator. - * \param d - * \return - */ - derived& operator=(derived&& d) = delete; - - /** - * Implemented function - */ - void init() override; -}; diff --git a/src/Box.cpp b/src/Box.cpp new file mode 100644 index 0000000..06ef61d --- /dev/null +++ b/src/Box.cpp @@ -0,0 +1,23 @@ +#include + +#include + +namespace BoxNesting +{ + +Box::Box(double x, double y, double z) : sideLengths({x, y, z}) +{ + std::sort(this->sideLengths.begin(), this->sideLengths.end()); +} + +bool Box::isNestable(const Box& b) const +{ + for (std::size_t i = 0; i < this->sideLengths.size(); ++i) { + if (this->sideLengths.at(i) >= b.sideLengths.at(i)) { + return false; + } + } + + return true; +} +} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e3c7366..acda72c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ # build code as object library for easier linking to tests add_library(${PROJECT_NAME}_obj OBJECT - derived.cpp) + Box.cpp) add_executable(${PROJECT_NAME} main.cpp diff --git a/src/derived.cpp b/src/derived.cpp deleted file mode 100644 index 15ed750..0000000 --- a/src/derived.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include - -#include - -std::string class_types_to_string(class_type t) -{ - switch (t) { - case class_type::INTERFACE: - return "INTERFACE"; - case class_type::ABSTRACT: - return "ABSTRACT"; - case class_type::NORMAL: - return "NORMAL"; - default: - return "unknown class type"; - } -} - -void derived::init() -{ - std::cout << "hello from ::derived::init()" << std::endl; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fce3db6..4d7ba24 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -16,9 +16,6 @@ int main(int argc, char** argv) std::cout << std::flush; - ::derived der; - der.init(); - std::cout << "project info:\n" << "\tname: " << BOX_NESTING_NAME << '\n' << "\tdesc: " << BOX_NESTING_DESCRIPTION << '\n' diff --git a/test/Box.cpp b/test/Box.cpp new file mode 100644 index 0000000..286889c --- /dev/null +++ b/test/Box.cpp @@ -0,0 +1,49 @@ +#include +#include + +#include + +SCENARIO("Boxes can nest inside another if their dimensions allow it", "[Box]") +{ + GIVEN("a set of boxes") + { + BoxNesting::Box a(0.6, 0.6, 0.6); + BoxNesting::Box b(0.6, 0.6, 0.5); + BoxNesting::Box c(0.6, 0.5, 0.6); + BoxNesting::Box d(0.5, 0.6, 0.6); + BoxNesting::Box e(0.5, 0.5, 0.5); + BoxNesting::Box f(0.4, 0.4, 0.4); + + THEN("they can nest inside eachother if their dimensions allow it") + { + REQUIRE(e.isNestable(a) == true); + } + + THEN("a box cannot nest inside itself") + { + REQUIRE(a.isNestable(a) == false); + REQUIRE(b.isNestable(b) == false); + REQUIRE(c.isNestable(c) == false); + REQUIRE(d.isNestable(d) == false); + REQUIRE(e.isNestable(e) == false); + } + + THEN("a larger box cannot nest inside a smaller box") + { + REQUIRE(e.isNestable(d) == false); + REQUIRE(e.isNestable(c) == false); + REQUIRE(e.isNestable(b) == false); + } + + WHEN("a box a can nest inside a box b and box b can nest inside box c") + { + REQUIRE(f.isNestable(e) == true); + REQUIRE(e.isNestable(a) == true); + + THEN("box a can nest iniside box c") + { + REQUIRE(f.isNestable(a) == true); + } + } + } +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c03ee16..c4aaff5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,7 +6,7 @@ add_library(test_obj OBJECT # add all tests on this level separately foreach(file - derived) + Box) add_executable("test_${file}" "${file}.cpp" $ diff --git a/test/derived.cpp b/test/derived.cpp deleted file mode 100644 index 6bd5952..0000000 --- a/test/derived.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -#include - -SCENARIO("polymorphic classes", "[derived]") -{ - GIVEN("one class that derives from an abstract base") - { - auto der = std::make_shared<::derived>(); - REQUIRE(der); - - WHEN("derived class is cast to base") - { - auto base = std::dynamic_pointer_cast<::base>(der); - - THEN("the abstract base can be used") - { - REQUIRE(base); - base->init(); - } - } - } -}