Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/stdfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ float sign( float x )
template <typename... Args>
float minv( float arg0, Args... args )
{
float r;
( ( r = std::min( arg0, args ) ), ... );
float r = arg0;
( ( r = std::min( r, args ) ), ... );
return r;
}

template <typename... Args>
float maxv( float arg0, Args... args )
{
float r;
( ( r = std::max( arg0, args ) ), ... );
float r = arg0;
( ( r = std::max( r, args ) ), ... );
return r;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,24 @@ TEST( Functions, CanUseStandardFunctions )
ASSERT_EQ( 1, TestEval( "cos(0)" ) );
ASSERT_EQ( 3, TestEval( "max(1, 2, 3)" ) );
}

TEST( Functions, MinMaxFoldThroughAccumulator )
{
// Smallest/largest is neither the first nor the last argument: a
// buggy fold that compares each tail element against arg0 (rather
// than folding through the running result) would return the wrong
// answer here.
ASSERT_EQ( 3, TestEval( "min(5, 3, 7)" ) );
ASSERT_EQ( 9, TestEval( "max(1, 9, 4)" ) );
ASSERT_EQ( 1, TestEval( "min(5, 1, 5, 5, 5)" ) );
ASSERT_EQ( 9, TestEval( "max(2, 9, 2, 2, 2)" ) );
}

TEST( Functions, MinMaxSingleArgument )
{
// One-arg form: a fold over an empty parameter pack must still
// produce a defined result equal to that single argument.
ASSERT_EQ( 7, TestEval( "min(7)" ) );
ASSERT_EQ( 7, TestEval( "max(7)" ) );
ASSERT_EQ( -3, TestEval( "min(-3)" ) );
}