OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / MatchLinkUTest.cxxtest

131 lines · cxxtest ref:
/*
 * tests/query/MatchLinkUTest.cxxtest
 *
 * Copyright (C) 2011 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 MatchLink :  public CxxTest::TestSuite
{
	private:
		AtomSpacePtr as;
		Handle untyped_link_match, typed_link_match, untyped_any_match;
		Handle typed_memb_link_match, typed_pet_node_match;

	public:

		MatchLink(void)
		{
			logger().set_level(Logger::DEBUG);
			logger().set_print_to_stdout_flag(true);
#include "test-types.cc"
		}

		~MatchLink()
		{
			// 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_exec(void);
};

/*
 * This function sets up an implication link, and some data.
 */
#define an as->add_node
#define al as->add_link
#define getarity(hand) hand->get_arity()
#define getlink(hand,pos) hand->getOutgoingAtom(pos)

void MatchLink::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/match-link.scm\")");

	// Create an implication link that will be tested.
	untyped_link_match = eval->apply("untyped-link-match", Handle::UNDEFINED);
	typed_link_match = eval->apply("typed-link-match", Handle::UNDEFINED);
	untyped_any_match = eval->apply("untyped-any-match", Handle::UNDEFINED);
	typed_memb_link_match = eval->apply("typed-memb-link-match", Handle::UNDEFINED);
	typed_pet_node_match = eval->apply("typed-pet-node-match", Handle::UNDEFINED);
}

/*
 * Basic implication link test.
 */
void MatchLink::test_exec(void)
{
	Handle result;
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	// Result should be a ListLink w/ three solutions
	result = HandleCast(untyped_link_match->execute(as.get()));
	logger().debug("untyped-link-match is: \n%s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 3, getarity(result));

	// Result should be a ListLink w/ one solution
	result = HandleCast(typed_link_match->execute(as.get()));
	logger().debug("typed-link-match is: \n%s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

	// There are currently four arity-2 associative-links in the atomspace...
	result = HandleCast(untyped_any_match->execute(as.get()));
	logger().debug("untyped-any-match is: \n%s\n", oc_to_string(result).c_str());
#define NO_SELF_GROUNDING 1
#ifdef NO_SELF_GROUNDING
	TSM_ASSERT_EQUALS("wrong number of solutions found", 9, getarity(result));
#else
	TSM_ASSERT_EQUALS("wrong number of solutions found", 10, getarity(result));
#endif

	// There's just one arity-2 associative-link with MemberLink in second place.
	result = HandleCast(typed_memb_link_match->execute(as.get()));
	logger().debug("typed-memb-link-match is: \n%s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 3, getarity(result));

	result = HandleCast(typed_pet_node_match->execute(as.get()));
	logger().debug("typed-pet-node-match is: \n%s\n", oc_to_string(result).c_str());
	TSM_ASSERT_EQUALS("wrong number of solutions found", 2, getarity(result));

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