From 2bc5290ff1a0e96a34b37977ec10b9d7649318ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Wed, 27 Jun 2018 10:42:41 +0200 Subject: [PATCH] Clean unary tests --- expression_reference.h | 7 +-- layout_cursor.h | 2 +- test.cpp | 99 ++++++++++++++++++++++++++++++++---------- tree_pool.h | 10 +++++ tree_reference.h | 6 ++- 5 files changed, 95 insertions(+), 29 deletions(-) diff --git a/expression_reference.h b/expression_reference.h index 0ba47873c..3c1898e6b 100644 --- a/expression_reference.h +++ b/expression_reference.h @@ -9,10 +9,7 @@ template class ExpressionReference : public TreeReference { public: - ExpressionReference() : TreeReference() { } - - /*ExpressionReference(const ExpressionReference & er) { - }*/ + using TreeReference::TreeReference; // Allow every ExpressionReference to be transformed into an ExpressionReference, i.e. Expression operator ExpressionReference() const { @@ -26,7 +23,7 @@ public: } ExpressionReference childAtIndex(int i) { - return TreeReference::childAtIndex(i); + return ExpressionReference(TreeReference::treeChildAtIndex(i).node()); } void replaceChildAtIndex(int oldChildIndex, ExpressionReference newChild) { diff --git a/layout_cursor.h b/layout_cursor.h index e654bd9b9..823812962 100644 --- a/layout_cursor.h +++ b/layout_cursor.h @@ -30,7 +30,7 @@ public: /* Getters and setters */ LayoutRef layoutReference() { return m_layoutRef; } - int layoutReferenceIdentifier() { return m_layoutRef.identifier(); } + int layoutIdentifier() { return m_layoutRef.identifier(); } void setLayoutReference(LayoutRef r) { m_layoutRef = r; } void setLayoutNode(LayoutNode * n) { m_layoutRef = LayoutRef(n); } Position position() const { return m_position; } diff --git a/test.cpp b/test.cpp index 1eef0d97c..e8de3d777 100644 --- a/test.cpp +++ b/test.cpp @@ -9,42 +9,97 @@ AdditionRef buildAddition() { FloatRef smallFloat(0.2f); FloatRef bigFloat(3.4f); AdditionRef a(smallFloat, bigFloat); - TreePool::sharedPool()->log(); + assert(TreePool::sharedPool()->numberOfNodes() == 3); return a; } -int main() { - /*Addition a = buildAddition(); +void testAddition() { + printf("Addition test\n"); + assert(TreePool::sharedPool()->numberOfNodes() == 0); + AdditionRef a = buildAddition(); + assert(TreePool::sharedPool()->numberOfNodes() == 3); + float result = a.approximate(); - Float smallFloat(1.3f); + assert(result = 3.6f); + + FloatRef smallFloat(1.3f); a.replaceChildAtIndex(0, smallFloat); float result2 = a.approximate(); - a.swapChildren(1,0); - TreePool::sharedPool()->log();*/ + assert(result2 == 4.7f); - printf("\nCHAR LAYOUT\n"); - CharLayoutRef aChar('c'); + a.swapChildren(1,0); + assert(a.childAtIndex(0).identifier() == 1); + assert(a.childAtIndex(1).identifier() == 3); +} + +void createNodes() { + FloatRef smallFloat(0.2f); + FloatRef bigFloat(3.4f); + AdditionRef a(smallFloat, bigFloat); +} + +void testPoolEmpties() { + printf("Pool empties test\n"); + assert(TreePool::sharedPool()->numberOfNodes() == 0); + createNodes(); + assert(TreePool::sharedPool()->numberOfNodes() == 0); +} + +void testCursorCreateAndRetain() { + printf("Cursor create and retain test\n"); + CharLayoutRef aChar('a'); CharLayoutRef bChar('b'); - TreePool::sharedPool()->log(); + assert(aChar.identifier() == 0); + assert(bChar.identifier() == 1); + assert(aChar.nodeRetainCount() == 1); + assert(bChar.nodeRetainCount() == 1); + assert(strcmp(aChar.node()->description(), "Char a") == 0); + assert(strcmp(bChar.node()->description(), "Char b") == 0); + assert(TreePool::sharedPool()->numberOfNodes() == 2); HorizontalLayoutRef h(aChar, bChar); - TreePool::sharedPool()->log(); + assert(aChar.identifier() == 0); + assert(bChar.identifier() == 1); + assert(h.identifier() == 2); + assert(aChar.nodeRetainCount() == 2); + assert(bChar.nodeRetainCount() == 2); + assert(h.nodeRetainCount() == 1); + assert(aChar == h.childAtIndex(0)); + assert(bChar == h.childAtIndex(1)); + + LayoutCursor cursorA = aChar.cursor(); + assert(cursorA.layoutIdentifier() == aChar.identifier()); + assert(aChar.nodeRetainCount() == 3); +} + +void testCursorMoveLeft() { + printf("Cursor move left test\n"); + CharLayoutRef aChar('a'); + CharLayoutRef bChar('b'); + HorizontalLayoutRef h(aChar, bChar); LayoutCursor cursor = h.childAtIndex(1).cursor(); - // LayoutCursor cursor2 = aChar.cursor(); - /*cursor.log(); - bool recompute = false; - cursor.moveLeft(&recompute); - cursor.log(); - cursor.moveLeft(&recompute); - cursor.log();*/ - TreePool::sharedPool()->log(); + assert(bChar.nodeRetainCount() == 3); + assert(cursor.layoutIdentifier() == h.childAtIndex(1).identifier()); - /*cursor.log(); bool recompute = false; + assert(cursor.layoutIdentifier() == bChar.identifier()); + assert(cursor.position() == LayoutCursor::Position::Right); cursor.moveLeft(&recompute); - cursor.log(); - cursor.moveRight(&recompute); - cursor.log();*/ + assert(cursor.layoutIdentifier() == bChar.identifier()); + assert(cursor.position() == LayoutCursor::Position::Left); + assert(bChar.nodeRetainCount() == 3); + assert(aChar.nodeRetainCount() == 2); + cursor.moveLeft(&recompute); + assert(cursor.layoutIdentifier() == aChar.identifier()); + assert(cursor.position() == LayoutCursor::Position::Left); + assert(aChar.nodeRetainCount() == 3); +} + +int main() { + testAddition(); + testPoolEmpties(); + testCursorCreateAndRetain(); + testCursorMoveLeft(); return 0; } diff --git a/tree_pool.h b/tree_pool.h index 292d7ab4b..6c3acf556 100644 --- a/tree_pool.h +++ b/tree_pool.h @@ -87,6 +87,16 @@ public: void log(); #endif + // Debug + int numberOfNodes() const { + int count = 0; + AllPool nodes = const_cast(this)->allNodes(); + for (TreeNode * t : nodes) { + count++; + } + return count; + } + protected: constexpr static int BufferSize = 256; constexpr static int MaxNumberOfNodes = BufferSize/sizeof(TreeNode); diff --git a/tree_reference.h b/tree_reference.h index ebfaa7df7..0c17cb87b 100644 --- a/tree_reference.h +++ b/tree_reference.h @@ -26,6 +26,8 @@ public: return *this; } + inline bool operator==(TreeReference t) { return m_identifier == t.identifier(); } + void setTo(const TreeReference & tr) { m_identifier = tr.identifier(); TreePool::sharedPool()->node(m_identifier)->retain(); @@ -38,13 +40,15 @@ public: ~TreeReference() { assert(node()); - printf("Delete TreeReference of m_id %d, nodeId %d\n", m_identifier, node()->identifier()); + //printf("Delete TreeReference of m_id %d, nodeId %d\n", m_identifier, node()->identifier()); assert(node()->identifier() == m_identifier); node()->release(); } bool isDefined() const { return m_identifier >= 0 && TreePool::sharedPool()->node(m_identifier) != nullptr; } + int nodeRetainCount() const { return node()->retainCount(); } + operator TreeReference() const { // TODO: make sure this is kosher // static_assert(sizeof(ExpressionReference) == sizeof(ExpressionReference), "All ExpressionReference are supposed to have the same size");