OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / VirtualUTest.cxxtest

110 lines · cxxtest ref:
/*
 * tests/query/VirtualUTest.cxxtest
 *
 * Copyright (C) 2019 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/util/Logger.h>
using namespace opencog;

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

public:
	VirtualUTest(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 "\")");

	}

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

void VirtualUTest::tearDown(void)
{
}

void VirtualUTest::setUp(void)
{
}

#define getarity(hand) hand->get_arity()

/*
 * SatisfactionLink should work just like any other VirtualLink
 * (i.e. it should work just like GreaterThanLink)
 */
void VirtualUTest::test_satisfaction(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	Handle mouse_set = eval->eval_h("(cog-execute! (CollectionOf get-mouse))");
	printf("Expecting mouse, got %s", mouse_set->to_string().c_str());

	TS_ASSERT_EQUALS(1, getarity(mouse_set));

	Handle mouse = as->add_node(CONCEPT_NODE, "mouse");
	TS_ASSERT_EQUALS(mouse, mouse_set->getOutgoingAtom(0));

	// -----------------------
	Handle snail_set = eval->eval_h("(cog-execute! (CollectionOf get-snail))");
	printf("Expecting snail, got %s", snail_set->to_string().c_str());

	TS_ASSERT_EQUALS(1, getarity(snail_set));

	Handle snail = as->add_node(CONCEPT_NODE, "snail");
	TS_ASSERT_EQUALS(snail, snail_set->getOutgoingAtom(0));

	// -----------------------
	Handle two_set = eval->eval_h("(cog-execute! (CollectionOf get-mouse-pair))");
	printf("Expecting two mouse pairs, got %s", two_set->to_string().c_str());

	TS_ASSERT_EQUALS(2, getarity(two_set));

	Handle list_one = two_set->getOutgoingAtom(0);
	TS_ASSERT_EQUALS(mouse, list_one->getOutgoingAtom(0));

	Handle list_two = two_set->getOutgoingAtom(1);
	TS_ASSERT_EQUALS(mouse, list_two->getOutgoingAtom(0));

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