OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / scm / SCMUtf8StringUTest.cxxtest

180 lines · cxxtest ref:
/*
 * tests/scm/SCMUtf8StringUTest.cxxtest
 *
 * Copyright (C) 2009, 2011, 2014, 2025 Linas Vepstas <linasvepstas@gmail.com>
 * 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/atomspace/AtomSpace.h>
#include <opencog/guile/SchemeEval.h>
#include <opencog/util/Logger.h>

#include "../query/test-types.h"

using namespace opencog;

class SCMUtf8StringUTest :  public CxxTest::TestSuite
{
	private:
		AtomSpacePtr as;
		SchemeEval* evaluator;

	public:

		SCMUtf8StringUTest(void)
		{
			logger().set_level(Logger::DEBUG);
			logger().set_print_to_stdout_flag(true);
#include "../query/test-types.cc"
		}

		~SCMUtf8StringUTest()
		{
			// 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 check_conv(std::string);
		void test_blumchen(void);
		void test_elakelaiset(void);
		void test_wutang(void);
};

/*
 * This function sets up an implication link, and some data.
 */
#define an as->add_node
#define al as->add_link
void SCMUtf8StringUTest::setUp(void)
{
	as = createAtomSpace();
	evaluator = SchemeEval::get_scheme_evaluator(as);
	evaluator->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");
	evaluator->eval("(add-to-load-path \"../../..\")");
}

void SCMUtf8StringUTest::tearDown(void)
{
}

#define CHKERR \
	TSM_ASSERT("Caught scm error during eval", \
		(false == evaluator->eval_error()));
/*
 * Test UTF8 String handling.
 */

static void prtstr(std::string s)
{
	for (size_t i=0; i<s.length(); i++)
		printf("It is %lu %x %c\n", i, s[i], s[i]);
}

void SCMUtf8StringUTest::check_conv(std::string name1)
{
	Handle n1 = an(CONCEPT_NODE, std::string(name1));

	// Should not crash
	evaluator->eval("(cog-prt-atomspace)");
	CHKERR;
	Handle cnh = evaluator->eval_h("(car (cog-get-atoms 'Concept))");
	CHKERR;
	std::string n1str = cnh->get_name();
	printf("got %s vs %s\n", n1str.c_str(), name1.c_str());
	TSM_ASSERT("String miscompare", name1 == n1str);
	TSM_ASSERT("String miscompare", 0 == name1.compare(n1str));

	// Also should not crash
	std::string utf = evaluator->eval("(cog-name (car (cog-get-atoms 'Concept)))");
	CHKERR;
	printf("Got the name %s\n", utf.c_str());
	prtstr(utf);

	evaluator->eval("(define foon (cog-name (car (cog-get-atoms 'Concept))))");
	CHKERR;

	std::string utf2 = evaluator->eval("foon");
	CHKERR;
	printf("Still Got the name %s\n", utf2.c_str());
	prtstr(utf2);
	TSM_ASSERT("String miscompare", utf == utf2);

	// Test string copy
	Handle pnh = evaluator->eval_h("(Predicate foon)");
	CHKERR;
	std::string p1str = pnh->get_name();
	printf("Pred name %s\n", p1str.c_str());
	prtstr(p1str);
	TSM_ASSERT("String miscompare", p1str == name1);
	TSM_ASSERT("String miscompare", 0 == name1.compare(p1str));

	Handle pnh2 = evaluator->eval_h("(car (cog-get-atoms 'Predicate))");
	CHKERR;
	std::string p2str = pnh2->get_name();
	TSM_ASSERT("String miscompare", p2str == name1);
	TSM_ASSERT("String miscompare", 0 == name1.compare(p2str));
}

void SCMUtf8StringUTest::test_blumchen(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Some microsoft-1252 codepage crap.
	std::string name1 = "Bl";
	name1.push_back(0xc4);
	name1 += "mchen";

	check_conv(name1);

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

void SCMUtf8StringUTest::test_elakelaiset(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Some microsoft-1252 codepage crap.
	std::string name1 = "el";
	name1.push_back(0xb4);
	name1 += "kel";
	name1.push_back(0xb4);
	name1 += "iset";

	check_conv(name1);

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

void SCMUtf8StringUTest::test_wutang(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Some microsoft-1252 codepage crap.
	std::string name1 = "Die Meistersinger von N";
	name1.push_back(0xfc);
	name1 += "rnberg";

	check_conv(name1);

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