diff --git a/CMakeLists.txt b/CMakeLists.txt index 67efe0a..97dc1b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ crap_test(hassinglebitvalue) crap_test(innerproductvalue) crap_test(isheapuntilltype) crap_test(isheapuntillvalue) +crap_test(isheapuvalue) crap_test(lowerboundtype) crap_test(lowerboundvalue) crap_test(mismatchtype) diff --git a/include/crap/algorithm.d/isheapvalue.h b/include/crap/algorithm.d/isheapvalue.h index 4644ff9..ae45be0 100644 --- a/include/crap/algorithm.d/isheapvalue.h +++ b/include/crap/algorithm.d/isheapvalue.h @@ -1,6 +1,8 @@ #ifndef CRAP_ALGORITHM_ISHEAPVALUE #define CRAP_ALGORITHM_ISHEAPVALUE +#include "../algorithm.d/findifvalue.h" +#include "../numeric.d/iotavalue.h" #include "../utility.d/valuelist.h" namespace crap @@ -46,13 +48,13 @@ namespace crap template class Operator, Type ... Values> struct isHeapValue { private: - template struct Implementation; - template struct Implementation; - template struct Implementation; - template struct Implementation; + template struct CheckIfFails; + template using CheckIndices = + findIfValue; + using Implementation = + iotaValue :: template type; public: - constexpr const static bool value = - Implementation <0u, (1u < sizeof...(Values)), (2u < sizeof...(Values))> :: value; + constexpr const static bool value = (Implementation :: value == Impementation :: npos); using value_type = decltype(value); constexpr operator value_type () const noexcept; }; @@ -73,97 +75,16 @@ namespace crap template class Operator, Type ... Values> template - struct isHeapValue :: Implementation - { - constexpr const static bool value = true; - }; - - template class Operator, Type ... Values> - template - struct isHeapValue :: Implementation - { - private: - constexpr const static Type parent = - valueList :: template At :: value; - constexpr const static Type leftChild = - valueList :: template At <(2u * Index) + 1u> :: value; - public: - constexpr const static bool value = !(Operator :: value); - }; - - template class Operator, Type ... Values> - template - struct isHeapValue :: Implementation + struct isHeapValue :: CheckIfFails { private: + constexpr const static std :: size_t parentIndex = (Index - 1u) / 2u; constexpr const static Type parent = + valueList :: template At :: value; + constexpr const static Type child = valueList :: template At :: value; - constexpr const static Type leftChild = - valueList :: template At <(2u * Index) + 1u> :: value; - template struct CheckRight; - template struct CheckRight; - template struct CheckRight; - template struct CheckSubTree; - template struct CheckSubTree; - template struct CheckSubTree; - constexpr const static bool rightFine = - CheckRight :: value), parent> :: value; - constexpr const static bool leftSubTreeFine = - CheckSubTree :: value; - public: - constexpr const static bool value = - CheckSubTree :: value; - }; - - template class Operator, Type ... Values> - template - template - struct isHeapValue :: - Implementation :: - CheckRight - { - private: - constexpr const static Type rightChild = - CheckRight :: value), parent> :: value; public: - constexpr const static bool value = !(Operator :: value); - }; - - template class Operator, Type ... Values> - template - template - struct isHeapValue :: - Implementation :: - CheckRight - { - constexpr const static bool value = false; - }; - - template class Operator, Type ... Values> - template - template - struct isHeapValue :: - Implementation :: - CheckSubTree -#if (!defined(__clang__) && defined(__GNUC__) && (__GNUC__ < 10)) - : isHeapValue :: template -#else - : isHeapValue :: -#endif - Implementation - { - }; - - template class Operator, Type ... Values> - template - template - struct isHeapValue :: - Implementation :: - CheckSubTree - { - constexpr const static bool value = false; + constexpr const static bool value = Operator :: value; }; } diff --git a/test/isheapvaluetest.cpp b/test/isheapvaluetest.cpp new file mode 100644 index 0000000..ec84b5c --- /dev/null +++ b/test/isheapvaluetest.cpp @@ -0,0 +1,55 @@ +#include "../include/crap/algorithm.d/isheapfortype.h" + +#include +#include +#include +#include + +#include "testutils.h" +#include "../include/crap/functional.d/comparatorsfortype.h" + +bool test_isHeapValueTrivialPassingTest() +{ + using valueTestType = unsigned int; + + //TODO: Make sequence random. + using testList1 = + crap :: valueList; + using testedFun = crap :: isHeapForType< + valueTestType, + lessConstrainedForType :: template type>; + using testResult_t = typename testList1 :: copy ; + constexpr const static auto testResult = testResult_t :: value; + static_assert(testResult, "Set is a heap!"); + return true; +} + +bool test_isHeapValueTrivialFailingTest() +{ + using valueTestType = unsigned int; + constexpr const static valueTestType Constrain = 42u; + constexpr const static valueTestType Subject = 101u; + + //TODO: Make sequence random. + using testList1 = + crap :: valueList; + using testedFun = crap :: isHeapForType< + valueTestType, + crap :: comparatorsForType :: template Less>; + using testResult_t = typename testList1 :: copy ; + constexpr const static auto testResult = testResult_t :: value; + static_assert(!testResult, "Set is not a heap!"); + return true; +} + +int main() +{ + const bool results[] = { + test_isHeapValueTrivialPassingTest(), + test_isHeapValueTrivialFailingTest() + }; + return std :: accumulate(std :: begin(results), std :: end(results), true, std :: logical_and()) + ? EXIT_SUCCESS + : EXIT_FAILURE; +} +