[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master b73b1b4 3/4: Add an each_equal() overload and
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master b73b1b4 3/4: Add an each_equal() overload and unit test |
Date: |
Sun, 22 Jan 2017 01:55:05 +0000 (UTC) |
branch: master
commit b73b1b4a52390e9292d295b620191cbb0ca0f4a5
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Add an each_equal() overload and unit test
See:
http://lists.nongnu.org/archive/html/lmi/2017-01/msg00098.html
---
miscellany.hpp | 12 ++++++++++++
miscellany_test.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+)
diff --git a/miscellany.hpp b/miscellany.hpp
index 3aae000..9343a30 100644
--- a/miscellany.hpp
+++ b/miscellany.hpp
@@ -45,6 +45,18 @@ bool each_equal(InputIterator first, InputIterator last, T
const& t)
return std::distance(first, last) == std::count(first, last, t);
}
+/// Test whether every element in a range equals the specified constant.
+
+template<typename RangeExpression, typename T>
+bool each_equal(RangeExpression const& range, T const& t)
+{
+ for(auto const& i : range)
+ {
+ if(t != i) return false;
+ }
+ return true;
+}
+
/// Test whether two files are identical. Arguments are filenames.
bool files_are_identical(std::string const&, std::string const&);
diff --git a/miscellany_test.cpp b/miscellany_test.cpp
index e8fb104..0b9d0ee 100644
--- a/miscellany_test.cpp
+++ b/miscellany_test.cpp
@@ -28,6 +28,51 @@
#include <cstdio> // std::remove()
#include <fstream>
+void test_each_equal()
+{
+ int a0[] {0, 0, 0, 0};
+ int const a1[] {0, 1, 1, 1};
+ int volatile a2[] {0, 1, 2, 2};
+ int const volatile a3[] {0, 1, 2, 3};
+
+ // There can be no volatile standard container.
+ std::vector<int> v0 {0, 0, 0, 0};
+ std::vector<int> const v1 {0, 1, 1, 1};
+ std::vector<int> v2 {0, 1, 2, 2};
+ std::vector<int> const v3 {0, 1, 2, 3};
+
+ // Test with containers.
+
+ BOOST_TEST( each_equal(a0, 0));
+ BOOST_TEST(!each_equal(a1, 0));
+ BOOST_TEST(!each_equal(a2, 0));
+ BOOST_TEST(!each_equal(a3, 0));
+
+ BOOST_TEST( each_equal(v0, 0));
+ BOOST_TEST(!each_equal(v1, 0));
+ BOOST_TEST(!each_equal(v2, 0));
+ BOOST_TEST(!each_equal(v3, 0));
+
+ // Test with iterators.
+
+ BOOST_TEST( each_equal(v0.begin(), v0.end(), 0));
+ BOOST_TEST(!each_equal(v1.begin(), v1.end(), 0));
+ BOOST_TEST(!each_equal(v2.begin(), v2.end(), 0));
+ BOOST_TEST(!each_equal(v3.begin(), v3.end(), 0));
+
+ // Iterators are more flexible, of course.
+
+ BOOST_TEST( each_equal(v0.begin() + 0, v0.end(), 0));
+ BOOST_TEST( each_equal(v1.begin() + 1, v1.end(), 1));
+ BOOST_TEST( each_equal(v2.begin() + 2, v2.end(), 2));
+ BOOST_TEST( each_equal(v3.begin() + 3, v3.end(), 3));
+
+ // Iterators are also more prone to error.
+ // This crashes with g++-mingw-w64-i686 4.9.1:
+
+// BOOST_TEST( each_equal(v0.begin() + 7, v0.end(), 0));
+}
+
void test_files_are_identical()
{
char const* f0("unlikely_file_name_0");
@@ -178,6 +223,7 @@ void test_trimming()
int test_main(int, char*[])
{
+ test_each_equal();
test_files_are_identical();
test_minmax();
test_prefix_and_suffix();