• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Test for BOOST_TEST_ALL_WITH
3 //
4 // Copyright (c) 2017 Bjorn Reese
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt
9 //
10 
11 #include <cmath>
12 #include <functional>
13 #include <vector>
14 #include <boost/core/lightweight_test.hpp>
15 
test_vector()16 void test_vector()
17 {
18     {
19         std::vector<int> x, y;
20         x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 );
21         y.push_back( 1 ); y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 );
22         BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to<int>() );
23     }
24 
25     {
26         std::vector<int> x, y;
27         x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 );
28         y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); y.push_back( 5 );
29         BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::not_equal_to<int>() );
30     }
31 
32     {
33         std::vector<int> x, y;
34         x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 );
35         y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); y.push_back( 5 );
36         BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::less<int>() );
37     }
38 }
39 
40 template <typename T>
41 struct with_tolerance
42 {
with_tolerancewith_tolerance43     with_tolerance(T tolerance) : tolerance(tolerance) {}
operator ()with_tolerance44     bool operator()(T lhs, T rhs)
45     {
46         return (std::abs(lhs - rhs) <= tolerance);
47     }
48 
49 private:
50     T tolerance;
51 };
52 
test_tolerance_predicate()53 void test_tolerance_predicate()
54 {
55     {
56         std::vector<double> x, y;
57         x.push_back( 1.0 ); x.push_back( 2.0 ); x.push_back( 3.0 ); x.push_back( 4.0 );
58         y.push_back( 1.0 ); y.push_back( 2.0 ); y.push_back( 3.0 ); y.push_back( 4.0 );
59         BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), with_tolerance<double>(1e-5) );
60     }
61 
62     {
63         std::vector<double> x, y;
64         x.push_back( 1.0 ); x.push_back( 1.0 );
65         y.push_back( 1.0 - 1e-6 ); y.push_back( 1.0 + 1e-6 );
66         BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), with_tolerance<double>(1e-5) );
67     }
68 }
69 
main()70 int main()
71 {
72     test_vector();
73     test_tolerance_predicate();
74 
75     return boost::report_errors();
76 }
77