OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / ChoiceLinkUTest.cxxtest

340 lines · cxxtest ref:
/*
 * tests/query/ChoiceLinkUTest.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/value/LinkValue.h>
#include <opencog/util/Logger.h>

using namespace opencog;

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

public:
	ChoiceLinkUTest(void)
	{
		logger().set_level(Logger::DEBUG);
		logger().set_print_to_stdout_flag(true);
		logger().set_timestamp_flag(false);

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

	}

	~ChoiceLinkUTest()
	{
		// 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_basic_or(void);
	void test_embed_or(void);
	void test_nest_or(void);
	void test_top_nest_or(void);
	void test_nest_bad_or(void);
	void test_top_nest_bad_or(void);
	void test_top_disco(void);
	void test_embed_disco(void);
	void xxxtest_double_or(void);
	void test_unary(void);
	void test_typed(void);
	void test_constant(void);
	void test_present(void);
	void test_compound(void);
};

void ChoiceLinkUTest::tearDown(void)
{
	as->clear();
}

void ChoiceLinkUTest::setUp(void)
{
	as->clear();
}

static size_t getarity(const ValuePtr& v)
{
	LinkValuePtr lv = LinkValueCast(v);
	return lv->size();
}

/*
 * ChoiceLink most basic unit test.
 */
void ChoiceLinkUTest::test_basic_or(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (basic))");

	TS_ASSERT_EQUALS(2, getarity(items));

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

/*
 * ChoiceLink embedded unit test.
 */
void ChoiceLinkUTest::test_embed_or(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (embed))");

	TS_ASSERT_EQUALS(2, getarity(items));

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

/*
 * ChoiceLink nested embedded unit test.
 */
void ChoiceLinkUTest::test_nest_or(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (nest))");

	printf ("Nest found:\n%s\n", items->to_short_string().c_str());
	TS_ASSERT_EQUALS(5, getarity(items));

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

/*
 * ChoiceLink top-level nested unit test.
 */
void ChoiceLinkUTest::test_top_nest_or(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (top-nest))");

	printf ("Top-nest found:\n%s\n", items->to_short_string().c_str());
	TS_ASSERT_EQUALS(5, getarity(items));

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

/*
 * ChoiceLink bad nested embedded unit test.
 */
void ChoiceLinkUTest::test_nest_bad_or(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (nest-bad))");

	printf ("Nest-bad found:\n%s\n", items->to_short_string().c_str());
	TS_ASSERT_EQUALS(4, getarity(items));

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

/*
 * ChoiceLink bad nested embedded unit test.
 */
void ChoiceLinkUTest::test_top_nest_bad_or(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (top-nest-bad))");

	printf ("Top-nest-bad found:\n%s\n", items->to_short_string().c_str());
	TS_ASSERT_EQUALS(4, getarity(items));

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

/*
 * ChoiceLink disconnected unit test.
 */
void ChoiceLinkUTest::test_top_disco(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (top-disco))");

	printf ("Top-disco found:\n%s\n", items->to_short_string().c_str());
	TS_ASSERT_EQUALS(2, getarity(items));

	ValuePtr wrap = eval->eval_v("(cog-execute! (wrapped-disco))");

	printf ("Top-wrapped-disco found:\n%s\n", wrap->to_short_string().c_str());
	TS_ASSERT_EQUALS(2, getarity(wrap));

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

/*
 * ChoiceLink disconnected-embeded unit test.
 * (Well, its not really disconnected, it just sort-of looks that way.)
 */
void ChoiceLinkUTest::test_embed_disco(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! (embed-disco))");

	printf ("Embed pseudo-disco found:\n%s\n", items->to_short_string().c_str());
	TS_ASSERT_EQUALS(2, getarity(items));

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

/*
 * double ChoiceLink unit test.
 * used to pass, in the past, by acccident.
 * Needs to be fixed. and improved to detect future accidents.
 * See https://github.com/opencog/atomspace/issues/2753
 */
void ChoiceLinkUTest::xxxtest_double_or(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr items = eval->eval_v("(cog-execute! double)");
	logger().debug("double-or found %s", items->to_short_string().c_str());

	TS_ASSERT_EQUALS(3, getarity(items));

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

/*
 * unary ChoiceLink unit test.
 */
void ChoiceLinkUTest::test_unary(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

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

	ValuePtr doors = eval->eval_v("(cog-execute! (get-a))");

	printf("unary door-a found:\n%s\n", doors->to_short_string().c_str());
	TS_ASSERT_EQUALS(1, getarity(doors));

	doors = eval->eval_v("(cog-execute! (get-bc))");

	printf("unary door-bc found:\n%s\n", doors->to_short_string().c_str());
	TS_ASSERT_EQUALS(4, getarity(doors));

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

/*
 * ChoiceLinks with typed ariables in them.
 */
void ChoiceLinkUTest::test_typed(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/choice-typed.scm\")");
	ValuePtr rules = eval->eval_v("(cog-execute! get-nodes1)");

	printf("typed get-nodes found:\n%s\n", rules->to_short_string().c_str());
	TS_ASSERT_EQUALS(2, getarity(rules));

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

/*
 * ChoiceLink with no variables in it!
 */
void ChoiceLinkUTest::test_constant(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/choice-constant.scm\")");
	ValuePtr result = eval->eval_v("(cog-execute! query)");

	printf("constant found:\n%s\n", result->to_short_string().c_str());
	TS_ASSERT_EQUALS(1, getarity(result));

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

/*
 * ChoiceLink with PresentLink in it
 */
void ChoiceLinkUTest::test_present(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/choice-present.scm\")");
	Handle result = eval->eval_h("(cog-execute! (CollectionOf who-is-hungry?))");
	Handle answer = eval->eval_h("expected");

	printf("present found:\n%s\n", result->to_short_string().c_str());
	TS_ASSERT_EQUALS(1, result->get_arity());
	TS_ASSERT_EQUALS(result, answer);

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

/*
 * ChoiceLink with multiple PresentLinks in it
 */
void ChoiceLinkUTest::test_compound(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/choice-compound.scm\")");
	Handle result = eval->eval_h("(cog-execute! (CollectionOf who-needs-help?))");
	Handle answer = eval->eval_h("answer");

	printf("compound found:\n%s\n", result->to_short_string().c_str());
	TS_ASSERT_EQUALS(3, result->get_arity());
	TS_ASSERT_EQUALS(result, answer);

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