From 570b8343678fbf246e038e7dc9e82e44afc1dc02 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 17 Dec 2012 00:51:10 +0900 Subject: [PATCH] add some algorithm test --- libs/algorithm/test/clamp.cpp | 47 ++++++++++++++++ libs/algorithm/test/is_decreasing.cpp | 38 +++++++++++++ libs/algorithm/test/is_increasing.cpp | 38 +++++++++++++ .../algorithm/test/is_strictly_decreasing.cpp | 53 +++++++++++++++++++ .../algorithm/test/is_strictly_increasing.cpp | 53 +++++++++++++++++++ libs/algorithm/test/non_modifying.cpp | 10 ++++ sprout/algorithm/is_decreasing.hpp | 4 ++ sprout/algorithm/is_increasing.hpp | 4 ++ sprout/algorithm/is_strictly_decreasing.hpp | 4 ++ sprout/algorithm/is_strictly_increasing.hpp | 4 ++ 10 files changed, 255 insertions(+) create mode 100644 libs/algorithm/test/clamp.cpp create mode 100644 libs/algorithm/test/is_decreasing.cpp create mode 100644 libs/algorithm/test/is_increasing.cpp create mode 100644 libs/algorithm/test/is_strictly_decreasing.cpp create mode 100644 libs/algorithm/test/is_strictly_increasing.cpp diff --git a/libs/algorithm/test/clamp.cpp b/libs/algorithm/test/clamp.cpp new file mode 100644 index 00000000..d1010569 --- /dev/null +++ b/libs/algorithm/test/clamp.cpp @@ -0,0 +1,47 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_CLAMP_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_CLAMP_CPP + +#include +#include +#include + +namespace testspr { + static void algorithm_clamp_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{4, 7}}; + + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::clamp( + 0, + arr1[0], + arr1[1] + ); + TESTSPR_BOTH_ASSERT(result == arr1[0]); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::clamp( + 10, + arr1[0], + arr1[1] + ); + TESTSPR_BOTH_ASSERT(result == arr1[1]); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::clamp( + 5, + arr1[0], + arr1[1] + ); + TESTSPR_BOTH_ASSERT(result == 5); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_clamp_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_CLAMP_CPP diff --git a/libs/algorithm/test/is_decreasing.cpp b/libs/algorithm/test/is_decreasing.cpp new file mode 100644 index 00000000..e4617b8a --- /dev/null +++ b/libs/algorithm/test/is_decreasing.cpp @@ -0,0 +1,38 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_DECREASING_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_IS_DECREASING_CPP + +#include +#include +#include +#include + +namespace testspr { + static void algorithm_is_decreasing_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{10, 8, 6, 4, 2, 9, 7, 5, 3, 1}}; + + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_decreasing( + sprout::begin(arr1), + sprout::end(arr1) + ); + TESTSPR_BOTH_ASSERT(!result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_decreasing( + sprout::begin(arr1), + sprout::begin(arr1) + 5 + ); + TESTSPR_BOTH_ASSERT(result); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_is_decreasing_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_DECREASING_CPP diff --git a/libs/algorithm/test/is_increasing.cpp b/libs/algorithm/test/is_increasing.cpp new file mode 100644 index 00000000..2440b9f6 --- /dev/null +++ b/libs/algorithm/test/is_increasing.cpp @@ -0,0 +1,38 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_INCREASING_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_IS_INCREASING_CPP + +#include +#include +#include +#include + +namespace testspr { + static void algorithm_is_increasing_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 3, 5, 7, 9, 2, 4, 6, 8, 10}}; + + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_increasing( + sprout::begin(arr1), + sprout::end(arr1) + ); + TESTSPR_BOTH_ASSERT(!result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_increasing( + sprout::begin(arr1), + sprout::begin(arr1) + 5 + ); + TESTSPR_BOTH_ASSERT(result); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_is_increasing_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_INCREASING_CPP diff --git a/libs/algorithm/test/is_strictly_decreasing.cpp b/libs/algorithm/test/is_strictly_decreasing.cpp new file mode 100644 index 00000000..863e1e56 --- /dev/null +++ b/libs/algorithm/test/is_strictly_decreasing.cpp @@ -0,0 +1,53 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_STRICTLY_DECREASING_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_IS_STRICTLY_DECREASING_CPP + +#include +#include +#include +#include + +namespace testspr { + static void algorithm_is_strictly_decreasing_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{10, 8, 6, 4, 2, 9, 7, 5, 3, 1}}; + SPROUT_STATIC_CONSTEXPR auto arr2 = array{{10, 10, 6, 4, 2, 9, 7, 5, 1, 1}}; + + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_decreasing( + sprout::begin(arr1), + sprout::end(arr1) + ); + TESTSPR_BOTH_ASSERT(!result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_decreasing( + sprout::begin(arr2), + sprout::end(arr2) + ); + TESTSPR_BOTH_ASSERT(!result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_decreasing( + sprout::begin(arr1), + sprout::begin(arr1) + 5 + ); + TESTSPR_BOTH_ASSERT(result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_decreasing( + sprout::begin(arr2), + sprout::begin(arr2) + 5 + ); + TESTSPR_BOTH_ASSERT(!result); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_is_strictly_decreasing_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_STRICTLY_DECREASING_CPP diff --git a/libs/algorithm/test/is_strictly_increasing.cpp b/libs/algorithm/test/is_strictly_increasing.cpp new file mode 100644 index 00000000..4c38c8cb --- /dev/null +++ b/libs/algorithm/test/is_strictly_increasing.cpp @@ -0,0 +1,53 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_STRICTLY_INCREASING_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_IS_STRICTLY_INCREASING_CPP + +#include +#include +#include +#include + +namespace testspr { + static void algorithm_is_strictly_increasing_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 3, 5, 7, 9, 2, 4, 6, 8, 10}}; + SPROUT_STATIC_CONSTEXPR auto arr2 = array{{1, 1, 5, 7, 9, 2, 4, 6, 10, 10}}; + + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_increasing( + sprout::begin(arr1), + sprout::end(arr1) + ); + TESTSPR_BOTH_ASSERT(!result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_increasing( + sprout::begin(arr2), + sprout::end(arr2) + ); + TESTSPR_BOTH_ASSERT(!result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_increasing( + sprout::begin(arr1), + sprout::begin(arr1) + 5 + ); + TESTSPR_BOTH_ASSERT(result); + } + { + SPROUT_STATIC_CONSTEXPR auto result = sprout::is_strictly_increasing( + sprout::begin(arr2), + sprout::begin(arr2) + 5 + ); + TESTSPR_BOTH_ASSERT(!result); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_is_strictly_increasing_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_IS_STRICTLY_INCREASING_CPP diff --git a/libs/algorithm/test/non_modifying.cpp b/libs/algorithm/test/non_modifying.cpp index 7ac63545..341a3160 100644 --- a/libs/algorithm/test/non_modifying.cpp +++ b/libs/algorithm/test/non_modifying.cpp @@ -45,6 +45,11 @@ #include "./any_of_equal.cpp" #include "./none_of_equal.cpp" #include "./one_of_equal.cpp" +#include "./is_increasing.cpp" +#include "./is_decreasing.cpp" +#include "./is_strictly_increasing.cpp" +#include "./is_strictly_decreasing.cpp" +#include "./clamp.cpp" #ifdef TESTSPR_CPP_INCLUDE_DISABLE_SPROUT_LIBS_ALGORITHM_TEST_NON_MODIFYIING_CPP # undef TESTSPR_CPP_INCLUDE @@ -91,6 +96,11 @@ namespace testspr { testspr::algorithm_any_of_equal_test(); testspr::algorithm_none_of_equal_test(); testspr::algorithm_one_of_equal_test(); + testspr::algorithm_is_increasing_test(); + testspr::algorithm_is_decreasing_test(); + testspr::algorithm_is_strictly_increasing_test(); + testspr::algorithm_is_strictly_decreasing_test(); + testspr::algorithm_clamp_test(); } } // namespace testspr diff --git a/sprout/algorithm/is_decreasing.hpp b/sprout/algorithm/is_decreasing.hpp index 87a094e0..e674589b 100644 --- a/sprout/algorithm/is_decreasing.hpp +++ b/sprout/algorithm/is_decreasing.hpp @@ -10,6 +10,10 @@ namespace sprout { // // is_decreasing // + // recursion depth: + // [first, last) is RandomAccessIterator -> O(log N) + // otherwise -> O(N) + // template inline SPROUT_CONSTEXPR bool is_decreasing(ForwardIterator first, ForwardIterator last) { diff --git a/sprout/algorithm/is_increasing.hpp b/sprout/algorithm/is_increasing.hpp index 7f1506c9..247d3f25 100644 --- a/sprout/algorithm/is_increasing.hpp +++ b/sprout/algorithm/is_increasing.hpp @@ -10,6 +10,10 @@ namespace sprout { // // is_increasing // + // recursion depth: + // [first, last) is RandomAccessIterator -> O(log N) + // otherwise -> O(N) + // template inline SPROUT_CONSTEXPR bool is_increasing(ForwardIterator first, ForwardIterator last) { diff --git a/sprout/algorithm/is_strictly_decreasing.hpp b/sprout/algorithm/is_strictly_decreasing.hpp index 59a1f80d..cd183f2e 100644 --- a/sprout/algorithm/is_strictly_decreasing.hpp +++ b/sprout/algorithm/is_strictly_decreasing.hpp @@ -10,6 +10,10 @@ namespace sprout { // // is_strictly_decreasing // + // recursion depth: + // [first, last) is RandomAccessIterator -> O(log N) + // otherwise -> O(N) + // template inline SPROUT_CONSTEXPR bool is_strictly_decreasing(ForwardIterator first, ForwardIterator last) { diff --git a/sprout/algorithm/is_strictly_increasing.hpp b/sprout/algorithm/is_strictly_increasing.hpp index 331c1ca0..07f668a3 100644 --- a/sprout/algorithm/is_strictly_increasing.hpp +++ b/sprout/algorithm/is_strictly_increasing.hpp @@ -10,6 +10,10 @@ namespace sprout { // // is_strictly_increasing // + // recursion depth: + // [first, last) is RandomAccessIterator -> O(log N) + // otherwise -> O(N) + // template inline SPROUT_CONSTEXPR bool is_strictly_increasing(ForwardIterator first, ForwardIterator last) {