lightweight_test_all_with_test.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <cmath>
  11. #include <functional>
  12. #include <vector>
  13. #include <boost/core/lightweight_test.hpp>
  14. void test_vector()
  15. {
  16. {
  17. std::vector<int> x, y;
  18. x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 );
  19. y.push_back( 1 ); y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 );
  20. BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to<int>() );
  21. }
  22. {
  23. std::vector<int> x, y;
  24. x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 );
  25. y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); y.push_back( 5 );
  26. BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::not_equal_to<int>() );
  27. }
  28. {
  29. std::vector<int> x, y;
  30. x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 );
  31. y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); y.push_back( 5 );
  32. BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::less<int>() );
  33. }
  34. }
  35. template <typename T>
  36. struct with_tolerance
  37. {
  38. with_tolerance(T tolerance) : tolerance(tolerance) {}
  39. bool operator()(T lhs, T rhs)
  40. {
  41. return (std::abs(lhs - rhs) <= tolerance);
  42. }
  43. private:
  44. T tolerance;
  45. };
  46. void test_tolerance_predicate()
  47. {
  48. {
  49. std::vector<double> x, y;
  50. x.push_back( 1.0 ); x.push_back( 2.0 ); x.push_back( 3.0 ); x.push_back( 4.0 );
  51. y.push_back( 1.0 ); y.push_back( 2.0 ); y.push_back( 3.0 ); y.push_back( 4.0 );
  52. BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), with_tolerance<double>(1e-5) );
  53. }
  54. {
  55. std::vector<double> x, y;
  56. x.push_back( 1.0 ); x.push_back( 1.0 );
  57. y.push_back( 1.0 - 1e-6 ); y.push_back( 1.0 + 1e-6 );
  58. BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), with_tolerance<double>(1e-5) );
  59. }
  60. }
  61. int main()
  62. {
  63. test_vector();
  64. test_tolerance_predicate();
  65. return boost::report_errors();
  66. }