OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / SubstitutionUTest.cxxtest

214 lines · cxxtest ref:
/*
 * tests/query/SubstitutionUTest.cxxtest
 *
 * Copyright (C) 2015 Linas Vepstas
 * 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/atomspace/AtomSpace.h>
#include <opencog/atoms/scope/VariableList.h>
#include <opencog/util/exceptions.h>
#include <opencog/util/Logger.h>

using namespace opencog;

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

public:
	SubstitutionUTest(void)
	{
		logger().set_level(Logger::DEBUG);
		logger().set_print_to_stdout_flag(true);

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

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

	~SubstitutionUTest()
	{
		// 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_simple_substitution(void);
	void test_free_substitution(void);
	void test_typed_substitution(void);
	void test_typecheck(void);
	void test_bad_stuff(void);
};

void SubstitutionUTest::tearDown(void)
{
}

void SubstitutionUTest::setUp(void)
{
}

/*
 * Basic VariableList unit test.
 * It just make sure that the substitute method works, at least
 * in the very simplest case.
 */
void SubstitutionUTest::test_simple_substitution(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	Handle hvarlist = eval->eval_h("varlist");
	Handle htemplate = eval->eval_h("template");
	Handle harglist = eval->eval_h("arglist");
	Handle hexpected = eval->eval_h("answer");

	printf("Variable list is %s\n", hvarlist->to_string().c_str());

	Type vt = hvarlist->get_type();
	TSM_ASSERT("Didn't get the variable listtype", vt == VARIABLE_LIST);

	VariableListPtr vlp(VariableListCast(hvarlist));
	TSM_ASSERT("Didn't get the variable list", vlp != NULL);

	LinkPtr argl(LinkCast(harglist));
	TSM_ASSERT("Didn't get the arguent list", argl != NULL);

	Handle hanswer = vlp->substitute(htemplate, argl->getOutgoingSet());
	hanswer = as->get_atom(hanswer);

	printf("Expected this: %s\n", hexpected->to_string().c_str());
	printf("Found this answer: %s\n", hanswer->to_string().c_str());

	TSM_ASSERT("Didn't get the expected grounding", hanswer == hexpected);
}

/*
 * Make sure that only the bound variables are substituted.
 */
void SubstitutionUTest::test_free_substitution(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	Handle hvarlist = eval->eval_h("varlist");
	Handle htemplate = eval->eval_h("free-template");
	Handle harglist = eval->eval_h("arglist");
	Handle hexpected = eval->eval_h("free-answer");

	printf("Variable list is %s\n", hvarlist->to_string().c_str());

	Type vt = hvarlist->get_type();
	TSM_ASSERT("Didn't get the variable listtype", vt == VARIABLE_LIST);

	VariableListPtr vlp(VariableListCast(hvarlist));
	TSM_ASSERT("Didn't get the variable list", vlp != NULL);

	LinkPtr argl(LinkCast(harglist));
	TSM_ASSERT("Didn't get the arguent list", argl != NULL);

	Handle hanswer = vlp->substitute(htemplate, argl->getOutgoingSet());
	hanswer = as->get_atom(hanswer);

	printf("Expected this: %s\n", hexpected->to_string().c_str());
	printf("Found this answer: %s\n", hanswer->to_string().c_str());

	TSM_ASSERT("Didn't get the expected grounding", hanswer == hexpected);
}

/*
 * As above, but make sure that type restrictions are applied.
 */
void SubstitutionUTest::test_typed_substitution(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	Handle hvarlist = eval->eval_h("typed-varlist");
	Handle htemplate = eval->eval_h("template");
	Handle harglist = eval->eval_h("arglist");
	Handle hexpected = eval->eval_h("answer");

	printf("Variable list is %s\n", hvarlist->to_string().c_str());

	Type vt = hvarlist->get_type();
	TSM_ASSERT("Didn't get the variable listtype", vt == VARIABLE_LIST);

	VariableListPtr vlp(VariableListCast(hvarlist));
	TSM_ASSERT("Didn't get the variable list", vlp != NULL);

	LinkPtr argl(LinkCast(harglist));
	TSM_ASSERT("Didn't get the arguent list", argl != NULL);

	Handle hanswer = vlp->substitute(htemplate, argl->getOutgoingSet());
	hanswer = as->get_atom(hanswer);

	printf("Expected this: %s\n", hexpected->to_string().c_str());
	printf("Found this answer: %s\n", hanswer->to_string().c_str());

	TSM_ASSERT("Didn't get the expected grounding", hanswer == hexpected);
}
/*
 * As above, but make sure that type restrictions are applied.
 */
void SubstitutionUTest::test_typecheck(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	Handle hvarlist = eval->eval_h("typed-varlist");
	Handle hbadlist = eval->eval_h("bad-arglist");
	Handle harglist = eval->eval_h("arglist");

	VariableListPtr vlp(VariableListCast(hvarlist));
	TSM_ASSERT("Didn't get the variable list", vlp != NULL);

	LinkPtr badl(LinkCast(hbadlist));
	TSM_ASSERT("Didn't get the arguent list", badl != NULL);

	LinkPtr argl(LinkCast(harglist));
	TSM_ASSERT("Didn't get the arguent list", argl != NULL);

	bool mismatch = vlp->is_type(badl->getOutgoingSet());
	TSM_ASSERT("failed to typecheck bad", mismatch == false);

	bool match = vlp->is_type(argl->getOutgoingSet());
	TSM_ASSERT("failed to typecheck good", match == true);
}

void SubstitutionUTest::test_bad_stuff(void)
{
	// Avoid spew of warnings due to the exception
	logger().set_level(Logger::NONE);
	bool caught = false;
	try
	{
		eval->eval_h("bad-varlist");
	}
	catch (...)
	{
		caught = true;
	}
	TSM_ASSERT("failed to catch exception", caught == true);
}