OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / atoms / execution / DefinedSchemaUTest.cxxtest

207 lines · cxxtest ref:
/*
 * tests/atoms/execution/DefinedSchemaUTest.cxxtest
 *
 * Copyright (C) 2020 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 DefinedSchemaUTest: public CxxTest::TestSuite
{
private:
	AtomSpacePtr as;
	SchemeEval* eval;

public:
	DefinedSchemaUTest(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 "\")");
		eval->eval("(load-from-path \"tests/atoms/execution/defined-schema.scm\")");
	}

	~DefinedSchemaUTest()
	{
		delete eval;
		// 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_make_edge(void);
	void test_get_tail(void);
	void test_compose(void);
	void test_nest(void);
	void xtest_reversive(void);
	void xtest_recursive(void);
};

void DefinedSchemaUTest::tearDown(void)
{
}

void DefinedSchemaUTest::setUp(void)
{
}

/*
 * DefinedSchema unit test.
 */
void DefinedSchemaUTest::test_make_edge(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	Handle result = eval->eval_h("(cog-execute! mk-edge)");

	Handle edge = eval->eval_h(
	"	(Edge"
	"		(Predicate \"yikes\")"
	"		(List"
	"			(Concept \"X\")"
	"			(Concept \"Y\")))"
	);

	printf("got %s\n", result->to_string().c_str());
	printf("expected %s\n", edge->to_string().c_str());

	TS_ASSERT(result == edge);

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

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

	ValuePtr result = eval->eval_v("(cog-execute! get-tl)");

	ValuePtr tail = eval->eval_v("(LinkValue (Concept \"B\"))");

	printf("got %s\n", result->to_string().c_str());
	printf("expected %s\n", tail->to_string().c_str());

	TS_ASSERT(*result == *tail);

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

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

	Handle result = eval->eval_h("(cog-execute! rw-one)");

	Handle edge = eval->eval_h(
	"	(Edge"
	"		(Predicate \"yikes\")"
	"		(List"
	"			(Concept \"A\")"
	"			(Concept \"B\")))"
	);

	printf("got %s\n", result->to_string().c_str());
	printf("expected %s\n", edge->to_string().c_str());

	TS_ASSERT(result == edge);

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

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

	Handle result = eval->eval_h("(cog-execute! nest)");

	Handle edge = eval->eval_h(
	"(List"
	"	(Concept \"B\")"
	"	(Edge"
	"		(Predicate \"yikes\")"
	"		(List"
	"			(Concept \"A\")"
	"			(Concept \"root\"))))"
	);

	printf("got %s\n", result->to_string().c_str());
	printf("expected %s\n", edge->to_string().c_str());

	TS_ASSERT(result == edge);

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

// Test the recursive operation of ExecutionOutputLinks, where the
// DefinedSchema is used to define recursive execution.
// This is called "reversive" because it inverts the tree,
// upside-down, with a path from each leaf to the root.
//
// The test used to work but is now disabled. See the long explanation
// in defined-schema.scm for details.
void DefinedSchemaUTest::xtest_reversive(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	Handle result = eval->eval_h("(cog-execute! reversive)");

	printf("got %s\n", result->to_string().c_str());

	Handle leaves = eval->eval_h("reversive-result");

	printf("expected %s\n", leaves->to_string().c_str());

	TS_ASSERT(result == leaves);

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

// Test the recursive operation of ExecutionOutputLinks, where the
// DefinedSchema is used to define recursive execution.
void DefinedSchemaUTest::xtest_recursive(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	Handle result = eval->eval_h("(cog-execute! recursive)");

	printf("got %s\n", result->to_string().c_str());

#if 0
	Handle leaves = eval->eval_h("recursive-result");

	printf("expected %s\n", leaves->to_string().c_str());

	TS_ASSERT(result == leaves);
#endif

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