Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 37605 invoked from network); 22 Feb 2006 02:16:29 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 22 Feb 2006 02:16:29 -0000 Received: (qmail 33100 invoked by uid 500); 22 Feb 2006 02:16:29 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 33089 invoked by uid 500); 22 Feb 2006 02:16:28 -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 33076 invoked by uid 500); 22 Feb 2006 02:16:28 -0000 Delivered-To: apmail-incubator-stdcxx-cvs@incubator.apache.org Received: (qmail 33071 invoked by uid 99); 22 Feb 2006 02:16:28 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Feb 2006 18:16:28 -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; Tue, 21 Feb 2006 18:16:27 -0800 Received: (qmail 37512 invoked by uid 65534); 22 Feb 2006 02:16:07 -0000 Message-ID: <20060222021607.37511.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r379649 - /incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp Date: Wed, 22 Feb 2006 02:16:06 -0000 To: stdcxx-cvs@incubator.apache.org From: sebor@apache.org X-Mailer: svnmailer-1.0.6 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: sebor Date: Tue Feb 21 18:16:02 2006 New Revision: 379649 URL: http://svn.apache.org/viewcvs?rev=379649&view=rev Log: 2006-02-21 Anton Pevtsov Martin Sebor * 26.accumulate.cpp: New test exercising lib.accumulate. Added: incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp (with props) Added: incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp?rev=379649&view=auto ============================================================================== --- incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp (added) +++ incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp Tue Feb 21 18:16:02 2006 @@ -0,0 +1,338 @@ +/*************************************************************************** + * + * 26.accumulate.cpp - test exercising 26.4.1 [lib.accumulate] + * + * $Id$ + * + *************************************************************************** + * + * + * Copyright 2006 The Apache Software Foundation or its licensors, + * as applicable. + * + * Copyright 2006 Rogue Wave Software. + * + * 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 accumulate +#include // for size_t + +#include +#include // for rw_test() + +/**************************************************************************/ + +// plus-assign +template +struct plus_asgn: T +{ + plus_asgn& operator+= (const plus_asgn& rhs) { + unused = rhs.unused; + return *this; + } +private: + // unused member prevents bogus HP aCC warnings (see Onyx #23561) + int unused; +}; + +_RWSTD_NAMESPACE (std) { + +// disable explicit instantiation for compilers (like MSVC) +// that can't handle it +#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION + +template +plus_asgn > > +accumulate (InputIter > > >, + InputIter > > >, + plus_asgn > >); + +template +assign > +accumulate (InputIter > >, + InputIter > >, + assign >, + binary_func > >); + +#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION + +} // namespace std + +/**************************************************************************/ + +struct Y: public X +{ + // number of times the object's += operator has been invoked, + // regardless of whether the operation threw an exception or not + std::size_t n_op_plus_assign_; + + static std::size_t n_total_op_plus_assign_; // ... += operators ... + + // class thrown from the respective functions + struct OpPlusAssign: Exception { }; + + // throw object's `id' wrapped in the appropriate struct when the + // corresponding n_total_xxx_ counter reaches the value pointed to + // by the respective pointer below + static std::size_t* op_plus_assign_throw_ptr_; + + // objects to which the pointers above initally point + static std::size_t op_plus_assign_throw_count_; + + Y (): X () { /* empty */ } + + Y (const Y &rhs): X (rhs) { /* empty */ } + + Y& operator+= (const Y& rhs) { + + // verify id validity and uniqueness + RW_ASSERT (id_ && id_ <= id_gen_); + RW_ASSERT (rhs.id_ && rhs.id_ <= id_gen_); + RW_ASSERT (this == &rhs || id_ != rhs.id_); + + // increment the number of times each distinct object + // has been used as the argument to operator+= + // (do so even if the function throws an exception below) + ++n_op_plus_assign_; + + if (this != &rhs) + ++_RWSTD_CONST_CAST (Y*, &rhs)->n_op_plus_assign_; + + // increment the total number of invocations of the operator + // (do so even if the function throws an exception below) + ++n_total_op_plus_assign_; + +#ifndef _RWSTD_NO_EXCEPTIONS + + // throw an exception if the number of calls + // to operator== reaches the given value + + if ( op_plus_assign_throw_ptr_ + && n_total_op_plus_assign_ == *op_plus_assign_throw_ptr_) { + OpPlusAssign ex; + ex.id_ = id_; + throw ex; + } + +#endif // _RWSTD_NO_EXCEPTIONS + + val_ += rhs.val_; + return *this; + } +}; + +/* static */ size_t Y::n_total_op_plus_assign_; +/* static */ size_t* Y::op_plus_assign_throw_ptr_ = + &Y::op_plus_assign_throw_count_; +/* static */ size_t Y::op_plus_assign_throw_count_ = + std::size_t (-1); + +/**************************************************************************/ + +template +struct conv_to_T +{ + static conv_to_T make (T val) { + return conv_to_T (val); + } + + // strictly convertible to a T value + operator T () const { + return val_; + } + +private: + // not (publicly) Default-Constructible + conv_to_T (T val): val_ (val) { } + + void operator= (conv_to_T); // not Assignable + void operator!() const; // not defined + + T val_; +}; + +/**************************************************************************/ + +struct Accumulator +{ + static std::size_t funcalls_; + + // dummy arguments provided to prevent the class from being + // default constructible and implicit conversion from int + Accumulator (int /* dummy */, int /* dummy */) { + funcalls_ = 0; + } + + // return a type convertible to Y + conv_to_T operator() (const Y &x, const Y &y) /* non-const */ { + ++funcalls_; + Y res (x); + res.val_ += y.val_; + return conv_to_T::make (res); + } + +private: + void operator= (Accumulator&); // not assignable +}; + +std::size_t Accumulator::funcalls_; + +/**************************************************************************/ + +// exercises accumulate (26.4.1) +template +void test_accumulate (const std::size_t N, + const InputIterator &it, + const T*, + const BinaryOp *op) +{ + const char* const itname = type_name (it, (T*)0); + const char* const tname = "Y"; + const char* const opname = "BinaryOperation"; + + rw_info (0, 0, 0, "std::accumulate (%s, %1$s, %s%{?}, %s%{;})", + itname, tname, 0 != op, opname); + + // construct initial T + const T init; + int sum = init.val_; + + T::gen_ = gen_seq; + + T* const buf = new T [N]; + + for (std::size_t i = 0; i != N; ++i) { + + T* const buf_end = buf + i; + + const InputIterator first (buf, buf, buf_end); + const InputIterator last (buf_end, buf, buf_end); + + BinaryOp bin_op (0, 0); + + const T res = op ? + std::accumulate (first, last, init, bin_op) + : std::accumulate (first, last, init); + + // verify the result 26.4.1, p1 + bool success = sum == res.val_; + rw_assert (success, 0, __LINE__, + "step %zu: accumulate <%s, %s%{?}, %s%{;}> " + "= %d, expected %d", + i + 1, itname, tname, 0 != op, opname, res.val_, sum); + + sum += buf [i].val_; + + if (!success) + break; + } + + delete[] buf; +} + +/**************************************************************************/ + +/* extern */ int rw_opt_nloops = 256; // --nloops +/* extern */ int rw_opt_no_binary_op; // --no-binary_op +/* extern */ int rw_opt_no_input_iter; // --no-InputIterator +/* extern */ int rw_opt_no_fwd_iter; // --no-ForwardIterator +/* extern */ int rw_opt_no_bidir_iter; // --no-BidirectionalIterator +/* extern */ int rw_opt_no_rnd_iter; // --no-RandomAccessIterator + +/**************************************************************************/ + +template +void test_accumulate (const std::size_t N, + const T*, + const BinaryOp *op) +{ + static const InputIter input_iter (0, 0, 0); + static const ConstFwdIter fwd_iter (0, 0, 0); + static const ConstBidirIter bidir_iter (0, 0, 0); + static const ConstRandomAccessIter rand_iter (0, 0, 0); + + rw_info (0, 0, 0, + "template " + "%2$s std::accumulate (%1$s, %1$s, %2$s%{?}, %s%{;})", + "InputIterator", "T", 0 != op, "BinaryOperation", + 0 != op, "BinaryOperation"); + + if (rw_opt_no_input_iter) { + rw_note (0, 0, __LINE__, "InputIterator test disabled"); + } + else { + test_accumulate (N, input_iter, (T*)0, op); + } + + if (rw_opt_no_fwd_iter) { + rw_note (0, 0, __LINE__, "ForwardIterator test disabled"); + } + else { + test_accumulate (N, fwd_iter, (T*)0, op); + } + + if (rw_opt_no_bidir_iter) { + rw_note (0, 0, __LINE__, "BidirectionalIterator test disabled"); + } + else { + test_accumulate (N, bidir_iter, (T*)0, op); + } + + if (rw_opt_no_rnd_iter) { + rw_note (0, 0, __LINE__, "RandomAccessIterator test disabled"); + } + else { + test_accumulate (N, rand_iter, (T*)0, op); + } + +} + +/**************************************************************************/ + +static int +run_test (int, char*[]) +{ + const std::size_t N = std::size_t (rw_opt_nloops); + + test_accumulate (N, (Y*)0, (Accumulator*)0); + + if (rw_opt_no_binary_op) + rw_note (0, 0, 0, "accumulate with binary operation test disabled"); + else + test_accumulate (N, (Y*)0, (Accumulator*)1); + + return 0; +} + +/**************************************************************************/ + +int main (int argc, char *argv[]) +{ + return rw_test (argc, argv, __FILE__, + "lib.accumulate", + 0 /* no comment */, run_test, + "|-nloops#0 " // must be non-negative + "|-no-binary_op#" + "|-no-InputIterator# " + "|-no-ForwardIterator# " + "|-no-BidirectionalIterator# " + "|-no-RandomAccessIterator#", + &rw_opt_nloops, + &rw_opt_no_binary_op, + &rw_opt_no_input_iter, + &rw_opt_no_fwd_iter, + &rw_opt_no_bidir_iter, + &rw_opt_no_rnd_iter); +} Propchange: incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/stdcxx/trunk/tests/numerics/26.accumulate.cpp ------------------------------------------------------------------------------ svn:keywords = Id