changeset 252:19bfc11f77c2

TreeNode: resurrect indexOf
author David Demelier <markand@malikania.fr>
date Thu, 02 Oct 2014 11:34:04 +0200
parents 0b7566e27eaa
children a4770082e7d6
files C++/Tests/TreeNode/main.cpp C++/TreeNode.h
diffstat 2 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Tests/TreeNode/main.cpp	Thu Oct 02 11:17:08 2014 +0200
+++ b/C++/Tests/TreeNode/main.cpp	Thu Oct 02 11:34:04 2014 +0200
@@ -37,8 +37,15 @@
 	{
 		return m_name;
 	}
+
+	friend bool operator==(const Object &o1, const Object &o2);
 };
 
+bool operator==(const Object &o1, const Object &o2)
+{
+	return o1.name() == o2.name();
+}
+
 /*
  * Random trees are created and then we test each node with the following preferred order:
  *
@@ -1233,6 +1240,27 @@
 	ASSERT_TRUE(&copy == &copy[1].parent());
 }
 
+/* --------------------------------------------------------
+ * Miscellaneous
+ * -------------------------------------------------------- */
+
+TEST(Misc, indexOf)
+{
+	Object root("root");
+
+	root.append(Object("a"));
+	root.append(Object("b"));
+	root.append(Object("c"));
+
+	try {
+		auto index = root.indexOf(Object("b"));
+
+		ASSERT_EQ(index, 1);
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
 int main(int argc, char **argv)
 {
 	testing::InitGoogleTest(&argc, argv);
--- a/C++/TreeNode.h	Thu Oct 02 11:17:08 2014 +0200
+++ b/C++/TreeNode.h	Thu Oct 02 11:34:04 2014 +0200
@@ -93,9 +93,8 @@
 	 * @param other the other node
 	 */
 	TreeNode(const TreeNode &other)
+		: m_parent(nullptr)
 	{
-		m_parent = nullptr;
-
 		for (const auto &c : other.m_children) {
 			m_children.push_back(std::make_unique<T>(*c));
 			m_children.back()->m_parent = this;
@@ -108,10 +107,9 @@
 	 * @param other the other node
 	 */
 	TreeNode(TreeNode &&other)
+		: m_parent(nullptr)
+		, m_children(std::move(other.m_children))
 	{
-		m_parent = nullptr;
-		m_children = std::move(other.m_children);
-
 		// Update children to update to *this
 		for (auto &c : m_children)
 			c->m_parent = this;
@@ -286,7 +284,7 @@
 	int indexOf(const Value &child, typename std::enable_if<TypeTraits<Value>::equalityComparable>::type * = nullptr) const
 	{
 		for (int i = 0; i < (int)m_children.size(); ++i)
-			if (m_children[i] == child)
+			if (*m_children[i] == child)
 				return i;
 
 		return -1;