/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//** Main authors:* Gregory Crosswhite <gcross@phys.washington.edu>** Copyright:* Gregory Crosswhite, 2011** This file is part of Gecode, the generic constraint* development environment:* http://www.gecode.org** Permission is hereby granted, free of charge, to any person obtaining* a copy of this software and associated documentation files (the* "Software"), to deal in the Software without restriction, including* without limitation the rights to use, copy, modify, merge, publish,* distribute, sublicense, and/or sell copies of the Software, and to* permit persons to whom the Software is furnished to do so, subject to* the following conditions:** The above copyright notice and this permission notice shall be* included in all copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**/#include <gecode/kernel.hh>#include <gecode/int.hh>#include "test/test.hh"/// Check the test result and handle failed test#define CHECK_TEST(T,M) \if (opt.log) \olog << ind(3) << "Check: " << (M) << std::endl; \if (!(T)) { \problem = (M); goto failed; \}/// Start new test#define START_TEST(T) \if (opt.log) { \olog.str(""); \olog << ind(2) << "Testing: " << (T) << std::endl; \} \test = (T);namespace Test {/// Tests for arraysnamespace Array {/// Test name prefixstatic const std::string prefix("Array::Iterator::");/// %Base class for testing iteratorsclass Iterator : public Test::Base {protected:/// Maximum array sizestatic const int n = 16;/// Initialize testIterator(const std::string& name) : Test::Base(prefix + name) {}/// Perform actual teststemplate<class Array> bool runTestForArray(Array& a) {// Test/problem information.const char* test = "NONE";const char* problem = "NONE";// Constant reference to the arrayconst Array& const_a = a;START_TEST("Iteration");{typedef typename Array::reference reference;typedef typename Array::pointer pointer;typedef typename Array::iterator iterator;const iterator begin = a.begin(), end = a.end();CHECK_TEST(end-begin==a.size(),"Distance != size");int index = 0;iterator iter = begin;for(; iter != end; ++iter, ++index) {reference ref = *iter;const pointer ptr = &ref;CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)");}CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)");for(; iter != begin; --iter, --index) {reference ref = *(iter-1);const pointer ptr = &ref;CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)");}CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");}START_TEST("Read-only iteration");{typedef typename Array::const_reference reference;typedef typename Array::const_pointer pointer;typedef typename Array::const_iterator iterator;const iterator begin = const_a.begin(), end = const_a.end();CHECK_TEST(end-begin==const_a.size(),"Distance != size");int index = 0;iterator iter = begin;for(; iter != end; ++iter, ++index) {reference ref = *iter;const pointer ptr = &ref;CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)");}CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)");for(; iter != begin; --iter, --index) {reference ref = *(iter-1);const pointer ptr = &ref;CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)");}CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)");}START_TEST("Reverse iteration");{typedef typename Array::reference reference;typedef typename Array::pointer pointer;typedef typename Array::reverse_iterator iterator;const iterator begin = a.rbegin(), end = a.rend();CHECK_TEST(end-begin==a.size(),"Distance != size");int index = a.size();iterator iter = begin;for(; iter != end; ++iter, --index) {reference ref = *iter;const pointer ptr = &ref;CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going forward)");}CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");for(; iter != begin; --iter, ++index) {reference ref = *(iter-1);const pointer ptr = &ref;CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going backwards)");}CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");}START_TEST("Reverse read-only iteration");{typedef typename Array::const_reference reference;typedef typename Array::const_pointer pointer;typedef typename Array::const_reverse_iterator iterator;const iterator begin = const_a.rbegin(), end = const_a.rend();CHECK_TEST(end-begin==const_a.size(),"Distance != size");int index = a.size();iterator iter = begin;for(; iter != end; ++iter, --index) {reference ref = *iter;const pointer ptr = &ref;CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going forward)");}CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going forward)");for(; iter != begin; --iter, ++index) {reference ref = *(iter-1);const pointer ptr = &ref;CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going backwards)");}CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going backward)");}return true;failed:if (opt.log)olog << "FAILURE" << std::endl<< ind(1) << "Test: " << test << std::endl<< ind(1) << "Problem: " << problem << std::endl;return false;}};/// Test spaceclass TestSpace : public Gecode::Space {public:TestSpace(void) : Space() {}TestSpace(TestSpace& s) : Space(s) {}virtual Space* copy(void) {return new TestSpace(*this);}};/// %Class for testing the VarArray iteratorclass VarArrayIterator : public Iterator {protected:/// Maximum array sizestatic const int n = 16;/// Array type being testedtypedef Gecode::VarArray<Gecode::IntVar> Array;public:/// Initialize testVarArrayIterator(void) : Iterator("VarArray") {}/// Perform actual testsbool run(void) {// Space for the testTestSpace s;// VarArray for the testArray a(s,rand(n));// Run the iterator testreturn runTestForArray(a);}} varArrayIteratorTest;/// %Class for testing the VarArgs iteratorclass VarArgsIterator : public Iterator {protected:/// Maximum array sizestatic const int n = 16;/// Array type being testedtypedef Gecode::ArgArrayBase<int> Array;public:/// Initialize testVarArgsIterator(void) : Iterator("VarArgs") {}/// Perform actual testsbool run(void) {// Space for the testTestSpace s;// VarArray for the testArray a(rand(n));// Run the iterator testreturn runTestForArray(a);}} varArgsIteratorTest;/// %Class for testing the ViewArray iteratorclass ViewArrayIterator : public Iterator {protected:/// Maximum array sizestatic const int n = 16;/// Array type being testedtypedef Gecode::ViewArray<Gecode::IntVar> Array;public:/// Initialize testViewArrayIterator(void) : Iterator("ViewArray") {}/// Perform actual testsbool run(void) {// Space for the testTestSpace s;// VarArray for the testArray a(s,rand(n));// Run the iterator testreturn runTestForArray(a);}} viewArrayIteratorTest;/// %Class for testing the SharedArray iteratorclass SharedArrayIterator : public Iterator {protected:/// Maximum array sizestatic const int n = 16;/// Array type being testedtypedef Gecode::SharedArray<int> Array;public:/// Initialize testSharedArrayIterator(void) : Iterator("SharedArray") {}/// Perform actual testsbool run(void) {// SharedArray for the testArray a(rand(n));// Run the iterator testreturn runTestForArray(a);}} sharedArrayIteratorTest;}}// STATISTICS: test-core
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。