this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Copyright: 7 * Christian Schulte, 2009 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34#include <gecode/kernel.hh> 35#include <gecode/int.hh> 36 37#include "test/test.hh" 38 39namespace Test { 40 41 /// %Test for %AFC infrastructure 42 class AFC : public Test::Base { 43 protected: 44 /// Test space 45 class TestSpace : public Gecode::Space { 46 protected: 47 /// Two integer variables 48 Gecode::IntVar x, y; 49 public: 50 /// Constructor for creation 51 TestSpace(void) : x(*this,0,10), y(*this,0,10) {} 52 /// Constructor for cloning \a s 53 TestSpace(TestSpace& s) : Space(s) { 54 x.update(*this,s.x); 55 y.update(*this,s.y); 56 } 57 /// Post arbitrary propagator 58 void post(void) { 59 Gecode::rel(*this, x, Gecode::IRT_LE, y); 60 } 61 /// Copy during cloning 62 virtual Space* copy(void) { 63 return new TestSpace(*this); 64 } 65 }; 66 /// How many test operations to be performed 67 static const int n_ops = 8 * 1024; 68 /// How many spaces to maintain 69 static const int n = 16; 70 /// Return random index of non-null space 71 int space(TestSpace* s[]) { 72 int i = _rand(n); 73 while (s[i] == nullptr) 74 i = (i+1) % n; 75 return i; 76 } 77 /// Return random index 78 int index(void) { 79 return _rand(n); 80 } 81 public: 82 /// Initialize test 83 AFC(void) : Test::Base("AFC") {} 84 /// Perform actual tests 85 bool run(void) { 86 // Array of spaces for tests 87 TestSpace* s[n]; 88 // How many spaces exist in s 89 int n_s = 1; 90 91 for (int i=n; i--; ) 92 s[i] = nullptr; 93 s[0] = new TestSpace; 94 95 for (int o=n_ops; o--; ) 96 switch (_rand(3)) { 97 case 0: 98 // clone space 99 { 100 int i = index(); 101 if ((s[i] != nullptr)) { 102 if (n_s > 1) { 103 delete s[i]; s[i]=nullptr; n_s--; 104 } else { 105 break; 106 } 107 } 108 int j = space(s); 109 (void) s[j]->status(); 110 s[i] = static_cast<TestSpace*>(s[j]->clone()); 111 n_s++; 112 } 113 break; 114 case 1: 115 // delete space 116 if (n_s > 1) { 117 int i = space(s); 118 delete s[i]; s[i]=nullptr; n_s--; 119 } 120 break; 121 case 2: 122 // post propagator 123 s[space(s)]->post(); 124 break; 125 default: 126 GECODE_NEVER; 127 } 128 // Delete all remaining spaces 129 for (int i=n; i--; ) 130 delete s[i]; 131 return true; 132 } 133 }; 134 135 AFC afc; 136 137} 138 139// STATISTICS: test-core