OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / atoms / base / AtomUTest.cxxtest

125 lines · cxxtest ref:
/*
 * tests/atoms/base/AtomUTest.cxxtest
 *
 * Copyright (C) 2002-2007 Novamente LLC
 * All Rights Reserved
 *
 * Written by Welter Silva <welter@vettalabs.com>
 *
 * 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 <algorithm>

#include <opencog/atoms/base/Atom.h>
#include <opencog/atoms/base/Link.h>
#include <opencog/atoms/core/UnorderedLink.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/util/platform.h>
#include <opencog/util/exceptions.h>

using namespace opencog;

#define FLOAT_ACCEPTABLE_ERROR 0.000001

class AtomUTest :  public CxxTest::TestSuite
{
private:

    AtomSpace as;
    HandleSeq sortedHandles;
    Handle inh01, inh12, l012;

public:
    AtomUTest() {
        sortedHandles = { as.add_node(CONCEPT_NODE, "0"),
                          as.add_node(CONCEPT_NODE, "1"),
                          as.add_node(CONCEPT_NODE, "2") };
        std::sort(sortedHandles.begin(), sortedHandles.end(), std::less<Handle>());
        inh01 = as.add_link(INHERITANCE_LINK, sortedHandles[0], sortedHandles[1]);
        inh12 = as.add_link(INHERITANCE_LINK, sortedHandles[1], sortedHandles[2]);
        l012 = as.add_link(LIST_LINK,
                           sortedHandles[0], sortedHandles[1], sortedHandles[2]);
    }

    void setUp() {
    }

    void tearDown() {
    }

    void testConstructor() {
        HandleSeq outgoing = { sortedHandles[2],
                               sortedHandles[0],
                               sortedHandles[1] };

        Atom* atom = new Link(std::move(outgoing), ORDERED_LINK);
        Link *link = dynamic_cast<Link *>(atom);
        TS_ASSERT(link != NULL);

        // Specific tests for ordered and unnordered links:
        TS_ASSERT(link->getOutgoingSet()[0] == sortedHandles[2]);
        TS_ASSERT(link->getOutgoingSet()[1] == sortedHandles[0]);
        TS_ASSERT(link->getOutgoingSet()[2] == sortedHandles[1]);
        delete atom;
        // Unordered lists sort their outgoing sets
        outgoing = { sortedHandles[2],
                     sortedHandles[0],
                     sortedHandles[1] };
        atom = new UnorderedLink(std::move(outgoing));
        link = dynamic_cast<Link *>(atom);
        TS_ASSERT(link != NULL);
        TS_ASSERT(link->getOutgoingSet()[0] == sortedHandles[0]);
        TS_ASSERT(link->getOutgoingSet()[1] == sortedHandles[1]);
        TS_ASSERT(link->getOutgoingSet()[2] == sortedHandles[2]);
        delete atom;
    }

    void test_getIncomingSet() {
        // We use an actual set to compare sorted objects

        // {inh01, l012} is the incoming set of ConceptNode "0"
        IncomingSet i0 = sortedHandles[0]->getIncomingSet();
        std::set<Handle> expected_i0 = {inh01, l012};
        TS_ASSERT_EQUALS(std::set<Handle>(i0.begin(), i0.end()), expected_i0);

        // {inh01, inh12, 012} is the incoming set of ConceptNode "1"
        IncomingSet i1 = sortedHandles[1]->getIncomingSet();
        std::set<Handle> expected_i1 = { inh01, inh12, l012 };
        TS_ASSERT_EQUALS(std::set<Handle>(i1.begin(), i1.end()), expected_i1);
    }

    void test_getIncomingSetByType()
    {
        // We use a set to compare sorted objects

        // {inh01} is the incoming set of ConceptNode "0" by type
        // InheritanceLink
        IncomingSet i0 = sortedHandles[0]->getIncomingSetByType(INHERITANCE_LINK);
        std::set<Handle> expected_i0 = {inh01};
        printf("Expected %s\n", inh01->to_string().c_str());
        printf("got a set of size %zu\n", i0.size());

        TS_ASSERT_EQUALS(std::set<Handle>(i0.begin(), i0.end()), expected_i0);

        // {inh01, inh12} is the incoming set of ConceptNode "1" by
        // type InheritanceLink
        IncomingSet i1 = sortedHandles[1]->getIncomingSetByType(INHERITANCE_LINK);
        std::set<Handle> expected_i1 = {inh01, inh12};
        TS_ASSERT_EQUALS(std::set<Handle>(i1.begin(), i1.end()), expected_i1);
    }
};