From stdcxx-commits-return-269-apmail-incubator-stdcxx-commits-archive=incubator.apache.org@incubator.apache.org Mon Jan 02 00:19:34 2006 Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 79785 invoked from network); 2 Jan 2006 00:19:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 2 Jan 2006 00:19:34 -0000 Received: (qmail 78695 invoked by uid 500); 2 Jan 2006 00:19:34 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 78682 invoked by uid 500); 2 Jan 2006 00:19:34 -0000 Mailing-List: contact stdcxx-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-commits@incubator.apache.org Received: (qmail 78671 invoked by uid 500); 2 Jan 2006 00:19:34 -0000 Delivered-To: apmail-incubator-stdcxx-cvs@incubator.apache.org Received: (qmail 78668 invoked by uid 99); 2 Jan 2006 00:19:34 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 01 Jan 2006 16:19:34 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 01 Jan 2006 16:19:33 -0800 Received: (qmail 79766 invoked by uid 65534); 2 Jan 2006 00:19:13 -0000 Message-ID: <20060102001912.79765.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r360601 - /incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp Date: Mon, 02 Jan 2006 00:19:12 -0000 To: stdcxx-cvs@incubator.apache.org From: sebor@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: sebor Date: Sun Jan 1 16:19:08 2006 New Revision: 360601 URL: http://svn.apache.org/viewcvs?rev=360601&view=rev Log: 2006-01-01 Martin Sebor STDCXX-4 * 25.upper.bound.cpp: New test exercising lib.upper.bound. Added: incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp (with props) Added: incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp?rev=360601&view=auto ============================================================================== --- incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp (added) +++ incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp Sun Jan 1 16:19:08 2006 @@ -0,0 +1,286 @@ +/*************************************************************************** + * + * 25.upper.bound.cpp - test exercising 25.3.3.2 [lib.upper.bound] + * + * $Id$ + * + *************************************************************************** + * + * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave + * Software division. Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. Unless required by + * applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under + * the License. + * + **************************************************************************/ + +#include + +#if defined (__IBMCPP__) && !defined (_RWSTD_NO_IMPLICIT_INCLUSION) + // disable implicit inclusion to work around a limitation + // of IBM VisualAge 5.0 (see PR #26959) +# define _RWSTD_NO_IMPLICIT_INCLUSION +#endif + +#include // for upper_bound() +#include // for strlen() + +#include // for iterators +#include // for rw_test(), ... + +/**************************************************************************/ + +// distinct and not Less-Than-Comparable with class X (except as +// defined below) to detect unwarranted assumptions made by the +// implementation of the algorithms +struct Y +{ + X xval_; + + // not Default-Constructible + Y (int /* dummy */, int /*dummy */): xval_ () { } + + // CopyConstructible + Y (const Y &rhs): xval_ (rhs.xval_) { } + +private: + + void operator=(Y&); // not Assignable +}; + + +inline conv_to_bool +operator< (const X &lhs, const Y &rhs) +{ + return conv_to_bool::make (lhs < rhs.xval_); +} + +inline conv_to_bool +operator< (const Y &lhs, const X &rhs) +{ + return conv_to_bool::make (lhs.xval_ < rhs); +} + +// predicate used as the Compare argument to upper_bound() +struct LessThan +{ + static std::size_t funcalls_; + + LessThan (int /* dummy */, int /* dummy */) { + funcalls_ = 0; + } + + // return a type other than bool but one that is implicitly + // convertible to bool to detect incorrect assumptions + conv_to_bool operator() (const X &lhs, const Y &rhs) { + ++funcalls_; + return conv_to_bool::make (lhs < rhs.xval_); + } + + conv_to_bool operator() (const Y &lhs, const X &rhs) { + ++funcalls_; + return conv_to_bool::make (lhs.xval_ < rhs); + } + +private: + void operator= (LessThan&); // not assignable +}; + +std::size_t LessThan::funcalls_; + +/**************************************************************************/ + +template +void test_upper_bound (int line, + const char *src_str, + char val_char, + std::size_t result_off, + std::size_t ncomp, + const ForwardIterator*, + const char *itname, + bool predicate) +{ + RW_ASSERT (0 != src_str); + + const std::size_t nsrc = std::strlen (src_str); + X* const xsrc = X::from_char (src_str, nsrc); + + if (nsrc < result_off) + result_off = nsrc; + + // construct the source range to pass to the algorithm + const ForwardIterator first (xsrc, xsrc, xsrc + nsrc); + const ForwardIterator last (xsrc + nsrc, xsrc + nsrc, xsrc + nsrc); + + // construct the object to pass to the algorithm + // the type of the object is distinct from the iterator's value_type + // to detect unwarranted assumptions made by the implementation + Y value (0, 0 /* dummy arguments */); + value.xval_.val_ = val_char; + + // construct the Compare function object to pass to the algorithm + // when `predicate' is true + const LessThan comp (0, 0 /* dummy arguments */); + + // reset the counter of invocations of X::operator<() + X::n_total_op_lt_ = 0; + + // invoke the appropriate form of the algorithm, storing + // the resturned value + const ForwardIterator result = predicate + ? std::upper_bound (first, last, value, comp) + : std::upper_bound (first, last, value); + + // silence bogus EDG eccp 3.6 remark #550-D: + // variable was set but never used + _RWSTD_UNUSED (result); + + // verify correctness + const std::size_t off = std::size_t (result.cur_ - xsrc); + + rw_assert (off == result_off, 0, line, + "upper_bound(%s = \"%s\", ...%{?}%#c%{;}) ==> " + "first + %zu, got %zu", + itname, src_str, !predicate, val_char, + off, result_off); + + // verify complexity + const std::size_t funcalls = predicate + ? LessThan::funcalls_ : X::n_total_op_lt_; + + rw_assert (off == result_off, 0, line, + "upper_bound(%s = \"%s\", ...%{?}%#c%{;}) complexity: " + "invoked predicate at most %zu times, got %zu", + itname, src_str, !predicate, val_char, + funcalls, ncomp); + + delete[] xsrc; +} + +/**************************************************************************/ + +template +void test_upper_bound (const ForwardIterator*, + const char *itname, + bool predicate) +{ +#define TEST(str, val, off, comp) \ + test_upper_bound (__LINE__, str, val, std::size_t (off), \ + std::size_t (comp), (ForwardIterator*)0, \ + itname, predicate) + + rw_info (0, 0, 0, "std::upper_bound (%s, %1$s, const X&%{?}, %s%{;})", + itname, predicate, "LessThan"); + + // +--------------- source sequence + // | +--------- value argument + // | | +----- offset of the resturned iterator, -1 for end + // | | | +-- complexity: at most (log(last - first) + 1) + // | | | | comparisons (or applications of the predicate) + // | | | | + // V V V V + TEST ("", 'a', 0, 0); + TEST ("a", 'a', 1, 1); + TEST ("a", 'b', 1, 1); + TEST ("b", 'a', 0, 1); + + TEST ("aa", 'a', 2, 2); + TEST ("ab", 'a', 1, 2); + TEST ("ab", 'b', 2, 2); + TEST ("bb", 'a', 0, 1); + + TEST ("ace", 'a', 1, 2); + TEST ("ace", 'b', 1, 2); + TEST ("ace", 'c', 2, 3); + TEST ("ace", 'd', 2, 3); + TEST ("ace", 'e', 3, 3); + TEST ("ace", 'f', 3, 3); + + TEST ("aceg", 'a', 1, 2); + TEST ("aceg", 'b', 1, 2); + TEST ("aceg", 'c', 2, 3); + TEST ("aceg", 'd', 2, 3); + TEST ("aceg", 'e', 3, 4); + TEST ("aceg", 'f', 3, 4); + TEST ("aceg", 'g', 4, 5); + TEST ("aceg", 'h', 4, 5); +} + +/**************************************************************************/ + +static int rw_opt_no_fwd_iter; +static int rw_opt_no_bidir_iter; +static int rw_opt_no_rnd_iter; +static int rw_opt_no_predicate; + + +static void +test_upper_bound (bool predicate) +{ + rw_info (0, 0, 0, "template " + "std::upper_bound (%1$s, %1$s, const X&%{?}, %3$s%{;})", + "ForwardIterator", "X", predicate, "Compare", predicate); + + if (rw_opt_no_fwd_iter) { + rw_note (0, 0, 0, "ForwardIterator test disabled"); + } + else { + test_upper_bound ((FwdIter*)0, "ForwardIterator", predicate); + } + + if (rw_opt_no_bidir_iter) { + rw_note (0, 0, 0, "BidirectionalIterator test disabled"); + } + else { + test_upper_bound ((BidirIter*)0, "BidirectionalIterator", predicate); + } + + if (rw_opt_no_fwd_iter) { + rw_note (0, 0, 0, "RandomAccessIterator test disabled"); + } + else { + test_upper_bound ((RandomAccessIter*)0, "RandomAccessIterator", + predicate); + } +} + +/**************************************************************************/ + +static int +run_test (int, char*[]) +{ + test_upper_bound (false); + + if (rw_opt_no_predicate) { + rw_note (0, __FILE__, __LINE__, + "test of the Predicate form of std::upper_bound disabled"); + } + else { + test_upper_bound (true); + } + + return 0; +} + +/**************************************************************************/ + +int main (int argc, char *argv[]) +{ + return rw_test (argc, argv, __FILE__, + "lib.upper.bound", + 0 /* no comment */, + run_test, + "|-no-Predicate# " + "|-no-ForwardIterator# " + "|-no-BidirectionalIterator# " + "|-no-RandomAccessIterator#", + &rw_opt_no_predicate, + &rw_opt_no_fwd_iter, + &rw_opt_no_bidir_iter, + &rw_opt_no_rnd_iter); +} Propchange: incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/stdcxx/trunk/tests/algorithms/25.upper.bound.cpp ------------------------------------------------------------------------------ svn:keywords = Id