Skip to content

Commit

Permalink
Merge pull request #1 from jbytheway/large_volumes
Browse files Browse the repository at this point in the history
Test item::charges_per_volume
  • Loading branch information
Zetsukaze authored Oct 30, 2018
2 parents e529906 + f118fa2 commit 7c380bf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <utility>
#include <cstddef>
#include <limits>
#include <ostream>

namespace units
{
Expand Down Expand Up @@ -345,6 +346,25 @@ inline constexpr double to_kilogram( const mass &v )
return v.value() / 1000.0;
}

// Streaming operators for debugging and tests
// (for UI output other functions should be used which render in the user's
// chosen units)
inline std::ostream &operator<<( std::ostream &o, mass_in_gram_tag )
{
return o << "g";
}

inline std::ostream &operator<<( std::ostream &o, volume_in_milliliter_tag )
{
return o << "ml";
}

template<typename value_type, typename tag_type>
inline std::ostream &operator<<( std::ostream &o, const quantity<value_type, tag_type> &v )
{
return o << v.value() << tag_type{};
}

} // namespace units

// Implicitly converted to volume, which has int as value_type!
Expand Down
21 changes: 21 additions & 0 deletions tests/item_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "catch/catch.hpp"

#include "item.h"
#include "units.h"

TEST_CASE( "item_volume", "[item]" )
{
item i( "battery", 0, item::default_charges_tag() );
// Would be better with Catch2 generators
units::volume big_volume = units::from_milliliter( std::numeric_limits<int>::max() / 2 );
for( units::volume v : {
0_ml, 1_ml, i.volume(), big_volume
} ) {
INFO( "checking batteries that fit in " << v );
auto charges_that_should_fit = i.charges_per_volume( v );
i.charges = charges_that_should_fit;
CHECK( i.volume() <= v );
i.charges++;
CHECK( i.volume() > v );
}
}

0 comments on commit 7c380bf

Please sign in to comment.