Skip to content

Commit

Permalink
Clean up template stuff and created box class
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanG077 committed Nov 5, 2018
1 parent 2d011cf commit 86a1b7b
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 197 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: 0
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignEscapedNewlines: DontAlign
AlignOperands: false
Expand Down
76 changes: 76 additions & 0 deletions include/box_nesting/Box.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <array>

/**
* @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<double, 3> sideLengths;
};
}
49 changes: 0 additions & 49 deletions include/box_nesting/base.hpp

This file was deleted.

95 changes: 0 additions & 95 deletions include/box_nesting/derived.hpp

This file was deleted.

23 changes: 23 additions & 0 deletions src/Box.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <box_nesting/Box.hpp>

#include <algorithm>

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;
}
}
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 0 additions & 22 deletions src/derived.cpp

This file was deleted.

5 changes: 1 addition & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <box_nesting/derived.hpp>
#include <box_nesting/Box.hpp>
#include <box_nesting/version.hpp>

#include <cstdlib>
Expand All @@ -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'
Expand Down
49 changes: 49 additions & 0 deletions test/Box.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <box_nesting/Box.hpp>
#include <catch2/catch.hpp>

#include <memory>

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);
}
}
}
}
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
$<TARGET_OBJECTS:${PROJECT_NAME}_obj>
Expand Down
24 changes: 0 additions & 24 deletions test/derived.cpp

This file was deleted.

0 comments on commit 86a1b7b

Please sign in to comment.