Skip to content

Commit

Permalink
Minimal item length to volume sanity check
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingranade committed May 11, 2020
1 parent 20290c0 commit ae6d67f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/item_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cmath>
#include <initializer_list>
#include <limits>
#include <memory>
Expand All @@ -6,7 +7,9 @@
#include "catch/catch.hpp"
#include "enums.h"
#include "item.h"
#include "item_factory.h"
#include "itype.h"
#include "monstergenerator.h"
#include "ret_val.h"
#include "units.h"
#include "value_ptr.h"
Expand Down Expand Up @@ -157,3 +160,33 @@ TEST_CASE( "stacking_over_time", "[item]" )
}
}
}

static void assert_minimum_length_to_volume_ratio( const item &target )
{
if( target.made_of( LIQUID ) || target.is_soft() ) {
// Nothing to assert about these.
return;
}
// Minimum possible length is if the item is a sphere.
const float minimal_diameter = std::cbrt( ( 3.0 * units::to_milliliter( target.volume() ) ) /
( 4.0 * M_PI ) );
CAPTURE( target.type->get_id() );
CAPTURE( target.volume() );
CHECK( units::to_centimeter( target.length() ) >= minimal_diameter );
}

TEST_CASE( "item length sanity check", "[item]" )
{
for( const itype *type : item_controller->all() ) {
const item sample( type );
assert_minimum_length_to_volume_ratio( sample );
}
}

TEST_CASE( "corpse length sanity check", "[item]" )
{
for( const mtype &type : MonsterGenerator::generator().get_all_mtypes() ) {
const item sample = item::make_corpse( type.id );
assert_minimum_length_to_volume_ratio( sample );
}
}

0 comments on commit ae6d67f

Please sign in to comment.