File: array.cpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (277 lines) | stat: -rw-r--r-- 10,184 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- 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)                                         \
do {                                                            \
if (opt.log)                                                    \
  olog << ind(3) << "Check: " << (M) << std::endl;              \
if (!(T)) {                                                     \
  problem = (M); goto failed;                                   \
}                                                               \
} while (false)

/// Start new test
#define START_TEST(T)                                           \
do {                                                            \
  if (opt.log) {                                                \
     olog.str("");                                              \
     olog << ind(2) << "Testing: " << (T) << std::endl;         \
  }                                                             \
  test = (T);                                                   \
} while (false)

namespace Test {

  /// Tests for arrays
  namespace Array {

  /// Test name prefix
  static const std::string prefix("Array::Iterator::");

  /// %Base class for testing iterators
  class Iterator : public Test::Base {
  protected:
    /// Maximum array size
    static const int n = 16;
    /// Initialize test
    Iterator(const std::string& name) : Test::Base(prefix + name) {}
    /// Perform actual tests
    template<class Array> bool runTestForArray(Array& a) {
      // Test/problem information.
      const char* test    = "NONE";
      const char* problem = "NONE";
      // Constant reference to the array
      const 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 space
  class 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 iterator
  class VarArrayIterator : public Iterator {
  protected:
    /// Maximum array size
    static const int n = 16;
    /// Array type being tested
    typedef Gecode::VarArray<Gecode::IntVar> Array;
  public:
    /// Initialize test
    VarArrayIterator(void) : Iterator("VarArray") {}
    /// Perform actual tests
    bool run(void) {
      // Space for the test
      TestSpace s;
      // VarArray for the test
      Array a(s,_rand(n));
      // Run the iterator test
      return runTestForArray(a);
    }
  } varArrayIteratorTest;

  /// %Class for testing the VarArgs iterator
  class VarArgsIterator : public Iterator {
  protected:
    /// Maximum array size
    static const int n = 16;
    /// Array type being tested
    typedef Gecode::ArgArrayBase<int> Array;
  public:
    /// Initialize test
    VarArgsIterator(void) : Iterator("VarArgs") {}
    /// Perform actual tests
    bool run(void) {
      // Space for the test
      TestSpace s;
      // VarArray for the test
      Array a(_rand(n));
      // Run the iterator test
      return runTestForArray(a);
    }
  } varArgsIteratorTest;

  /// %Class for testing the ViewArray iterator
  class ViewArrayIterator : public Iterator {
  protected:
    /// Maximum array size
    static const int n = 16;
    /// Array type being tested
    typedef Gecode::ViewArray<Gecode::IntVar> Array;
  public:
    /// Initialize test
    ViewArrayIterator(void) : Iterator("ViewArray") {}
    /// Perform actual tests
    bool run(void) {
      // Space for the test
      TestSpace s;
      // VarArray for the test
      Array a(s,_rand(n));
      // Run the iterator test
      return runTestForArray(a);
    }
  } viewArrayIteratorTest;

  /// %Class for testing the SharedArray iterator
  class SharedArrayIterator : public Iterator {
  protected:
    /// Maximum array size
    static const int n = 16;
    /// Array type being tested
    typedef Gecode::SharedArray<int> Array;
  public:
    /// Initialize test
    SharedArrayIterator(void) : Iterator("SharedArray") {}
    /// Perform actual tests
    bool run(void) {
      // SharedArray for the test
      Array a(_rand(n));
      // Run the iterator test
      return runTestForArray(a);
    }
  } sharedArrayIteratorTest;

}}

// STATISTICS: test-core