diff options
Diffstat (limited to 'test/core')
| -rw-r--r-- | test/core/managed/ManagedContainerTest.cpp | 41 | ||||
| -rw-r--r-- | test/core/model/DocumentTest.cpp | 44 | ||||
| -rw-r--r-- | test/core/model/DomainTest.cpp | 100 | ||||
| -rw-r--r-- | test/core/model/TestDocument.hpp | 84 | ||||
| -rw-r--r-- | test/core/model/TestDomain.hpp | 94 | 
5 files changed, 353 insertions, 10 deletions
diff --git a/test/core/managed/ManagedContainerTest.cpp b/test/core/managed/ManagedContainerTest.cpp index c34541a..7ff819f 100644 --- a/test/core/managed/ManagedContainerTest.cpp +++ b/test/core/managed/ManagedContainerTest.cpp @@ -38,7 +38,7 @@ TEST(ManagedVector, managedVector)  	{  		Rooted<Managed> root{new Managed{mgr}}; -		std::vector<TestManaged*> elems; +		std::vector<TestManaged *> elems;  		for (int i = 0; i < nElem; i++) {  			elems.push_back(new TestManaged{mgr, a[i]});  		} @@ -49,7 +49,8 @@ TEST(ManagedVector, managedVector)  		ManagedVector<TestManaged> v(root, elems.begin(), elems.end()); -		// Remove the last element from the list. It should be garbage collected. +		// Remove the last element from the list. It should be garbage +		// collected.  		v.pop_back();  		ASSERT_FALSE(a[nElem - 1]); @@ -85,7 +86,6 @@ TEST(ManagedVector, managedVector)  	}  } -  TEST(ManagedVector, moveAssignment)  {  	constexpr int nElem = 16; @@ -220,16 +220,39 @@ TEST(ManagedVector, moveWithNewOwner)  	}  } -class TestManagedWithContainer : public Managed { +TEST(ManagedVector, accessOperator) +{ +	Manager mgr{1}; +	Rooted<Managed> root{new Managed{mgr}}; +	ManagedVector<Managed> instance{root}; +	Rooted<Managed> elem{new Managed{mgr}}; +	instance.push_back(elem); + +	ASSERT_EQ(elem, instance[0]); + +	// Test out of bounds. +	bool caught = false; +	try { +		instance[1]; +	} +	catch (std::out_of_range ex) { +		caught = true; +	} +	ASSERT_TRUE(caught); + +	instance.push_back(elem); +	ASSERT_EQ(elem, instance[1]); +} +class TestManagedWithContainer : public Managed {  public:  	ManagedVector<TestManaged> elems; -	TestManagedWithContainer(Manager &mgr) : Managed(mgr), elems(this) {}; - +	TestManagedWithContainer(Manager &mgr) : Managed(mgr), elems(this){};  }; -TEST(ManagedVector, embedded) { +TEST(ManagedVector, embedded) +{  	// Note: This test depends on the correct deletion order -- otherwise  	// valgrind shows an error  	bool a; @@ -248,7 +271,6 @@ TEST(ManagedVector, embedded) {  	ASSERT_FALSE(a);  } -  TEST(ManagedMap, managedMap)  {  	// TODO: This test is highly incomplete @@ -260,7 +282,7 @@ TEST(ManagedMap, managedMap)  	{  		Rooted<Managed> root{new Managed{mgr}}; -		std::map<int, TestManaged*> elems; +		std::map<int, TestManaged *> elems;  		for (int i = 0; i < nElem; i++) {  			elems.insert(std::make_pair(i, new TestManaged{mgr, a[i]}));  		} @@ -298,6 +320,5 @@ TEST(ManagedMap, managedMap)  		ASSERT_FALSE(v);  	}  } -  } diff --git a/test/core/model/DocumentTest.cpp b/test/core/model/DocumentTest.cpp new file mode 100644 index 0000000..26553dd --- /dev/null +++ b/test/core/model/DocumentTest.cpp @@ -0,0 +1,44 @@ +/* +    Ousía +    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel + +    This program is free software: you can redistribute it and/or modify +    it under the terms of the GNU General Public License as published by +    the Free Software Foundation, either version 3 of the License, or +    (at your option) any later version. + +    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 General Public License +    along with this program.  If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <gtest/gtest.h> + +#include <core/model/Document.hpp> +#include <core/model/Domain.hpp> + +#include "TestDocument.hpp" +#include "TestDomain.hpp" + +namespace ousia { +namespace model { + + +TEST(Document, testDocumentConstruction) +{ +	// Construct Manager +	Manager mgr{1}; +	// Get the domain. +	Rooted<Domain> domain = constructBookDomain(mgr); +	// Construct the document. +	Rooted<Document> doc = constructBookDocument(mgr, domain); +	 +	// If that works we are happy already. +	ASSERT_FALSE(doc.isNull()); +} +} +} diff --git a/test/core/model/DomainTest.cpp b/test/core/model/DomainTest.cpp new file mode 100644 index 0000000..f6dff3c --- /dev/null +++ b/test/core/model/DomainTest.cpp @@ -0,0 +1,100 @@ +/* +    Ousía +    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel + +    This program is free software: you can redistribute it and/or modify +    it under the terms of the GNU General Public License as published by +    the Free Software Foundation, either version 3 of the License, or +    (at your option) any later version. + +    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 General Public License +    along with this program.  If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <gtest/gtest.h> + +#include <iostream> + +#include <core/model/Domain.hpp> + +#include "TestDomain.hpp" + +namespace ousia { +namespace model { + +void assert_path(std::vector<Rooted<Managed>> &result, size_t idx, +                 const std::type_info &expected_type, +                 std::vector<std::string> expected_path) +{ +	ASSERT_TRUE(result.size() > idx); +	// check class/type +	ASSERT_EQ(expected_type, typeid(*result[idx])); +	// transform to node +	Managed *m = &(*result[idx]); +	Node *n = dynamic_cast<Node *>(m); +	ASSERT_TRUE(n); +	// extract actual path +	std::vector<std::string> actual_path = n->path(); +	// check path +	ASSERT_EQ(expected_path, actual_path); +} + +TEST(Document, testDomainResolving) +{ +	// Construct Manager +	Manager mgr{1}; +	// Get the domain. +	Rooted<Domain> domain = constructBookDomain(mgr); + +	/* +	 * Start with the "book" search keyword. This should resolve to the domain +	 * itself (because it is called "book"), as well as the structure "book" +	 * node. +	 */ +	std::vector<Rooted<Managed>> res = +	    domain->resolve(std::vector<std::string>{"book"}); +	// First we expect the book domain. +	assert_path(res, 0, typeid(Domain), {"book"}); +	// Then the book structure. +	assert_path(res, 1, typeid(StructuredClass), {"book", "book"}); +	ASSERT_EQ(2, res.size()); + +	/* +	 * If we explicitly ask for the "book, book" path, then only the +	 * StructuredClass should be returned. +	 */ +	res = domain->resolve(std::vector<std::string>{"book", "book"}); +	assert_path(res, 0, typeid(StructuredClass), {"book", "book"}); +	ASSERT_EQ(1, res.size()); + +	/* +	 * If we ask for "section" the result should be unique as well. +	 */ +	res = domain->resolve(std::vector<std::string>{"section"}); +	// TODO: Is that the path result we want? +	assert_path(res, 0, typeid(StructuredClass), {"book", "section"}); +	ASSERT_EQ(1, res.size()); + +	/* +	 * If we ask for the path "book", "book", "" we reference the +	 * FieldDescriptor of the StructuredClass "book". +	 */ +	res = domain->resolve(std::vector<std::string>{"book", "book", ""}); +	assert_path(res, 0, typeid(FieldDescriptor), {"book", "book", ""}); +	ASSERT_EQ(1, res.size()); + +	/* +	 * If we ask for "paragraph" it is references two times in the Domain graph, +	 * but should be returned only once. +	 */ +	res = domain->resolve("paragraph"); +	assert_path(res, 0, typeid(StructuredClass), {"book", "paragraph"}); +	ASSERT_EQ(1, res.size()); +} +} +} diff --git a/test/core/model/TestDocument.hpp b/test/core/model/TestDocument.hpp new file mode 100644 index 0000000..a1a3434 --- /dev/null +++ b/test/core/model/TestDocument.hpp @@ -0,0 +1,84 @@ +/* +    Ousía +    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel + +    This program is free software: you can redistribute it and/or modify +    it under the terms of the GNU General Public License as published by +    the Free Software Foundation, either version 3 of the License, or +    (at your option) any later version. + +    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 General Public License +    along with this program.  If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _MODEL_TEST_DOCUMENT_HPP_ +#define _MODEL_TEST_DOCUMENT_HPP_ + +#include <core/model/Document.hpp> +#include <core/model/Domain.hpp> +#include <core/model/Typesystem.hpp> + +namespace ousia { +namespace model { + +/** + * This constructs a fairly simple test document for the "book" domain. The + * structure of the document can be seen in the Code below. + */ +static Rooted<Document> constructBookDocument(Manager &mgr, +                                              Rooted<Domain> bookDomain) +{ +	// Start with the (empty) document. +	Rooted<Document> doc{new Document(mgr, "myDoc.oxd")}; + +	// Add the root. +	Rooted<StructuredEntity> root = +	    StructuredEntity::buildRootEntity(doc, {bookDomain}, "book"); +	if (root.isNull()) { +		return {nullptr}; +	} + +	// Add a paragraph. +	Rooted<StructuredEntity> foreword = +	    StructuredEntity::buildEntity(root, {bookDomain}, "paragraph"); +	if (foreword.isNull()) { +		return {nullptr}; +	} +	// Add its text. +	Variant text{std::map<std::string, Variant>{ +	    {"content", Variant("Some introductory text")}}}; +	Rooted<DocumentPrimitive> foreword_text = +	    DocumentPrimitive::buildEntity(foreword, text, "text"); +	if (foreword_text.isNull()) { +		return {nullptr}; +	} +	// Add a section. +	Rooted<StructuredEntity> section = +	    StructuredEntity::buildEntity(root, {bookDomain}, "section"); +	// Add a paragraph for it. +	Rooted<StructuredEntity> main = +	    StructuredEntity::buildEntity(section, {bookDomain}, "paragraph"); +	if (main.isNull()) { +		return {nullptr}; +	} +	// Add its text. +	text = Variant{std::map<std::string, Variant>{ +	    {"content", Variant("Some introductory text")}}}; +	Rooted<DocumentPrimitive> main_text = +	    DocumentPrimitive::buildEntity(foreword, text, "text"); +	if (main_text.isNull()) { +		return {nullptr}; +	} + +	return doc; +} +} +} + +#endif /* _TEST_DOCUMENT_HPP_ */ + diff --git a/test/core/model/TestDomain.hpp b/test/core/model/TestDomain.hpp new file mode 100644 index 0000000..41fcdef --- /dev/null +++ b/test/core/model/TestDomain.hpp @@ -0,0 +1,94 @@ +/* +    Ousía +    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel + +    This program is free software: you can redistribute it and/or modify +    it under the terms of the GNU General Public License as published by +    the Free Software Foundation, either version 3 of the License, or +    (at your option) any later version. + +    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 General Public License +    along with this program.  If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _MODEL_TEST_DOMAIN_HPP_ +#define _MODEL_TEST_DOMAIN_HPP_ + +#include <core/model/Domain.hpp> +#include <core/model/Typesystem.hpp> + +namespace ousia { +namespace model { + +/** + * This constructs a somewhat trivial system of standard types. + * + * Currently contained: string, text (struct wrapper for string) + */ +static Rooted<Typesystem> constructTypeSystem(Manager &mgr) +{ +	Rooted<Typesystem> sys{new Typesystem(mgr, "std")}; +	Rooted<StringType> string{new StringType(mgr, sys)}; +	sys->addType(string); +	Rooted<StructType> string_struct{new StructType( +	    mgr, "text", sys, {{"content", "", false, sys->acquire(string)}})}; +	sys->addType(string_struct); + +	return sys; +} + +/** + * This constructs the "book" domain for test purposes. The structure of the + * domain is fairly simple and can be seen from the construction itself. + */ +static Rooted<Domain> constructBookDomain(Manager &mgr) +{ +	// Start with the Domain itself. +	Rooted<Domain> domain{new Domain(mgr, "book")}; +	// The standard type system. +	domain->getTypesystems().push_back(constructTypeSystem(mgr)); +	// Set up the cardinalities we'll need. +	Cardinality single; +	single.merge({1}); +	Cardinality any; +	any.merge(Range<size_t>::typeRangeFrom(0)); + +	// Set up the "book" node. +	Rooted<StructuredClass> book{ +	    new StructuredClass(mgr, "book", domain, single)}; +	domain->getRootStructures().push_back(book); +	// The structure field of it. +	Rooted<FieldDescriptor> book_field{new FieldDescriptor(mgr, book)}; +	book->getFieldDescriptors().push_back(book_field); + +	// From there on the "section". +	Rooted<StructuredClass> section{ +	    new StructuredClass(mgr, "section", domain, any)}; +	book_field->getChildren().push_back(section); +	// And the field of it. +	Rooted<FieldDescriptor> section_field{new FieldDescriptor(mgr, section)}; +	section->getFieldDescriptors().push_back(section_field); + +	// We also add the "paragraph", which is transparent. +	Rooted<StructuredClass> paragraph{new StructuredClass( +	    mgr, "paragraph", domain, any, {nullptr}, {nullptr}, true)}; +	section_field->getChildren().push_back(paragraph); +	book_field->getChildren().push_back(paragraph); +	// ... and has a primitive field. +	Rooted<FieldDescriptor> paragraph_field{new FieldDescriptor( +	    mgr, paragraph, domain->getTypesystems()[0]->getTypes()[1], "text", +	    false)}; +	paragraph->getFieldDescriptors().push_back(paragraph_field); + +	return domain; +} +} +} + +#endif /* _TEST_DOMAIN_HPP_ */ +  | 
