OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / atoms / core / FindUtilsUTest.cxxtest

156 lines · cxxtest ref:
/*
 * tests/atoms/core/FindUtilsUTest.cxxtest
 *
 * Copyright (C) 2015 OpenCog Foundation
 * 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/util/Logger.h>
#include <opencog/atoms/free/FindUtils.h>
#include <opencog/atomspace/AtomSpace.h>
#include <cxxtest/TestSuite.h>

using namespace opencog;

class FindUtilsUTest :  public CxxTest::TestSuite
{
private:
	AtomSpace as;
	Handle G1, G2, X, Y, P, free_evaluation, scoped_implication, quotation,
		quoted_lambda, quoted_put, quoted_glob_lambda;

public:
	FindUtilsUTest()
	{
		logger().set_level(Logger::FINE);
		logger().set_print_to_stdout_flag(true);
	}

	void setUp();

	void test_is_unscoped_in_tree();
	void test_min_quotation_level();
	void test_max_quotation_level();
	void test_is_closed();
	void test_is_free_in_tree();
	void test_get_free_variables();
	void test_get_free_variables_glob();
};

void FindUtilsUTest::setUp(void)
{
#define al as.add_link
#define an as.add_node

	G1 = an(GLOB_NODE, "$G1");
	G2 = an(GLOB_NODE, "$G2");
	X = an(VARIABLE_NODE, "$X");
	Y = an(VARIABLE_NODE, "$Y");
	P = an(PREDICATE_NODE, "P");

	free_evaluation =
		al(EVALUATION_LINK, P, X);

	scoped_implication =
		al(IMPLICATION_LINK,
			P,
			al(LAMBDA_LINK, X, free_evaluation));

	quotation =
		al(QUOTE_LINK,
			al(AND_LINK,
				al(UNQUOTE_LINK, X),
				al(QUOTE_LINK,
					al(OR_LINK,
						X,
						al(UNQUOTE_LINK, Y)))));

	quoted_lambda =
		al(QUOTE_LINK,
			al(LAMBDA_LINK,
				al(UNQUOTE_LINK, X),
					al(UNQUOTE_LINK,
					al(AND_LINK, X, Y))));

	quoted_put =
		al(QUOTE_LINK,
			al(PUT_LINK,
				al(LAMBDA_LINK, X, X),
				al(UNQUOTE_LINK, Y)));

	quoted_glob_lambda =
			al(QUOTE_LINK,
			   al(LAMBDA_LINK,
			      al(UNQUOTE_LINK, G1),
			      al(UNQUOTE_LINK,
			         al(AND_LINK, G1, G2))));

#undef al
#undef an
}

// Test is_unscoped_in_tree()
void FindUtilsUTest::test_is_unscoped_in_tree()
{
	bool free_result = is_unscoped_in_tree(free_evaluation, X);
	TS_ASSERT_EQUALS(free_result, true);

	bool scoped_result = is_unscoped_in_tree(scoped_implication, X);
	TS_ASSERT_EQUALS(scoped_result, false);
}

// Test min_quotation_level()
void FindUtilsUTest::test_min_quotation_level()
{
	TS_ASSERT_EQUALS(min_quotation_level(quotation, X), 0);
	TS_ASSERT_EQUALS(min_quotation_level(quotation, Y), 1);
}

// Test max_quotation_level()
void FindUtilsUTest::test_max_quotation_level()
{
	TS_ASSERT_EQUALS(max_quotation_level(quotation, X), 2);
	TS_ASSERT_EQUALS(max_quotation_level(quotation, Y), 1);
}

// Test is_closed()
void FindUtilsUTest::test_is_closed()
{
	TS_ASSERT(not is_closed(free_evaluation));
	TS_ASSERT(is_closed(scoped_implication));
	TS_ASSERT(not is_closed(quotation));
}

// Test is_free_in_any_tree(). More specifically the interplay between
// scope and quotation.
void FindUtilsUTest::test_is_free_in_tree()
{
	TS_ASSERT(is_free_in_tree(quoted_lambda, X));
	TS_ASSERT(is_free_in_tree(quoted_put, Y));
}

void FindUtilsUTest::test_get_free_variables()
{
	TS_ASSERT_EQUALS(get_free_variables(quoted_lambda), HandleSet({X, Y}));
}

void FindUtilsUTest::test_get_free_variables_glob()
{
	TS_ASSERT_EQUALS(get_free_variables(quoted_glob_lambda), HandleSet({G1, G2}));
}