literal_test.cpp
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 #include "test.h"
00021 #include <clasp/solver_types.h>
00022 
00023 namespace Clasp { namespace Test {
00024 
00025 class LiteralTest : public CppUnit::TestFixture {
00026   CPPUNIT_TEST_SUITE(LiteralTest);
00027         CPPUNIT_TEST(testCtor);
00028         CPPUNIT_TEST(testIndex);
00029         CPPUNIT_TEST(testIndexIgnoresWatchedFlag);
00030         CPPUNIT_TEST(testFromIndex);
00031         CPPUNIT_TEST(testWatchedFlag);
00032         CPPUNIT_TEST(testWatchedFlagCopy);
00033         CPPUNIT_TEST(testComplement);
00034         CPPUNIT_TEST(testComplementIsNotWatched);
00035         CPPUNIT_TEST(testEquality);
00036         CPPUNIT_TEST(testValue);
00037         CPPUNIT_TEST(testLess);
00038 
00039         CPPUNIT_TEST(testAntecedentAssumptions);
00040         CPPUNIT_TEST(testAntecedentNullPointer);
00041         CPPUNIT_TEST(testAntecedentPointer);
00042         CPPUNIT_TEST(testAntecedentBin);
00043         CPPUNIT_TEST(testAntecedentTern);
00044         CPPUNIT_TEST_SUITE_END();
00045 public:
00046         LiteralTest() {
00047                 min = posLit(0), mid = posLit(varMax / 2),  max = posLit(varMax - 1);
00048         }
00049         void testCtor() {
00050                 Literal p, q(42, true);
00051                 CPPUNIT_ASSERT_EQUAL(Var(0), p.var());
00052                 CPPUNIT_ASSERT_EQUAL(false, p.sign());
00053                 CPPUNIT_ASSERT_EQUAL(false, p.watched());
00054 
00055                 CPPUNIT_ASSERT_EQUAL(Var(42), q.var());
00056                 CPPUNIT_ASSERT_EQUAL(true, q.sign());
00057                 CPPUNIT_ASSERT_EQUAL(false, q.watched());
00058 
00059                 Literal x = posLit(7);
00060                 Literal y = negLit(7);
00061                 CPPUNIT_ASSERT(x.var() == y.var() && y.var() == Var(7));
00062                 CPPUNIT_ASSERT_EQUAL(false, x.sign());
00063                 CPPUNIT_ASSERT_EQUAL(true, y.sign());
00064         }
00065         void testIndex() {
00066                 uint32 minIdx   = min.index();
00067                 uint32 maxIdx   = max.index();
00068                 uint32 midIdx   = mid.index();
00069 
00070                 CPPUNIT_ASSERT_EQUAL(uint32(0), minIdx);
00071                 CPPUNIT_ASSERT_EQUAL(uint32(1), (~min).index());
00072                 
00073                 CPPUNIT_ASSERT_EQUAL(uint32((max.var()*2)), maxIdx);
00074                 CPPUNIT_ASSERT_EQUAL(uint32((max.var()*2)+1), (~max).index());
00075                 
00076                 CPPUNIT_ASSERT_EQUAL(uint32((mid.var()*2)), midIdx);
00077                 CPPUNIT_ASSERT_EQUAL(uint32((mid.var()*2)+1), (~mid).index());
00078 
00079                 CPPUNIT_ASSERT_EQUAL( minIdx, index(min.var()) );
00080                 CPPUNIT_ASSERT_EQUAL( maxIdx, index(max.var()) );
00081                 CPPUNIT_ASSERT_EQUAL( midIdx, index(mid.var()) );
00082         }
00083         void testIndexIgnoresWatchedFlag() {
00084                 Literal maxW = max;
00085                 maxW.watch();
00086                 CPPUNIT_ASSERT_EQUAL(max.index(), maxW.index());
00087         }
00088         void testFromIndex() {
00089                 CPPUNIT_ASSERT(min == Literal::fromIndex(min.index()));
00090                 CPPUNIT_ASSERT(max == Literal::fromIndex(max.index()));
00091                 CPPUNIT_ASSERT(mid == Literal::fromIndex(mid.index()));
00092         }
00093         void testWatchedFlag() {
00094                 Literal p = posLit(42);
00095                 p.watch();
00096                 CPPUNIT_ASSERT_EQUAL(true, p.watched());
00097                 p.clearWatch();
00098                 CPPUNIT_ASSERT_EQUAL(false, p.watched());
00099         }
00100         void testWatchedFlagCopy() {
00101                 Literal p = posLit(42);
00102                 p.watch();
00103                 Literal q = p;
00104                 CPPUNIT_ASSERT_EQUAL(true, q.watched());
00105         }
00106         void testComplement() {
00107                 Literal lit = posLit(7);
00108                 Literal cLit = ~lit;
00109                 CPPUNIT_ASSERT_EQUAL(lit.var(), cLit.var());
00110                 CPPUNIT_ASSERT_EQUAL(false, lit.sign());
00111                 CPPUNIT_ASSERT_EQUAL(true, cLit.sign());
00112                 CPPUNIT_ASSERT_EQUAL(true, lit == ~~lit);
00113         }
00114         void testComplementIsNotWatched() {
00115                 Literal lit = posLit(7);
00116                 lit.watch();
00117                 Literal cLit = ~lit;
00118                 CPPUNIT_ASSERT_EQUAL(false, cLit.watched());
00119         }
00120 
00121         void testEquality() {
00122                 Literal p = posLit(42);
00123                 Literal q = negLit(42);
00124                 CPPUNIT_ASSERT_EQUAL(p, p);
00125                 CPPUNIT_ASSERT_EQUAL(p, ~q);
00126                 CPPUNIT_ASSERT_EQUAL(false, p == q);
00127                 CPPUNIT_ASSERT_EQUAL(Literal(), Literal());
00128         }
00129 
00130         void testValue() {
00131                 CPPUNIT_ASSERT_EQUAL(value_true, trueValue(posLit(0)));
00132                 CPPUNIT_ASSERT_EQUAL(value_false, trueValue(negLit(0)));
00133                 CPPUNIT_ASSERT_EQUAL(value_false, falseValue(posLit(0)));
00134                 CPPUNIT_ASSERT_EQUAL(value_true, falseValue(negLit(0)));
00135         }
00136 
00137         void testLess() {
00138                 Literal p = posLit(42), q = negLit(42);
00139                 CPPUNIT_ASSERT_EQUAL(false, p < p);
00140                 CPPUNIT_ASSERT_EQUAL(false, q < q);
00141                 CPPUNIT_ASSERT_EQUAL(true, p < q);
00142                 CPPUNIT_ASSERT_EQUAL(false, q < p);
00143 
00144                 Literal one(1, false);
00145                 Literal two(2, true);
00146                 Literal negOne = ~one;
00147                 CPPUNIT_ASSERT_EQUAL(true, one < two);
00148                 CPPUNIT_ASSERT_EQUAL(true, negOne < two);
00149                 CPPUNIT_ASSERT_EQUAL(false, two < negOne);
00150         }
00151         void testAntecedentAssumptions() {
00152                 Antecedent::checkPlatformAssumptions();
00153         }
00154         
00155         void testAntecedentNullPointer() {
00156                 Antecedent a;
00157                 Antecedent b( (Constraint*) 0 );
00158                 CPPUNIT_ASSERT_EQUAL(true, a.isNull());
00159                 CPPUNIT_ASSERT_EQUAL(true, b.isNull());
00160         }
00161 
00162         void testAntecedentPointer() {
00163                 struct Con : Constraint {
00164                         PropResult propagate(Solver&, Literal, uint32&) { return PropResult(true, true); }
00165                         void reason(Solver&, Literal, LitVec&) {}
00166                         Constraint* cloneAttach(Solver&) { return 0; }
00167                 };
00168                 
00169                 Constraint* c = new Con;
00170                 Antecedent a(c);
00171                 CPPUNIT_ASSERT_EQUAL(false, a.isNull());
00172                 CPPUNIT_ASSERT_EQUAL(Antecedent::generic_constraint, a.type());
00173                 CPPUNIT_ASSERT_EQUAL(c, a.constraint());
00174                 c->destroy();
00175         }
00176 
00177         void testAntecedentBin() {
00178                 CPPUNIT_ASSERT_EQUAL(true, testBin(max));
00179                 CPPUNIT_ASSERT_EQUAL(true, testBin(min));
00180                 CPPUNIT_ASSERT_EQUAL(true, testBin(mid));
00181         }
00182 
00183         void testAntecedentTern() {
00184                 CPPUNIT_ASSERT_EQUAL(true, testTern(max, max));
00185                 CPPUNIT_ASSERT_EQUAL(true, testTern(max, mid));
00186                 CPPUNIT_ASSERT_EQUAL(true, testTern(max, min));
00187                 CPPUNIT_ASSERT_EQUAL(true, testTern(mid, max));
00188                 CPPUNIT_ASSERT_EQUAL(true, testTern(mid, mid));
00189                 CPPUNIT_ASSERT_EQUAL(true, testTern(mid, min));
00190                 CPPUNIT_ASSERT_EQUAL(true, testTern(min, max));
00191                 CPPUNIT_ASSERT_EQUAL(true, testTern(min, mid));
00192                 CPPUNIT_ASSERT_EQUAL(true, testTern(min, min));
00193         }
00194 private:
00195         Literal min, mid, max;
00196         bool testBin(const Literal& p) {
00197                 Antecedent ap(p);
00198                 Antecedent aNotp(~p);
00199                 CPPUNIT_ASSERT_EQUAL(false, ap.isNull());
00200                 CPPUNIT_ASSERT_EQUAL(Antecedent::binary_constraint, ap.type());
00201                 CPPUNIT_ASSERT(p == ap.firstLiteral());
00202 
00203                 CPPUNIT_ASSERT_EQUAL(false, aNotp.isNull());
00204                 CPPUNIT_ASSERT_EQUAL(Antecedent::binary_constraint, aNotp.type());
00205                 CPPUNIT_ASSERT(~p == aNotp.firstLiteral());
00206                 return true;
00207         }
00208         bool testTern(const Literal& p, const Literal& q) {
00209                 Antecedent app(p, q);
00210                 Antecedent apn(p, ~q);
00211                 Antecedent anp(~p, q);
00212                 Antecedent ann(~p, ~q);
00213 
00214                 CPPUNIT_ASSERT_EQUAL(false, app.isNull());
00215                 CPPUNIT_ASSERT_EQUAL(Antecedent::ternary_constraint, app.type());
00216                 CPPUNIT_ASSERT_EQUAL(false, apn.isNull());
00217                 CPPUNIT_ASSERT_EQUAL(Antecedent::ternary_constraint, apn.type());
00218                 CPPUNIT_ASSERT_EQUAL(false, anp.isNull());
00219                 CPPUNIT_ASSERT_EQUAL(Antecedent::ternary_constraint, anp.type());
00220                 CPPUNIT_ASSERT_EQUAL(false, ann.isNull());
00221                 CPPUNIT_ASSERT_EQUAL(Antecedent::ternary_constraint, ann.type());
00222                 
00223                 CPPUNIT_ASSERT(p == app.firstLiteral() && q == app.secondLiteral());
00224                 CPPUNIT_ASSERT(p == apn.firstLiteral() && ~q == apn.secondLiteral());
00225                 CPPUNIT_ASSERT(~p == anp.firstLiteral() && q == anp.secondLiteral());
00226                 CPPUNIT_ASSERT(~p == ann.firstLiteral() && ~q == ann.secondLiteral());
00227                 return true;
00228         }
00229 };
00230 
00231 CPPUNIT_TEST_SUITE_REGISTRATION(LiteralTest);
00232 } } 


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:39