Skip to content

Commit

Permalink
Add additional comparison function objects into functional header
Browse files Browse the repository at this point in the history
Change-Id: I6314ec8582e1d9cc284adf877e115433e95a168e
Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/58217
Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP Build CI <op-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP HW <op-hw-jenkins+hostboot@us.ibm.com>
Tested-by: FSP CI Jenkins <fsp-CI-jenkins+hostboot@us.ibm.com>
Reviewed-by: Daniel M. Crowell <dcrowell@us.ibm.com>
  • Loading branch information
aamarin authored and dcrowell77 committed May 11, 2018
1 parent e33bd00 commit 89bbfaf
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 31 deletions.
138 changes: 108 additions & 30 deletions src/include/functional
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/include/functional.C $ */
/* $Source: src/include/functional $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2011,2014 */
/* Contributors Listed Below - COPYRIGHT 2011,2018 */
/* [+] International Business Machines Corp. */
/* */
/* */
Expand All @@ -30,26 +30,27 @@
namespace std
{
template<typename A1, typename A2, typename R>
struct binary_function
{
typedef A1 first_argument_type; ///< type of the first argument
typedef A2 second_argument_type; ///< type of the second argument
typedef R result_type; ///< type of the return type
};
struct binary_function
{
typedef A1 first_argument_type; ///< type of the first argument
typedef A2 second_argument_type; ///< type of the second argument
typedef R result_type; ///< type of the return type
};

template<typename A, typename R>
struct unary_function
{
typedef A argument_type;
typedef R result_type;
};
struct unary_function
{
typedef A argument_type;
typedef R result_type;
};

/**
* less template
*/
template<typename T>
struct less : public binary_function<T, T, bool>
struct less : public binary_function<T, T, bool>
{

/**
* operator()
* @param[in] x first object
Expand All @@ -62,8 +63,29 @@ namespace std
}
};

/**
* less_equal template
*/
template<typename T>
struct less_equal : public binary_function<T, T, bool>
{
/**
* operator()
* @param[in] x first object
* @param[in] y second object
* @return true if x <= y otherwise false
*/
bool operator()(const T& x, const T& y) const
{
return x <= y;
}
};

/**
* greater template
*/
template<typename T>
struct greater : public binary_function<T, T, bool>
struct greater : public binary_function<T, T, bool>
{
/**
* operator()
Expand All @@ -72,7 +94,63 @@ namespace std
* @return true if x > y otherwise false
*/
bool operator()(const T& x, const T& y) const
{ return x > y; }
{
return x > y;
}
};

/**
* greater_equal template
*/
template<typename T>
struct greater_equal : public binary_function<T, T, bool>
{
/**
* operator()
* @param[in] x first object
* @param[in] y second object
* @return true if x >= y otherwise false
*/
bool operator()(const T& x, const T& y) const
{
return x >= y;
}
};

/**
* not_equal_to template
*/
template<typename T>
struct not_equal_to : public binary_function<T, T, bool>
{
/**
* operator()
* @param[in] x first object
* @param[in] y second object
* @return true if x != y otherwise false
*/
bool operator()(const T& x, const T& y) const
{
return x != y;
}
};

/**
* equal_to template
*/
template<typename T>
struct equal_to : public binary_function<T, T, bool>
{
/**
* operator()
* @param[in] x first object
* @param[in] y second object
* @return true if x == y otherwise false
*/
bool operator()(const T& x, const T& y) const
{
return x == y;
}
};

// --------------- ptr_fun templates --------------- //
Expand Down Expand Up @@ -130,7 +208,7 @@ namespace std
protected:
Result (X::*func)() const;
};

template<typename Result, typename X>
mem_fun_t<Result, X> mem_fun(Result (X::*f)())
{
Expand Down Expand Up @@ -198,7 +276,7 @@ namespace std
protected:
Result (X::*func)() const;
};

template<typename Result, typename X>
mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)())
{
Expand Down Expand Up @@ -248,20 +326,20 @@ namespace std
// --------------- bind1st templates --------------- //

template<typename AdaptableBinaryFunction>
struct binder1st :
struct binder1st :
public unary_function<
typename AdaptableBinaryFunction::second_argument_type,
typename AdaptableBinaryFunction::second_argument_type,
typename AdaptableBinaryFunction::result_type
>
{
binder1st(const AdaptableBinaryFunction& F,
typename AdaptableBinaryFunction::first_argument_type c) :
func(F), arg(c) {}

typename AdaptableBinaryFunction::result_type
operator()(const typename
typename AdaptableBinaryFunction::result_type
operator()(const typename
AdaptableBinaryFunction::second_argument_type& x) const
{
{
return func(arg, x);
}

Expand All @@ -275,27 +353,27 @@ namespace std
bind1st(const AdaptableBinaryFunction& F,
const T& c)
{
return binder1st<AdaptableBinaryFunction>(F,
return binder1st<AdaptableBinaryFunction>(F,
typename AdaptableBinaryFunction::first_argument_type(c));
};

// --------------- bind2nd templates --------------- //

template<typename AdaptableBinaryFunction>
struct binder2nd :
struct binder2nd :
public unary_function<
typename AdaptableBinaryFunction::first_argument_type,
typename AdaptableBinaryFunction::first_argument_type,
typename AdaptableBinaryFunction::result_type
>
{
binder2nd(const AdaptableBinaryFunction& F,
typename AdaptableBinaryFunction::second_argument_type c) :
func(F), arg(c) {}

typename AdaptableBinaryFunction::result_type
operator()(const typename
typename AdaptableBinaryFunction::result_type
operator()(const typename
AdaptableBinaryFunction::first_argument_type& x) const
{
{
return func(x, arg);
}

Expand All @@ -309,7 +387,7 @@ namespace std
bind2nd(const AdaptableBinaryFunction& F,
const T& c)
{
return binder2nd<AdaptableBinaryFunction>(F,
return binder2nd<AdaptableBinaryFunction>(F,
typename AdaptableBinaryFunction::second_argument_type(c));
};

Expand Down
62 changes: 61 additions & 1 deletion src/usr/testcore/lib/stl_functional.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2014,2016 */
/* Contributors Listed Below - COPYRIGHT 2014,2018 */
/* [+] International Business Machines Corp. */
/* */
/* */
Expand All @@ -22,6 +22,9 @@
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */



#ifndef __LIB_STL_FUNCTIONAL_H
#define __LIB_STL_FUNCTIONAL_H

Expand Down Expand Up @@ -169,6 +172,63 @@ class STLFunctionalTest : public CxxTest::TestSuite

}

void test_lessFun()
{
TS_ASSERT( std::less<uint32_t>()(1,2) );
TS_ASSERT( std::less<uint16_t>()(5,20) );
TS_ASSERT( !std::less<uint8_t>()(100,2) );
TS_ASSERT( !std::less<int>()(-40,-40) );
TS_ASSERT( !std::less<int32_t>()(-40,-30) );
}

void test_lessEqualFun()
{
TS_ASSERT( std::less_equal<int>()(-40,-40) );
TS_ASSERT( std::less_equal<uint8_t>()(4,4) );
TS_ASSERT( std::less_equal<uint8_t>()(4,200) );
TS_ASSERT( std::less_equal<uint32_t>()(5,20) );
TS_ASSERT( !std::less_equal<uint16_t>()(300,50) );
}

void test_greaterFun()
{
TS_ASSERT( !std::greater<int>()(-40,-40) );
TS_ASSERT( !std::greater<uint8_t>()(4,4) );
TS_ASSERT( !std::greater<uint8_t>()(4,200) );
TS_ASSERT( !std::greater<uint32_t>()(5,20) );
TS_ASSERT( std::greater<int64_t>()(-5,-20) );
TS_ASSERT( std::greater<uint16_t>()(300,50) );
}

void test_greaterEqualFun()
{
TS_ASSERT( std::greater_equal<int>()(-40,-40) );
TS_ASSERT( std::greater_equal<uint8_t>()(4,4) );
TS_ASSERT( !std::greater_equal<uint8_t>()(4,200) );
TS_ASSERT( !std::greater_equal<uint32_t>()(5,20) );
TS_ASSERT( std::greater_equal<int64_t>()(-5,-20) );
TS_ASSERT( std::greater_equal<uint16_t>()(300,50) );
}

void test_equalTolFun()
{
TS_ASSERT( std::equal_to<int>()(-40,-40) );
TS_ASSERT( std::equal_to<uint8_t>()(4,4) );
TS_ASSERT( !std::equal_to<uint8_t>()(4,200) );
TS_ASSERT( !std::equal_to<uint32_t>()(5,20) );
TS_ASSERT( !std::equal_to<int64_t>()(-5,-20) );
TS_ASSERT( !std::equal_to<uint16_t>()(300,50) );
}

void test_NotEqualTolFun()
{
TS_ASSERT( std::not_equal_to<int>()(-40,-40) );
TS_ASSERT( std::not_equal_to<uint8_t>()(4,4) );
TS_ASSERT( !std::not_equal_to<uint8_t>()(4,200) );
TS_ASSERT( !std::not_equal_to<uint32_t>()(5,20) );
TS_ASSERT( !std::not_equal_to<int64_t>()(-5,-20) );
TS_ASSERT( !std::not_equal_to<uint16_t>()(300,50) );
}
};

#endif

0 comments on commit 89bbfaf

Please sign in to comment.