OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / atoms / reduct / HeavisideUTest.cxxtest

162 lines · cxxtest ref:
/*
 * tests/atoms/HeavisideUTest.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/atoms/core/NumberNode.h>
#include <opencog/util/Logger.h>

using namespace opencog;

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

public:
	HeavisideUTest(void)
	{
		logger().set_level(Logger::DEBUG);
		logger().set_print_to_stdout_flag(true);

		as = createAtomSpace();
		eval = SchemeEval::get_scheme_evaluator(as);
	}

	~HeavisideUTest()
	{
		// 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_heaviside(void);
	void test_infnan(void);
	void test_reduct(void);
};

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

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

/*
 * Basic HeavisideLink unit test.
 */
void HeavisideUTest::test_heaviside(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// ---------
	Handle step = eval->eval_h(
		"(cog-execute! (Heaviside (Number -3 -2 -1 0 1 2 3)))"
	);
	printf("got step: %s\n", step->to_short_string().c_str());

	Handle estep = eval->eval_h("(NumberNode 0 0 0 1 1 1 1)");
	TS_ASSERT_EQUALS(step, estep);

	// ---------
	Handle squa = eval->eval_h(
		"(cog-execute! (Heaviside (Number -1 1 -1 1 -1 1 -3)))"
	);
	printf("got squa: %s\n", squa->to_short_string().c_str());

	Handle esqua = eval->eval_h("(NumberNode 0 1 0 1 0 1 0)");
	TS_ASSERT_EQUALS(squa, esqua);

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

/*
 * Behavior of inf and nan.
 */
void HeavisideUTest::test_infnan(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// ---------
	Handle inf = eval->eval_h(
			"(cog-execute! (Heaviside (NumberNode \"inf -inf inf -inf\")))"
	);
	printf("got inf: %s\n", inf->to_short_string().c_str());

	Handle einf = eval->eval_h("(NumberNode 1 0 1 0)");
	TS_ASSERT_EQUALS(inf, einf)

	// ---------
	Handle nan = eval->eval_h(
			"(cog-execute! (Heaviside (NumberNode \"nan 0 inf 0 -nan\")))"
	);
	printf("got nan: %s\n", nan->to_short_string().c_str());

	Handle enan = eval->eval_h("(NumberNode 1 1 1 1 0)");
	TS_ASSERT_EQUALS(nan, enan)

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

/*
 * Expressions with variables.
 */
void HeavisideUTest::test_reduct(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// ---------
	Handle var = eval->eval_h(
			"(cog-execute! (Heaviside (Plus "
			"     (NumberNode 3 4  -5 -6) (Variable \"$x\"))))"
	);
	printf("got var: %s\n", var->to_short_string().c_str());

	Handle evar = eval->eval_h(
			"(cog-execute! (Heaviside (Plus "
			"     (Variable \"$x\") (NumberNode 3 4  -5 -6))))"
	);
	TS_ASSERT_EQUALS(var, evar)

	// ---------
	Handle des = eval->eval_h(
			"(cog-execute! (Plus (Heaviside "
			"     (NumberNode 3 4  -5 -6)) (Number 6 6 11 11)))"
	);
	printf("got 7/11: %s\n", des->to_short_string().c_str());

	Handle edes = eval->eval_h("(NumberNode 7 7 11 11)");
	TS_ASSERT_EQUALS(des, edes)

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