Add identity function object

This commit is contained in:
Glen Fernandes
2021-12-12 14:15:52 -05:00
parent 09d7a311a5
commit 0be868143d
6 changed files with 241 additions and 0 deletions
+2
View File
@@ -6,4 +6,6 @@ import testing ;
test-suite functional :
[ run function_test.cpp ]
[ run identity_test.cpp ]
[ run identity_rvalue_test.cpp ]
;
+75
View File
@@ -0,0 +1,75 @@
/*
Copyright 2021 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#include <boost/functional/identity.hpp>
#include <boost/core/lightweight_test.hpp>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
enum kind {
rvalue,
crvalue,
other
};
kind result(std::string&&)
{
return rvalue;
}
kind result(const std::string&&)
{
return crvalue;
}
template<class T>
kind result(T&&)
{
return other;
}
void simple_test()
{
typedef std::string string;
BOOST_TEST(boost::identity()(string("a")) == string("a"));
BOOST_TEST(result(boost::identity()(string("a"))) == rvalue);
typedef const std::string cstring;
BOOST_TEST(boost::identity()(cstring("a")) == cstring("a"));
BOOST_TEST(result(boost::identity()(cstring("a"))) == crvalue);
}
void algorithm_test()
{
std::vector<std::string> v1;
v1.push_back(std::string("a"));
v1.push_back(std::string("b"));
v1.push_back(std::string("c"));
std::vector<std::string> v2(v1);
std::vector<std::string> v3;
std::transform(std::make_move_iterator(v2.begin()),
std::make_move_iterator(v2.end()),
std::back_inserter(v3),
boost::identity());
BOOST_TEST(v3 == v1);
}
int main()
{
simple_test();
algorithm_test();
return boost::report_errors();
}
#else
int main()
{
return 0;
}
#endif
+72
View File
@@ -0,0 +1,72 @@
/*
Copyright 2021 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/functional/identity.hpp>
#include <boost/core/lightweight_test.hpp>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
enum kind {
ref,
cref,
other
};
kind result(std::string&)
{
return ref;
}
kind result(const std::string&)
{
return cref;
}
template<class T>
kind result(T&)
{
return other;
}
template<class T>
kind result(const T&)
{
return other;
}
void simple_test()
{
std::string s1("a");
BOOST_TEST(boost::identity()(s1) == s1);
BOOST_TEST(&boost::identity()(s1) == &s1);
BOOST_TEST(result(boost::identity()(s1)) == ref);
const std::string s2("a");
BOOST_TEST(boost::identity()(s2) == s2);
BOOST_TEST(&boost::identity()(s2) == &s2);
BOOST_TEST(result(boost::identity()(s2)) == cref);
}
void algorithm_test()
{
std::vector<std::string> v1;
v1.push_back(std::string("a"));
v1.push_back(std::string("b"));
v1.push_back(std::string("c"));
std::vector<std::string> v2;
std::transform(v1.begin(), v1.end(), std::back_inserter(v2),
boost::identity());
BOOST_TEST(v2 == v1);
}
int main()
{
simple_test();
algorithm_test();
return boost::report_errors();
}