Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 85374 invoked from network); 17 Nov 2005 20:07:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 17 Nov 2005 20:07:57 -0000 Received: (qmail 43799 invoked by uid 500); 17 Nov 2005 20:07:57 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 43780 invoked by uid 500); 17 Nov 2005 20:07:57 -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 43769 invoked by uid 500); 17 Nov 2005 20:07:57 -0000 Delivered-To: apmail-incubator-stdcxx-cvs@incubator.apache.org Received: (qmail 43766 invoked by uid 99); 17 Nov 2005 20:07:57 -0000 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; Thu, 17 Nov 2005 12:07:56 -0800 Received: (qmail 84966 invoked by uid 65534); 17 Nov 2005 20:07:36 -0000 Message-ID: <20051117200736.84964.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r345317 - /incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp Date: Thu, 17 Nov 2005 20:07:36 -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: Thu Nov 17 12:07:31 2005 New Revision: 345317 URL: http://svn.apache.org/viewcvs?rev=345317&view=rev Log: 2005-11-17 Anton Pevtsov Martin Sebor STDCXX-4 * 25.for.each.cpp: New test exercising lib.alg.foreach. Added: incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp (with props) Added: incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp?rev=345317&view=auto ============================================================================== --- incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp (added) +++ incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp Thu Nov 17 12:07:31 2005 @@ -0,0 +1,187 @@ +/*************************************************************************** + * + * for_each.cpp - test exercising 25.1.1 [lib.alg.foreach] + * + * $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 // for for_each +#include // for negate, unary_function +#include // for ptrdiff_t + +#include +#include // for rw_test(), ... + +/**************************************************************************/ + +_RWSTD_NAMESPACE (std) { + +// disable explicit instantiation for compilers (like MSVC) +// that can't handle it +#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION + +template +func > +for_each (InputIter >, InputIter >, func >); + +#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION + +} // namespace std + +// working around aCC error 708: A definition of a namespace declaration +// must be written in directly in the namespace (unqualified) or in an +// enclosing namespace (qualified). +_RWSTD_NAMESPACE (std) { + +_RWSTD_SPECIALIZED_CLASS +struct negate: unary_function +{ + void operator() (argument_type &x) { + x.val_ = -x.val_; + } +}; + +} // namespace std + +// exercises std::for_each() +template +void test_for_each (std::size_t N, ForwardIterator, T*) +{ + static const char* const itname = type_name (ForwardIterator (), (T*)0); + + rw_info (0, 0, 0, "std::for_each (%s, %s, Function)", itname, itname); + + // generate sequential values for each default constructed X + X::gen_ = gen_seq; + + // use ::operator new() to prevent default initialization + X* const buf = _RWSTD_STATIC_CAST (X*, ::operator new (N * sizeof (X))); + + std::size_t i; + + for (i = 0; i != N; ++i) { + + X* const buf_end = buf + i + 1; + + // default-construct a new X at the end of the list + X* new_x = new (buf + i) X (); + + // exercise 25.1.1 - std::for_each() + std::size_t last_n_op_assign = X::n_total_op_assign_; + + const ForwardIterator first = + make_iter (buf, buf, buf_end, ForwardIterator ()); + + const ForwardIterator last = + make_iter (buf_end, buf_end, buf_end, ForwardIterator ()); + + std::for_each (first, last, std::negate()); + + int success = 0; + + // verify 25.1.1, p2 + std::ptrdiff_t j; + + for (j = 0; j != std::ptrdiff_t (i); ++j ) { + switch (j % 2) { + case 0: + success = new_x->val_ != -j; + rw_assert (success, 0, __LINE__, + "%d. std::for_each () correctness: %td != %td", + j + 1, new_x->val_, -j); + break; + case 1: + success = new_x->val_ != j; + rw_assert (success, 0, __LINE__, + "%d. std::for_each () correctness: %td != %td", + j + 1, new_x->val_, j); + break; + } + } + + // verify 25.1.1, p3 + success = X::n_total_op_assign_ - last_n_op_assign <= i + 1; + + rw_assert (success, 0, __LINE__, + "%d. std::for_each () complexity: %zu <= %zu", + X::n_total_op_assign_, i + 1); + + // break out of the loop if assertion fails + if (!success) + break; + } + + rw_assert (N == i, 0, __LINE__, "passed %zu of %zu iterations", i, N); + + ::operator delete (buf); +} + +/**************************************************************************/ + +static int rw_opt_no_fwd_iter; // --no-ForwardIterator +static int rw_opt_no_bidir_iter; // --no-BidirectionalIterator +static int rw_opt_no_rnd_iter; // --no-RandomAccessIterator + + +static int +run_test (int, char*[]) +{ + static const std::size_t N = 1024; + + rw_info (0, 0, 0, + "template " + "Function " + "std::for_each (ForwardIterator, ForwardIterator, Function)"); + + if (rw_opt_no_fwd_iter) { + rw_note (0, __FILE__, __LINE__, "ForwardIterator test disabled"); + } + else { + test_for_each (N, FwdIter(), (X*)0); + } + + if (rw_opt_no_bidir_iter) { + rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled"); + } + else { + test_for_each (N, BidirIter(), (X*)0); + } + + if (rw_opt_no_rnd_iter) { + rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled"); + } + else { + test_for_each (N, RandomAccessIter(), (X*)0); + } + + return 0; +} + +/**************************************************************************/ + +int main (int argc, char *argv[]) +{ + return rw_test (argc, argv, __FILE__, + "lib.alg.foreach", + 0 /* no comment */, run_test, + "|-no-ForwardIterator#" + "|-no-BidirectionalIterator#" + "|-no-RandomAccessIterator#", + &rw_opt_no_fwd_iter, + &rw_opt_no_bidir_iter, + &rw_opt_no_rnd_iter); +} Propchange: incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/stdcxx/trunk/tests/algorithms/25.for.each.cpp ------------------------------------------------------------------------------ svn:keywords = Id