Generated on Thu Apr 11 13:59:14 2019 for Gecode by doxygen 1.6.3

afc.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2009
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/kernel.hh>
00035 #include <gecode/int.hh>
00036 
00037 #include "test/test.hh"
00038 
00039 namespace Test {
00040 
00042   class AFC : public Test::Base {
00043   protected:
00045     class TestSpace : public Gecode::Space {
00046     protected:
00048       Gecode::IntVar x, y;
00049     public:
00051       TestSpace(void) : x(*this,0,10), y(*this,0,10) {}
00053       TestSpace(TestSpace& s) : Space(s) {
00054         x.update(*this,s.x);
00055         y.update(*this,s.y);
00056       }
00058       void post(void) {
00059         Gecode::rel(*this, x, Gecode::IRT_LE, y);
00060       }
00062       virtual Space* copy(void) {
00063         return new TestSpace(*this);
00064       }
00065     };
00067     static const int n_ops = 8 * 1024;
00069     static const int n = 16;
00071     int space(TestSpace* s[]) {
00072       int i = rand(n);
00073       while (s[i] == NULL)
00074         i = (i+1) % n;
00075       return i;
00076     }
00078     int index(void) {
00079       return rand(n);
00080     }
00081   public:
00083     AFC(void) : Test::Base("AFC") {}
00085     bool run(void) {
00086       // Array of spaces for tests
00087       TestSpace* s[n];
00088       // How many spaces exist in s
00089       int n_s = 1;
00090 
00091       for (int i=n; i--; )
00092         s[i] = NULL;
00093       s[0] = new TestSpace;
00094 
00095       for (int o=n_ops; o--; )
00096         switch (rand(3)) {
00097         case 0:
00098           // clone space
00099           {
00100             int i = index();
00101             if ((s[i] != NULL)) {
00102               if (n_s > 1) {
00103                 delete s[i]; s[i]=NULL; n_s--;
00104               } else {
00105                 break;
00106               }
00107             }
00108             int j = space(s);
00109             (void) s[j]->status();
00110             s[i] = static_cast<TestSpace*>(s[j]->clone());
00111             n_s++;
00112           }
00113           break;
00114         case 1:
00115           // delete space
00116           if (n_s > 1) {
00117             int i = space(s);
00118             delete s[i]; s[i]=NULL; n_s--;
00119           }
00120           break;
00121         case 2:
00122           // post propagator
00123           s[space(s)]->post();
00124           break;
00125         default:
00126           GECODE_NEVER;
00127         }
00128       // Delete all remaining spaces
00129       for (int i=n; i--; )
00130         delete s[i];
00131       return true;
00132     }
00133   };
00134 
00135   AFC afc;
00136 
00137 }
00138 
00139 // STATISTICS: test-core