From stdcxx-commits-return-1095-apmail-incubator-stdcxx-commits-archive=incubator.apache.org@incubator.apache.org Tue Jan 16 01:04:34 2007 Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 75416 invoked from network); 16 Jan 2007 01:04:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 16 Jan 2007 01:04:34 -0000 Received: (qmail 60164 invoked by uid 500); 16 Jan 2007 01:04:40 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 60141 invoked by uid 500); 16 Jan 2007 01:04:40 -0000 Mailing-List: contact stdcxx-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-commits@incubator.apache.org Received: (qmail 60130 invoked by uid 99); 16 Jan 2007 01:04:40 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jan 2007 17:04:40 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jan 2007 17:04:32 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id CB1501A981A; Mon, 15 Jan 2007 17:03:29 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r496557 - /incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp Date: Tue, 16 Jan 2007 01:03:29 -0000 To: stdcxx-commits@incubator.apache.org From: sebor@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070116010329.CB1501A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sebor Date: Mon Jan 15 17:03:28 2007 New Revision: 496557 URL: http://svn.apache.org/viewvc?view=rev&rev=496557 Log: 2007-01-15 Martin Sebor * 26.valarray.cons.cpp: New test exercising valarray constructors. Added: incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp (with props) Added: incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp?view=auto&rev=496557 ============================================================================== --- incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp (added) +++ incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp Mon Jan 15 17:03:28 2007 @@ -0,0 +1,443 @@ +/*************************************************************************** + * + * 26.valarray.cons.cpp - tests exercising valarray constructors + * + * $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. + * + **************************************************************************/ + +#include // for free(), strtol(), size_t +#include // for indirect_array, valarray + +#include // for class X +#include // for rw_test() +#include // for rw_asnprintf() + +/**************************************************************************/ + +// returns an array of size elements of type T constructed from a string +// of comma-separated values +template +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 +void +delete_array (const T *array, std::size_t) +{ + T* const a = _RWSTD_CONST_CAST (T*, array); + delete[] a; +} + + +template +const std::size_t* count (const T*) { return 0; } + + +template +T value (const T &val) { return val; } + +/**************************************************************************/ + +// returns an array of size elements of type X constructed from a string +// of comma-separated values +X* +make_array (const X*, const char *s, std::size_t *psize) +{ + std::size_t nelems = psize ? *psize : 0; + + void* const raw = operator new ((nelems ? nelems : 1024) * sizeof (X)); + X* const buf = _RWSTD_STATIC_CAST (X*, 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) X (); + buf [i].val_ = int (val); + + if (0 == end || '\0' == *end) { + while (++i < nelems) + new (buf + i) X (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 X *array, std::size_t nelems) +{ + X* const a = _RWSTD_CONST_CAST (X*, array); + + for (std::size_t i = 0; i != nelems; ++i) + (a + i)->~X (); + + operator delete (a); +} + + +const std::size_t* count (const X*) { return &X::count_; } + +int value (const X &val) { return val.val_; } + +/**************************************************************************/ + +enum CtorId { + DefaultCtor, // valarray::valarray() + SizeCtor, // valarray::valarra(size_t) + ValueCtor, // valarray::valarray(const T&, size_t) + ArrayCtor // valarray::valarray(const T*, size_t) +}; + + +template +void +test_ctor (const T*, const char *tname, CtorId which, bool copy, + int line, const char *str, std::size_t nelems) +{ + std::valarray *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=X) + 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; + break; + + case SizeCtor: + rw_asnprintf (&fname, &size, + "valarray<%s>::valarray(size_t = %zu)", + tname, nelems); + pva = new std::valarray(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(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(array, nelems); + break; + } + + } + + std::valarray *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(*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=X) + 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 +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 +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 +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 +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 +void +test_copy_ctor (const T*, const char *tname) +{ + rw_info (0, 0, __LINE__, + "std::valarray<%s>::valarray(const valarray<%1$s>&)", tname); +} + +/**************************************************************************/ + +template +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 (X); + + 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 */); +} Propchange: incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/stdcxx/trunk/tests/numerics/26.valarray.cons.cpp ------------------------------------------------------------------------------ svn:keywords = Id