Generated on Thu Mar 22 10:39:45 2012 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  *  Last modified:
00010  *     $Date: 2010-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ by $Author: schulte $
00011  *     $Revision: 10684 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include <gecode/kernel.hh>
00039 #include <gecode/int.hh>
00040 
00041 #include "test/test.hh"
00042 
00043 namespace Test {
00044 
00046   class AFC : public Test::Base {
00047   protected:
00049     class TestSpace : public Gecode::Space {
00050     protected:
00052       Gecode::IntVar x, y;
00053     public:
00055       TestSpace(void) : x(*this,0,10), y(*this,0,10) {}
00057       TestSpace(bool share, TestSpace& s) : Space(share,s) {
00058         x.update(*this,share,s.x);
00059         y.update(*this,share,s.y);
00060       }
00062       void post(void) {
00063         Gecode::rel(*this, x, Gecode::IRT_LE, y);
00064       }
00066       virtual Space* copy(bool share) {
00067         return new TestSpace(share,*this);
00068       }
00069     };
00071     static const int n_ops = 8 * 1024;
00073     static const int n = 16;
00075     int space(TestSpace* s[]) {
00076       int i = rand(n);
00077       while (s[i] == NULL)
00078         i = (i+1) % n;
00079       return i;
00080     }
00082     int index(void) {
00083       return rand(n);
00084     }
00085   public:
00087     AFC(void) : Test::Base("AFC") {}
00089     bool run(void) {
00090       // Array of spaces for tests
00091       TestSpace* s[n];
00092       // How many spaces exist in s
00093       int n_s = 1;
00094       
00095       for (int i=n; i--; )
00096         s[i] = NULL;
00097       s[0] = new TestSpace;
00098 
00099       for (int o=n_ops; o--; )
00100         switch (rand(3)) {
00101         case 0:
00102           // clone space
00103           {
00104             int i = index();
00105             if ((s[i] != NULL)) {
00106               if (n_s > 1) {
00107                 delete s[i]; s[i]=NULL; n_s--;
00108               } else {
00109                 break;
00110               }
00111             }
00112             int j = space(s);
00113             (void) s[j]->status();
00114             s[i] = static_cast<TestSpace*>(s[j]->clone());
00115             n_s++;
00116           }
00117           break;
00118         case 1:
00119           // delete space
00120           if (n_s > 1) {
00121             int i = space(s);
00122             delete s[i]; s[i]=NULL; n_s--;
00123           }
00124           break;
00125         case 2:
00126           // post propagator
00127           s[space(s)]->post();
00128           break;
00129         default:
00130           GECODE_NEVER;
00131         }
00132       // Delete all remaining spaces
00133       for (int i=n; i--; )
00134         delete s[i];
00135       return true;
00136     }
00137   };
00138 
00139   AFC afc;
00140 
00141 }
00142 
00143 // STATISTICS: test-kernel