/*
* tests/query/BuggyNotUTest.cxxtest
*
* Copyright (C) 2009, 2013 Linas Vepstas <linasvepstas@gmail.com>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/guile/SchemeEval.h>
#include <opencog/util/Logger.h>
#include "test-types.h"
using namespace opencog;
class BuggyNot : public CxxTest::TestSuite
{
private:
AtomSpacePtr as;
SchemeEval* eval;
public:
BuggyNot(void)
{
logger().set_level(Logger::DEBUG);
logger().set_print_to_stdout_flag(true);
#include "test-types.cc"
as = createAtomSpace();
eval = SchemeEval::get_scheme_evaluator(as);
eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");
}
~BuggyNot()
{
// Erase the log file if no assertions failed.
if (!CxxTest::TestTracker::tracker().suiteFailed())
std::remove(logger().get_filename().c_str());
}
void setUp(void);
void tearDown(void) { }
void test_bugnot(void);
};
/*
* This function sets up an implication link, and some data.
*/
#define an as->addNode
#define al as->addLink
#define getarity(hand) hand->get_arity()
#define getlink(hand,pos) hand->getOutgoingAtom(pos)
void BuggyNot::setUp(void)
{
as->clear();
eval->eval("(load-from-path \"tests/query/test_types.scm\")");
}
/*
* NotLink causing weird trouble.
*/
void BuggyNot::test_bugnot(void)
{
logger().debug("BEGIN TEST: %s", __FUNCTION__);
eval->eval("(load-from-path \"tests/query/buggy-not.scm\")");
// Create an implication link that will be tested.
Handle same_rule = eval->apply("is-same-rule", Handle::UNDEFINED);
Handle trans_rule = eval->apply("transitive-rule", Handle::UNDEFINED);
// Make sure the scheme file actually loaded!
TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != same_rule);
TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != trans_rule);
// First, deduce if two seemingly different people are the same ...
Handle same = HandleCast(same_rule->execute(as.get()));
logger().debug("Same-people list is %s\n", oc_to_string(same).c_str());
TSM_ASSERT_EQUALS("wrong number of solutions found", 4, getarity(same));
// Next, deduce that the same properties apply to each person.
// The bug that this test used to trip had to do with using the same
// "pm" twice, to perform a second round of matching. It failed to
// clear all internal state, and in particular, failed to clear the
// "NotLink" in the optional match. As a result, the second implication
// failed, because it was still using a left-over portion of the previous
// implication link. This was a rather distressing bug ... a very obvious
// but poor bug, and hadn't been noticed before. Alas.
Handle trans = HandleCast(trans_rule->execute(as.get()));
logger().debug("Transitive list is %s\n", oc_to_string(trans).c_str());
TSM_ASSERT_EQUALS("wrong number of solutions found", 4, getarity(trans));
logger().debug("END TEST: %s", __FUNCTION__);
}