Return-Path: Delivered-To: apmail-incubator-stdcxx-dev-archive@www.apache.org Received: (qmail 90071 invoked from network); 14 Feb 2006 16:13:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 14 Feb 2006 16:13:23 -0000 Received: (qmail 36842 invoked by uid 500); 14 Feb 2006 16:13:21 -0000 Delivered-To: apmail-incubator-stdcxx-dev-archive@incubator.apache.org Received: (qmail 36754 invoked by uid 500); 14 Feb 2006 16:13:20 -0000 Mailing-List: contact stdcxx-dev-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-dev@incubator.apache.org Received: (qmail 36726 invoked by uid 99); 14 Feb 2006 16:13:20 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Feb 2006 08:13:20 -0800 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of AntonP@moscow.vdiweb.com designates 195.210.189.132 as permitted sender) Received: from [195.210.189.132] (HELO exmsk.moscow.vdiweb.com) (195.210.189.132) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Feb 2006 08:13:18 -0800 Received: from [10.11.0.166] ([10.11.0.166]) by exmsk.moscow.vdiweb.com with Microsoft SMTPSVC(6.0.3790.1830); Tue, 14 Feb 2006 19:12:55 +0300 Message-ID: <43F20181.1050703@moscow.vdiweb.com> Date: Tue, 14 Feb 2006 19:12:49 +0300 From: Anton Pevtsov User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040514 X-Accept-Language: en-us, en MIME-Version: 1.0 To: stdcxx-dev@incubator.apache.org Subject: test for lib.alg.permutation.generators Content-Type: multipart/mixed; boundary="------------010604030504080801060004" X-OriginalArrivalTime: 14 Feb 2006 16:12:55.0638 (UTC) FILETIME=[83ADB760:01C63181] X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N --------------010604030504080801060004 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit The attached file contains the test for the lib.alg.permutation.generators algorithms (prev_permutation, next_permutation). With best wishes, Anton Pevtsov --------------010604030504080801060004 Content-Type: text/plain; name="25.permutation.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="25.permutation.cpp" /*************************************************************************** * * permutation.cpp - test exercising 25.3.9 [lib.alg.permutation.generators] * * $Id: //stdlib/dev/tests/stdlib/algorithm/permutation.cpp#9 $ * *************************************************************************** * * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave * Software division. Licensed under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0. Unless required by * applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License * for the specific language governing permissions and limitations under * the License. * **************************************************************************/ #include // for prev_permutation, next_permutation #include // for strlen, size_t #include #include // for rw_test() _RWSTD_NAMESPACE (std) { // disable explicit instantiation for compilers that can't handle it #ifndef _RWSTD_NO_EXPLICIT_INSTANTIATION template bool prev_permutation (BidirIter > > >, BidirIter > > >); template bool prev_permutation (BidirIter > > >, BidirIter > > >, binary_predicate > > >); template bool next_permutation (BidirIter > > >, BidirIter > > >); template bool next_permutation (BidirIter > > >, BidirIter > > >, binary_predicate > > >); #endif // _RWSTD_NO_EXPLICIT_INSTANTIATION } // namespace std template struct Less { static std::size_t funcalls_; // dummy arguments provided to prevent the class from being // default constructible and implicit conversion from int Less (int /* dummy */, int /* dummy */) { funcalls_ = 0; } // return a type other than bool but one that is implicitly // convertible to bool to detect incorrect assumptions conv_to_bool operator() (const T &x, const T &y) /* non-const */ { ++funcalls_; return conv_to_bool::make (x.val_ < y.val_); } static const char* name () { return "Less"; } private: void operator= (Less&); // not assignable }; template std::size_t Less::funcalls_; /**************************************************************************/ // exercise prev_permutation and next_permutation 25.3.9 template void test_permutations (int line, const char *src, const char *dst, bool res, const BidirectIterator &it, const T*, const Predicate *ppred, bool prev) { const char* const itname = type_name (it, (T*)0); const char* const fname = prev ? "prev_permutation" : "next_permutation"; const char* const funname = Predicate::name(); const std::size_t nsrc = std::strlen (src); T* const xsrc = T::from_char (src, nsrc); T* const xsrc_end = xsrc + nsrc; const BidirectIterator first = make_iter (xsrc, xsrc, xsrc_end, it); const BidirectIterator last = make_iter (xsrc_end, xsrc, xsrc_end, it); Predicate pred (0, 0); const std::size_t last_n_op_assign = T::n_total_op_assign_; bool result = false; if (ppred) { result = prev ? std::prev_permutation (first, last, pred) : std::next_permutation (first, last, pred); } else { result = prev ? std::prev_permutation (first, last) : std::next_permutation (first, last); } std::size_t n_ops_assign = T::n_total_op_assign_ - last_n_op_assign; // verify the returned value: 25.3.9, p1 and p4 bool success = result == res; rw_assert (success, 0, line, "line %d %s <%s%{?}, %s%{;}> (\"%s\", ...) == %b, got %b", __LINE__, fname, itname, 0 != ppred, funname, src, result, res); // verify the permutation result std::size_t i = 0; for ( ; i < nsrc; i++) { success = xsrc[i].val_ == dst[i]; if (!success) break; } // to avoid errors in --trace mode i = i < nsrc ? i : nsrc - 1; rw_assert (success, 0, line, "line %d %s <%s%{?}, %s%{;}> (\"%s\", ...) " "==> \"%{X=*.*}\", expected %s", __LINE__, fname, itname, 0 != ppred, funname, src, int (nsrc), i, xsrc, dst); // verify the complexity: 25.3.9 p2 and p5. // (last - first) / 2 swaps, 2 assign per swap success = n_ops_assign <= nsrc; rw_assert (success, 0, line, "line %d %s <%s%{?}, %s%{;}> (\"%s\", ...) " "complexity: got %zu assigns, expected no more than %zu", __LINE__, fname, itname, 0 != ppred, funname, src, n_ops_assign, nsrc); delete[] xsrc; } template void test_permutations (const BidirectIterator &it, const T*, const Predicate *ppred, bool prev) { const char* const itname = type_name (it, (T*)0); const char* const fname = prev ? "prev_permutation" : "next_permutation"; const char* const funname = Predicate::name(); rw_info (0, 0, 0, "std::%s(%s, %2$s%{?}, %s%{;})", fname, itname, 0 != ppred, funname); #define TEST(src, dst, res) \ test_permutations (__LINE__, src, dst, res, it, (T*)0, ppred, prev) if (prev) { TEST ("a", "a", false); TEST ("aa", "aa", false); TEST ("ab", "ba", false); TEST ("ba", "ab", true); TEST ("bcaefigjhd", "bcaefigjdh", true); TEST ("bcaefihdgj", "bcaefigjhd", true); TEST ("hefdiacjbg", "hefdiacgjb", true); TEST ("hefdiacjgb", "hefdiacjbg", true); TEST ("aaaaaaaaaa", "aaaaaaaaaa", false); TEST ("aaaaabbbbb", "bbbbbaaaaa", false); TEST ("ababababab", "abababaabb", true); TEST ("bbbbbaaaaa", "bbbbabaaaa", true); TEST ("abcdefghij", "jihgfedcba", false); TEST ("jihgfedcba", "jihgfedcab", true); } else { TEST ("a", "a", false); TEST ("aa", "aa", false); TEST ("ab", "ba", true); TEST ("ba", "ab", false); TEST ("bcaefigjhd", "bcaefihdgj", true); TEST ("bcaefigjdh", "bcaefigjhd", true); TEST ("hefdiacjbg", "hefdiacjgb", true); TEST ("hefdiacgjb", "hefdiacjbg", true); TEST ("aaaaaaaaaa", "aaaaaaaaaa", false); TEST ("aaaaabbbbb", "aaaababbbb", true); TEST ("ababababab", "ababababba", true); TEST ("bbbbbaaaaa", "aaaaabbbbb", false); TEST ("abcdefghij", "abcdefghji", true); TEST ("jihgfedcba", "abcdefghij", false); } } /**************************************************************************/ /* extern */ int rw_opt_no_prev_permutation; // --no-prev_permutation /* extern */ int rw_opt_no_next_permutation; // --no-next_permutation /* extern */ int rw_opt_no_predicate; // --no-predicate /* extern */ int rw_opt_no_bidir_iter; // --no-BidirectionalIterator /* extern */ int rw_opt_no_rnd_iter; // --no-RandomAccessIterator /**************************************************************************/ template void test_permutations (const T*, const Predicate* ppred, bool prev) { static const BidirIter bidir_iter (0, 0, 0); static const RandomAccessIter rand_iter (0, 0, 0); rw_info (0, 0, 0, "template " "bool %s (%1$s, %1$s%{?}, %s%{;})", "BidirectionalIterator", 0 != ppred, "Compare", prev ? "prev_permutation" : "next_permutation", 0 != ppred, "Compare"); if (rw_opt_no_bidir_iter) { rw_note (0, __FILE__, __LINE__, "BidirectionalIterator test disabled"); } else { test_permutations (bidir_iter, (T*)0, ppred, prev); } if (rw_opt_no_rnd_iter) { rw_note (0, __FILE__, __LINE__, "RandomAccessIterator test disabled"); } else { test_permutations (rand_iter, (T*)0, ppred, prev); } } template void test_permutations (const T*, const Predicate* ppred) { if (rw_opt_no_prev_permutation) { rw_note (0, __FILE__, __LINE__, "std::prev_permutation test disabled"); } else { test_permutations ((X*)0, ppred, true); } if (rw_opt_no_next_permutation) { rw_note (0, __FILE__, __LINE__, "std::next_permutation test disabled"); } else { test_permutations ((X*)0, ppred, false); } } static int run_test (int, char*[]) { test_permutations ((X*)0, (Less*)0); if (rw_opt_no_predicate) { rw_note (0, __FILE__, __LINE__, "permutations predicate test disabled"); } else { test_permutations ((X*)0, (Less*)1); } return 0; } /**************************************************************************/ int main (int argc, char *argv[]) { return rw_test (argc, argv, __FILE__, "lib.alg.permutation.generators", 0 /* no comment */, run_test, "|-no-prev_permutation# " "|-no-next_permutation# " "|-no-predicate# " "|-no-BidirectionalIterator# " "|-no-RandomAccessIterator#", &rw_opt_no_prev_permutation, &rw_opt_no_next_permutation, &rw_opt_no_predicate, &rw_opt_no_bidir_iter, &rw_opt_no_rnd_iter); } --------------010604030504080801060004--