OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / BuggyEqualUTest.cxxtest

157 lines · cxxtest ref:
/*
 * tests/query/BuggyEqualUTest.cxxtest
 *
 * Copyright (C) 2015 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/atoms/core/NumberNode.h>
#include <opencog/atoms/value/LinkValue.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/guile/SchemeEval.h>
#include <opencog/util/Logger.h>

using namespace opencog;

class BuggyEqual :  public CxxTest::TestSuite
{
	private:
		AtomSpacePtr as;
		SchemeEval* eval;

	public:

		BuggyEqual(void)
		{
			logger().set_level(Logger::DEBUG);
			logger().set_print_to_stdout_flag(true);
			logger().set_timestamp_flag(false);
			logger().set_sync_flag(true);

			as = createAtomSpace();
			eval = SchemeEval::get_scheme_evaluator(as);
			eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");
		}

		~BuggyEqual()
		{
			// 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) { as->clear(); }

		void test_bugeq(void);
		void test_bugalt(void);
		void test_arithmetic(void);
		void test_unify(void);
};

void BuggyEqual::setUp(void)
{
	as->clear();
}

#define getlink(hand,pos) hand->getOutgoingAtom(pos)
#define getarity(hand) hand->get_arity()

/*
 * NotLink causing weird trouble.
 */
void BuggyEqual::test_bugeq(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/buggy-equal.scm\")");

	Handle pln = eval->eval_h("(cog-execute! (CollectionOf pln-rule-deduction))");
	printf("Deduction results:\n%s\n", pln->to_short_string().c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 4, getarity(pln));

	logger().debug("END TEST: %s", __FUNCTION__);
}

/*
 * NotLink causing weird trouble.
 */
void BuggyEqual::test_bugalt(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/buggy-equal.scm\")");

	Handle alt = eval->eval_h("(cog-execute! (CollectionOf pln-alt))");
	printf("Alt results:\n%s\n", alt->to_short_string().c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 4, getarity(alt));

	logger().debug("END TEST: %s", __FUNCTION__);
}

/*
 * Failed arithmetic search
 */
void BuggyEqual::test_arithmetic(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/buggy-equal-arithmetic.scm\")");

	Handle sixset = eval->eval_h("(cog-execute! (CollectionOf arithmetic-search))");
	printf("Arithmetic results:\n%s\n", sixset->to_short_string().c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions", 1, getarity(sixset));

	Handle six = sixset->getOutgoingAtom(0);
	TSM_ASSERT_EQUALS("Expected number node!", NUMBER_NODE, six->get_type());

	NumberNodePtr nn(NumberNodeCast(six));
	double dsix = nn->get_value();
	TS_ASSERT_LESS_THAN_EQUALS(5.99999, dsix);
	TS_ASSERT_LESS_THAN_EQUALS(dsix, 6.00001);

	logger().debug("END TEST: %s", __FUNCTION__);
}

/*
 * Failed variable unification
 */
void BuggyEqual::test_unify(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/buggy-equal-unify.scm\")");

	ValuePtr listl = eval->eval_v("(cog-execute! qunify)");
	printf("Unify result:\n%s\n", listl->to_short_string().c_str());
	TSM_ASSERT("Wrong return type!",
		nameserver().isA(listl->get_type(), LINK_VALUE));
	TSM_ASSERT_EQUALS("Wrong number of solutions!",
		LinkValueCast(listl)->value().size(), 1);
	// Handle gotu = HandleCast(LinkValueCast(listl)->value()[0]);
	ValuePtr gotu = LinkValueCast(listl)->value()[0];

	Handle expect = eval->eval_h("expected");
	printf("Unify expect: %s\n", expect->to_short_string().c_str());

	TSM_ASSERT_EQUALS("Bad unification!", expect, gotu);

	logger().debug("END TEST: %s", __FUNCTION__);
}