OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / StackMoreUTest.cxxtest

216 lines · cxxtest ref:
/*
 * tests/query/StackMoreUTest.cxxtest
 *
 * Copyright (C) 2009, 2011, 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/guile/SchemeEval.h>
#include <opencog/util/Logger.h>
#include "test-types.h"

using namespace opencog;

class StackMoreUTest :  public CxxTest::TestSuite
{
	private:
		AtomSpacePtr as;
		Handle bind_oo, bind_ou, many_ou, bind_ouu;
		Handle bind_uo, bind_uu, bind_uuu;

	public:

		StackMoreUTest(void)
		{
			logger().set_level(Logger::DEBUG);
			// logger().set_level(Logger::FINE);
			logger().set_timestamp_flag(false);
			logger().set_print_to_stdout_flag(true);
#include "test-types.cc"
		}

		~StackMoreUTest()
		{
			// 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_oo(void);
		void test_ou(void);
		void test_many_ou(void);
		void test_ouu(void);
		void test_uo(void);
		void test_uu(void);
		void test_uuu(void);
};

/*
 * This function sets up an implication link, and some data.
 */
#define an as->add_node
#define al as->add_link
void StackMoreUTest::setUp(void)
{
	as = createAtomSpace();
	SchemeEval* eval = SchemeEval::get_scheme_evaluator(as);

	eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");

	eval->eval("(load-from-path \"tests/query/test_types.scm\")");
	eval->eval("(load-from-path \"tests/query/stackmore-o-o.scm\")");
	eval->eval("(load-from-path \"tests/query/stackmore-o-u.scm\")");
	eval->eval("(load-from-path \"tests/query/stackmany-o-u.scm\")");
	eval->eval("(load-from-path \"tests/query/stackmore-o-uu.scm\")");
	eval->eval("(load-from-path \"tests/query/stackmore-u-o.scm\")");
	eval->eval("(load-from-path \"tests/query/stackmore-u-u.scm\")");
	eval->eval("(load-from-path \"tests/query/stackmore-u-uu.scm\")");

	// Create an implication link that will be tested.
	bind_oo = eval->apply("bind_oo", Handle::UNDEFINED);
	bind_ou = eval->apply("bind_ou", Handle::UNDEFINED);
	many_ou = eval->apply("many_ou", Handle::UNDEFINED);
	bind_ouu = eval->apply("bind_ouu", Handle::UNDEFINED);
	bind_uo = eval->apply("bind_uo", Handle::UNDEFINED);
	bind_uu = eval->apply("bind_uu", Handle::UNDEFINED);
	bind_uuu = eval->apply("bind_uuu", Handle::UNDEFINED);
}

/*
 * Test of pattern matching on multi-variable matches with
 * confounding intermediate data.
 */
#define getlink(hand,pos) hand->getOutgoingAtom(pos)
#define getarity(hand) hand->get_arity()

void StackMoreUTest::test_oo(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != bind_oo);

	// Result should be a ListLink w/ one solution
	Handle result = HandleCast(bind_oo->execute(as.get()));

	logger().debug("result is %s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

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

void StackMoreUTest::test_ou(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != bind_ou);

	// Result should be a ListLink w/ one solution
	Handle result = HandleCast(bind_ou->execute(as.get()));

	logger().debug("result is %s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

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

void StackMoreUTest::test_many_ou(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != many_ou);

	// Result should be a ListLink w/ one solution
	Handle result = HandleCast(many_ou->execute(as.get()));

	logger().debug("result is %s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

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

void StackMoreUTest::test_ouu(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != bind_ouu);

	// Result should be a ListLink w/ one solution
	Handle result = HandleCast(bind_ouu->execute(as.get()));

	logger().debug("result is %s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

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

void StackMoreUTest::test_uo(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != bind_uo);

	// Result should be a ListLink w/ one solution
	Handle result = HandleCast(bind_uo->execute(as.get()));

	logger().debug("result is %s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

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

void StackMoreUTest::test_uu(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != bind_uu);

	// Result should be a ListLink w/ one solution
	Handle result = HandleCast(bind_uu->execute(as.get()));

	logger().debug("result is %s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

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

void StackMoreUTest::test_uuu(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != bind_uuu);

	// Result should be a ListLink w/ one solution
	Handle result = HandleCast(bind_uuu->execute(as.get()));

	logger().debug("result is %s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

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