mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-04 14:14:09 +00:00
add test: some algorithms
This commit is contained in:
parent
e2b207d3be
commit
830fc27394
8 changed files with 176 additions and 12 deletions
|
@ -29,6 +29,46 @@ namespace testspr {
|
||||||
);
|
);
|
||||||
TESTSPR_BOTH_ASSERT(!result);
|
TESTSPR_BOTH_ASSERT(!result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto result = sprout::binary_search(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(result);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto result = sprout::binary_search(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(!result);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto result = sprout::binary_search(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(result);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto result = sprout::binary_search(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(!result);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace testspr
|
} // namespace testspr
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <sprout/algorithm/equal_range.hpp>
|
#include <sprout/algorithm/equal_range.hpp>
|
||||||
#include <sprout/array.hpp>
|
#include <sprout/array.hpp>
|
||||||
#include <sprout/container.hpp>
|
#include <sprout/container.hpp>
|
||||||
|
#include <sprout/iterator.hpp>
|
||||||
#include <testspr/tools.hpp>
|
#include <testspr/tools.hpp>
|
||||||
|
|
||||||
namespace testspr {
|
namespace testspr {
|
||||||
|
@ -20,7 +21,7 @@ namespace testspr {
|
||||||
);
|
);
|
||||||
TESTSPR_BOTH_ASSERT(found.first == sprout::begin(arr1) + 6);
|
TESTSPR_BOTH_ASSERT(found.first == sprout::begin(arr1) + 6);
|
||||||
TESTSPR_BOTH_ASSERT(found.second == sprout::begin(arr1) + 9);
|
TESTSPR_BOTH_ASSERT(found.second == sprout::begin(arr1) + 9);
|
||||||
TESTSPR_BOTH_ASSERT(found.second - found.first == 3);
|
TESTSPR_BOTH_ASSERT(sprout::distance(found.first, found.second) == 3);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
SPROUT_STATIC_CONSTEXPR auto found = sprout::equal_range(
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::equal_range(
|
||||||
|
@ -31,8 +32,54 @@ namespace testspr {
|
||||||
);
|
);
|
||||||
TESTSPR_BOTH_ASSERT(found.first == sprout::begin(arr1) + 5);
|
TESTSPR_BOTH_ASSERT(found.first == sprout::begin(arr1) + 5);
|
||||||
TESTSPR_BOTH_ASSERT(found.second == sprout::begin(arr1) + 5);
|
TESTSPR_BOTH_ASSERT(found.second == sprout::begin(arr1) + 5);
|
||||||
TESTSPR_BOTH_ASSERT(found.second - found.first == 0);
|
TESTSPR_BOTH_ASSERT(sprout::distance(found.first, found.second) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::equal_range(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.first.base() == sprout::begin(arr1) + 6);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.second.base() == sprout::begin(arr1) + 9);
|
||||||
|
TESTSPR_BOTH_ASSERT(sprout::distance(found.first.base(), found.second.base()) == 3);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::equal_range(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.first.base() == sprout::begin(arr1) + 5);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.second.base() == sprout::begin(arr1) + 5);
|
||||||
|
TESTSPR_BOTH_ASSERT(sprout::distance(found.first.base(), found.second.base()) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::equal_range(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.first.base() == sprout::begin(arr1) + 6);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.second.base() == sprout::begin(arr1) + 9);
|
||||||
|
TESTSPR_BOTH_ASSERT(sprout::distance(found.first.base(), found.second.base()) == 3);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::equal_range(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.first.base() == sprout::begin(arr1) + 5);
|
||||||
|
TESTSPR_BOTH_ASSERT(found.second.base() == sprout::begin(arr1) + 5);
|
||||||
|
TESTSPR_BOTH_ASSERT(sprout::distance(found.first.base(), found.second.base()) == 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace testspr
|
} // namespace testspr
|
||||||
|
|
|
@ -29,6 +29,44 @@ namespace testspr {
|
||||||
);
|
);
|
||||||
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::lower_bound(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 6);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::lower_bound(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::lower_bound(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 6);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::lower_bound(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace testspr
|
} // namespace testspr
|
||||||
|
|
|
@ -29,6 +29,44 @@ namespace testspr {
|
||||||
);
|
);
|
||||||
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::upper_bound(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 9);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::upper_bound(
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_forward(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::upper_bound(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::end(arr1)),
|
||||||
|
7
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 9);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
SPROUT_STATIC_CONSTEXPR auto found = sprout::upper_bound(
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1)),
|
||||||
|
testspr::reduct_random_access(sprout::begin(arr1) + 5),
|
||||||
|
7,
|
||||||
|
testspr::less<int>()
|
||||||
|
).base();
|
||||||
|
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace testspr
|
} // namespace testspr
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|
#ifndef SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|
||||||
#define SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|
#define SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/algorithm/lower_bound.hpp>
|
#include <sprout/algorithm/lower_bound.hpp>
|
||||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||||
|
@ -17,8 +18,7 @@ namespace sprout {
|
||||||
// 25.4.3.4 binary_search
|
// 25.4.3.4 binary_search
|
||||||
//
|
//
|
||||||
// recursion depth:
|
// recursion depth:
|
||||||
// [first, last) is RandomAccessIterator -> O(log N)
|
// O(log N)
|
||||||
// otherwise -> O(N)
|
|
||||||
//
|
//
|
||||||
template<typename ForwardIterator, typename T, typename Compare>
|
template<typename ForwardIterator, typename T, typename Compare>
|
||||||
inline SPROUT_CONSTEXPR bool
|
inline SPROUT_CONSTEXPR bool
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef SPROUT_ALGORITHM_EQUAL_RANGE_HPP
|
#ifndef SPROUT_ALGORITHM_EQUAL_RANGE_HPP
|
||||||
#define SPROUT_ALGORITHM_EQUAL_RANGE_HPP
|
#define SPROUT_ALGORITHM_EQUAL_RANGE_HPP
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/algorithm/lower_bound.hpp>
|
#include <sprout/algorithm/lower_bound.hpp>
|
||||||
#include <sprout/algorithm/upper_bound.hpp>
|
#include <sprout/algorithm/upper_bound.hpp>
|
||||||
|
@ -13,13 +14,15 @@ namespace sprout {
|
||||||
// 25.4.3.3 equal_range
|
// 25.4.3.3 equal_range
|
||||||
//
|
//
|
||||||
// recursion depth:
|
// recursion depth:
|
||||||
// [first, last) is RandomAccessIterator -> O(log N)
|
// O(log N)
|
||||||
// otherwise -> O(N)
|
|
||||||
//
|
//
|
||||||
template<typename ForwardIterator, typename T, typename Compare>
|
template<typename ForwardIterator, typename T, typename Compare>
|
||||||
inline SPROUT_CONSTEXPR pair<ForwardIterator, ForwardIterator>
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
||||||
equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||||
return {sprout::lower_bound(first, last, value, comp), sprout::upper_bound(first, last, value, comp)};
|
return sprout::pair<ForwardIterator, ForwardIterator>{
|
||||||
|
sprout::lower_bound(first, last, value, comp),
|
||||||
|
sprout::upper_bound(first, last, value, comp)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ForwardIterator, typename T>
|
template<typename ForwardIterator, typename T>
|
||||||
|
|
|
@ -12,8 +12,7 @@ namespace sprout {
|
||||||
// 25.4.3.1 lower_bound
|
// 25.4.3.1 lower_bound
|
||||||
//
|
//
|
||||||
// recursion depth:
|
// recursion depth:
|
||||||
// [first, last) is RandomAccessIterator -> O(log N)
|
// O(log N)
|
||||||
// otherwise -> O(N)
|
|
||||||
//
|
//
|
||||||
template<typename ForwardIterator, typename T, typename Compare>
|
template<typename ForwardIterator, typename T, typename Compare>
|
||||||
inline SPROUT_CONSTEXPR ForwardIterator
|
inline SPROUT_CONSTEXPR ForwardIterator
|
||||||
|
|
|
@ -12,8 +12,7 @@ namespace sprout {
|
||||||
// 25.4.3.2 upper_bound
|
// 25.4.3.2 upper_bound
|
||||||
//
|
//
|
||||||
// recursion depth:
|
// recursion depth:
|
||||||
// [first, last) is RandomAccessIterator -> O(log N)
|
// O(log N)
|
||||||
// otherwise -> O(N)
|
|
||||||
//
|
//
|
||||||
template<typename ForwardIterator, typename T, typename Compare>
|
template<typename ForwardIterator, typename T, typename Compare>
|
||||||
inline SPROUT_CONSTEXPR ForwardIterator
|
inline SPROUT_CONSTEXPR ForwardIterator
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue