From bf5b5293464a0a7e0f93e96f94df00dbeac70ae0 Mon Sep 17 00:00:00 2001
From: vro <vro@opencascade.com>
Date: Thu, 27 Feb 2014 18:43:38 +0400
Subject: [PATCH] 0024645: Pointer to the last is wrong for a tree node

Information on last child is added to the Dump() method + fixed a problem of lost last child tree node on removal (detach) operation.
Test case for issue CR24645
---
 src/TDataStd/TDataStd_TreeNode.cxx | 11 ++++----
 tests/bugs/caf/bug24645            | 45 ++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 6 deletions(-)
 create mode 100644 tests/bugs/caf/bug24645

diff --git a/src/TDataStd/TDataStd_TreeNode.cxx b/src/TDataStd/TDataStd_TreeNode.cxx
index 1b0b66437f..de47e20c29 100644
--- a/src/TDataStd/TDataStd_TreeNode.cxx
+++ b/src/TDataStd/TDataStd_TreeNode.cxx
@@ -235,12 +235,7 @@ Standard_Boolean TDataStd_TreeNode::Remove ()
   }
   
   if(Father()->HasLast()) {
-    if (Handle(TDataStd_TreeNode)::DownCast(this) == Father()->Last()) {
-      if(HasPrevious()) {
-        Father()->SetLast(Previous());
-      }
-      else Father()->SetLast(bid);
-    }
+    Father()->SetLast(bid);
   }
 
   SetFather(bid);
@@ -698,6 +693,10 @@ Standard_OStream& TDataStd_TreeNode::Dump (Standard_OStream& anOS) const
     anOS<<"  First=";
     if (!myFirst->Label().IsNull()) myFirst->Label().EntryDump(anOS);
   }
+  if (myLast) {
+    anOS<<"  Last=";
+    if (!myLast->Label().IsNull()) myLast->Label().EntryDump(anOS);
+  }
   anOS<<endl;
   return anOS;
 }
diff --git a/tests/bugs/caf/bug24645 b/tests/bugs/caf/bug24645
new file mode 100644
index 0000000000..c9035f658b
--- /dev/null
+++ b/tests/bugs/caf/bug24645
@@ -0,0 +1,45 @@
+puts "=========="
+puts "OCC24645"
+puts "=========="
+puts ""
+###################################################
+# Pointer to the last is wrong for a tree node
+###################################################
+
+NewDocument D
+UndoLimit D 1000
+
+NewCommand D
+SetNode D 0:1
+
+NewCommand D
+AppendNode D 0:1 0:1:1
+NewCommand D
+AppendNode D 0:1 0:1:2
+NewCommand D
+AppendNode D 0:1 0:1:3
+NewCommand D
+AppendNode D 0:1 0:1:4
+
+NewCommand D
+AppendNode D 0:1 0:1:5
+
+if { [regexp "Last=0:1:5" [XDumpDF D] ] == 1 } {
+    puts "OK : Last node is good"
+} else {
+    puts "Error : Last node is bad"
+}
+puts ""
+
+DetachNode D 0:1:3
+DetachNode D 0:1:4
+
+NewCommand D
+Undo D
+
+if { [regexp "Last" [XDumpDF D] ] != 1 } {
+    puts "OK : Last node is not defined"
+} else {
+    puts "Error : Last node is bad"
+}
+puts ""