stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r366906 - /incubator/stdcxx/trunk/tests/algorithms/25.adjacent.find.cpp
Date Sat, 07 Jan 2006 20:39:38 GMT
Author: sebor
Date: Sat Jan  7 12:39:35 2006
New Revision: 366906

URL: http://svn.apache.org/viewcvs?rev=366906&view=rev
Log:
2006-01-07  Martin Sebor  <sebor@roguewave.com>

	STDCXX-72
	* 25.adjacent.find.cpp (EqualityPredicate): New class template.
	(do_test): Used it instead of std::equal_to.

Modified:
    incubator/stdcxx/trunk/tests/algorithms/25.adjacent.find.cpp

Modified: incubator/stdcxx/trunk/tests/algorithms/25.adjacent.find.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/algorithms/25.adjacent.find.cpp?rev=366906&r1=366905&r2=366906&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/algorithms/25.adjacent.find.cpp (original)
+++ incubator/stdcxx/trunk/tests/algorithms/25.adjacent.find.cpp Sat Jan  7 12:39:35 2006
@@ -20,7 +20,6 @@
  **************************************************************************/
 
 #include <algorithm>   // for adjacent_find()
-#include <functional>  // for equal_to
 #include <cstring>     // for size_t, strlen()
 
 #include <alg_test.h>
@@ -43,15 +42,47 @@
     return UChar (*cur++);
 }
 
+/**************************************************************************/
+
+template <class T, class U>
+struct EqualityPredicate
+{
+    static std::size_t funcalls_;
+
+    // dummy arguments provided to prevent the class
+    // from being default constructible
+    EqualityPredicate (T* /* dummy */, U* /* dummy */) {
+        funcalls_ = 0;
+    }
+
+    // return a type other than bool but one that is implicitly
+    // convertible to bool to detect incorrect assumptions
+    class ConvertibleToBool {
+        bool result_;
+    public:
+        ConvertibleToBool (bool res): result_ (res) { /* empty */ }
+        operator bool() const { return result_; }
+    };
+
+    ConvertibleToBool operator() (const T &x, const U &y) /* non-const */ {
+        ++funcalls_;
+        return x == y;
+    }
+};
+
+template <class T, class U>
+std::size_t EqualityPredicate<T, U>::funcalls_;
+
+/**************************************************************************/
 
 // exercises std::adjacent_find()
 template <class ForwardIterator, class T>
-void do_test (int         line,     // line number of test case
-              const char *src,      // source sequence
-              std::size_t resoff,   // offset of result
-              ForwardIterator    dummy_iter,
+void do_test (int             line,     // line number of test case
+              const char     *src,      // source sequence
+              std::size_t     resoff,   // offset of result
+              ForwardIterator dummy_iter,
               const T*,
-              const char* predicate)
+              const char*     predname)
 {
     static const char* const itname = type_name (dummy_iter, (T*)0);
 
@@ -72,18 +103,24 @@
     const ForwardIterator last =
         make_iter (xsrc + nsrc, xsrc, xsrc + nsrc, dummy_iter);
 
-    // compute the number of invocations of the predicate
-    std::size_t n_total_pred = X::n_total_op_eq_;
-
-    const ForwardIterator res =
-        predicate ? std::adjacent_find (first, last, std::equal_to<X>())
-                  : std::adjacent_find (first, last);
+    // reset predicate counters
+    X::n_total_op_eq_                  = 0;
+    EqualityPredicate<T, T>::funcalls_ = 0;
+
+    // construct a predicate object
+    const EqualityPredicate<T, T> pred (0, 0);
+
+    const ForwardIterator res = predname ?
+          std::adjacent_find (first, last, pred)
+        : std::adjacent_find (first, last);
 
     // silence a bogus EDG eccp remark #550-D:
     // variable "res" was set but never used
     _RWSTD_UNUSED (res);
 
-    n_total_pred = X::n_total_op_eq_ - n_total_pred;
+    const std::size_t n_total_pred = predname ?
+          EqualityPredicate<T, T>::funcalls_
+        : X::n_total_op_eq_;
 
     // verify that the returned iterator is set as expected
     int success = res.cur_ == first.cur_ + resoff;
@@ -117,22 +154,22 @@
                "line %d: adjacent_find<%s>(\"%s\", ...) "
                "invoked %s %zu times, expected %td",
                __LINE__, itname, src,
-               predicate ? predicate : "operator==()", 
+               predname ? predname : "operator==()", 
                n_total_pred, n_expect_pred);
 }
 
 /**************************************************************************/
 
 template <class ForwardIterator, class T>
-void run_tests (ForwardIterator dummy_iter, const T*, const char* predicate)
+void run_tests (ForwardIterator dummy_iter, const T*, const char* predname)
 {
     static const char* const itname = type_name (dummy_iter, (T*)0);
 
     rw_info (0, 0, 0, "std::adjacent_find (%s, %1$s%{?}, %s%{;})", 
-             itname, 0 != predicate, predicate);
+             itname, 0 != predname, predname);
     
 #define TEST(src, off) \
-    do_test (__LINE__, src, std::size_t (off), dummy_iter, (X*)0, predicate)
+    do_test (__LINE__, src, std::size_t (off), dummy_iter, (X*)0, predname)
 
     //    +------------------ subject sequence
     //    |               +-- offset of returned iterator,
@@ -183,33 +220,33 @@
 /* extern */ int rw_opt_no_predicate;    // --no-Predicate
 
 static 
-void test_adjacent_find (const char* predicate)
+void test_adjacent_find (const char* predname)
 {
     rw_info (0, 0, 0, 
              "template <class %s%{?}, class %s%{;}> "
              "%1$s std::adjacent_find (%1$s, %1$s%{?}, %2$s%{;})",
              "ForwardIterator",
-             0 != predicate, "BinaryPredicate", 0 != predicate);
+             0 != predname, "BinaryPredicate", 0 != predname);
 
     if (rw_opt_no_fwd_iter) {
         rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled");
     }
     else {
-        run_tests (FwdIter<X>(), (X*)0, predicate);
+        run_tests (FwdIter<X>(), (X*)0, predname);
     }
 
     if (rw_opt_no_bidir_iter) {
         rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled");
     }
     else {
-        run_tests (BidirIter<X>(), (X*)0, predicate);
+        run_tests (BidirIter<X>(), (X*)0, predname);
     }
 
     if (rw_opt_no_rnd_iter) {
         rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled");
     }
     else {
-        run_tests (RandomAccessIter<X>(), (X*)0, predicate);
+        run_tests (RandomAccessIter<X>(), (X*)0, predname);
     }
 }
 
@@ -224,7 +261,7 @@
         rw_note (0, __FILE__, __LINE__, "Predicate test disabled");
     }
     else {
-        test_adjacent_find ("std::equal_to<X>");
+        test_adjacent_find ("EqualityPredicate<X, X>");
     }
                 
     return 0;



Mime
View raw message