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 "test/int.hh" 35 36#include <gecode/minimodel.hh> 37 38namespace Test { namespace Int { 39 40 /// %Tests for synchronized execution 41 namespace Exec { 42 43 /** 44 * \defgroup TaskTestIntExec Synchronized execution 45 * \ingroup TaskTestInt 46 */ 47 //@{ 48 /// Simple test for wait (integer variables) 49 class IntWait : public Test { 50 protected: 51 /// Whether to use std::function 52 bool sf; 53 public: 54 /// Create and register test 55 IntWait(int n, bool sf0) 56 : Test("Wait::Int::"+str(n)+"::"+ 57 (sf0 ? "std::function" : "funptr"),n,0,n,false), sf(sf0) {} 58 /// Check whether \a x is solution 59 virtual bool solution(const Assignment& x) const { 60 for (int i=0; i<x.size(); i++) 61 for (int j=i+1; j<x.size(); j++) 62 if (x[i] == x[j]) 63 return false; 64 return true; 65 } 66 /// Post wait on \a x 67 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 68 using namespace Gecode; 69 auto f = static_cast<std::function<void(Space&)>> 70 ([](Space& home) { c(home); }); 71 if (x.size() > 1) { 72 if (sf) 73 Gecode::wait(home, x, f); 74 else 75 Gecode::wait(home, x, &c); 76 } else { 77 if (sf) 78 Gecode::wait(home, x[0], f); 79 else 80 Gecode::wait(home, x[0], &c); 81 } 82 } 83 /// Continuation to be executed 84 static void c(Gecode::Space& _home) { 85 TestSpace& home = static_cast<TestSpace&>(_home); 86 for (int i=0; i<home.x.size(); i++) 87 for (int j=i+1; j<home.x.size(); j++) 88 if (home.x[i].val() == home.x[j].val()) 89 home.fail(); 90 } 91 }; 92 93 /// Simple test for wait (Boolean variables) 94 class BoolWait : public Test { 95 protected: 96 /// Whether to use std::function 97 bool sf; 98 public: 99 /// Create and register test 100 BoolWait(int n, bool sf0) 101 : Test("Wait::Bool::"+str(n)+"::"+ 102 (sf0 ? "std::function" : "funptr"),n,0,1,false), sf(sf0) {} 103 /// Check whether \a x is solution 104 virtual bool solution(const Assignment& x) const { 105 int t=0; 106 for (int i=0; i<x.size(); i++) 107 t += x[i]; 108 return t==2; 109 } 110 /// Post wait on \a x 111 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 112 using namespace Gecode; 113 BoolVarArgs b(x.size()); 114 for (int i=b.size(); i--; ) 115 b[i]=channel(home,x[i]); 116 auto f = static_cast<std::function<void(Space&)>> 117 ([](Space& home) { c(home); }); 118 if (b.size() > 1) { 119 if (sf) 120 Gecode::wait(home, b, f); 121 else 122 Gecode::wait(home, b, &c); 123 } else { 124 if (sf) 125 Gecode::wait(home, b[0], f); 126 else 127 Gecode::wait(home, b[0], &c); 128 } 129 } 130 /// Continuation to be executed 131 static void c(Gecode::Space& _home) { 132 TestSpace& home = static_cast<TestSpace&>(_home); 133 int t=0; 134 for (int i=0; i<home.x.size(); i++) 135 t += home.x[i].val(); 136 if (t!=2) 137 home.fail(); 138 } 139 }; 140 141 /// Simple test for when 142 class When : public Test { 143 protected: 144 /// Whether to use std::function 145 bool sf; 146 public: 147 /// Create and register test 148 When(bool sf0) 149 : Test(std::string("When::")+ 150 (sf0 ? "std::function" : "funptr"),1,0,1,false), sf(sf0) {} 151 /// Check whether \a x is solution 152 virtual bool solution(const Assignment& x) const { 153 return x[0]==0; 154 } 155 /// Post when on \a x 156 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 157 using namespace Gecode; 158 if (sf) { 159 auto sft = static_cast<std::function<void(Space&)>> 160 ([](Space& home) { t(home); }); 161 auto sfe = static_cast<std::function<void(Space&)>> 162 ([](Space& home) { e(home); }); 163 when(home, channel(home, x[0]), sft, sfe); 164 } else { 165 when(home, channel(home, x[0]), &t, &e); 166 } 167 } 168 /// Then-function to be executed 169 static void t(Gecode::Space& home) { 170 home.fail(); 171 } 172 /// Else-function to be executed 173 static void e(Gecode::Space& home) { 174 (void) home; 175 } 176 }; 177 178 IntWait iw1t(1,true), iw2t(2,true), iw3t(3,true), iw4t(4,true); 179 IntWait iw1f(1,false), iw2f(2,false), iw3f(3,false), iw4f(4,false); 180 BoolWait bw1t(1,true), bw2t(2,true), bw3t(3,true), bw4t(4,true); 181 BoolWait bw1f(1,false), bw2f(2,false), bw3f(3,false), bw4f(4,false); 182 183 184 When whent(true); 185 When whenf(false); 186 //@} 187 188 } 189 190}} 191 192// STATISTICS: test-int