/*
* tests/atoms/EqualUTest.cxxtest
*
* Copyright (C) 2016 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/atoms/base/Atom.h>
#include <opencog/atomspace/AtomSpace.h>
using namespace opencog;
// Test the EqualLink.
//
class EqualLinkUTest : public CxxTest::TestSuite
{
private:
AtomSpace _as;
public:
EqualLinkUTest()
{
logger().set_print_to_stdout_flag(true);
}
void setUp() {}
void tearDown() {}
void test_equality();
void test_alpha();
void test_nway();
};
#define N _as.add_node
#define L _as.add_link
// Test equality vs. identity
void EqualLinkUTest::test_equality()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
Handle thing_a = N(CONCEPT_NODE, "thing A");
Handle thing_b = N(CONCEPT_NODE, "thing B");
Handle semantic_a =
L(PUT_LINK, N(VARIABLE_NODE, "$x"), thing_a);
Handle semantic_b =
L(PUT_LINK, N(VARIABLE_NODE, "$x"), thing_b);
// These can be added to AtomSpace (identical or has executable)
Handle ident_aa = L(IDENTICAL_LINK, thing_a, thing_a);
// Cannot be added
TS_ASSERT_THROWS_ANYTHING(L(IDENTICAL_LINK, thing_a, thing_b));
TS_ASSERT_THROWS_ANYTHING(L(IDENTICAL_LINK, thing_a, semantic_a));
TS_ASSERT_THROWS_ANYTHING(L(IDENTICAL_LINK, thing_a, semantic_b));
// Create some naked IdenticalLinks
Handle ident_ab = createLink(IDENTICAL_LINK, thing_a, thing_b);
Handle ident_asa = createLink(IDENTICAL_LINK, thing_a, semantic_a);
Handle ident_asb = createLink(IDENTICAL_LINK, thing_a, semantic_b);
// -------
// These can be added to AtomSpace (equal or has executable)
Handle equal_aa = L(EQUAL_LINK, thing_a, thing_a);
Handle equal_asa = L(EQUAL_LINK, thing_a, semantic_a);
Handle equal_asb = L(EQUAL_LINK, thing_a, semantic_b);
// These must use createLink (non-equal, non-executable)
Handle equal_ab = createLink(EQUAL_LINK, thing_a, thing_b);
// -------
bool result = ident_aa->bevaluate(&_as);
TS_ASSERT(result); // true
result = ident_asa->bevaluate(&_as);
TS_ASSERT(!result); // false
result = ident_ab->bevaluate(&_as);
TS_ASSERT(!result); // false
result = ident_asb->bevaluate(&_as);
TS_ASSERT(!result); // false
// -------
result = equal_aa->bevaluate(&_as);
TS_ASSERT(result); // true
result = equal_asa->bevaluate(&_as);
TS_ASSERT(result); // true
result = equal_ab->bevaluate(&_as);
TS_ASSERT(!result); // false
result = equal_asb->bevaluate(&_as);
TS_ASSERT(!result); // false
// Adding non-identical/non-equal atoms should throw
TS_ASSERT_THROWS_ANYTHING(L(IDENTICAL_LINK, thing_a, thing_b));
TS_ASSERT_THROWS_ANYTHING(L(EQUAL_LINK, thing_a, thing_b));
logger().info("END TEST: %s", __FUNCTION__);
}
// Test alpha-conversion equality
void EqualLinkUTest::test_alpha()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
Handle x_plus_2 =
L(PLUS_LINK, N(VARIABLE_NODE, "$x"), N(NUMBER_NODE, "2"));
Handle x_plus_3 =
L(PLUS_LINK, N(VARIABLE_NODE, "$x"), N(NUMBER_NODE, "3"));
Handle y_plus_3 =
L(PLUS_LINK, N(VARIABLE_NODE, "$y"), N(NUMBER_NODE, "3"));
Handle equal_23 = L(EQUAL_LINK, x_plus_2, x_plus_3);
Handle alpha_23 = L(ALPHA_EQUAL_LINK, x_plus_2, x_plus_3);
Handle equal_xx = L(EQUAL_LINK, x_plus_3, x_plus_3);
Handle alpha_xx = L(ALPHA_EQUAL_LINK, x_plus_3, x_plus_3);
Handle equal_xy = L(EQUAL_LINK, x_plus_3, y_plus_3);
Handle alpha_xy = L(ALPHA_EQUAL_LINK, x_plus_3, y_plus_3);
// -------
bool result = equal_23->bevaluate(&_as);
TS_ASSERT(!result); // false
result = alpha_23->bevaluate(&_as);
TS_ASSERT(!result); // false
result = equal_xx->bevaluate(&_as);
TS_ASSERT(result); // true
result = alpha_xx->bevaluate(&_as);
TS_ASSERT(result); // true
result = equal_xy->bevaluate(&_as);
TS_ASSERT(!result); // false
result = alpha_xy->bevaluate(&_as);
TS_ASSERT(result); // true
logger().info("END TEST: %s", __FUNCTION__);
}
// Test 3-way, 4-way tests
void EqualLinkUTest::test_nway()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
Handle thing_a = N(CONCEPT_NODE, "thing A");
Handle thing_b = N(CONCEPT_NODE, "thing B");
// These can be added to AtomSpace (all equal)
Handle equal_aaa = L(EQUAL_LINK, thing_a, thing_a, thing_a);
Handle equal_aaaa = L(EQUAL_LINK, thing_a, thing_a, thing_a, thing_a);
// These must use createLink (non-equal atoms)
Handle equal_aba = createLink(EQUAL_LINK, thing_a, thing_b, thing_a);
Handle equal_abaa = createLink(EQUAL_LINK, thing_a, thing_b, thing_a, thing_a);
// -------
bool result = equal_aaa->bevaluate(&_as);
TS_ASSERT(result); // true
result = equal_aaaa->bevaluate(&_as);
TS_ASSERT(result); // true
result = equal_aba->bevaluate(&_as);
TS_ASSERT(!result); // false
result = equal_abaa->bevaluate(&_as);
TS_ASSERT(!result); // false
// Adding non-equal atoms should throw
TS_ASSERT_THROWS_ANYTHING(L(EQUAL_LINK, thing_a, thing_b, thing_a););
logger().info("END TEST: %s", __FUNCTION__);
}