first commit
This commit is contained in:
339
extern/stdcxx/4.2.1/tests/numerics/26.accumulate.cpp
vendored
Normal file
339
extern/stdcxx/4.2.1/tests/numerics/26.accumulate.cpp
vendored
Normal file
@@ -0,0 +1,339 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.accumulate.cpp - test exercising 26.4.1 [lib.accumulate]
|
||||
*
|
||||
* $Id: 26.accumulate.cpp 510970 2007-02-23 14:57:45Z faridz $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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.
|
||||
*
|
||||
* Copyright 2006 Rogue Wave Software.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
#include <numeric> // for accumulate
|
||||
#include <cstddef> // for size_t
|
||||
|
||||
#include <alg_test.h>
|
||||
#include <rw_value.h> // for UserClass
|
||||
#include <driver.h> // for rw_test()
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// plus-assign
|
||||
template <class T>
|
||||
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<assign<base<cpy_ctor> > >
|
||||
accumulate (InputIter<plus_asgn<assign<base<cpy_ctor> > > >,
|
||||
InputIter<plus_asgn<assign<base<cpy_ctor> > > >,
|
||||
plus_asgn<assign<base<cpy_ctor> > >);
|
||||
|
||||
template
|
||||
assign<base<cpy_ctor> >
|
||||
accumulate (InputIter<assign<base<cpy_ctor> > >,
|
||||
InputIter<assign<base<cpy_ctor> > >,
|
||||
assign<base<cpy_ctor> >,
|
||||
binary_func<assign<base<cpy_ctor> > >);
|
||||
|
||||
#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
|
||||
|
||||
} // namespace std
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
struct Y: public UserClass
|
||||
{
|
||||
// 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 (): UserClass () { /* empty */ }
|
||||
|
||||
Y (const Y &rhs): UserClass (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
|
||||
|
||||
data_.val_ += rhs.data_.val_;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
/* static */ std::size_t Y::n_total_op_plus_assign_;
|
||||
/* static */ std::size_t* Y::op_plus_assign_throw_ptr_ =
|
||||
&Y::op_plus_assign_throw_count_;
|
||||
/* static */ std::size_t Y::op_plus_assign_throw_count_ =
|
||||
std::size_t (-1);
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
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<Y> operator() (const Y &x, const Y &y) /* non-const */ {
|
||||
++funcalls_;
|
||||
Y res (x);
|
||||
res.data_.val_ += y.data_.val_;
|
||||
return conv_to_T<Y>::make (res);
|
||||
}
|
||||
|
||||
private:
|
||||
void operator= (Accumulator&); // not assignable
|
||||
};
|
||||
|
||||
std::size_t Accumulator::funcalls_;
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// exercises accumulate (26.4.1)
|
||||
template <class T, class InputIterator, class BinaryOp>
|
||||
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.data_.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.data_.val_;
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"step %zu: accumulate <%s, %s%{?}, %s%{;}> "
|
||||
"= %d, expected %d",
|
||||
i + 1, itname, tname, 0 != op, opname, res.data_.val_, sum);
|
||||
|
||||
sum += buf [i].data_.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 <class T, class BinaryOp>
|
||||
void test_accumulate (const std::size_t N,
|
||||
const T*,
|
||||
const BinaryOp *op)
|
||||
{
|
||||
static const InputIter<T> input_iter (0, 0, 0);
|
||||
static const ConstFwdIter<T> fwd_iter (0, 0, 0);
|
||||
static const ConstBidirIter<T> bidir_iter (0, 0, 0);
|
||||
static const ConstRandomAccessIter<T> rand_iter (0, 0, 0);
|
||||
|
||||
rw_info (0, 0, 0,
|
||||
"template <class %s, class %s%{?}, class %s%{;}> "
|
||||
"%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);
|
||||
}
|
||||
402
extern/stdcxx/4.2.1/tests/numerics/26.adjacent.diff.cpp
vendored
Normal file
402
extern/stdcxx/4.2.1/tests/numerics/26.adjacent.diff.cpp
vendored
Normal file
@@ -0,0 +1,402 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.adjacent.difference.cpp - test exercising lib.adjacent.difference
|
||||
*
|
||||
* $Id: 26.adjacent.diff.cpp 510970 2007-02-23 14:57:45Z faridz $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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.
|
||||
*
|
||||
* Copyright 2006 Rogue Wave Software.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
#include <numeric> // for adjacent_difference
|
||||
#include <cstddef> // for size_t
|
||||
|
||||
#include <alg_test.h>
|
||||
#include <rw_value.h> // for UserClass
|
||||
#include <driver.h> // for rw_test()
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// minus
|
||||
template <class T>
|
||||
struct minus: T
|
||||
{
|
||||
private:
|
||||
// unused member prevents bogus HP aCC warnings (see Onyx #23561)
|
||||
int unused;
|
||||
};
|
||||
|
||||
|
||||
template <class T>
|
||||
const minus<T>& operator- (const minus<T>& lhs, const minus<T>& rhs) {
|
||||
_RWSTD_UNUSED(rhs);
|
||||
return lhs;
|
||||
}
|
||||
|
||||
_RWSTD_NAMESPACE (std) {
|
||||
|
||||
// disable explicit instantiation for compilers (like MSVC)
|
||||
// that can't handle it
|
||||
#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
|
||||
|
||||
template
|
||||
OutputIter<minus<assign<base<cpy_ctor> > > >
|
||||
adjacent_difference (InputIter<minus<assign<base<cpy_ctor> > > >,
|
||||
InputIter<minus<assign<base<cpy_ctor> > > >,
|
||||
OutputIter<minus<assign<base<cpy_ctor> > > >);
|
||||
|
||||
template
|
||||
OutputIter<assign<base<cpy_ctor> > >
|
||||
adjacent_difference (InputIter<assign<base<cpy_ctor> > >,
|
||||
InputIter<assign<base<cpy_ctor> > >,
|
||||
OutputIter<assign<base<cpy_ctor> > >,
|
||||
binary_func<assign<base<cpy_ctor> > >);
|
||||
|
||||
#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
|
||||
|
||||
} // namespace std
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
UserClass operator- (const UserClass &lhs, const UserClass &rhs)
|
||||
{
|
||||
return UserClass (lhs)-= rhs;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
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 UserClass
|
||||
conv_to_T<UserClass> operator() (const UserClass &x,
|
||||
const UserClass &y) /* non-const */ {
|
||||
++funcalls_;
|
||||
UserClass res (x);
|
||||
res.data_.val_ -= y.data_.val_;
|
||||
return conv_to_T<UserClass>::make (res);
|
||||
}
|
||||
|
||||
private:
|
||||
void operator= (Accumulator&); // not assignable
|
||||
};
|
||||
|
||||
std::size_t Accumulator::funcalls_;
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
struct AdjacentDiffBase
|
||||
{
|
||||
virtual ~AdjacentDiffBase() {}
|
||||
|
||||
const char* iter_names [2];
|
||||
|
||||
// pure virtual
|
||||
virtual UserClass*
|
||||
adjacent_difference (const UserClass *xsrc, const UserClass *xsrc_end,
|
||||
UserClass *xdst, const UserClass *xdst_end,
|
||||
const Accumulator *op) const = 0;
|
||||
};
|
||||
|
||||
template <class InputIterator, class OutputIterator>
|
||||
struct AdjacentDiff : AdjacentDiffBase
|
||||
{
|
||||
AdjacentDiff () {
|
||||
iter_names [0] = type_name (InputIterator (0, 0, 0), (UserClass*)0);
|
||||
iter_names [1] = type_name (OutputIterator (0, 0, 0), (UserClass*)0);
|
||||
}
|
||||
|
||||
virtual UserClass*
|
||||
adjacent_difference (const UserClass *xsrc, const UserClass *xsrc_end,
|
||||
UserClass *xdst, const UserClass *xdst_end,
|
||||
const Accumulator *op) const {
|
||||
|
||||
const InputIterator first (xsrc, xsrc, xsrc_end);
|
||||
const InputIterator last (xsrc_end, xsrc, xsrc_end);
|
||||
const OutputIterator result (xdst, xdst, xdst_end);
|
||||
|
||||
const OutputIterator res = op ?
|
||||
std::adjacent_difference (first, last, result, *op)
|
||||
: std::adjacent_difference (first, last, result);
|
||||
|
||||
// silence EDG eccp 3.7 and prior remark #550-D:
|
||||
// variable was set but never used
|
||||
_RWSTD_UNUSED (res);
|
||||
|
||||
return res.cur_;
|
||||
}
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// exercises adjacent_difference (26.4.4)
|
||||
void test_adjacent_difference (const std::size_t N,
|
||||
const AdjacentDiffBase &alg,
|
||||
bool binop,
|
||||
bool same_seq)
|
||||
{
|
||||
const char* const itname = alg.iter_names [0];
|
||||
const char* const outname = alg.iter_names [1];
|
||||
const char* const opname = "Minus";
|
||||
|
||||
rw_info (0, 0, 0,
|
||||
"std::adjacent_difference(%s, %1$s, %s%{?}, %s%{;})%{?}, %s%{;}",
|
||||
itname, outname, binop, opname, same_seq, "first == result");
|
||||
|
||||
UserClass::gen_ = gen_seq;
|
||||
|
||||
UserClass* const src = new UserClass [N + 1];
|
||||
UserClass* dst = same_seq ? src : new UserClass [N + 1];
|
||||
|
||||
for (std::size_t i = 0; i != N; ++i) {
|
||||
|
||||
UserClass* const src_end = src + i;
|
||||
UserClass* const dst_end = dst + i;
|
||||
|
||||
std::size_t last_n_op_minus_assign =
|
||||
UserClass::n_total_op_minus_assign_;
|
||||
|
||||
const Accumulator acc (0, 0);
|
||||
const Accumulator* const pbinop = binop ? &acc : 0;
|
||||
|
||||
std::size_t k = 0 < i ? i - 1 : 0;
|
||||
int* const tmp_val = new int [i + 1];
|
||||
|
||||
for (; 0 < k; --k)
|
||||
tmp_val [k] = src [k].data_.val_ - src [k - 1].data_.val_;
|
||||
|
||||
tmp_val [0] = src [0].data_.val_;
|
||||
|
||||
const UserClass* const res =
|
||||
alg.adjacent_difference (src, src_end, dst, dst_end, pbinop);
|
||||
|
||||
const std::size_t minus_ops = binop ?
|
||||
Accumulator::funcalls_
|
||||
: UserClass::n_total_op_minus_assign_ - last_n_op_minus_assign;
|
||||
|
||||
// verify the returned iterator 26.4.4, p2
|
||||
bool success = res == dst_end;
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"adjacent_difference <%s, %s%{?}, %s%{;}>"
|
||||
"({%{X=+*}}, ...) == result + %td, got result + %td",
|
||||
itname, outname, binop, opname,
|
||||
int (i), src, dst_end - dst, res - dst);
|
||||
|
||||
for (k = 0; k < i; k++) {
|
||||
success = dst [k].data_.val_ == tmp_val [k];
|
||||
if (!success)
|
||||
break;
|
||||
}
|
||||
|
||||
// verify the result 26.4.4, p1
|
||||
if (0 < i) {
|
||||
// to avoid errors in --trace mode
|
||||
k = k < i ? k : i - 1;
|
||||
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"adjacent_difference <%s, %s%{?}, %s%{;}>"
|
||||
"({%{X=+*}}, ...) ==> {%{X=+*.*}}, expected %d",
|
||||
itname, outname, binop, opname,
|
||||
int (i), src, int (i), int (k), dst, tmp_val [k]);
|
||||
}
|
||||
|
||||
delete[] tmp_val;
|
||||
|
||||
if (!success)
|
||||
break;
|
||||
|
||||
// verify the complexity, 26.4.4, p3
|
||||
const std::size_t exp_minus_ops = 0 < i ? i - 1 : 0;
|
||||
success = minus_ops == exp_minus_ops;
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"adjacent_difference <%s, %s%{?}, %s%{;}>"
|
||||
"({%{X=+*}}, ...) complexity: got %zu invocations "
|
||||
"of %s, expected %zu",
|
||||
itname, outname, binop, opname,
|
||||
int (i), src, minus_ops,
|
||||
binop ? "BinaryMinus" : "operator-", exp_minus_ops);
|
||||
|
||||
if (!success)
|
||||
break;
|
||||
}
|
||||
|
||||
delete[] src;
|
||||
|
||||
if (!same_seq)
|
||||
delete[] dst;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
/* extern */ int rw_opt_nloops = 64; // --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_output_iter; // --no-OutputIterator
|
||||
/* 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 <class InputIterator, class OutputIterator>
|
||||
void gen_adjacent_difference_test (const std::size_t N,
|
||||
const InputIterator&,
|
||||
const OutputIterator&,
|
||||
bool binop)
|
||||
{
|
||||
const AdjacentDiff<InputIterator, OutputIterator> alg;
|
||||
|
||||
// test the algorithm than input and output arrays differ
|
||||
test_adjacent_difference (N, alg, binop, false);
|
||||
|
||||
// test the algorithm than input and output arrays are the same
|
||||
test_adjacent_difference (N, alg, binop, true);
|
||||
}
|
||||
|
||||
|
||||
template <class InputIterator>
|
||||
void gen_adjacent_difference_test (const std::size_t N,
|
||||
const InputIterator &it,
|
||||
bool binop)
|
||||
{
|
||||
if (0 == rw_opt_no_output_iter)
|
||||
gen_adjacent_difference_test (
|
||||
N, it, OutputIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_fwd_iter)
|
||||
gen_adjacent_difference_test (
|
||||
N, it, FwdIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_bidir_iter)
|
||||
gen_adjacent_difference_test (
|
||||
N, it, BidirIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_rnd_iter)
|
||||
gen_adjacent_difference_test (
|
||||
N, it, RandomAccessIter<UserClass>(0, 0, 0), binop);
|
||||
}
|
||||
|
||||
// generates a specialization of the partial_sum test for each of the required
|
||||
// iterator categopries
|
||||
void gen_adjacent_difference_test (const std::size_t N,
|
||||
bool binop)
|
||||
{
|
||||
rw_info (0, 0, 0,
|
||||
"template <class %s, class %s%{?}, class %s%{;}> "
|
||||
"%2$s adjacent_difference (%1$s, %1$s, %2$s%{?}, %s%{;})",
|
||||
"InputIterator", "OutputIterator", binop, "BinaryOperation",
|
||||
binop, "BinaryOperation");
|
||||
|
||||
if (rw_opt_no_output_iter)
|
||||
rw_note (0, 0, 0, "OutputIterator test disabled");
|
||||
|
||||
if (rw_opt_no_input_iter)
|
||||
rw_note (0, 0, 0, "InputIterator test disabled");
|
||||
else
|
||||
gen_adjacent_difference_test (N, InputIter<UserClass>(0, 0, 0), binop);
|
||||
|
||||
if (rw_opt_no_fwd_iter)
|
||||
rw_note (0, 0, 0, "ForwardIterator test disabled");
|
||||
else
|
||||
gen_adjacent_difference_test (N, ConstFwdIter<UserClass>(0, 0, 0),
|
||||
binop);
|
||||
|
||||
if (rw_opt_no_bidir_iter)
|
||||
rw_note (0, 0, 0, "BidirectionalIterator test disabled");
|
||||
else
|
||||
gen_adjacent_difference_test (N, ConstBidirIter<UserClass>(0, 0, 0),
|
||||
binop);
|
||||
|
||||
if (rw_opt_no_rnd_iter)
|
||||
rw_note (0, 0, 0, "RandomAccessIterator test disabled");
|
||||
else
|
||||
gen_adjacent_difference_test (N,
|
||||
ConstRandomAccessIter<UserClass>(0, 0, 0), binop);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char*[])
|
||||
{
|
||||
const std::size_t N = std::size_t (rw_opt_nloops);
|
||||
|
||||
gen_adjacent_difference_test (N, false);
|
||||
|
||||
if (rw_opt_no_binary_op)
|
||||
rw_note (0, 0, 0,
|
||||
"adjacent_difference with binary operation test disabled");
|
||||
else
|
||||
gen_adjacent_difference_test (N, true);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"lib.adjacent.difference",
|
||||
0 /* no comment */, run_test,
|
||||
"|-nloops#0 " // must be non-negative
|
||||
"|-no-binary_op#"
|
||||
"|-no-InputIterator# "
|
||||
"|-no-OutputIterator# "
|
||||
"|-no-ForwardIterator# "
|
||||
"|-no-BidirectionalIterator# "
|
||||
"|-no-RandomAccessIterator#",
|
||||
&rw_opt_nloops,
|
||||
&rw_opt_no_binary_op,
|
||||
&rw_opt_no_input_iter,
|
||||
&rw_opt_no_output_iter,
|
||||
&rw_opt_no_fwd_iter,
|
||||
&rw_opt_no_bidir_iter,
|
||||
&rw_opt_no_rnd_iter);
|
||||
}
|
||||
363
extern/stdcxx/4.2.1/tests/numerics/26.c.math.cpp
vendored
Normal file
363
extern/stdcxx/4.2.1/tests/numerics/26.c.math.cpp
vendored
Normal file
@@ -0,0 +1,363 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* c_math.cpp - test exercising [lib.c.math]
|
||||
*
|
||||
* $Id: 26.c.math.cpp 648752 2008-04-16 17:01:56Z faridz $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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.
|
||||
*
|
||||
* Copyright 2001-2006 Rogue Wave Software.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
// this may be the "native" header that comes with the compiler
|
||||
// do not assume that any of our stuff (i.e., macros) is available
|
||||
|
||||
#if defined (__IBMCPP__) && !defined (_RWSTD_NO_IMPLICIT_INCLUSION)
|
||||
// Disable implicit inclusion to work around
|
||||
// a limitation in IBM's VisualAge 5.0.2.0 (see PR#26959)
|
||||
|
||||
# define _RWSTD_NO_IMPLICIT_INCLUSION
|
||||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
|
||||
#include <any.h>
|
||||
#include <driver.h>
|
||||
#include <valcmp.h>
|
||||
|
||||
|
||||
// define function templates with the same signatures as
|
||||
// the required overloads; if the required overloads are
|
||||
// not defined, the corresponding specializations of the
|
||||
// function templates will be instantiated and called,
|
||||
// triggering an assertion
|
||||
|
||||
#define FUN_1(name) \
|
||||
template <class T> \
|
||||
T name (T) { \
|
||||
static const char* const tname = rw_any_t (T ()).type_name (); \
|
||||
rw_assert (0, 0, __LINE__, "std::%s(%s) not declared\n", \
|
||||
#name, tname); \
|
||||
return T (); \
|
||||
} typedef void unused_typedef
|
||||
|
||||
#define FUN_2(name) \
|
||||
template <class T> \
|
||||
T name (T, T) { \
|
||||
static const char* const tname = rw_any_t (T ()).type_name (); \
|
||||
rw_assert (0, 0, __LINE__, "std::%s (%s, %s) not declared\n", \
|
||||
#name, tname, tname); \
|
||||
return T (); \
|
||||
} typedef void unused_typedef
|
||||
|
||||
#define FUN_2_PTR(name) \
|
||||
template <class T> \
|
||||
T name (T, T*) { \
|
||||
static const char* const tname = rw_any_t (T ()).type_name (); \
|
||||
rw_assert (0, 0, __LINE__, "std::%s (%s, %s*) not declared\n", \
|
||||
#name, tname, tname); \
|
||||
return T (); \
|
||||
} typedef void unused_typedef
|
||||
|
||||
#define FUN_2_INT(name) \
|
||||
template <class T> \
|
||||
T name (T, int) { \
|
||||
static const char* const tname = rw_any_t (T ()).type_name (); \
|
||||
rw_assert (0, 0, __LINE__, "std::%s (%s, int) not declared\n", \
|
||||
#name, tname); \
|
||||
return T (); \
|
||||
} typedef void unused_typedef
|
||||
|
||||
#define FUN_2_PINT(name) \
|
||||
template <class T> \
|
||||
T name (T, int*) { \
|
||||
static const char* const tname = rw_any_t (T ()).type_name (); \
|
||||
rw_assert (0, 0, __LINE__, "std::%s (%s, int*) not declared\n", \
|
||||
#name, tname); \
|
||||
return T (); \
|
||||
} typedef void unused_typedef
|
||||
|
||||
|
||||
_RWSTD_NAMESPACE (std) {
|
||||
|
||||
// abs() is not declared in the C header <math.h> but is required
|
||||
// to be declared in the C++ headers <math.h> and <cmath>
|
||||
FUN_1 (abs);
|
||||
FUN_1 (acos);
|
||||
FUN_1 (asin);
|
||||
FUN_1 (atan);
|
||||
FUN_2 (atan2);
|
||||
FUN_1 (ceil);
|
||||
FUN_1 (cos);
|
||||
FUN_1 (cosh);
|
||||
FUN_1 (div);
|
||||
FUN_1 (exp);
|
||||
FUN_1 (fabs);
|
||||
FUN_1 (floor);
|
||||
FUN_2 (fmod);
|
||||
FUN_2_PINT (frexp);
|
||||
FUN_2_INT (ldexp);
|
||||
FUN_1 (log);
|
||||
FUN_1 (log10);
|
||||
FUN_2_PTR (modf);
|
||||
FUN_2 (pow);
|
||||
FUN_2_INT (pow);
|
||||
FUN_1 (sin);
|
||||
FUN_1 (sinh);
|
||||
FUN_1 (sqrt);
|
||||
FUN_1 (tan);
|
||||
FUN_1 (tanh);
|
||||
|
||||
} // namespace std
|
||||
|
||||
|
||||
// returns true if all `size' bytes starting at `s' are 0
|
||||
static bool
|
||||
check_bits (const char *s, unsigned size)
|
||||
{
|
||||
while (size-- && !s [size]);
|
||||
return unsigned (-1) == size;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
clear_bytes (void *pb, _RWSTD_SIZE_T size)
|
||||
{
|
||||
for (_RWSTD_SIZE_T i = 0; i != size; ++i)
|
||||
((char*)pb)[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_behavior ()
|
||||
{
|
||||
union {
|
||||
float f;
|
||||
double d;
|
||||
|
||||
#ifndef _RWSTD_NO_LONG_DOUBLE
|
||||
long double l;
|
||||
#endif // _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
char buf [sizeof (long double) * 2];
|
||||
} u;
|
||||
|
||||
#if !defined (__SUNPRO_CC) || __SUNPRO_CC > 0x530
|
||||
|
||||
// make sure functions do not overflow buffer
|
||||
clear_bytes (u.buf, sizeof u);
|
||||
|
||||
const float f = std::modf (3.141592f, &u.f);
|
||||
|
||||
rw_assert ( 3000 == int (u.f * 1000) && 141592 == int (f * 1000000)
|
||||
&& check_bits (u.buf + sizeof u.f, sizeof u - sizeof u.f),
|
||||
0, __LINE__, "float std::modf (float)");
|
||||
|
||||
#endif // SunPro > 5.3
|
||||
|
||||
clear_bytes (u.buf, sizeof u);
|
||||
const double d = std::modf (3.1415926, &u.d);
|
||||
|
||||
rw_assert ( 3000 == int (u.d * 1000) && 1415926 == int (d * 10000000)
|
||||
&& check_bits (u.buf + sizeof u.d, sizeof u - sizeof u.d),
|
||||
0, __LINE__, "double std::modf (double)");
|
||||
|
||||
#ifndef _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
# if !defined (__SUNPRO_CC) || __SUNPRO_CC > 0x530
|
||||
|
||||
clear_bytes (u.buf, sizeof u);
|
||||
const long double l = std::modf (3.1415926L, &u.l);
|
||||
|
||||
rw_assert ( 3000 == int (u.l * 1000) && 1415926 == int (l * 10000000)
|
||||
&& check_bits (u.buf + sizeof u.l, sizeof u - sizeof u.l),
|
||||
0, __LINE__, "long double std::modf (long double)");
|
||||
|
||||
# endif // SunPro > 5.3
|
||||
|
||||
#endif // _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
|
||||
// check overloads of std::pow()
|
||||
for (int i = -10; i != 10; ++i) {
|
||||
|
||||
for (int j = -10; j != 10; ++j) {
|
||||
|
||||
if (-9 < j && j < 9) {
|
||||
const float fi = float (i);
|
||||
const float fj = float (j);
|
||||
|
||||
// verify that both versions are equivalent
|
||||
const float xf = std::pow (fi, j);
|
||||
const float yf = std::pow (fi, fj);
|
||||
|
||||
rw_assert (rw_equal (xf, yf) || !i && j < 0,
|
||||
0, __LINE__,
|
||||
"std::pow (%d.0f, %d) = %g, "
|
||||
"std::pow (%d,0f, %d.0f) = %g",
|
||||
i, j, xf, i, j, yf);
|
||||
}
|
||||
|
||||
const double id = double (i);
|
||||
const double jd = double (j);
|
||||
|
||||
const double xd = std::pow (id, j);
|
||||
const double yd = std::pow (id, jd);
|
||||
|
||||
rw_assert (rw_equal (xd, yd) || !i && j < 0,
|
||||
0, __LINE__,
|
||||
"std::pow (%d.0, %d) = %g, "
|
||||
"std::pow (%d.0, %d.0) = %g",
|
||||
i, j, xd, i, j, yd);
|
||||
|
||||
#ifndef _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
const long double il = _RWSTD_STATIC_CAST (long double, i);
|
||||
const long double jl = _RWSTD_STATIC_CAST (long double, j);
|
||||
|
||||
const long double xl = std::pow (il, j);
|
||||
const long double yl = std::pow (il, jl);
|
||||
|
||||
rw_assert (rw_equal (xl, yl) || !i && j < 0,
|
||||
0, __LINE__,
|
||||
"std::pow (%d.0L, %d) = %Lg, "
|
||||
"std::pow (%d.0L, %d.0L) = %Lg",
|
||||
i, j, xl, i, j, yl);
|
||||
|
||||
#endif // _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_declarations ()
|
||||
{
|
||||
_USING (namespace std);
|
||||
|
||||
int i = 1;
|
||||
long l = 1;
|
||||
|
||||
float f = 0.1;
|
||||
double d = 0.1;
|
||||
|
||||
#ifndef _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
long double ld = 0.1;
|
||||
|
||||
# define CHECK_OVERLOADS(name) \
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads ", #name); \
|
||||
(void)name (f); \
|
||||
(void)name (d); \
|
||||
(void)name (ld)
|
||||
#else // if defined (_RWSTD_NO_LONG_DOUBLE)
|
||||
# define CHECK_OVERLOADS(name) \
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads", #name); \
|
||||
(void)name (f); \
|
||||
(void)name (d)
|
||||
#endif // _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
CHECK_OVERLOADS (abs);
|
||||
CHECK_OVERLOADS (acos);
|
||||
CHECK_OVERLOADS (asin);
|
||||
CHECK_OVERLOADS (atan);
|
||||
CHECK_OVERLOADS (atan);
|
||||
CHECK_OVERLOADS (ceil);
|
||||
CHECK_OVERLOADS (cos);
|
||||
CHECK_OVERLOADS (cosh);
|
||||
CHECK_OVERLOADS (exp);
|
||||
CHECK_OVERLOADS (fabs);
|
||||
CHECK_OVERLOADS (floor);
|
||||
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads", "fmod");
|
||||
(void)fmod (f, f); (void)fmod (d, d);
|
||||
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads", "frexp");
|
||||
(void)frexp (f, &i); (void)frexp (d, &i);
|
||||
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads", "ldexp");
|
||||
(void)ldexp (f, i); (void)ldexp (d, i);
|
||||
|
||||
CHECK_OVERLOADS (log);
|
||||
CHECK_OVERLOADS (log10);
|
||||
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads", "modf");
|
||||
(void)modf (f, &f); (void)modf (d, &d);
|
||||
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads", "pow");
|
||||
(void)pow (f, f); (void)pow (d, d);
|
||||
(void)pow (f, i); (void)pow (d, i);
|
||||
|
||||
CHECK_OVERLOADS (sin);
|
||||
CHECK_OVERLOADS (sinh);
|
||||
CHECK_OVERLOADS (sqrt);
|
||||
CHECK_OVERLOADS (tan);
|
||||
CHECK_OVERLOADS (tanh);
|
||||
|
||||
#ifndef _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
(void)atan2 (ld, ld);
|
||||
(void)fmod (ld, ld);
|
||||
(void)frexp (ld, &i);
|
||||
(void)ldexp (ld, i);
|
||||
(void)modf (ld, &ld);
|
||||
(void)pow (ld, ld);
|
||||
(void)pow (ld, i);
|
||||
|
||||
#endif // _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
rw_info (0, 0, __LINE__, "std::%s() overloads", "div");
|
||||
(void)div (i, i);
|
||||
(void)div (l, l);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int no_declarations;
|
||||
static int no_behavior;
|
||||
|
||||
|
||||
int run_test (int, char*[])
|
||||
{
|
||||
if (no_declarations)
|
||||
rw_note (0, __FILE__, __LINE__, "test of declarations disabled");
|
||||
else
|
||||
test_declarations ();
|
||||
|
||||
if (no_behavior)
|
||||
rw_note (0, __FILE__, __LINE__, "test of behavior disabled");
|
||||
else
|
||||
test_behavior ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"lib.c.math",
|
||||
0 /* no comment */, run_test,
|
||||
"|-no-declarations# |-no-behavior#",
|
||||
&no_declarations, &no_behavior);
|
||||
}
|
||||
241
extern/stdcxx/4.2.1/tests/numerics/26.class.gslice.cpp
vendored
Normal file
241
extern/stdcxx/4.2.1/tests/numerics/26.class.gslice.cpp
vendored
Normal file
@@ -0,0 +1,241 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.class.gslice.cpp - tests exercising class std::gslice
|
||||
*
|
||||
* $Id: 26.class.gslice.cpp 648752 2008-04-16 17:01:56Z faridz $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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 <cstdlib> // for strtoul(), size_t
|
||||
#include <valarray> // for gslice, valarray
|
||||
|
||||
#include <driver.h>
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns a valarray constructed from a string of comma-separated values
|
||||
static std::valarray<std::size_t>
|
||||
make_array (const char *s)
|
||||
{
|
||||
if (0 == s)
|
||||
return std::valarray<std::size_t>();
|
||||
|
||||
std::size_t buf [256];
|
||||
|
||||
for (std::size_t i = 0; ; ++i) {
|
||||
|
||||
char *end;
|
||||
unsigned long val = std::strtoul (s, &end, 10);
|
||||
|
||||
RW_ASSERT ('\0' == *end || ',' == *end);
|
||||
|
||||
buf [i] = std::size_t (val);
|
||||
|
||||
if ('\0' == *end)
|
||||
return std::valarray<std::size_t>(buf, i + 1);
|
||||
|
||||
s = end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns the size of the index array represented by the genreralized slice
|
||||
static std::size_t
|
||||
get_array_size (const std::gslice &gsl)
|
||||
{
|
||||
const std::valarray<std::size_t> sizes = gsl.size ();
|
||||
|
||||
std::size_t asize = sizes.size () ? 1 : 0;
|
||||
|
||||
for (std::size_t i = 0; i != sizes.size (); ++i) {
|
||||
asize *= sizes [i];
|
||||
}
|
||||
|
||||
return asize;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static std::size_t
|
||||
next_index (const std::gslice &gsl, std::valarray<std::size_t> &factors)
|
||||
{
|
||||
const std::size_t start = gsl.start ();
|
||||
const std::valarray<std::size_t> asizes = gsl.size ();
|
||||
const std::valarray<std::size_t> astrides = gsl.stride ();
|
||||
|
||||
const std::size_t ndims = asizes.size ();
|
||||
|
||||
std::size_t inx = ndims;
|
||||
|
||||
if (0 == factors.size ()) {
|
||||
factors.resize (ndims);
|
||||
|
||||
return start;
|
||||
}
|
||||
|
||||
while (inx && factors [inx - 1] == asizes [inx - 1] - 1)
|
||||
--inx;
|
||||
|
||||
if (0 == inx) {
|
||||
factors = 0;
|
||||
|
||||
return start;
|
||||
}
|
||||
|
||||
++factors [inx - 1];
|
||||
|
||||
if (inx < factors.size ()) {
|
||||
for (std::size_t i = inx; i != ndims; ++i)
|
||||
factors [i] = 0;
|
||||
}
|
||||
else
|
||||
inx = factors.size ();
|
||||
|
||||
std::size_t index = start;
|
||||
|
||||
for (std::size_t i = 0; i != inx; ++i)
|
||||
index += factors [i] * astrides [i];
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns a valarray of indices reprsented by the generalized slice
|
||||
static std::valarray<std::size_t>
|
||||
get_index_array (const std::gslice &gsl)
|
||||
{
|
||||
const std::size_t size = get_array_size (gsl);
|
||||
|
||||
std::valarray<std::size_t> indices (size);
|
||||
|
||||
std::valarray<std::size_t> tmpstore;
|
||||
|
||||
for (std::size_t i = 0; i != size; ++i)
|
||||
indices [i] = next_index (gsl, tmpstore);
|
||||
|
||||
return indices;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static void
|
||||
test_gslice (std::size_t start,
|
||||
const char *sizes,
|
||||
const char *strides)
|
||||
{
|
||||
const std::valarray<std::size_t> asizes (make_array (sizes));
|
||||
const std::valarray<std::size_t> astrides (make_array (strides));
|
||||
|
||||
const std::gslice gsl (start, asizes, astrides);
|
||||
|
||||
const std::valarray<std::size_t> indices = get_index_array (gsl);
|
||||
|
||||
std::size_t maxinx = 0;
|
||||
|
||||
for (std::size_t i = 0; i != indices.size (); ++i)
|
||||
if (maxinx < indices [i])
|
||||
maxinx = indices [i];
|
||||
|
||||
std::valarray<std::size_t> va (maxinx + 1);
|
||||
for (std::size_t i = 0; i != va.size (); ++i)
|
||||
va [i] = i;
|
||||
|
||||
for (int i = 0; i != 3; ++i) {
|
||||
// repeat each test three to verify that operator[](gslice)
|
||||
// doesn't change the observable state of its argument and
|
||||
// that the same result is obtained for a copy of gslice
|
||||
|
||||
const std::valarray<std::size_t> array_slice =
|
||||
i < 2 ? va [gsl] : va [std::gslice (gsl)];
|
||||
|
||||
bool equal = array_slice.size () == indices.size ();
|
||||
|
||||
rw_assert (equal, 0, __LINE__,
|
||||
"size() == %zu, got %zu\n",
|
||||
indices.size (), array_slice.size ());
|
||||
|
||||
if (equal) {
|
||||
for (std::size_t j = 0; j != array_slice.size (); ++j) {
|
||||
|
||||
equal = array_slice [j] == va [indices [j]];
|
||||
|
||||
rw_assert (equal, 0, __LINE__,
|
||||
"mismatch at %u, index %u: expected %u, got %u\n",
|
||||
j, indices [j], va [indices [j]],
|
||||
array_slice [j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static void
|
||||
test_gslice (const char *sizes, const char *strides)
|
||||
{
|
||||
for (std::size_t i = 0; i != 10; ++i)
|
||||
test_gslice (i, sizes, strides);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char**)
|
||||
{
|
||||
test_gslice ("1", "1");
|
||||
test_gslice ("1", "2");
|
||||
test_gslice ("2", "1");
|
||||
test_gslice ("2", "3");
|
||||
test_gslice ("3", "2");
|
||||
|
||||
test_gslice ("1,1", "1,1");
|
||||
test_gslice ("1,1", "1,2");
|
||||
test_gslice ("1,1", "2,1");
|
||||
test_gslice ("1,9", "2,1");
|
||||
test_gslice ("9,1", "2,1");
|
||||
|
||||
test_gslice ("1,1,1", "1,1,1");
|
||||
test_gslice ("1,2,3", "4,5,6");
|
||||
test_gslice ("6,5,4", "3,2,1");
|
||||
test_gslice ("10,11,12", "13,14,15");
|
||||
|
||||
// includes example from class.gslice, p3
|
||||
test_gslice ("2,4,3", "19,4,1");
|
||||
|
||||
// includes example of a degenerate gslice from p5
|
||||
test_gslice ("2,4,3", "19,4,1");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"lib.class.gslice",
|
||||
0 /* no comment */,
|
||||
run_test,
|
||||
"",
|
||||
(void*)0 /* sentinel */);
|
||||
}
|
||||
421
extern/stdcxx/4.2.1/tests/numerics/26.indirect.array.cpp
vendored
Normal file
421
extern/stdcxx/4.2.1/tests/numerics/26.indirect.array.cpp
vendored
Normal file
@@ -0,0 +1,421 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.template.indirect.array.cpp - tests exercising class template
|
||||
* indirect_array
|
||||
*
|
||||
* $Id: 26.indirect.array.cpp 492025 2007-01-03 02:48:52Z sebor $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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 <cstdlib> // for strtol(), size_t
|
||||
#include <valarray> // for indirect_array, valarray
|
||||
|
||||
#include <driver.h>
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns a valarray constructed from a string of comma-separated values
|
||||
// if there are fewer than n values in the string the remaining values are
|
||||
// taken to be the same as the last one, i.e., the call
|
||||
// make_array("1,2,3", 5) returns valarray({1,2,3,3})
|
||||
template <class T>
|
||||
std::valarray<T>
|
||||
make_array (const char *s, std::size_t n = 0)
|
||||
{
|
||||
if (0 == s || '\0' == *s)
|
||||
return n ? std::valarray<T>(T (), n) : std::valarray<T>();
|
||||
|
||||
T buf [256];
|
||||
|
||||
for (std::size_t i = 0; ; ++i) {
|
||||
|
||||
char *end;
|
||||
long val = std::strtol (s, &end, 0);
|
||||
|
||||
RW_ASSERT (s < end);
|
||||
RW_ASSERT ('\0' == *end || ',' == *end);
|
||||
|
||||
buf [i] = T (val);
|
||||
|
||||
if ('\0' == *end) {
|
||||
while (++i < n)
|
||||
buf [i] = buf [i - 1];
|
||||
|
||||
return std::valarray<T>(buf, i);
|
||||
}
|
||||
|
||||
s = end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static void
|
||||
test_indirect_array (int line,
|
||||
const char *array,
|
||||
const char *inxs,
|
||||
const char *opname,
|
||||
const char *args,
|
||||
const char *result,
|
||||
int nmatch)
|
||||
|
||||
{
|
||||
typedef int T;
|
||||
|
||||
const char tname[] = "int";
|
||||
|
||||
/* */ std::valarray<T> asrc (make_array<int>(array));
|
||||
const std::valarray<std::size_t> ainx (make_array<std::size_t> (inxs));
|
||||
const std::valarray<T> aarg (make_array<int>(args));
|
||||
|
||||
if (0 == opname)
|
||||
opname = "";
|
||||
|
||||
// construct the expected result
|
||||
const std::valarray<T> expect =
|
||||
make_array<int>(result, *opname ? 0 : asrc.size ());
|
||||
|
||||
// create an indirect array object from the source array
|
||||
// and the array of indices
|
||||
const std::indirect_array<T> ia (asrc [ainx]);
|
||||
|
||||
// iterate over the series of assignment operators, invoking
|
||||
// each in sequence on the source array, modifying it in place
|
||||
for (const char *op = opname; *op; ) {
|
||||
|
||||
// skip optional whitespace
|
||||
for ( ; ' ' == *op; ++op);
|
||||
|
||||
switch (*op) {
|
||||
// invoke fill assignment embedded within a series
|
||||
// of compound assignments, e.g., "*=,,+=" meaning
|
||||
// ia *= aarg; ia = aarg [0]; ia += aarg
|
||||
case ',': ia = aarg.size () ? aarg [0] : 0; break;
|
||||
|
||||
// invoke ordinary assignment
|
||||
case '=': ia = aarg; op += 1; break;
|
||||
|
||||
// invoke computed assignment
|
||||
case '*': ia *= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '/': ia /= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '%': ia %= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '+': ia += aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '-': ia -= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '^': ia ^= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '&': ia &= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '|': ia |= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '<': ia <<= aarg; RW_ASSERT ('=' == op [2]); op += 3; break;
|
||||
case '>': ia >>= aarg; RW_ASSERT ('=' == op [2]); op += 3; break;
|
||||
default:
|
||||
RW_ASSERT (!"unknown operator");
|
||||
}
|
||||
|
||||
// skip optional whitespace
|
||||
for ( ; ' ' == *op; ++op);
|
||||
|
||||
// assignment operators are separated by commas
|
||||
if (',' == *op)
|
||||
++op;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if ('\0' == *opname) {
|
||||
// fill assignment
|
||||
ia = aarg.size () ? aarg [0] : 0;
|
||||
}
|
||||
|
||||
int nequal = 0;
|
||||
for (std::size_t i = 0; i != asrc.size (); ++i, ++nequal) {
|
||||
if (asrc [i] != expect [i])
|
||||
break;
|
||||
}
|
||||
|
||||
if (-1 == nmatch)
|
||||
nmatch = int (asrc.size ());
|
||||
|
||||
const int* const expect_begin = expect.size () ? &expect [0] : 0;
|
||||
const int* const actual_begin = asrc.size () ? &asrc [0] : 0;
|
||||
|
||||
rw_assert (nequal == nmatch, 0, line,
|
||||
"valarray<%s>({%s})[valarray<size_t>({%s})] %s "
|
||||
"valarray<%1$s>({%s}) %c= {%{*.*Ad}}, got {%{*.*Ad}}",
|
||||
tname, array, inxs, opname, result,
|
||||
nmatch == int (asrc.size ()) ? '=' : '!',
|
||||
int (sizeof (int)), int (expect.size ()), expect_begin,
|
||||
int (sizeof (int)), int (asrc.size ()), actual_begin);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char**)
|
||||
{
|
||||
#define TEST(array, inx, op, arg, res, nmatch) \
|
||||
test_indirect_array (__LINE__, array, inx, op, arg, res, nmatch)
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array<T>::operator=(T), fill assignment
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::indirect_array<int>::operator=(int)");
|
||||
|
||||
// +-- source array
|
||||
// | +-- array of indices (indirect_array ctor argument)
|
||||
// | | +-- zero for fill assignment
|
||||
// | | | +-- argument of fill assignment
|
||||
// | | | | +-- expected result
|
||||
// | | | | | +-- number of matching elements
|
||||
// | | | | | |
|
||||
// V V V V V V
|
||||
TEST ("", "", 0, "0", "", -1);
|
||||
TEST ("1", "", 0, "0", "1", -1);
|
||||
|
||||
TEST ("1", "0", 0, "2", "2", -1);
|
||||
TEST ("1,2", "0", 0, "3", "3,2", -1);
|
||||
TEST ("1,2,3", "0", 0, "4", "4,2,3", -1);
|
||||
TEST ("1,2,3,4", "0", 0, "5", "5,2,3,4", -1);
|
||||
TEST ("1,2,3,4,5", "0", 0, "6", "6,2,3,4,5", -1);
|
||||
|
||||
TEST ("1,2,3,4,5", "0,1", 0, "6", "6,6,3,4,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,2", 0, "6", "6,2,6,4,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,3", 0, "6", "6,2,3,6,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,4", 0, "6", "6,2,3,4,6", -1);
|
||||
|
||||
TEST ("1,2,3,4,5", "0,1,2", 0, "6", "6,6,6,4,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,1,3", 0, "6", "6,6,3,6,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,1,4", 0, "6", "6,6,3,4,6", -1);
|
||||
|
||||
TEST ("1,2,3,4,5", "0,2,1", 0, "6", "6,6,6,4,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,2,3", 0, "6", "6,2,6,6,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,2,4", 0, "6", "6,2,6,4,6", -1);
|
||||
|
||||
TEST ("1,2,3,4,5", "0,3,1", 0, "6", "6,6,3,6,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,3,2", 0, "6", "6,2,6,6,5", -1);
|
||||
TEST ("1,2,3,4,5", "0,3,4", 0, "6", "6,2,3,6,6", -1);
|
||||
|
||||
#define INFO(op) \
|
||||
rw_info (0, 0, __LINE__, \
|
||||
"std::indirect_array<int>::operator%s(std::valarray<int>)", op);
|
||||
|
||||
INFO ("=");
|
||||
|
||||
// +-- source array
|
||||
// | +-- array of indices (indirect_array ctor argument)
|
||||
// | | +-- assignment operators to test (0 for none)
|
||||
// | | | +-- argument of assignment (0 for none)
|
||||
// | | | | +-- expected result
|
||||
// | | | | | +-- number of matching elements
|
||||
// | | | | | |
|
||||
// V V V V V V
|
||||
TEST ("", "", "=", "", "", -1 /* all */);
|
||||
TEST ("1", "", "=", "", "1", -1 /* all */);
|
||||
TEST ("1", "0", "=", "2", "2", -1 /* all */);
|
||||
TEST ("1", "0", "=", "3", "3", -1 /* all */);
|
||||
TEST ("2", "0", "=", "4", "1", 0 /* mismatch at offset 0 */);
|
||||
|
||||
TEST ("1,2", "", "=", "", "1,2", -1);
|
||||
TEST ("1,2", "0", "=", "3", "3,2", -1);
|
||||
TEST ("1,2", "1", "=", "3", "1,3", -1);
|
||||
|
||||
TEST ("1,2,3", "", "=", "", "1,2,3", -1);
|
||||
TEST ("1,2,3", "0,1", "=", "5,4", "5,4,3", -1);
|
||||
TEST ("1,2,3", "0,2", "=", "5,4", "5,2,4", -1);
|
||||
TEST ("1,2,3", "1,0", "=", "5,4", "4,5,3", -1);
|
||||
TEST ("1,2,3", "1,2", "=", "5,4", "1,5,4", -1);
|
||||
TEST ("1,2,3", "2,0", "=", "5,4", "4,2,5", -1);
|
||||
TEST ("1,2,3", "2,1", "=", "5,4", "1,4,5", -1);
|
||||
|
||||
TEST ("1,2,3,4", "", "=", "", "1,2,3,4", -1);
|
||||
TEST ("1,2,3,4", "0", "=", "9", "9,2,3,4", -1);
|
||||
TEST ("1,2,3,4", "1", "=", "9", "1,9,3,4", -1);
|
||||
TEST ("1,2,3,4", "2", "=", "9", "1,2,9,4", -1);
|
||||
TEST ("1,2,3,4", "3", "=", "9", "1,2,3,9", -1);
|
||||
|
||||
TEST ("1,2,3,4", "0,1", "=", "8,9", "8,9,3,4", -1);
|
||||
TEST ("1,2,3,4", "0,2", "=", "8,9", "8,2,9,4", -1);
|
||||
TEST ("1,2,3,4", "0,3", "=", "8,9", "8,2,3,9", -1);
|
||||
|
||||
TEST ("1,2,3,4", "1,0", "=", "8,9", "9,8,3,4", -1);
|
||||
TEST ("1,2,3,4", "1,2", "=", "8,9", "1,8,9,4", -1);
|
||||
TEST ("1,2,3,4", "1,3", "=", "8,9", "1,8,3,9", -1);
|
||||
|
||||
TEST ("1,2,3,4", "2,0", "=", "8,9", "9,2,8,4", -1);
|
||||
TEST ("1,2,3,4", "2,1", "=", "8,9", "1,9,8,4", -1);
|
||||
TEST ("1,2,3,4", "2,3", "=", "8,9", "1,2,8,9", -1);
|
||||
|
||||
TEST ("1,2,3,4", "3,0", "=", "8,9", "9,2,3,8", -1);
|
||||
TEST ("1,2,3,4", "3,1", "=", "8,9", "1,9,3,8", -1);
|
||||
TEST ("1,2,3,4", "3,2", "=", "8,9", "1,2,9,8", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator*=()
|
||||
INFO ("*=");
|
||||
|
||||
TEST ("", "", "*=", "", "", -1);
|
||||
TEST ("1", "", "*=", "", "1", -1);
|
||||
TEST ("1", "0", "*=", "9", "9", -1);
|
||||
|
||||
TEST ("1,2", "", "*=", "", "1, 2", -1);
|
||||
TEST ("1,2", "0", "*=", "9", "9, 2", -1);
|
||||
TEST ("1,2", "1", "*=", "9", "1,18", -1);
|
||||
|
||||
TEST ("1,2", "0,1", "*=", "8,9", "8,18", -1);
|
||||
TEST ("1,2", "1,0", "*=", "8,9", "9,16", -1);
|
||||
|
||||
TEST ("1,2,3", "", "*=", "", "1, 2, 3", -1);
|
||||
TEST ("1,2,3", "0,1", "*=", "8,9", "8,18, 3", -1);
|
||||
TEST ("1,2,3", "0,2", "*=", "8,9", "8, 2,27", -1);
|
||||
|
||||
TEST ("1,2,3", "1,0", "*=", "8,9", "9,16, 3", -1);
|
||||
TEST ("1,2,3", "1,2", "*=", "8,9", "1,16,27", -1);
|
||||
|
||||
TEST ("1,2,3", "2,0", "*=", "8,9", "9, 2,24", -1);
|
||||
TEST ("1,2,3", "2,1", "*=", "8,9", "1,18,24", -1);
|
||||
|
||||
TEST ("1,2,3,4", "0", "*=", "0", "0,2,3,4", -1);
|
||||
TEST ("1,2,3,4", "1", "*=", "0", "1,0,3,4", -1);
|
||||
TEST ("1,2,3,4", "2", "*=", "0", "1,2,0,4", -1);
|
||||
TEST ("1,2,3,4", "3", "*=", "0", "1,2,3,0", -1);
|
||||
|
||||
TEST ("1,2,3,4", "0,1", "*=", "5,6", "5,12, 3, 4", -1);
|
||||
TEST ("1,2,3,4", "0,2", "*=", "5,6", "5, 2,18, 4", -1);
|
||||
TEST ("1,2,3,4", "0,3", "*=", "5,6", "5, 2, 3,24", -1);
|
||||
|
||||
TEST ("1,2,3,4", "1,0", "*=", "5,6", "6,10, 3, 4", -1);
|
||||
TEST ("1,2,3,4", "1,2", "*=", "5,6", "1,10,18, 4", -1);
|
||||
TEST ("1,2,3,4", "1,3", "*=", "5,6", "1,10, 3,24", -1);
|
||||
|
||||
TEST ("1,2,3,4", "2,0", "*=", "5,6", "6, 2,15, 4", -1);
|
||||
TEST ("1,2,3,4", "2,1", "*=", "5,6", "1,12,15, 4", -1);
|
||||
TEST ("1,2,3,4", "2,3", "*=", "5,6", "1, 2,15,24", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator/=()
|
||||
INFO ("/=");
|
||||
|
||||
TEST ("10", "0", "/=", "2", " 5", -1);
|
||||
TEST ("10,20", "0", "/=", "2", " 5,20", -1);
|
||||
TEST ("10,20", "1", "/=", "2", "10,10", -1);
|
||||
|
||||
TEST ("10,20", "0,1", "/=", "5,4", "2,5", -1);
|
||||
TEST ("10,20", "1,0", "/=", "5,4", "2,4", -1);
|
||||
|
||||
TEST ("10,20,30", "0,1", "/=", "5,4", "2, 5,30", -1);
|
||||
TEST ("10,20,30", "0,2", "/=", "5,4", "2,20, 7", -1);
|
||||
|
||||
TEST ("10,20,30", "1,0", "/=", "5,4", " 2,4,30", -1);
|
||||
TEST ("10,20,30", "1,2", "/=", "5,4", "10,4, 7", -1);
|
||||
|
||||
TEST ("10,20,30", "2,0", "/=", "5,4", " 2,20, 6", -1);
|
||||
TEST ("10,20,30", "2,1", "/=", "5,4", "10, 5, 6", -1);
|
||||
|
||||
// exercise compound assignment, i.e.,
|
||||
// ({1,2,3,4,5}[{0,2,4}] *= {6,7,8}) /= {6,7,8}
|
||||
TEST (" 1, 2, 3, 4, 5", "0,2,4", "*=,/=", "6,7,8", "1, 2, 3, 4, 5", -1);
|
||||
// ({1,2,3,4,5}[{0,2,4}] /= {6,7,8}) *= {6,7,8}
|
||||
TEST ("10,20,30,40,50", "0,2,4", "/=,*=", "9,8,7", "9,20,24,40,49", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator%=()
|
||||
INFO ("%=");
|
||||
|
||||
TEST ("10,11,12,13,14,15", "1,3,5", "%=", "6,7,8", "10,5,12,6,14,7", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator+=()
|
||||
INFO ("+=");
|
||||
|
||||
TEST ("1,2,3,4,5,6", "1,3,5", "+=", "7,8,9", "1,9,3,12,5,15", -1);
|
||||
|
||||
// exercise compound assignment, i.e.,
|
||||
// ({1,2,3,4,5,6}[{1,3,5}] += {7,8,9}) += {7,8,9}
|
||||
TEST ("1,2,3,4,5,6", "1,3,5", "+=,+=", "7,8,9", "1,16,3,20,5,24", -1);
|
||||
// ({1,2,3,4,5,6}[{1,3,5}] += {7,8,9}) -= {7,8,9}
|
||||
TEST ("1,2,3,4,5,6", "1,3,5", "+=,-=", "7,8,9", "1,2,3,4,5,6", -1);
|
||||
// ({1,2,3,4,5,6}[{1,3,5}] += {7,8,9}) /= {7,8,9}
|
||||
TEST ("1,2,3,4,5,6", "1,3,5", "+=,/=", "7,8,9", "1,1,3,1,5,1", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator-=()
|
||||
INFO ("-=");
|
||||
|
||||
TEST ("9,8,7,6,5,4", "0,1,2", "-=", "9,8,7", "0,0,0,6,5,4", -1);
|
||||
TEST ("9,8,7,6,5,4", "1,2,3", "-=", "8,7,6", "9,0,0,0,5,4", -1);
|
||||
TEST ("9,8,7,6,5,4", "2,3,4", "-=", "7,6,5", "9,8,0,0,0,4", -1);
|
||||
TEST ("9,8,7,6,5,4", "3,4,5", "-=", "6,5,4", "9,8,7,0,0,0", -1);
|
||||
TEST ("9,8,7,6,5,4", "0,2,4", "-=", "3,2,1", "6,8,5,6,4,4", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator^=()
|
||||
INFO ("^=");
|
||||
|
||||
TEST ("1,3,5,7,9", "0,2,4", "^=", "2,4,5", "3,3,1,7,12", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator&=()
|
||||
INFO ("&=");
|
||||
|
||||
TEST ("1,3,5,7,9", "0,2,4", "&=", "3,1,7", "1,3,1,7,1", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator|=()
|
||||
INFO ("|=");
|
||||
|
||||
TEST ("1,3,5,7,9", "0,2,4", "|=", "3,1,7", "3,3,5,7,15", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator<<=()
|
||||
INFO ("<<=");
|
||||
|
||||
TEST ("1,3,5,7,9", "0,1,2", "<<=", "1,1,1", "2,6,10, 7, 9", -1);
|
||||
TEST ("1,3,5,7,9", "1,2,3", "<<=", "1,1,1", "1,6,10,14, 9", -1);
|
||||
TEST ("1,3,5,7,9", "2,3,4", "<<=", "1,1,1", "1,3,10,14,18", -1);
|
||||
|
||||
// exercise a compound assignment, i.e.,
|
||||
// ({1,3,5,7,9}[{2,3,4}] <<= {1,1,1}) >>= {1,1,1}
|
||||
TEST ("1,3,5,7,9", "2,3,4", "<<=,>>=", "1,1,1", "1,3,5,7,9", -1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise indirect_array::operator>>=()
|
||||
INFO (">>=");
|
||||
|
||||
TEST ("2,6,10, 7, 9", "0,1,2", ">>=", "1,1,1", "1,3,5,7,9", -1);
|
||||
TEST ("1,6,10,14, 9", "1,2,3", ">>=", "1,1,1", "1,3,5,7,9", -1);
|
||||
TEST ("1,3,10,14,18", "2,3,4", ">>=", "1,1,1", "1,3,5,7,9", -1);
|
||||
|
||||
// exercise a compound assignment, i.e.,
|
||||
// ({10,20,30,40,50}[{1,3,5}] >>= {1,1,1}) <<= {1,1,1}
|
||||
TEST ("10,11,12,13,14", "0,2,4", ">>=,<<=", "1,2,3", "10,11,12,13,8", -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
// FIXME: add command line options to enable/disable each operator
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"valarray.indirect.array",
|
||||
0 /* no comment */,
|
||||
run_test,
|
||||
"",
|
||||
(void*)0 /* sentinel */);
|
||||
}
|
||||
389
extern/stdcxx/4.2.1/tests/numerics/26.inner.product.cpp
vendored
Normal file
389
extern/stdcxx/4.2.1/tests/numerics/26.inner.product.cpp
vendored
Normal file
@@ -0,0 +1,389 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.inner.product.cpp - test exercising 26.4.2 [lib.inner.product]
|
||||
*
|
||||
* $Id: 26.inner.product.cpp 510970 2007-02-23 14:57:45Z faridz $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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.
|
||||
*
|
||||
* Copyright 2006 Rogue Wave Software.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
#include <numeric> // for inner_product
|
||||
#include <cstddef> // for size_t
|
||||
|
||||
#include <alg_test.h>
|
||||
#include <rw_value.h> // for UserClass
|
||||
#include <driver.h> // for rw_test()
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// plus-assign
|
||||
template <class T>
|
||||
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;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
const plus_asgn<T>&
|
||||
operator* (const plus_asgn<T> &lhs, const plus_asgn<T> &rhs)
|
||||
{
|
||||
_RWSTD_UNUSED (rhs);
|
||||
|
||||
return lhs;
|
||||
}
|
||||
|
||||
|
||||
_RWSTD_NAMESPACE (std) {
|
||||
|
||||
// disable explicit instantiation for compilers (like MSVC)
|
||||
// that can't handle it
|
||||
#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
|
||||
|
||||
template
|
||||
plus_asgn<assign<base<cpy_ctor> > >
|
||||
inner_product (InputIter<plus_asgn<assign<base<cpy_ctor> > > >,
|
||||
InputIter<plus_asgn<assign<base<cpy_ctor> > > >,
|
||||
InputIter<plus_asgn<assign<base<cpy_ctor> > > >,
|
||||
plus_asgn<assign<base<cpy_ctor> > >);
|
||||
|
||||
template
|
||||
assign<base<cpy_ctor> >
|
||||
inner_product (InputIter<assign<base<cpy_ctor> > >,
|
||||
InputIter<assign<base<cpy_ctor> > >,
|
||||
InputIter<assign<base<cpy_ctor> > >,
|
||||
assign<base<cpy_ctor> >,
|
||||
binary_func<assign<base<cpy_ctor> > >,
|
||||
binary_func<assign<base<cpy_ctor> > >);
|
||||
|
||||
#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
|
||||
|
||||
} // namespace std
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
UserClass operator* (const UserClass& lhs, const UserClass& rhs)
|
||||
{
|
||||
return UserClass (lhs) *= rhs;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
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 UserClass
|
||||
conv_to_T<UserClass> operator() (const UserClass &x,
|
||||
const UserClass &y) /* non-const */ {
|
||||
++funcalls_;
|
||||
UserClass res (x);
|
||||
res.data_.val_ += y.data_.val_;
|
||||
return conv_to_T<UserClass>::make (res);
|
||||
}
|
||||
|
||||
private:
|
||||
void operator= (Accumulator&); // not assignable
|
||||
};
|
||||
|
||||
std::size_t Accumulator::funcalls_;
|
||||
|
||||
|
||||
struct Multiplicator
|
||||
{
|
||||
static std::size_t funcalls_;
|
||||
|
||||
// dummy arguments provided to prevent the class from being
|
||||
// default constructible and implicit conversion from int
|
||||
Multiplicator (int /* dummy */, int /* dummy */) {
|
||||
funcalls_ = 0;
|
||||
}
|
||||
|
||||
// return a type convertible to UserClass
|
||||
conv_to_T<UserClass> operator() (const UserClass &x,
|
||||
const UserClass &y) /* non-const */ {
|
||||
++funcalls_;
|
||||
UserClass res (x);
|
||||
res.data_.val_ *= y.data_.val_;
|
||||
return conv_to_T<UserClass>::make (res);
|
||||
}
|
||||
|
||||
private:
|
||||
void operator= (Multiplicator&); // not assignable
|
||||
};
|
||||
|
||||
std::size_t Multiplicator::funcalls_;
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
struct InnerProductBase
|
||||
{
|
||||
virtual ~InnerProductBase() { /* no-op */ }
|
||||
|
||||
const char* iter_names [2];
|
||||
|
||||
// pure virtual
|
||||
virtual UserClass
|
||||
inner_product (const UserClass *xsrc1, const UserClass *xsrc1_end,
|
||||
const UserClass *xsrc2, const UserClass *xsrc2_end,
|
||||
const UserClass& init, const Accumulator *op1,
|
||||
const Multiplicator *op2) const = 0;
|
||||
};
|
||||
|
||||
template <class InputIterator1, class InputIterator2>
|
||||
struct InnerProduct : InnerProductBase
|
||||
{
|
||||
InnerProduct () {
|
||||
iter_names [0] = type_name (InputIterator1 (0, 0, 0), (UserClass*)0);
|
||||
iter_names [1] = type_name (InputIterator2 (0, 0, 0), (UserClass*)0);
|
||||
}
|
||||
|
||||
virtual UserClass
|
||||
inner_product (const UserClass *xsrc1, const UserClass *xsrc1_end,
|
||||
const UserClass *xsrc2, const UserClass *xsrc2_end,
|
||||
const UserClass& init, const Accumulator *op1,
|
||||
const Multiplicator *op2) const {
|
||||
|
||||
const InputIterator1 first1 (xsrc1, xsrc1, xsrc1_end);
|
||||
const InputIterator1 last1 (xsrc1_end, xsrc1, xsrc1_end);
|
||||
const InputIterator2 first2 (xsrc2, xsrc2, xsrc2_end);
|
||||
|
||||
const UserClass res = op1 ?
|
||||
std::inner_product (first1, last1, first2, init, *op1, *op2)
|
||||
: std::inner_product (first1, last1, first2, init);
|
||||
|
||||
// silence EDG eccp 3.7 and prior remark #550-D:
|
||||
// variable was set but never used
|
||||
_RWSTD_UNUSED (res);
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// exercises inner_product (26.4.2)
|
||||
void test_inner_product (const std::size_t N,
|
||||
const InnerProductBase &alg,
|
||||
bool binop)
|
||||
{
|
||||
const char* const it1name = alg.iter_names [0];
|
||||
const char* const it2name = alg.iter_names [1];
|
||||
const char* const tname = "UserClass";
|
||||
const char* const op1name = "Plus";
|
||||
const char* const op2name = "Multiple";
|
||||
|
||||
rw_info (0, 0, 0,
|
||||
"std::inner_product (%s, %1$s, %s, %s%{?}, %s, %s%{;})",
|
||||
it1name, it2name, tname, binop, op1name, op2name);
|
||||
|
||||
// construct initial UserClass
|
||||
const UserClass init = UserClass ();
|
||||
int sum = init.data_.val_;
|
||||
|
||||
UserClass::gen_ = gen_seq;
|
||||
|
||||
UserClass* const buf1 = new UserClass [N];
|
||||
UserClass* const buf2 = new UserClass [N];
|
||||
|
||||
for (std::size_t i = 0; i != N; ++i) {
|
||||
|
||||
UserClass* const buf1_end = buf1 + i;
|
||||
UserClass* const buf2_end = buf2 + i;
|
||||
|
||||
const Accumulator acc (0, 0);
|
||||
const Multiplicator mult (0, 0);
|
||||
|
||||
const Accumulator* const pbinop1 = binop ? &acc : 0;
|
||||
const Multiplicator* const pbinop2 = binop ? &mult : 0;
|
||||
|
||||
const UserClass res = alg.inner_product (buf1, buf1_end, buf2, buf2_end,
|
||||
init, pbinop1, pbinop2);
|
||||
|
||||
// verify the result 26.4.1, p1
|
||||
bool success = sum == res.data_.val_;
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"inner_product <%s, %s, %s%{?}, %s, %s%{;}>"
|
||||
"({%{X=+*}}, {%{X=+*}}) == %d, got %d",
|
||||
it1name, it2name, tname, binop, op1name, op2name,
|
||||
int (i), buf1, int (i), buf2, sum, res.data_.val_);
|
||||
|
||||
sum += (buf1 [i].data_.val_ * buf2 [i].data_.val_);
|
||||
|
||||
if (!success)
|
||||
break;
|
||||
}
|
||||
|
||||
delete[] buf1;
|
||||
delete[] buf2;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
/* extern */ int rw_opt_nloops = 64; // --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 <class InputIterator1, class InputIterator2>
|
||||
void gen_inner_product_test (const std::size_t N,
|
||||
const InputIterator1&,
|
||||
const InputIterator2&,
|
||||
bool binop)
|
||||
{
|
||||
const InnerProduct<InputIterator1, InputIterator2> alg;
|
||||
|
||||
test_inner_product (N, alg, binop);
|
||||
}
|
||||
|
||||
|
||||
template <class InputIterator1>
|
||||
void gen_inner_product_test (const std::size_t N,
|
||||
const InputIterator1 &it1,
|
||||
bool binop)
|
||||
{
|
||||
if (0 == rw_opt_no_input_iter)
|
||||
gen_inner_product_test (
|
||||
N, it1, InputIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_fwd_iter)
|
||||
gen_inner_product_test (
|
||||
N, it1, ConstFwdIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_bidir_iter)
|
||||
gen_inner_product_test (
|
||||
N, it1, ConstBidirIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_rnd_iter)
|
||||
gen_inner_product_test (
|
||||
N, it1, ConstRandomAccessIter<UserClass>(0, 0, 0), binop);
|
||||
}
|
||||
|
||||
// generates a specialization of the inner_product test for each of the required
|
||||
// iterator categopries
|
||||
void gen_inner_product_test (const std::size_t N,
|
||||
bool binop)
|
||||
{
|
||||
rw_info (0, 0, 0,
|
||||
"template <class %s, class %s, class %s%{?}, class %s, "
|
||||
"class %s%{;}> %3$s inner_product (%1$s, %1$s, %2$s, "
|
||||
"%3$s%{?}, %s, %s%{;})",
|
||||
"InputIterator1", "InputIterator2", "UserClass",
|
||||
binop, "BinaryOperation1", "BinaryOperation2", binop,
|
||||
"BinaryOperation1", "BinaryOperation2");
|
||||
|
||||
if (rw_opt_no_input_iter)
|
||||
rw_note (0, 0, 0, "InputIterator test disabled");
|
||||
else
|
||||
gen_inner_product_test (N, InputIter<UserClass>(0, 0, 0), binop);
|
||||
|
||||
if (rw_opt_no_fwd_iter)
|
||||
rw_note (0, 0, 0, "ForwardIterator test disabled");
|
||||
else
|
||||
gen_inner_product_test (N, ConstFwdIter<UserClass>(0, 0, 0), binop);
|
||||
|
||||
if (rw_opt_no_bidir_iter)
|
||||
rw_note (0, 0, 0, "BidirectionalIterator test disabled");
|
||||
else
|
||||
gen_inner_product_test (N, ConstBidirIter<UserClass>(0, 0, 0), binop);
|
||||
|
||||
if (rw_opt_no_rnd_iter)
|
||||
rw_note (0, 0, 0, "RandomAccessIterator test disabled");
|
||||
else
|
||||
gen_inner_product_test (N, ConstRandomAccessIter<UserClass>(0, 0, 0),
|
||||
binop);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char*[])
|
||||
{
|
||||
const std::size_t N = std::size_t (rw_opt_nloops);
|
||||
|
||||
gen_inner_product_test (N, false);
|
||||
|
||||
if (rw_opt_no_binary_op)
|
||||
rw_note (0, 0, 0,
|
||||
"inner_product with binary operations test disabled");
|
||||
else
|
||||
gen_inner_product_test (N, true);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"lib.inner.product",
|
||||
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);
|
||||
}
|
||||
513
extern/stdcxx/4.2.1/tests/numerics/26.mask.array.cpp
vendored
Normal file
513
extern/stdcxx/4.2.1/tests/numerics/26.mask.array.cpp
vendored
Normal file
@@ -0,0 +1,513 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.template.mask.array.cpp - tests exercising class template
|
||||
* mask_array
|
||||
*
|
||||
* $Id: 26.mask.array.cpp 493625 2007-01-07 00:46:13Z sebor $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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 <cstdlib> // for strtol(), size_t
|
||||
#include <valarray> // for mask_array, valarray
|
||||
|
||||
#include <driver.h>
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns a valarray constructed from a string of comma-separated values
|
||||
// if there are fewer than n values in the string the remaining values are
|
||||
// taken to be the same as the last one, i.e., the call
|
||||
// make_array("1,2,3", 5) returns valarray({1,2,3,3})
|
||||
template <class T>
|
||||
std::valarray<T>
|
||||
make_array (const char *s, std::size_t n = 0)
|
||||
{
|
||||
if (0 == s || '\0' == *s)
|
||||
return n ? std::valarray<T>(T (), n) : std::valarray<T>();
|
||||
|
||||
T buf [256];
|
||||
|
||||
for (std::size_t i = 0; ; ++i) {
|
||||
|
||||
char *end;
|
||||
long val = std::strtol (s, &end, 0);
|
||||
|
||||
RW_ASSERT (s < end);
|
||||
RW_ASSERT ('\0' == *end || ',' == *end);
|
||||
|
||||
buf [i] = T (val);
|
||||
|
||||
if ('\0' == *end) {
|
||||
while (++i < n)
|
||||
buf [i] = buf [i - 1];
|
||||
|
||||
return std::valarray<T>(buf, i);
|
||||
}
|
||||
|
||||
s = end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::valarray<bool>
|
||||
make_bool_array (const char *s, std::size_t n = 0)
|
||||
{
|
||||
typedef std::valarray<bool> BoolArray;
|
||||
|
||||
if (0 == s || '\0' == *s)
|
||||
return n ? BoolArray(false, n) : BoolArray ();
|
||||
|
||||
bool buf [256];
|
||||
|
||||
for (std::size_t i = 0; ; ++i) {
|
||||
|
||||
RW_ASSERT ('0' == *s || '1' == *s);
|
||||
|
||||
buf [i] = '1' == *s++;
|
||||
|
||||
if ('\0' == *s) {
|
||||
while (++i < n)
|
||||
buf [i] = buf [i - 1];
|
||||
|
||||
return BoolArray (buf, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static void
|
||||
test_mask_array (int line,
|
||||
const char *array,
|
||||
const char *mask,
|
||||
const char *opname,
|
||||
const char *args,
|
||||
const char *result,
|
||||
int nmatch)
|
||||
|
||||
{
|
||||
typedef int T;
|
||||
|
||||
const char tname[] = "int";
|
||||
|
||||
/* */ std::valarray<T> asrc (make_array<int>(array));
|
||||
const std::valarray<bool> amask (make_bool_array (mask));
|
||||
const std::valarray<T> aarg (make_array<int>(args));
|
||||
|
||||
if (0 == opname)
|
||||
opname = "";
|
||||
|
||||
// construct the expected result
|
||||
const std::valarray<T> expect =
|
||||
make_array<int>(result, *opname ? 0 : asrc.size ());
|
||||
|
||||
// create an mask array object from the source array
|
||||
// and the array of indices
|
||||
const std::mask_array<T> ia (asrc [amask]);
|
||||
|
||||
// iterate over the series of assignment operators, invoking
|
||||
// each in sequence on the source array, modifying it in place
|
||||
for (const char *op = opname; *op; ) {
|
||||
|
||||
// skip optional whitespace
|
||||
for ( ; ' ' == *op; ++op);
|
||||
|
||||
switch (*op) {
|
||||
// invoke fill assignment embedded within a series
|
||||
// of compound assignments, e.g., "*=,,+=" meaning
|
||||
// ia *= aarg; ia = aarg [0]; ia += aarg
|
||||
case ',': ia = aarg.size () ? aarg [0] : 0; break;
|
||||
|
||||
// invoke ordinary assignment
|
||||
case '=': ia = aarg; op += 1; break;
|
||||
|
||||
// invoke computed assignment
|
||||
case '*': ia *= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '/': ia /= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '%': ia %= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '+': ia += aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '-': ia -= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '^': ia ^= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '&': ia &= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '|': ia |= aarg; RW_ASSERT ('=' == op [1]); op += 2; break;
|
||||
case '<': ia <<= aarg; RW_ASSERT ('=' == op [2]); op += 3; break;
|
||||
case '>': ia >>= aarg; RW_ASSERT ('=' == op [2]); op += 3; break;
|
||||
default:
|
||||
RW_ASSERT (!"unknown operator");
|
||||
}
|
||||
|
||||
// skip optional whitespace
|
||||
for ( ; ' ' == *op; ++op);
|
||||
|
||||
// assignment operators are separated by commas
|
||||
if (',' == *op)
|
||||
++op;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if ('\0' == *opname) {
|
||||
// fill assignment
|
||||
ia = aarg.size () ? aarg [0] : 0;
|
||||
}
|
||||
|
||||
int nequal = 0;
|
||||
for (std::size_t i = 0; i != asrc.size (); ++i, ++nequal) {
|
||||
if (asrc [i] != expect [i])
|
||||
break;
|
||||
}
|
||||
|
||||
if (-1 == nmatch)
|
||||
nmatch = int (asrc.size ());
|
||||
|
||||
const int* const expect_begin = expect.size () ? &expect [0] : 0;
|
||||
const int* const actual_begin = asrc.size () ? &asrc [0] : 0;
|
||||
|
||||
if ('=' == opname [0] && '\0' == opname [1]) {
|
||||
rw_assert (nequal == nmatch, 0, line,
|
||||
"valarray<%s>({%s})[valarray<bool>({%s})] %s "
|
||||
"%d %c= {%{*.*Ad}}, got {%{*.*Ad}}",
|
||||
tname, array, mask, opname,
|
||||
aarg.size () ? aarg [0] : 0,
|
||||
nmatch == int (asrc.size ()) ? '=' : '!',
|
||||
int (sizeof (int)), int (expect.size ()), expect_begin,
|
||||
int (sizeof (int)), int (asrc.size ()), actual_begin);
|
||||
}
|
||||
else {
|
||||
rw_assert (nequal == nmatch, 0, line,
|
||||
"valarray<%s>({%s})[valarray<bool>({%s})] %s "
|
||||
"valarray<%1$s>({%s}) %c= {%{*.*Ad}}, got {%{*.*Ad}}",
|
||||
tname, array, mask, opname, result,
|
||||
nmatch == int (asrc.size ()) ? '=' : '!',
|
||||
int (sizeof (int)), int (expect.size ()), expect_begin,
|
||||
int (sizeof (int)), int (asrc.size ()), actual_begin);
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char**)
|
||||
{
|
||||
#define TEST(array, mask, op, arg, res) \
|
||||
test_mask_array (__LINE__, array, mask, op, arg, res, -1)
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array<T>::operator=(T), fill assignment
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::mask_array<int>::operator=(int)");
|
||||
|
||||
// +-- source array
|
||||
// | +-- mask array (bitset of 0's and 1's)
|
||||
// | | +-- zero for fill assignment
|
||||
// | | | +-- argument of fill assignment
|
||||
// | | | | +-- expected result
|
||||
// | | | | |
|
||||
// | | | | |
|
||||
// V V V V V
|
||||
TEST ("", "", 0, "0", "");
|
||||
TEST ("1", "", 0, "0", "1");
|
||||
|
||||
TEST ("1", "1", 0, "2", "2");
|
||||
TEST ("1,2", "10", 0, "3", "3,2");
|
||||
TEST ("1,2,3", "100", 0, "4", "4,2,3");
|
||||
TEST ("1,2,3,4", "1000", 0, "5", "5,2,3,4");
|
||||
TEST ("1,2,3,4,5", "10000", 0, "6", "6,2,3,4,5");
|
||||
|
||||
TEST ("1,2,3,4,5", "11000", 0, "6", "6,6,3,4,5");
|
||||
TEST ("1,2,3,4,5", "10100", 0, "6", "6,2,6,4,5");
|
||||
TEST ("1,2,3,4,5", "10010", 0, "6", "6,2,3,6,5");
|
||||
TEST ("1,2,3,4,5", "10001", 0, "6", "6,2,3,4,6");
|
||||
|
||||
TEST ("1,2,3,4,5", "11100", 0, "6", "6,6,6,4,5");
|
||||
TEST ("1,2,3,4,5", "11010", 0, "6", "6,6,3,6,5");
|
||||
TEST ("1,2,3,4,5", "11001", 0, "6", "6,6,3,4,6");
|
||||
|
||||
TEST ("1,2,3,4,5", "10110", 0, "6", "6,2,6,6,5");
|
||||
TEST ("1,2,3,4,5", "10101", 0, "6", "6,2,6,4,6");
|
||||
|
||||
TEST ("1,2,3,4,5", "11010", 0, "6", "6,6,3,6,5");
|
||||
TEST ("1,2,3,4,5", "10110", 0, "6", "6,2,6,6,5");
|
||||
TEST ("1,2,3,4,5", "10011", 0, "6", "6,2,3,6,6");
|
||||
|
||||
#define INFO(op) \
|
||||
rw_info (0, 0, __LINE__, \
|
||||
"std::mask_array<int>::operator%s(std::valarray<int>)", op);
|
||||
|
||||
INFO ("=");
|
||||
|
||||
// +-- source array
|
||||
// | +-- array of indices (mask_array ctor argument)
|
||||
// | | +-- assignment operators to test (0 for none)
|
||||
// | | | +-- argument of assignment (0 for none)
|
||||
// | | | | +-- expected result
|
||||
// | | | | |
|
||||
// | | | | |
|
||||
// V V V V V
|
||||
TEST ("", "", "=", "", "");
|
||||
TEST ("1", "", "=", "", "1");
|
||||
TEST ("1", "1", "=", "2", "2");
|
||||
TEST ("1", "1", "=", "3", "3");
|
||||
TEST ("2", "1", "=", "4", "4");
|
||||
|
||||
TEST ("1,2", "", "=", "", "1,2");
|
||||
TEST ("1,2", "01", "=", "3", "1,3");
|
||||
TEST ("1,2", "10", "=", "3", "3,2");
|
||||
TEST ("1,2", "11", "=", "3,4", "3,4");
|
||||
|
||||
TEST ("1,2,3", "", "=", "", "1,2,3");
|
||||
TEST ("1,2,3", "000", "=", "", "1,2,3");
|
||||
TEST ("1,2,3", "001", "=", "6", "1,2,6");
|
||||
TEST ("1,2,3", "010", "=", "6", "1,6,3");
|
||||
TEST ("1,2,3", "011", "=", "6,5", "1,6,5");
|
||||
TEST ("1,2,3", "100", "=", "6", "6,2,3");
|
||||
TEST ("1,2,3", "101", "=", "6,5", "6,2,5");
|
||||
TEST ("1,2,3", "110", "=", "6,5", "6,5,3");
|
||||
TEST ("1,2,3", "111", "=", "6,5,4", "6,5,4");
|
||||
|
||||
TEST ("1,2,3,4", "", "=", "", "1,2,3,4");
|
||||
TEST ("1,2,3,4", "0", "=", "", "1,2,3,4");
|
||||
TEST ("1,2,3,4", "1", "=", "5", "5,2,3,4");
|
||||
TEST ("1,2,3,4", "00", "=", "", "1,2,3,4");
|
||||
TEST ("1,2,3,4", "01", "=", "5", "1,5,3,4");
|
||||
TEST ("1,2,3,4", "10", "=", "5", "5,2,3,4");
|
||||
TEST ("1,2,3,4", "11", "=", "5,6", "5,6,3,4");
|
||||
|
||||
TEST ("1,2,3,4", "000", "=", "", "1,2,3,4");
|
||||
TEST ("1,2,3,4", "001", "=", "5", "1,2,5,4");
|
||||
TEST ("1,2,3,4", "010", "=", "5", "1,5,3,4");
|
||||
TEST ("1,2,3,4", "011", "=", "5,6", "1,5,6,4");
|
||||
TEST ("1,2,3,4", "100", "=", "5", "5,2,3,4");
|
||||
TEST ("1,2,3,4", "101", "=", "5,6", "5,2,6,4");
|
||||
TEST ("1,2,3,4", "110", "=", "5,6", "5,6,3,4");
|
||||
TEST ("1,2,3,4", "111", "=", "5,6,7", "5,6,7,4");
|
||||
|
||||
TEST ("1,2,3,4", "0000", "=", "", "1,2,3,4");
|
||||
TEST ("1,2,3,4", "0001", "=", "5", "1,2,3,5");
|
||||
TEST ("1,2,3,4", "0010", "=", "5", "1,2,5,4");
|
||||
TEST ("1,2,3,4", "0011", "=", "5,6", "1,2,5,6");
|
||||
TEST ("1,2,3,4", "0100", "=", "5", "1,5,3,4");
|
||||
TEST ("1,2,3,4", "0101", "=", "5,6", "1,5,3,6");
|
||||
TEST ("1,2,3,4", "0110", "=", "5,6", "1,5,6,4");
|
||||
TEST ("1,2,3,4", "0111", "=", "5,6,7", "1,5,6,7");
|
||||
|
||||
TEST ("1,2,3,4", "1000", "=", "5", "5,2,3,4");
|
||||
TEST ("1,2,3,4", "1001", "=", "5,6", "5,2,3,6");
|
||||
TEST ("1,2,3,4", "1010", "=", "5,6", "5,2,6,4");
|
||||
TEST ("1,2,3,4", "1011", "=", "5,6,7", "5,2,6,7");
|
||||
TEST ("1,2,3,4", "1100", "=", "5,6", "5,6,3,4");
|
||||
TEST ("1,2,3,4", "1101", "=", "5,6,7", "5,6,3,7");
|
||||
TEST ("1,2,3,4", "1110", "=", "5,6,7", "5,6,7,4");
|
||||
TEST ("1,2,3,4", "1111", "=", "5,6,7,8", "5,6,7,8");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator*=()
|
||||
INFO ("*=");
|
||||
|
||||
TEST ("", "", "*=", "", "");
|
||||
TEST ("1", "", "*=", "", "1");
|
||||
TEST ("1", "0", "*=", "", "1");
|
||||
TEST ("1", "0", "*=", "5", "1");
|
||||
TEST ("1", "1", "*=", "5", "5");
|
||||
|
||||
TEST ("1,2", "", "*=", "", "1, 2");
|
||||
TEST ("1,2", "0", "*=", "", "1, 2");
|
||||
TEST ("1,2", "00", "*=", "", "1, 2");
|
||||
TEST ("1,2", "01", "*=", "5", "1,10");
|
||||
TEST ("1,2", "10", "*=", "5", "5, 2");
|
||||
TEST ("1,2", "11", "*=", "5,6", "5,12");
|
||||
|
||||
TEST ("1,2,3", "", "*=", "", "1, 2, 3");
|
||||
TEST ("1,2,3", "0", "*=", "", "1, 2, 3");
|
||||
TEST ("1,2,3", "00", "*=", "", "1, 2, 3");
|
||||
TEST ("1,2,3", "000", "*=", "", "1, 2, 3");
|
||||
TEST ("1,2,3", "001", "*=", "5", "1, 2,15");
|
||||
TEST ("1,2,3", "010", "*=", "5", "1,10, 3");
|
||||
TEST ("1,2,3", "011", "*=", "5,6", "1,10,18");
|
||||
TEST ("1,2,3", "100", "*=", "5", "5, 2, 3");
|
||||
TEST ("1,2,3", "101", "*=", "5,6", "5, 2,18");
|
||||
TEST ("1,2,3", "110", "*=", "5,6", "5,12, 3");
|
||||
TEST ("1,2,3", "111", "*=", "5,6,7", "5,12,21");
|
||||
|
||||
TEST ("1,2,3,4", "", "*=", "", "1, 2, 3, 4");
|
||||
TEST ("1,2,3,4", "0", "*=", "", "1, 2, 3, 4");
|
||||
TEST ("1,2,3,4", "00", "*=", "", "1, 2, 3, 4");
|
||||
TEST ("1,2,3,4", "000", "*=", "", "1, 2, 3, 4");
|
||||
TEST ("1,2,3,4", "0000", "*=", "", "1, 2, 3, 4");
|
||||
TEST ("1,2,3,4", "0001", "*=", "5", "1, 2, 3,20");
|
||||
TEST ("1,2,3,4", "0010", "*=", "5", "1, 2,15, 4");
|
||||
TEST ("1,2,3,4", "0011", "*=", "5,6", "1, 2,15,24");
|
||||
TEST ("1,2,3,4", "0100", "*=", "5", "1,10, 3, 4");
|
||||
TEST ("1,2,3,4", "0101", "*=", "5,6", "1,10, 3,24");
|
||||
TEST ("1,2,3,4", "0110", "*=", "5,6", "1,10,18, 4");
|
||||
TEST ("1,2,3,4", "0111", "*=", "5,6,7", "1,10,18,28");
|
||||
TEST ("1,2,3,4", "1001", "*=", "5,6", "5, 2, 3,24");
|
||||
TEST ("1,2,3,4", "1010", "*=", "5,6", "5, 2,18, 4");
|
||||
TEST ("1,2,3,4", "1011", "*=", "5,6,7", "5, 2,18,28");
|
||||
TEST ("1,2,3,4", "1100", "*=", "5,6", "5,12, 3, 4");
|
||||
TEST ("1,2,3,4", "1101", "*=", "5,6,7", "5,12, 3,28");
|
||||
TEST ("1,2,3,4", "1110", "*=", "5,6,7", "5,12,21, 4");
|
||||
TEST ("1,2,3,4", "1111", "*=", "5,6,7,8", "5,12,21,32");
|
||||
|
||||
// exercise compound assignment, i.e.,
|
||||
// ({1,2,3,4,5,6}[{0,1,0,1,0,1}] *= {0,1,2}) *= {0,1,2}
|
||||
TEST ("1,2,3,4,5,6", "010101", "*=,*=", "0,1,2", "1,0,3,4,5,24");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator/=()
|
||||
INFO ("/=");
|
||||
|
||||
TEST ("", "", "/=", "", "");
|
||||
TEST ("10", "", "/=", "", "10");
|
||||
TEST ("10", "0", "/=", "", "10");
|
||||
TEST ("10", "0", "/=", "5", "10");
|
||||
TEST ("10", "1", "/=", "5", " 2");
|
||||
|
||||
TEST ("10,20", "", "/=", "", "10,20");
|
||||
TEST ("10,20", "0", "/=", "", "10,20");
|
||||
TEST ("10,20", "00", "/=", "", "10,20");
|
||||
TEST ("10,20", "01", "/=", "5", "10, 4");
|
||||
TEST ("10,20", "10", "/=", "5", " 2,20");
|
||||
TEST ("10,20", "11", "/=", "5,6", " 2, 3");
|
||||
|
||||
TEST ("10,20,30", "", "/=", "", "10,20,30");
|
||||
TEST ("10,20,30", "0", "/=", "", "10,20,30");
|
||||
TEST ("10,20,30", "00", "/=", "", "10,20,30");
|
||||
TEST ("10,20,30", "000", "/=", "", "10,20,30");
|
||||
TEST ("10,20,30", "001", "/=", "5", "10,20, 6");
|
||||
TEST ("10,20,30", "010", "/=", "5", "10, 4,30");
|
||||
TEST ("10,20,30", "011", "/=", "5,6", "10, 4, 5");
|
||||
TEST ("10,20,30", "100", "/=", "5", " 2,20,30");
|
||||
TEST ("10,20,30", "101", "/=", "5,6", " 2,20, 5");
|
||||
TEST ("10,20,30", "110", "/=", "5,6", " 2, 3,30");
|
||||
TEST ("10,20,30", "111", "/=", "5,6,7", " 2, 3, 4");
|
||||
|
||||
TEST ("10,20,30,40", "", "/=", "", "10,20,30,40");
|
||||
TEST ("10,20,30,40", "0", "/=", "", "10,20,30,40");
|
||||
TEST ("10,20,30,40", "00", "/=", "", "10,20,30,40");
|
||||
TEST ("10,20,30,40", "000", "/=", "", "10,20,30,40");
|
||||
TEST ("10,20,30,40", "0000", "/=", "", "10,20,30,40");
|
||||
TEST ("10,20,30,40", "0001", "/=", "5", "10,20,30, 8");
|
||||
TEST ("10,20,30,40", "0010", "/=", "5", "10,20, 6,40");
|
||||
TEST ("10,20,30,40", "0011", "/=", "5,6", "10,20, 6, 6");
|
||||
TEST ("10,20,30,40", "0100", "/=", "5", "10, 4,30,40");
|
||||
TEST ("10,20,30,40", "0101", "/=", "5,6", "10, 4,30, 6");
|
||||
TEST ("10,20,30,40", "0110", "/=", "5,6", "10, 4, 5,40");
|
||||
TEST ("10,20,30,40", "0111", "/=", "5,6,7", "10, 4, 5, 5");
|
||||
TEST ("10,20,30,40", "1000", "/=", "5", " 2,20,30,40");
|
||||
TEST ("10,20,30,40", "1001", "/=", "5,6", " 2,20,30, 6");
|
||||
TEST ("10,20,30,40", "1010", "/=", "5,6", " 2,20, 5,40");
|
||||
TEST ("10,20,30,40", "1011", "/=", "5,6,7", " 2,20, 5, 5");
|
||||
TEST ("10,20,30,40", "1100", "/=", "5,6", " 2, 3,30,40");
|
||||
TEST ("10,20,30,40", "1101", "/=", "5,6,7", " 2, 3,30, 5");
|
||||
TEST ("10,20,30,40", "1110", "/=", "5,6,7", " 2, 3, 4,40");
|
||||
TEST ("10,20,30,40", "1111", "/=", "5,6,7,8", " 2, 3, 4, 5");
|
||||
|
||||
// exercise compound assignment, i.e.,
|
||||
// ({1,2,3,4,5,6}[{0,1,0,1,0,1}] *= {7,8,9}) /= {7,8,9}
|
||||
TEST ("1,2,3,4,5,6", "010101", "*=,/=", "7,8,9", "1,2,3,4,5,6");
|
||||
// ({1,20,3,40,5,60}[{0,1,0,1,0,1}] /= {7,8,9}) *= {7,8,9}
|
||||
TEST ("1,20,3,40,5,60", "010101", "/=,*=", "7,8,9", "1,14,3,40,5,54");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator%=()
|
||||
INFO ("%=");
|
||||
|
||||
TEST ("1, 2,3, 4,5, 6", "010101", "%=", "3,5,7", "1,2,3,4,5,6");
|
||||
TEST ("1,11,2,13,4,15", "010101", "%=", "6,7,8", "1,5,2,6,4,7");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator+=()
|
||||
INFO ("+=");
|
||||
|
||||
TEST ("1,2,3,4,5,6", "010101", "+=", "7,8,9", "1,9,3,12,5,15");
|
||||
TEST ("1,2,3,4,5,6", "010101", "+=,+=", "1,2,3", "1,4,3, 8,5,12");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator-=()
|
||||
INFO ("-=");
|
||||
|
||||
TEST ("9,8,7,6,5,4", "111000", "-=", "9,8,7", "0,0,0,6,5,4");
|
||||
TEST ("9,8,7,6,5,4", "011100", "-=", "8,7,6", "9,0,0,0,5,4");
|
||||
TEST ("9,8,7,6,5,4", "001110", "-=", "7,6,5", "9,8,0,0,0,4");
|
||||
TEST ("9,8,7,6,5,4", "000111", "-=", "6,5,4", "9,8,7,0,0,0");
|
||||
TEST ("9,8,7,6,5,4", "010101", "-=", "3,2,1", "9,5,7,4,5,3");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator^=()
|
||||
INFO ("^=");
|
||||
|
||||
TEST ("1,3,5,7,9", "10101", "^=", "2,4,5", "3,3,1,7,12");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator&=()
|
||||
INFO ("&=");
|
||||
|
||||
TEST ("1,3,5,7,9", "10101", "&=", "3,1,7", "1,3,1,7,1");
|
||||
TEST ("1,2,3,4,5", "01010", "&=", "1,1", "1,0,3,0,5");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator|=()
|
||||
INFO ("|=");
|
||||
|
||||
TEST ("1,3,5,7,9", "10101", "|=", "3,1,7", "3,3,5,7,15");
|
||||
TEST ("1,2,3,4,5", "01010", "|=", "1,1", "1,3,3,5, 5");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator<<=()
|
||||
INFO ("<<=");
|
||||
|
||||
TEST ("1,3,5,7,9", "11100", "<<=", "1,1,1", " 2, 6,10, 7, 9");
|
||||
TEST ("1,3,5,7,9", "01110", "<<=", "1,1,1", " 1, 6,10,14, 9");
|
||||
TEST ("1,3,5,7,9", "00111", "<<=", "1,1,1", " 1, 3,10,14,18");
|
||||
TEST ("1,3,5,7,9", "11111", "<<=", "5,4,3,2,1", "32,48,40,28,18");
|
||||
|
||||
// exercise a compound assignment, i.e.,
|
||||
// ({1,3,5,7,9}[{2,3,4}] <<= {1,1,1}) >>= {1,1,1}
|
||||
TEST ("1,3,5,7,9", "00111", "<<=,>>=", "1,1,1", "1,3,5,7,9");
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// exercise mask_array::operator>>=()
|
||||
INFO (">>=");
|
||||
|
||||
TEST ("2,6,10, 7, 9", "11100", ">>=", "1,1,1", "1,3,5,7,9");
|
||||
TEST ("1,6,10,14, 9", "01110", ">>=", "1,1,1", "1,3,5,7,9");
|
||||
TEST ("1,3,10,14,18", "00111", ">>=", "1,1,1", "1,3,5,7,9");
|
||||
|
||||
// exercise a compound assignment, i.e.,
|
||||
// ({10,20,30,40,50}[{1,3,5}] >>= {1,1,1}) <<= {1,1,1}
|
||||
TEST ("10,11,12,13,14", "10101", ">>=,<<=", "1,2,3", "10,11,12,13,8");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
// FIXME: add command line options to enable/disable each operator
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"valarray.mask.array",
|
||||
0 /* no comment */,
|
||||
run_test,
|
||||
"",
|
||||
(void*)0 /* sentinel */);
|
||||
}
|
||||
398
extern/stdcxx/4.2.1/tests/numerics/26.partial.sum.cpp
vendored
Normal file
398
extern/stdcxx/4.2.1/tests/numerics/26.partial.sum.cpp
vendored
Normal file
@@ -0,0 +1,398 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.partial.sum.cpp - test exercising 26.4.3 [lib.partial.sum]
|
||||
*
|
||||
* $Id: 26.partial.sum.cpp 510970 2007-02-23 14:57:45Z faridz $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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.
|
||||
*
|
||||
* Copyright 2006 Rogue Wave Software.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
#include <numeric> // for partial_sum
|
||||
#include <cstddef> // for size_t
|
||||
|
||||
#include <alg_test.h>
|
||||
#include <rw_value.h> // for UserClass
|
||||
#include <driver.h> // for rw_test()
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// plus
|
||||
template <class T>
|
||||
struct plus: T
|
||||
{
|
||||
private:
|
||||
// unused member prevents bogus HP aCC warnings (see Onyx #23561)
|
||||
int unused;
|
||||
};
|
||||
|
||||
|
||||
template <class T>
|
||||
const plus<T>& operator+ (const plus<T>& lhs, const plus<T>& rhs)
|
||||
{
|
||||
_RWSTD_UNUSED (rhs);
|
||||
return lhs;
|
||||
}
|
||||
|
||||
_RWSTD_NAMESPACE (std) {
|
||||
|
||||
#ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION
|
||||
|
||||
template
|
||||
OutputIter<plus<assign<base<cpy_ctor> > > >
|
||||
partial_sum (InputIter<plus<assign<base<cpy_ctor> > > >,
|
||||
InputIter<plus<assign<base<cpy_ctor> > > >,
|
||||
OutputIter<plus<assign<base<cpy_ctor> > > >);
|
||||
|
||||
template
|
||||
OutputIter<assign<base<cpy_ctor> > >
|
||||
partial_sum (InputIter<assign<base<cpy_ctor> > >,
|
||||
InputIter<assign<base<cpy_ctor> > >,
|
||||
OutputIter<assign<base<cpy_ctor> > >,
|
||||
binary_func<assign<base<cpy_ctor> > >);
|
||||
|
||||
#endif // _RWSTD_NO_EXPLICIT_INSTANTIATION
|
||||
|
||||
} // namespace std
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
UserClass operator+ (const UserClass &lhs, const UserClass &rhs)
|
||||
{
|
||||
return UserClass (lhs)+= rhs;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
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 UserClass
|
||||
conv_to_T<UserClass> operator() (const UserClass &x,
|
||||
const UserClass &y) /* non-const */ {
|
||||
++funcalls_;
|
||||
UserClass res (x);
|
||||
res.data_.val_ += y.data_.val_;
|
||||
return conv_to_T<UserClass>::make (res);
|
||||
}
|
||||
|
||||
private:
|
||||
void operator= (Accumulator&); // not assignable
|
||||
};
|
||||
|
||||
std::size_t Accumulator::funcalls_;
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
struct PartialSumBase
|
||||
{
|
||||
virtual ~PartialSumBase() { /* no-op */ }
|
||||
|
||||
const char* iter_names [2];
|
||||
|
||||
// pure virtual
|
||||
virtual UserClass*
|
||||
partial_sum (const UserClass *xsrc, const UserClass *xsrc_end,
|
||||
UserClass *xdst, const UserClass *xdst_end,
|
||||
const Accumulator *op) const = 0;
|
||||
};
|
||||
|
||||
|
||||
template <class InputIterator, class OutputIterator>
|
||||
struct PartialSum: PartialSumBase
|
||||
{
|
||||
PartialSum () {
|
||||
iter_names [0] = type_name (InputIterator (0, 0, 0), (UserClass*)0);
|
||||
iter_names [1] = type_name (OutputIterator (0, 0, 0), (UserClass*)0);
|
||||
}
|
||||
|
||||
virtual UserClass*
|
||||
partial_sum (const UserClass *xsrc, const UserClass *xsrc_end,
|
||||
UserClass *xdst, const UserClass *xdst_end,
|
||||
const Accumulator *op) const {
|
||||
|
||||
const InputIterator first (xsrc, xsrc, xsrc_end);
|
||||
const InputIterator last (xsrc_end, xsrc, xsrc_end);
|
||||
const OutputIterator result (xdst, xdst, xdst_end);
|
||||
|
||||
const OutputIterator res = op ?
|
||||
std::partial_sum (first, last, result, *op)
|
||||
: std::partial_sum (first, last, result);
|
||||
|
||||
// silence EDG eccp 3.7 and prior remark #550-D:
|
||||
// variable was set but never used
|
||||
_RWSTD_UNUSED (res);
|
||||
|
||||
return res.cur_;
|
||||
}
|
||||
};
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// exercises partial_sum (26.4.3)
|
||||
void test_partial_sum (const std::size_t N,
|
||||
const PartialSumBase &alg,
|
||||
bool binop,
|
||||
bool same_seq)
|
||||
{
|
||||
const char* const itname = alg.iter_names [0];
|
||||
const char* const outname = alg.iter_names [1];
|
||||
const char* const opname = "Plus";
|
||||
|
||||
rw_info (0, 0, 0,
|
||||
"std::partial_sum (%s, %1$s, %s%{?}, %s%{;})%{?}, %s%{;}",
|
||||
itname, outname, binop, opname, same_seq, "first == result");
|
||||
|
||||
UserClass::gen_ = gen_seq;
|
||||
|
||||
UserClass* const src = new UserClass [N];
|
||||
UserClass* dst = same_seq ? src : new UserClass [N];
|
||||
|
||||
for (std::size_t i = 0; i != N; ++i) {
|
||||
|
||||
UserClass* const src_end = src + i;
|
||||
UserClass* const dst_end = dst + i;
|
||||
|
||||
std::size_t last_n_op_plus = UserClass::n_total_op_plus_assign_;
|
||||
|
||||
const Accumulator acc (0, 0);
|
||||
const Accumulator* const pbinop = binop ? &acc : 0;
|
||||
|
||||
std::size_t k = 0;
|
||||
int* const tmp_val = new int [i];
|
||||
for (; k < i; ++k)
|
||||
tmp_val [k] = src [k].data_.val_;
|
||||
|
||||
const UserClass* const res =
|
||||
alg.partial_sum (src, src_end, dst, dst_end, pbinop);
|
||||
|
||||
const std::size_t plus_ops = binop ? Accumulator::funcalls_ :
|
||||
UserClass::n_total_op_plus_assign_ - last_n_op_plus;
|
||||
|
||||
// verify the returned iterator 26.4.3, p2
|
||||
bool success = res == dst_end;
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"partial_sum<%s, %s%{?}, %s%{;}>"
|
||||
"({%{X=+*}}, ...) == result + %d, got result %td",
|
||||
itname, outname, binop, opname,
|
||||
int (i), src, dst_end - dst, res - dst_end);
|
||||
|
||||
int sum = 0;
|
||||
for (k = 0; k < i; ++k) {
|
||||
sum += tmp_val [k];
|
||||
success = dst [k].data_.val_ == sum;
|
||||
if (!success)
|
||||
break;
|
||||
}
|
||||
|
||||
// verify the result 26.4.3, p1
|
||||
if (i > 0) {
|
||||
// to avoid errors in --trace mode
|
||||
k = k < i ? k : i - 1;
|
||||
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"partial_sum<%s, %s%{?}, %s%{;}>"
|
||||
"({%{X=+*}}, ...) ==> {%{X=+*.*}}, expected %d",
|
||||
itname, outname, binop, opname,
|
||||
int (i), src, int (i), int (k), dst, sum);
|
||||
}
|
||||
|
||||
delete[] tmp_val;
|
||||
|
||||
if (!success)
|
||||
break;
|
||||
|
||||
// verify the complexity, 26.4.3, p3
|
||||
const std::size_t exp_plus_ops = i > 0 ? i - 1 : 0;
|
||||
success = plus_ops == exp_plus_ops;
|
||||
rw_assert (success, 0, __LINE__,
|
||||
"partial_sum <%s, %s%{?}, %s%{;}>"
|
||||
"({%{X=+*}}, ...) complexity: got %zu invocations "
|
||||
"of %s, expected %zu",
|
||||
itname, outname, binop, opname,
|
||||
int (i), src, plus_ops,
|
||||
binop ? "BinaryPlus" : "operator+", exp_plus_ops);
|
||||
|
||||
if (!success)
|
||||
break;
|
||||
}
|
||||
|
||||
delete[] src;
|
||||
|
||||
if (!same_seq)
|
||||
delete[] dst;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
/* extern */ int rw_opt_nloops = 64; // --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_output_iter; // --no-OutputIterator
|
||||
/* 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 <class InputIterator, class OutputIterator>
|
||||
void gen_partial_sum_test (const std::size_t N,
|
||||
const InputIterator&,
|
||||
const OutputIterator&,
|
||||
bool binop)
|
||||
{
|
||||
const PartialSum<InputIterator, OutputIterator> alg;
|
||||
|
||||
// test the algorithm than input and output arrays differ
|
||||
test_partial_sum (N, alg, binop, false);
|
||||
|
||||
// test the algorithm than input and output arrays are the same
|
||||
test_partial_sum (N, alg, binop, true);
|
||||
}
|
||||
|
||||
|
||||
template <class InputIterator>
|
||||
void gen_partial_sum_test (const std::size_t N,
|
||||
const InputIterator &it,
|
||||
bool binop)
|
||||
{
|
||||
if (0 == rw_opt_no_output_iter)
|
||||
gen_partial_sum_test (
|
||||
N, it, OutputIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_fwd_iter)
|
||||
gen_partial_sum_test (
|
||||
N, it, FwdIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_bidir_iter)
|
||||
gen_partial_sum_test (
|
||||
N, it, BidirIter<UserClass>(0, 0, 0), binop);
|
||||
if (0 == rw_opt_no_rnd_iter)
|
||||
gen_partial_sum_test (
|
||||
N, it, RandomAccessIter<UserClass>(0, 0, 0), binop);
|
||||
}
|
||||
|
||||
// generates a specialization of the partial_sum test for each of the required
|
||||
// iterator categopries
|
||||
void gen_partial_sum_test (const std::size_t N,
|
||||
bool binop)
|
||||
{
|
||||
rw_info (0, 0, 0,
|
||||
"template <class %s, class %s%{?}, class %s%{;}> "
|
||||
"%2$s partial_sum (%1$s, %1$s, %2$s%{?}, %s%{;})",
|
||||
"InputIterator", "OutputIterator", binop, "BinaryOperation",
|
||||
binop, "BinaryOperation");
|
||||
|
||||
if (rw_opt_no_output_iter)
|
||||
rw_note (0, 0, 0, "OutputIterator test disabled");
|
||||
|
||||
if (rw_opt_no_input_iter)
|
||||
rw_note (0, 0, 0, "InputIterator test disabled");
|
||||
else
|
||||
gen_partial_sum_test (N, InputIter<UserClass>(0, 0, 0), binop);
|
||||
|
||||
if (rw_opt_no_fwd_iter)
|
||||
rw_note (0, 0, 0, "ForwardIterator test disabled");
|
||||
else
|
||||
gen_partial_sum_test (N, ConstFwdIter<UserClass>(0, 0, 0), binop);
|
||||
|
||||
if (rw_opt_no_bidir_iter)
|
||||
rw_note (0, 0, 0, "BidirectionalIterator test disabled");
|
||||
else
|
||||
gen_partial_sum_test (N, ConstBidirIter<UserClass>(0, 0, 0), binop);
|
||||
|
||||
if (rw_opt_no_rnd_iter)
|
||||
rw_note (0, 0, 0, "RandomAccessIterator test disabled");
|
||||
else
|
||||
gen_partial_sum_test (N, ConstRandomAccessIter<UserClass>(0, 0, 0),
|
||||
binop);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char*[])
|
||||
{
|
||||
const std::size_t N = std::size_t (rw_opt_nloops);
|
||||
|
||||
gen_partial_sum_test (N, false);
|
||||
|
||||
if (rw_opt_no_binary_op)
|
||||
rw_note (0, 0, 0,
|
||||
"partial_sum with binary operation test disabled");
|
||||
else
|
||||
gen_partial_sum_test (N, true);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"lib.partial.sum",
|
||||
0 /* no comment */,
|
||||
run_test,
|
||||
"|-nloops#0 " // must be non-negative
|
||||
"|-no-binary_op#"
|
||||
"|-no-InputIterator# "
|
||||
"|-no-OutputIterator# "
|
||||
"|-no-ForwardIterator# "
|
||||
"|-no-BidirectionalIterator# "
|
||||
"|-no-RandomAccessIterator#",
|
||||
&rw_opt_nloops,
|
||||
&rw_opt_no_binary_op,
|
||||
&rw_opt_no_input_iter,
|
||||
&rw_opt_no_output_iter,
|
||||
&rw_opt_no_fwd_iter,
|
||||
&rw_opt_no_bidir_iter,
|
||||
&rw_opt_no_rnd_iter);
|
||||
}
|
||||
855
extern/stdcxx/4.2.1/tests/numerics/26.valarray.cassign.cpp
vendored
Normal file
855
extern/stdcxx/4.2.1/tests/numerics/26.valarray.cassign.cpp
vendored
Normal file
@@ -0,0 +1,855 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.valarray.cassign.cpp - tests exercising valarray computed assignment
|
||||
*
|
||||
* $Id: 26.valarray.cassign.cpp 650706 2008-04-23 00:32:39Z sebor $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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.
|
||||
*
|
||||
* Copyright (C) 2007-2008 Rogue Wave Software, Inc.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
#include <cstdlib> // for free(), strtod(), size_t
|
||||
#include <valarray> // for indirect_array, valarray
|
||||
|
||||
#include <rw_value.h> // for UserClass
|
||||
#include <driver.h> // for rw_test()
|
||||
#include <rw_printf.h> // for rw_asnprintf()
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns an array of size elements of type T constructed from a string
|
||||
// of comma-separated values
|
||||
template <class T>
|
||||
T*
|
||||
make_array (const T*, const char *s, std::size_t *psize)
|
||||
{
|
||||
std::size_t bufsize = 0; // capacity of buffer
|
||||
std::size_t nelems = 0; // number of elements in buffer
|
||||
|
||||
if (psize)
|
||||
*psize = 0;
|
||||
|
||||
if (0 == s)
|
||||
s = "";
|
||||
|
||||
T* buf = 0;
|
||||
|
||||
while (*s) {
|
||||
|
||||
// get the next value from the string
|
||||
char *end = 0;
|
||||
const double val = std::strtod (s, &end);
|
||||
|
||||
unsigned long repeat = 1;
|
||||
|
||||
if ('@' == *end) {
|
||||
// process repeat directive (e.g., "123@5" expands into
|
||||
// 5 copies of the number 123)
|
||||
char *e = 0;
|
||||
repeat = std::strtoul (++end, &e, 10);
|
||||
|
||||
// skip trailing whitespace
|
||||
for (; ' ' == *end; ++end);
|
||||
|
||||
// the next character must be either a NUL or comma
|
||||
RW_ASSERT ('\0' == *e || ',' == *e);
|
||||
|
||||
if (',' == *e)
|
||||
++e;
|
||||
|
||||
s = e;
|
||||
}
|
||||
else {
|
||||
// skip trailing whitespace
|
||||
for (; ' ' == *end; ++end);
|
||||
|
||||
// the next character must be NUL or a comma
|
||||
RW_ASSERT ('\0' == *end || ',' == *end);
|
||||
s = end;
|
||||
|
||||
if (*s)
|
||||
++s;
|
||||
}
|
||||
|
||||
while (repeat--) {
|
||||
|
||||
if (nelems == bufsize) {
|
||||
// reallocate buffer
|
||||
|
||||
const std::size_t newsize = (bufsize + 1) * 2;
|
||||
T* const tmp = new T [newsize];
|
||||
|
||||
// copy data to the newly allocated buffer
|
||||
for (std::size_t i = 0; i != nelems; ++i)
|
||||
tmp [i] = buf [i];
|
||||
|
||||
// increase the capacity
|
||||
bufsize = newsize;
|
||||
|
||||
// deallocate old buffer
|
||||
delete[] buf;
|
||||
buf = tmp;
|
||||
}
|
||||
|
||||
// append value to the end of the buffer
|
||||
buf [nelems++] = T (val);
|
||||
}
|
||||
}
|
||||
|
||||
if (psize)
|
||||
*psize = nelems;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
// deletes an array of elements of type T returned from make_array
|
||||
template <class T>
|
||||
void
|
||||
delete_array (const T *array, std::size_t)
|
||||
{
|
||||
T* const a = _RWSTD_CONST_CAST (T*, array);
|
||||
delete[] a;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
const std::size_t* count (const T*) { return 0; }
|
||||
|
||||
|
||||
template <class T>
|
||||
T value (const T &val) { return val; }
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// deletes an array of elements of type T returned from make_array
|
||||
void
|
||||
delete_array (const UserClass *array, std::size_t nelems)
|
||||
{
|
||||
UserClass* const a = _RWSTD_CONST_CAST (UserClass*, array);
|
||||
|
||||
for (std::size_t i = 0; i != nelems; ++i)
|
||||
(a + i)->~UserClass ();
|
||||
|
||||
operator delete (a);
|
||||
}
|
||||
|
||||
|
||||
// returns an array of size elements of type UserClass
|
||||
// constructed from a string of comma-separated values
|
||||
UserClass*
|
||||
make_array (const UserClass*, const char *s, std::size_t *psize)
|
||||
{
|
||||
std::size_t bufsize = 0; // capacity of buffer
|
||||
std::size_t nelems = 0; // number of elements in buffer
|
||||
|
||||
if (psize)
|
||||
*psize = 0;
|
||||
|
||||
if (0 == s)
|
||||
s = "";
|
||||
|
||||
UserClass* buf = 0;
|
||||
|
||||
while (*s) {
|
||||
|
||||
// get the next value from the string
|
||||
char *end = 0;
|
||||
const double val = std::strtod (s, &end);
|
||||
|
||||
unsigned long repeat = 1;
|
||||
|
||||
if ('@' == *end) {
|
||||
// process repeat directive (e.g., "123@5" expands into
|
||||
// 5 copies of the number 123)
|
||||
char *e = 0;
|
||||
repeat = std::strtoul (++end, &e, 10);
|
||||
|
||||
// skip trailing whitespace
|
||||
for (; ' ' == *end; ++end);
|
||||
|
||||
// the next character must be either a NUL or comma
|
||||
RW_ASSERT ('\0' == *e || ',' == *e);
|
||||
|
||||
if (',' == *e)
|
||||
++e;
|
||||
|
||||
s = e;
|
||||
}
|
||||
else {
|
||||
// skip trailing whitespace
|
||||
for (; ' ' == *end; ++end);
|
||||
|
||||
// the next character must be NUL or a comma
|
||||
RW_ASSERT ('\0' == *end || ',' == *end);
|
||||
|
||||
s = end;
|
||||
if (*s)
|
||||
++s;
|
||||
}
|
||||
|
||||
while (repeat--) {
|
||||
|
||||
if (nelems == bufsize) {
|
||||
static const std::size_t size = sizeof (UserClass);
|
||||
|
||||
void* const raw = operator new ((bufsize + 1) * 2 * size);
|
||||
UserClass* const tmp = _RWSTD_STATIC_CAST (UserClass*, raw);
|
||||
|
||||
for (std::size_t i = 0; i != nelems; ++i)
|
||||
new (tmp +i) UserClass (buf [i]);
|
||||
|
||||
bufsize = (bufsize + 1) * 2;
|
||||
|
||||
delete_array (buf, nelems);
|
||||
buf = tmp;
|
||||
}
|
||||
|
||||
new (buf + nelems) UserClass ();
|
||||
buf [nelems].data_.val_ = int (val);
|
||||
++nelems;
|
||||
}
|
||||
}
|
||||
|
||||
if (psize)
|
||||
*psize = nelems;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
const std::size_t* count (const UserClass*) { return &UserClass::count_; }
|
||||
|
||||
int value (const UserClass &val) { return val.data_.val_; }
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_op_assign (const T*,
|
||||
std::valarray<T>&
|
||||
(std::valarray<T>::*op_assign)(const std::valarray<T>&),
|
||||
const char *tname, // T's type name
|
||||
const char *opname, // which assignment operator
|
||||
int line, // test case line number
|
||||
const char *lhs_str, // left hand side of assignment
|
||||
const char *rhs_str, // right hand side of assignment
|
||||
const char *res_str) // result of assignment
|
||||
{
|
||||
std::size_t nelems = 0;
|
||||
std::size_t tmp;
|
||||
|
||||
// create an array of values of type T from the string lhs_str
|
||||
// representing the left-hand side argument of the assignment
|
||||
const T* const lhs_array = make_array ((const T*)0, lhs_str, &nelems);
|
||||
|
||||
// create an array of values of type T from the string rhs_str
|
||||
// representing the right-hand side argument of the assignment
|
||||
const T* const rhs_array = make_array ((const T*)0, rhs_str, &tmp);
|
||||
|
||||
// both arguments of the assignment must have the same size
|
||||
RW_ASSERT (tmp == nelems);
|
||||
|
||||
// create an array of values of type T from the string res_str
|
||||
// representing the result of the assignment
|
||||
const T* const res_array = make_array ((const T*)0, res_str, &tmp);
|
||||
|
||||
// the result of the assignment must have the same size as both
|
||||
// arguments
|
||||
RW_ASSERT (tmp == nelems);
|
||||
|
||||
// construct valarray arguments from the arrays created above
|
||||
/* const */ std::valarray<T> lhs_va (lhs_array, nelems);
|
||||
const std::valarray<T> rhs_va (rhs_array, nelems);
|
||||
|
||||
char* fname = 0;
|
||||
std::size_t size = 0;
|
||||
|
||||
// pointer to a counter keeping track of all objects of type T
|
||||
// in existence (non-null only for T=UserClass)
|
||||
const std::size_t* const pcounter = count ((const T*)0);
|
||||
|
||||
// get the number of objects of type T (only possible for user
|
||||
// defined T) before invoking the operator
|
||||
std::size_t nobjects = pcounter ? *pcounter : 0;
|
||||
|
||||
// format the name of the function call to be used in diagnostic
|
||||
// messages below
|
||||
rw_asnprintf (&fname, &size,
|
||||
"valarray<%s>(%s) %s std::valarray<%1$s>(%s)",
|
||||
tname, lhs_str, opname, rhs_str);
|
||||
|
||||
// invoke the assignment operator through the member pointer
|
||||
std::valarray<T> &res = (lhs_va.*op_assign)(rhs_va);
|
||||
|
||||
// verify that the resturned reference refers to the assignee
|
||||
rw_assert (&res == &lhs_va, 0, line,
|
||||
"line %d == %#p, got %#p",
|
||||
__LINE__, fname, &lhs_va, &res);
|
||||
|
||||
|
||||
// verify the size of the array
|
||||
rw_assert (lhs_va.size () == nelems, 0, line,
|
||||
"line %d. %s.size() == %zu, got %zu",
|
||||
__LINE__, fname, nelems, lhs_va.size ());
|
||||
|
||||
if (pcounter) {
|
||||
// verify that the assignment didn't leak any objects
|
||||
nobjects = *pcounter - nobjects;
|
||||
|
||||
rw_assert (0 == nobjects, 0, line,
|
||||
"line %d. %s constucted %zu objects, expected %zu",
|
||||
__LINE__, fname, nobjects, nelems);
|
||||
}
|
||||
|
||||
// verify the element values
|
||||
for (std::size_t i = 0; i != nelems; ++i) {
|
||||
if (!rw_assert (lhs_va [i] == res_array [i], 0, line,
|
||||
"line %d. %s: element at index %zu == %d, got %d",
|
||||
__LINE__, fname, i, value (res_array [i]),
|
||||
value (lhs_va [i])))
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
delete_array (lhs_array, nelems);
|
||||
delete_array (rhs_array, nelems);
|
||||
|
||||
std::free (fname);
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
#define OP(lhs, rhs, res) \
|
||||
test_op_assign ((T*)0, op, tname, opname, __LINE__, lhs, rhs, res)
|
||||
|
||||
|
||||
template <class T>
|
||||
void test_mul_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator*=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "0", "0");
|
||||
OP ("0", "1", "0");
|
||||
OP ("1", "0", "0");
|
||||
OP ("1", "1", "1");
|
||||
OP ("1", "2", "2");
|
||||
|
||||
OP ("1,2", "1,1", "1,2");
|
||||
OP ("1,2", "1,2", "1,4");
|
||||
OP ("2,3", "4,5", "8,15");
|
||||
OP ("3,4", "5,6", "15,24");
|
||||
|
||||
OP ("1,2,3", "4,5,6", "4,10,18");
|
||||
OP ("-1,-2,-3", "-4,-5,-6", "4,10,18");
|
||||
OP ("1,-2,3", "-4,5,-6", "-4,-10,-18");
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_div_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator/=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "1", "0");
|
||||
OP ("1", "1", "1");
|
||||
OP ("2", "1", "2");
|
||||
OP ("2", "2", "1");
|
||||
OP ("3", "1", "3");
|
||||
OP ("3", "2", "1.5");
|
||||
OP ("4", "2", "2");
|
||||
|
||||
OP ("0,1,2,3,4,5,6,7", "1,1,1,1,1,1,1,1", "0,1,2,3,4,5,6,7");
|
||||
OP ("2,4,6,8,10,12,14,16", "2,2,2,2,2,2,2,2", "1,2,3,4,5,6,7,8");
|
||||
OP ("2,4,6,8,10,12,14,16", "2,2,2,2,2,2,2,2", "1,2,3,4,5,6,7,8");
|
||||
|
||||
OP ("0@127", "1@127", "0@127");
|
||||
OP ("1@128", "1@128", "1@128");
|
||||
OP ("2@255", "1@255", "2@255");
|
||||
OP ("2@256", "2@256", "1@256");
|
||||
OP ("3@256", "1@256", "3@256");
|
||||
OP ("3@511", "2@511", "1.5@511");
|
||||
OP ("4@512", "2@512", "2@512");
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_mod_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator%=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "1", "0");
|
||||
OP ("1", "1", "0");
|
||||
OP ("2", "1", "0");
|
||||
OP ("2", "2", "0");
|
||||
OP ("3", "1", "0");
|
||||
OP ("3", "2", "1");
|
||||
OP ("3", "3", "0");
|
||||
OP ("4", "1", "0");
|
||||
OP ("4", "2", "0");
|
||||
OP ("4", "3", "1");
|
||||
OP ("4", "4", "0");
|
||||
|
||||
OP ("0,1", "2,2", "0,1");
|
||||
OP ("1,2,3", "4,4,4", "1,2,3");
|
||||
OP ("2,3,4,5", "6,6,6,6", "2,3,4,5");
|
||||
|
||||
OP ("0,1,2,3,4,5,6,7,8,9", "10,9,8,7,6,5,4,3,2,1", "0,1,2,3,4,0,2,1,0,0");
|
||||
}
|
||||
|
||||
#define CANNOT_TEST(T, name) \
|
||||
void test_##name (const T*, const char*, const char*) { } \
|
||||
typedef void swallow_semicolon
|
||||
|
||||
|
||||
CANNOT_TEST (float, mod_assign);
|
||||
CANNOT_TEST (double, mod_assign);
|
||||
CANNOT_TEST (UserClass, mod_assign);
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_add_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator+=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "0", "0");
|
||||
OP ("0", "1", "1");
|
||||
OP ("1", "0", "1");
|
||||
OP ("1", "-1", "0");
|
||||
OP ("-1", "1", "0");
|
||||
|
||||
OP ("0@31", "0@31", "0@31");
|
||||
OP ("0@31", "1@31", "1@31");
|
||||
OP ("1@31", "0@31", "1@31");
|
||||
OP ("1@31", "-1@31", "0@31");
|
||||
OP ("-1@31", "1@31", "0@31");
|
||||
|
||||
OP ("2@32", "3@32", "5@32");
|
||||
OP ("3@32", "5@32", "8@32");
|
||||
OP ("5@32", "7@32", "12@32");
|
||||
OP ("9@32", "-9@32", "0@32");
|
||||
|
||||
OP ("0@63", "0@63", "0@63");
|
||||
OP ("0@63", "1@63", "1@63");
|
||||
OP ("1@63", "0@63", "1@63");
|
||||
OP ("1@63", "-1@63", "0@63");
|
||||
OP ("-1@63", "1@63", "0@63");
|
||||
|
||||
OP ("0@63", "0@63", "0@63");
|
||||
OP ("0@63", "1@63", "1@63");
|
||||
OP ("1@63", "0@63", "1@63");
|
||||
OP ("1@63", "-1@63", "0@63");
|
||||
OP ("-1@63", "1@63", "0@63");
|
||||
|
||||
OP ("0,0", "0,0", "0,0");
|
||||
OP ("0,0", "0,1", "0,1");
|
||||
OP ("0,0", "1,0", "1,0");
|
||||
OP ("0,0", "1,1", "1,1");
|
||||
OP ("0,1", "0,1", "0,2");
|
||||
OP ("0,1", "1,0", "1,1");
|
||||
OP ("0,1", "1,1", "1,2");
|
||||
OP ("1,0", "0,0", "1,0");
|
||||
OP ("1,0", "0,1", "1,1");
|
||||
OP ("1,0", "1,0", "2,0");
|
||||
OP ("1,0", "1,1", "2,1");
|
||||
OP ("1,1", "0,0", "1,1");
|
||||
OP ("1,1", "0,1", "1,2");
|
||||
OP ("1,1", "1,0", "2,1");
|
||||
OP ("1,1", "1,1", "2,2");
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_sub_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator-=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP (" 0", " 0", " 0");
|
||||
OP (" 0", "+1", "-1");
|
||||
OP ("+1", " 0", "+1");
|
||||
OP ("+1", "-1", "+2");
|
||||
OP ("-1", "+1", "-2");
|
||||
|
||||
OP (" 0@12", " 0@12", " 0@12");
|
||||
OP (" 0@23", "+1@23", "-1@23");
|
||||
OP ("+1@34", " 0@34", "+1@34");
|
||||
OP ("+1@45", "-1@45", "+2@45");
|
||||
OP ("-1@56", "+1@56", "-2@56");
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_xor_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator^=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "0", "0");
|
||||
OP ("0", "1", "1");
|
||||
OP ("1", "0", "1");
|
||||
OP ("1", "1", "0");
|
||||
OP ("3", "0", "3");
|
||||
OP ("3", "1", "2");
|
||||
OP ("3", "2", "1");
|
||||
OP ("3", "3", "0");
|
||||
|
||||
OP ("0@10", "0@10", "0@10");
|
||||
OP ("0@11", "1@11", "1@11");
|
||||
OP ("1@12", "0@12", "1@12");
|
||||
OP ("1@13", "1@13", "0@13");
|
||||
OP ("3@14", "0@14", "3@14");
|
||||
OP ("3@15", "1@15", "2@15");
|
||||
OP ("3@16", "2@16", "1@16");
|
||||
OP ("3@17", "3@17", "0@17");
|
||||
}
|
||||
|
||||
CANNOT_TEST (float, xor_assign);
|
||||
CANNOT_TEST (double, xor_assign);
|
||||
CANNOT_TEST (UserClass, xor_assign);
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_and_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator&=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "0", "0");
|
||||
OP ("0", "1", "0");
|
||||
OP ("1", "0", "0");
|
||||
OP ("1", "1", "1");
|
||||
OP ("3", "0", "0");
|
||||
OP ("3", "1", "1");
|
||||
OP ("3", "2", "2");
|
||||
OP ("3", "3", "3");
|
||||
|
||||
OP ("0@10", "0@10", "0@10");
|
||||
OP ("0@11", "1@11", "0@11");
|
||||
OP ("1@12", "0@12", "0@12");
|
||||
OP ("1@13", "1@13", "1@13");
|
||||
OP ("3@14", "0@14", "0@14");
|
||||
OP ("3@15", "1@15", "1@15");
|
||||
OP ("3@16", "2@16", "2@16");
|
||||
OP ("3@17", "3@17", "3@17");
|
||||
}
|
||||
|
||||
CANNOT_TEST (float, and_assign);
|
||||
CANNOT_TEST (double, and_assign);
|
||||
CANNOT_TEST (UserClass, and_assign);
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_or_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator|=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "0", "0");
|
||||
OP ("0", "1", "1");
|
||||
OP ("1", "0", "1");
|
||||
OP ("1", "1", "1");
|
||||
OP ("3", "0", "3");
|
||||
OP ("3", "1", "3");
|
||||
OP ("3", "2", "3");
|
||||
OP ("3", "3", "3");
|
||||
|
||||
OP ("0@10", "0@10", "0@10");
|
||||
OP ("0@11", "1@11", "1@11");
|
||||
OP ("1@12", "0@12", "1@12");
|
||||
OP ("1@13", "1@13", "1@13");
|
||||
OP ("3@14", "0@14", "3@14");
|
||||
OP ("3@15", "1@15", "3@15");
|
||||
OP ("3@16", "2@16", "3@16");
|
||||
OP ("3@17", "3@17", "3@17");
|
||||
}
|
||||
|
||||
CANNOT_TEST (float, or_assign);
|
||||
CANNOT_TEST (double, or_assign);
|
||||
CANNOT_TEST (UserClass, or_assign);
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_shl_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator<<=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "0", "0");
|
||||
OP ("0", "1", "0");
|
||||
OP ("1", "0", "1");
|
||||
OP ("1", "1", "2");
|
||||
OP ("3", "0", "3");
|
||||
OP ("3", "1", "6");
|
||||
OP ("3", "2", "12");
|
||||
OP ("3", "3", "24");
|
||||
|
||||
OP ("0@10", "0@10", "0@10");
|
||||
OP ("0@11", "1@11", "0@11");
|
||||
OP ("1@12", "0@12", "1@12");
|
||||
OP ("1@13", "1@13", "2@13");
|
||||
OP ("3@14", "0@14", "3@14");
|
||||
OP ("3@15", "1@15", "6@15");
|
||||
OP ("3@16", "2@16", "12@16");
|
||||
OP ("3@17", "3@17", "24@17");
|
||||
}
|
||||
|
||||
CANNOT_TEST (float, shl_assign);
|
||||
CANNOT_TEST (double, shl_assign);
|
||||
CANNOT_TEST (UserClass, shl_assign);
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void test_shr_assign (const T*, const char *tname, const char *opname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::operator%s (const std::valarray<%1$s>&)",
|
||||
tname, opname);
|
||||
|
||||
std::valarray<T>& (std::valarray<T>::*op)(const std::valarray<T>&) =
|
||||
&std::valarray<T>::operator>>=;
|
||||
|
||||
OP ("", "", "");
|
||||
|
||||
OP ("0", "0", "0");
|
||||
OP ("0", "1", "0");
|
||||
OP ("1", "0", "1");
|
||||
OP ("2", "1", "1");
|
||||
OP ("3", "0", "3");
|
||||
OP ("6", "1", "3");
|
||||
OP ("12", "2", "3");
|
||||
OP ("24", "3", "3");
|
||||
|
||||
OP ("0@21", "0@21", "0@21");
|
||||
OP ("0@21", "1@21", "0@21");
|
||||
OP ("1@21", "0@21", "1@21");
|
||||
OP ("2@21", "1@21", "1@21");
|
||||
OP ("3@21", "0@21", "3@21");
|
||||
OP ("6@21", "1@21", "3@21");
|
||||
OP ("12@21", "2@21", "3@21");
|
||||
OP ("24@21", "3@21", "3@21");
|
||||
}
|
||||
|
||||
CANNOT_TEST (float, shr_assign);
|
||||
CANNOT_TEST (double, shr_assign);
|
||||
CANNOT_TEST (UserClass, shr_assign);
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
enum {
|
||||
inx_mul_assign,
|
||||
inx_div_assign,
|
||||
inx_mod_assign,
|
||||
inx_add_assign,
|
||||
inx_sub_assign,
|
||||
inx_xor_assign,
|
||||
inx_and_assign,
|
||||
inx_or_assign,
|
||||
inx_shl_assign,
|
||||
inx_shr_assign,
|
||||
n_assign_ops
|
||||
};
|
||||
|
||||
static int opt_assign [n_assign_ops];
|
||||
|
||||
|
||||
template <class T>
|
||||
void test_op_assign (const T*, const char *tname)
|
||||
{
|
||||
const std::size_t nassigns = sizeof opt_assign / sizeof *opt_assign;
|
||||
for (std::size_t i = 0; i != nassigns; ++i) {
|
||||
if (0 < opt_assign [i]) {
|
||||
for (std::size_t j = 0; j != nassigns; ++j) {
|
||||
if (0 == opt_assign [j])
|
||||
opt_assign [j] = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_mul_assign], 0, __LINE__,
|
||||
"tests of operator*= disabled"))
|
||||
test_mul_assign ((const T*)0, tname, "*=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_div_assign], 0, __LINE__,
|
||||
"tests of operator/= disabled"))
|
||||
test_div_assign ((const T*)0, tname, "/=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_mod_assign], 0, __LINE__,
|
||||
"tests of operator%= disabled"))
|
||||
test_mod_assign ((const T*)0, tname, "%=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_add_assign], 0, __LINE__,
|
||||
"tests of operator+= disabled"))
|
||||
test_add_assign ((const T*)0, tname, "+=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_sub_assign], 0, __LINE__,
|
||||
"tests of operator-= disabled"))
|
||||
test_sub_assign ((const T*)0, tname, "-=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_xor_assign], 0, __LINE__,
|
||||
"tests of operator^= disabled"))
|
||||
test_xor_assign ((const T*)0, tname, "^=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_and_assign], 0, __LINE__,
|
||||
"tests of operator&= disabled"))
|
||||
test_and_assign ((const T*)0, tname, "&=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_or_assign], 0, __LINE__,
|
||||
"tests of operator|= disabled"))
|
||||
test_or_assign ((const T*)0, tname, "|=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_shl_assign], 0, __LINE__,
|
||||
"tests of operator<<= disabled"))
|
||||
test_shr_assign ((const T*)0, tname, "<<=");
|
||||
|
||||
if (rw_note (0 <= opt_assign [inx_shr_assign], 0, __LINE__,
|
||||
"tests of operator>>= disabled"))
|
||||
test_shr_assign ((const T*)0, tname, ">>=");
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char**)
|
||||
{
|
||||
#undef TEST
|
||||
#define TEST(T) test_op_assign ((const T*)0, #T)
|
||||
|
||||
TEST (int);
|
||||
TEST (double);
|
||||
|
||||
#if 0x04020100 >= _RWSTD_VER
|
||||
|
||||
// test fails to compile with stdcxx 4.2.1 and prior due to
|
||||
// STDCXX-512: http://issues.apache.org/jira/browse/STDCXX-512
|
||||
rw_warn (0, 0, __LINE__,
|
||||
"test of UserClass disabled in stdcxx 4.2.0 and prior "
|
||||
"due to STDCXX-512");
|
||||
|
||||
#else // stdcxx >= 4.2.1
|
||||
|
||||
TEST (UserClass);
|
||||
|
||||
#endif // stdcxx version
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
// FIXME: add command line options to enable/disable each operator
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"valarray.cassign",
|
||||
0 /* no comment */,
|
||||
run_test,
|
||||
"|-times~ "
|
||||
"|-divide~ "
|
||||
"|-modulo~ "
|
||||
"|-plus~ "
|
||||
"|-minus~ "
|
||||
"|-xor~ "
|
||||
"|-and~ "
|
||||
"|-or~ "
|
||||
"|-shift_left~ "
|
||||
"|-shift_right~",
|
||||
opt_assign + inx_mul_assign,
|
||||
opt_assign + inx_div_assign,
|
||||
opt_assign + inx_mod_assign,
|
||||
opt_assign + inx_add_assign,
|
||||
opt_assign + inx_sub_assign,
|
||||
opt_assign + inx_xor_assign,
|
||||
opt_assign + inx_and_assign,
|
||||
opt_assign + inx_or_assign,
|
||||
opt_assign + inx_shl_assign,
|
||||
opt_assign + inx_shr_assign,
|
||||
(void*)0 /* sentinel */);
|
||||
}
|
||||
444
extern/stdcxx/4.2.1/tests/numerics/26.valarray.cons.cpp
vendored
Normal file
444
extern/stdcxx/4.2.1/tests/numerics/26.valarray.cons.cpp
vendored
Normal file
@@ -0,0 +1,444 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.valarray.cons.cpp - tests exercising valarray constructors
|
||||
*
|
||||
* $Id: 26.valarray.cons.cpp 510970 2007-02-23 14:57:45Z faridz $
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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 <cstdlib> // for free(), strtol(), size_t
|
||||
#include <valarray> // for indirect_array, valarray
|
||||
|
||||
#include <rw_value.h> // for UserClass
|
||||
#include <driver.h> // for rw_test()
|
||||
#include <rw_printf.h> // for rw_asnprintf()
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns an array of size elements of type T constructed from a string
|
||||
// of comma-separated values
|
||||
template <class T>
|
||||
T*
|
||||
make_array (const T*, const char *s, std::size_t *psize)
|
||||
{
|
||||
std::size_t nelems = psize ? *psize : 0;
|
||||
|
||||
T* const buf = new T [nelems ? nelems : 4096];
|
||||
|
||||
if (0 == nelems && (0 == s || '\0' == *s))
|
||||
return buf;
|
||||
|
||||
std::size_t i;
|
||||
|
||||
for (i = 0; ; ++i) {
|
||||
|
||||
char *end = 0;
|
||||
long val = s ? std::strtol (s, &end, 0) : 0L;
|
||||
|
||||
RW_ASSERT (0 == end || '\0' == *end || ',' == *end);
|
||||
|
||||
buf [i] = T (val);
|
||||
|
||||
if (0 == end || '\0' == *end) {
|
||||
while (++i < nelems)
|
||||
buf [i] = buf [i - 1];
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
s = end + 1;
|
||||
}
|
||||
|
||||
if (psize)
|
||||
*psize = i;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
// deletes an array of elements of type T returned from make_array
|
||||
template <class T>
|
||||
void
|
||||
delete_array (const T *array, std::size_t)
|
||||
{
|
||||
T* const a = _RWSTD_CONST_CAST (T*, array);
|
||||
delete[] a;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
const std::size_t* count (const T*) { return 0; }
|
||||
|
||||
|
||||
template <class T>
|
||||
T value (const T &val) { return val; }
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
// returns an array of size elements of type UserClass
|
||||
// constructed from a string of comma-separated values
|
||||
UserClass*
|
||||
make_array (const UserClass*, const char *s, std::size_t *psize)
|
||||
{
|
||||
std::size_t nelems = psize ? *psize : 0;
|
||||
|
||||
const std::size_t size = sizeof (UserClass);
|
||||
void* const raw = operator new ((nelems ? nelems : 1024) * size);
|
||||
UserClass* const buf = _RWSTD_STATIC_CAST (UserClass*, raw);
|
||||
|
||||
if (0 == nelems && (0 == s || '\0' == *s))
|
||||
return buf;
|
||||
|
||||
std::size_t i;
|
||||
|
||||
for (i = 0; ; ++i) {
|
||||
|
||||
char *end = 0;
|
||||
long val = s ? std::strtol (s, &end, 0) : 0L;
|
||||
|
||||
RW_ASSERT (0 == end || '\0' == *end || ',' == *end);
|
||||
|
||||
new (buf + i) UserClass ();
|
||||
buf [i].data_.val_ = int (val);
|
||||
|
||||
if (0 == end || '\0' == *end) {
|
||||
while (++i < nelems)
|
||||
new (buf + i) UserClass (buf [i - 1]);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
s = end + 1;
|
||||
}
|
||||
|
||||
if (psize)
|
||||
*psize = i;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
// deletes an array of elements of type T returned from make_array
|
||||
void
|
||||
delete_array (const UserClass *array, std::size_t nelems)
|
||||
{
|
||||
UserClass* const a = _RWSTD_CONST_CAST (UserClass*, array);
|
||||
|
||||
for (std::size_t i = 0; i != nelems; ++i)
|
||||
(a + i)->~UserClass ();
|
||||
|
||||
operator delete (a);
|
||||
}
|
||||
|
||||
|
||||
const std::size_t* count (const UserClass*) { return &UserClass::count_; }
|
||||
|
||||
int value (const UserClass &val) { return val.data_.val_; }
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
enum CtorId {
|
||||
DefaultCtor, // valarray<T>::valarray()
|
||||
SizeCtor, // valarray<T>::valarra(size_t)
|
||||
ValueCtor, // valarray<T>::valarray(const T&, size_t)
|
||||
ArrayCtor // valarray<T>::valarray(const T*, size_t)
|
||||
};
|
||||
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_ctor (const T*, const char *tname, CtorId which, bool copy,
|
||||
int line, const char *str, std::size_t nelems)
|
||||
{
|
||||
std::valarray<T> *pva = 0;
|
||||
|
||||
T* const array = make_array ((const T*)0, str, &nelems);
|
||||
|
||||
char* fname = 0;
|
||||
std::size_t size = 0;
|
||||
|
||||
// pointer to a counter keepint track of all objects of type T
|
||||
// in existence (non-null only for T=UserClass)
|
||||
const std::size_t* const pcounter = count ((const T*)0);
|
||||
|
||||
// get the number of objects of type T before invoking the ctor
|
||||
std::size_t nobjects = pcounter ? *pcounter : 0;
|
||||
|
||||
switch (which) {
|
||||
|
||||
case DefaultCtor:
|
||||
rw_asnprintf (&fname, &size, "valarray<%s>::valarray()", tname);
|
||||
pva = new std::valarray<T>;
|
||||
break;
|
||||
|
||||
case SizeCtor:
|
||||
rw_asnprintf (&fname, &size,
|
||||
"valarray<%s>::valarray(size_t = %zu)",
|
||||
tname, nelems);
|
||||
pva = new std::valarray<T>(nelems);
|
||||
break;
|
||||
|
||||
case ValueCtor: {
|
||||
rw_asnprintf (&fname, &size,
|
||||
"valarray<%s>::valarray(const %1$s& = %1$s(%d), "
|
||||
"size_t = %zu)",
|
||||
tname, value (array [0]), nelems);
|
||||
pva = new std::valarray<T>(array [0], nelems);
|
||||
break;
|
||||
}
|
||||
|
||||
case ArrayCtor: {
|
||||
rw_asnprintf (&fname, &size,
|
||||
"valarray<%s>::valarray(const %1$s* = {%s}, "
|
||||
"size_t = %zu)",
|
||||
tname, str, nelems);
|
||||
pva = new std::valarray<T>(array, nelems);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::valarray<T> *psave = 0;
|
||||
|
||||
if (copy) {
|
||||
char *tmpbuf = 0;
|
||||
std::size_t tmpsize = 0;
|
||||
|
||||
rw_asnprintf (&tmpbuf, &tmpsize, "valarray<%s>::valarray(%s)",
|
||||
tname, fname);
|
||||
|
||||
std::free (fname);
|
||||
fname = tmpbuf;
|
||||
size = tmpsize;
|
||||
|
||||
// replace the stored object counter value
|
||||
nobjects = pcounter ? *pcounter : 0;
|
||||
|
||||
// save the original and replace it with the new array
|
||||
psave = pva;
|
||||
|
||||
// invoke the copy ctor
|
||||
pva = new std::valarray<T>(*pva);
|
||||
}
|
||||
|
||||
// verify the size of the array
|
||||
rw_assert (pva->size () == nelems, 0, line,
|
||||
"line %d. %s.size() == %zu, got %zu",
|
||||
__LINE__, fname, nelems, pva->size ());
|
||||
|
||||
if (pcounter) {
|
||||
// compute the number of objects of type T constructed
|
||||
// by the ctor (valid only for T=UserClass)
|
||||
nobjects = *pcounter - nobjects;
|
||||
|
||||
rw_assert (nobjects == nelems, 0, line,
|
||||
"line %d. %s constucted %zu objects, expected %zu",
|
||||
__LINE__, fname, nobjects, nelems);
|
||||
}
|
||||
|
||||
// verify the element values
|
||||
for (std::size_t i = 0; i != nelems; ++i) {
|
||||
if (!((*pva)[i] == array [i])) {
|
||||
rw_assert (i == nelems, 0, line,
|
||||
"line %d. %s[%zu] == %s(%d), got %4$s(%d)",
|
||||
__LINE__, fname, i, tname,
|
||||
value (array [i]), value ((*pva)[i]));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delete_array (array, nelems);
|
||||
|
||||
// get the number of objects of type T before invoking the dtor
|
||||
nobjects = pcounter ? *pcounter : 0;
|
||||
|
||||
delete pva;
|
||||
|
||||
if (pcounter) {
|
||||
// compute the number of objects of type T destroyed by the dtor
|
||||
nobjects = nobjects - *pcounter;
|
||||
|
||||
// verify that all objects constructed by the ctor have been
|
||||
// destroyed (i.e., none leaked)
|
||||
rw_assert (nobjects == nelems, 0, line,
|
||||
"line %d. %s dtor destroyed %zu objects, expected %zu",
|
||||
__LINE__, fname, nobjects, nelems);
|
||||
}
|
||||
|
||||
delete psave;
|
||||
std::free (fname);
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_default_ctor (const T*, const char *tname, bool copy)
|
||||
{
|
||||
if (!copy)
|
||||
rw_info (0, 0, __LINE__, "std::valarray<%s>::valarray()", tname);
|
||||
|
||||
test_ctor ((const T*)0, tname, DefaultCtor, copy, __LINE__, 0, 0);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_size_ctor (const T*, const char *tname, bool copy)
|
||||
{
|
||||
if (!copy)
|
||||
rw_info (0, 0, __LINE__, "std::valarray<%s>::valarray(size_t)",
|
||||
tname);
|
||||
|
||||
#undef TEST
|
||||
#define TEST(n) \
|
||||
test_ctor ((const T*)0, tname, SizeCtor, copy, __LINE__, "0", n)
|
||||
|
||||
TEST (0);
|
||||
TEST (1);
|
||||
TEST (2);
|
||||
TEST (3);
|
||||
TEST (4);
|
||||
TEST (5);
|
||||
TEST (6);
|
||||
TEST (7);
|
||||
TEST (8);
|
||||
TEST (9);
|
||||
TEST (10);
|
||||
TEST (123);
|
||||
TEST (1023);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_value_ctor (const T*, const char *tname, bool copy)
|
||||
{
|
||||
if (!copy)
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::valarray(const %1$s&, size_t)",
|
||||
tname);
|
||||
#undef TEST
|
||||
#define TEST(str, n) \
|
||||
test_ctor ((const T*)0, tname, ValueCtor, copy, __LINE__, str, n)
|
||||
|
||||
TEST ("0", 0);
|
||||
TEST ("0", 1);
|
||||
TEST ("1", 1);
|
||||
TEST ("2", 2);
|
||||
TEST ("3", 3);
|
||||
TEST ("4", 4);
|
||||
TEST ("5", 5);
|
||||
TEST ("6", 12345);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_array_ctor (const T*, const char *tname, bool copy)
|
||||
{
|
||||
if (!copy)
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::valarray(const %1$s*, size_t)",
|
||||
tname);
|
||||
|
||||
#undef TEST
|
||||
#define TEST(str) \
|
||||
test_ctor ((const T*)0, tname, ArrayCtor, copy, __LINE__, str, 0)
|
||||
|
||||
TEST (""); // empty array
|
||||
TEST ("0");
|
||||
TEST ("0,1");
|
||||
TEST ("0,1,2");
|
||||
TEST ("0,1,2,3");
|
||||
TEST ("0,1,2,3,4");
|
||||
TEST ("0,1,2,3,4,5");
|
||||
TEST ("0,1,2,3,4,5,6");
|
||||
TEST ("0,1,2,3,4,5,6,7");
|
||||
TEST ("0,1,2,3,4,5,6,7,8");
|
||||
TEST ("0,1,2,3,4,5,6,7,8,9");
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_copy_ctor (const T*, const char *tname)
|
||||
{
|
||||
rw_info (0, 0, __LINE__,
|
||||
"std::valarray<%s>::valarray(const valarray<%1$s>&)", tname);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
void
|
||||
test_ctors (const T*, const char *tname)
|
||||
{
|
||||
for (int i = 0; i != 2; ++i) {
|
||||
|
||||
// exercise the respective ctor in the first iteration
|
||||
// and the copy ctor invoked an object constructed with
|
||||
// the same respective ctor as in the first iteration
|
||||
// then
|
||||
|
||||
const bool test_copy_ctor = 0 < i;
|
||||
|
||||
test_default_ctor ((T*)0, tname, test_copy_ctor);
|
||||
test_size_ctor ((T*)0, tname, test_copy_ctor);
|
||||
test_value_ctor ((T*)0, tname, test_copy_ctor);
|
||||
test_array_ctor ((T*)0, tname, test_copy_ctor);
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int, char**)
|
||||
{
|
||||
#undef TEST
|
||||
#define TEST(T) test_ctors ((const T*)0, #T)
|
||||
TEST (char);
|
||||
TEST (int);
|
||||
TEST (double);
|
||||
|
||||
TEST (UserClass);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
// FIXME: add command line options to enable/disable each operator
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"valarray.cons",
|
||||
0 /* no comment */,
|
||||
run_test,
|
||||
"",
|
||||
(void*)0 /* sentinel */);
|
||||
}
|
||||
166
extern/stdcxx/4.2.1/tests/numerics/26.valarray.transcend.cpp
vendored
Normal file
166
extern/stdcxx/4.2.1/tests/numerics/26.valarray.transcend.cpp
vendored
Normal file
@@ -0,0 +1,166 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 26.valarray.transcend.cpp - verify [lib.valarray.transcend] requirements
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
***************************************************************************
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you 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.
|
||||
*
|
||||
* Copyright 1994-2008 Rogue Wave Software.
|
||||
*
|
||||
**************************************************************************/
|
||||
|
||||
// disable implicit inclusion to work around a limitation in
|
||||
// IBM's VisualAge 5.0.2.0 (see PR#26959)
|
||||
#if defined __IBMCPP__ && !defined _RWSTD_NO_IMPLICIT_INCLUSION
|
||||
# define _RWSTD_NO_IMPLICIT_INCLUSION
|
||||
#endif // defined __IBMCPP__ && !defined _RWSTD_NO_IMPLICIT_INCLUSION
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef> // for std::size_t
|
||||
#include <valarray>
|
||||
|
||||
#include <driver.h>
|
||||
#include <valcmp.h>
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
template <class T>
|
||||
static void
|
||||
test_val (const char* tname)
|
||||
{
|
||||
|
||||
#define TEST(fun) { \
|
||||
rw_info (0, __FILE__, __LINE__, "template <> std::valarray<%s> " \
|
||||
"std::" #fun " (std::valarray<%s>)", tname, tname); \
|
||||
\
|
||||
std::valarray<T> val (10); \
|
||||
for (std::size_t i = 0; i != val.size (); ++i) \
|
||||
val [i] = T (i); \
|
||||
\
|
||||
const std::valarray<T> result = std::fun (val); \
|
||||
\
|
||||
bool success = 10 == val.size () && val.size () == result.size (); \
|
||||
\
|
||||
for (std::size_t j = 0; success && j != result.size (); ++j) { \
|
||||
if (!rw_equal (result [j], T (std::fun (T (j))))) \
|
||||
success = false; \
|
||||
} \
|
||||
\
|
||||
rw_assert (success, __FILE__, __LINE__, \
|
||||
"template <> std::valarray<%s> std::" #fun \
|
||||
"(std::valarray<%s>)", tname, tname); \
|
||||
} (void)0
|
||||
|
||||
#define TEST_2(fun, x) { \
|
||||
if (x < 0) \
|
||||
rw_info (0, __FILE__, __LINE__, \
|
||||
"template <> std::valarray<%s> " \
|
||||
"std::" #fun " (const %s&, const std::" \
|
||||
"valarray<%s>&)", tname, tname, tname); \
|
||||
else if (x > 0) \
|
||||
rw_info (0, __FILE__, __LINE__, \
|
||||
"template <> std::valarray<%s> " \
|
||||
"std::" #fun " (const std::valarray<%s>&," \
|
||||
"const %s&)", tname, tname, tname); \
|
||||
else \
|
||||
rw_info (0, __FILE__, __LINE__, \
|
||||
"template <> std::valarray<%s> " \
|
||||
"std::" #fun " (const std::valarray<%s>&," \
|
||||
"const std::valarray<%s>&)", tname, tname, tname); \
|
||||
\
|
||||
std::valarray<T> val (10); \
|
||||
for (std::size_t i = 0; i != val.size (); ++i) \
|
||||
val [i] = T (i); \
|
||||
\
|
||||
const std::valarray<T> result = \
|
||||
x < 0 ? std::fun (T (x), val) \
|
||||
: x > 0 ? std::fun (val, T (x)) \
|
||||
: std::fun (val, val); \
|
||||
\
|
||||
bool success = 10 == val.size () && val.size () == result.size (); \
|
||||
\
|
||||
for (std::size_t j = 0; success && j != result.size (); ++j) { \
|
||||
T res; \
|
||||
if (x < 0) res = T (std::fun (T (x), T (j))); \
|
||||
if (x > 0) res = T (std::fun (T (j), T (x))); \
|
||||
if (!x) res = T (std::fun (T (j), T (j))); \
|
||||
if (!rw_equal (result [j], res)) \
|
||||
success = false; \
|
||||
} \
|
||||
\
|
||||
rw_assert (success, __FILE__, __LINE__, \
|
||||
"template <> std::valarray<%s> std::" #fun "(...)", \
|
||||
tname); \
|
||||
} (void)0
|
||||
|
||||
TEST (abs);
|
||||
TEST (acos);
|
||||
TEST (abs);
|
||||
TEST (acos);
|
||||
TEST (asin);
|
||||
TEST (atan);
|
||||
|
||||
TEST_2 (atan2, -2);
|
||||
TEST_2 (atan2, +3);
|
||||
TEST_2 (atan2, 0);
|
||||
|
||||
TEST (cos);
|
||||
TEST (cosh);
|
||||
TEST (exp);
|
||||
TEST (log);
|
||||
TEST (log10);
|
||||
|
||||
TEST_2 (pow, -4);
|
||||
TEST_2 (pow, +5);
|
||||
TEST_2 (pow, 0);
|
||||
|
||||
TEST (sin);
|
||||
TEST (sinh);
|
||||
TEST (sqrt);
|
||||
TEST (tan);
|
||||
TEST (tanh);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
run_test (int /*unused*/, char* /*unused*/ [])
|
||||
{
|
||||
test_val<float> ("float");
|
||||
test_val<double> ("double");
|
||||
|
||||
#ifndef _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
test_val<long double> ("long double");
|
||||
|
||||
#endif // _RWSTD_NO_LONG_DOUBLE
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*extern*/ int
|
||||
main (int argc, char* argv [])
|
||||
{
|
||||
return rw_test (argc, argv, __FILE__,
|
||||
"lib.valarray.transcend",
|
||||
0, // no comment
|
||||
run_test, "", 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user