; ***************************************************************************
; copyright   : (C) 2025 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************



.equ TREE_TEST_OBJECT_OFFS_LIST   = 0
.equ TREE_TEST_OBJECT_OFFS_VALUE1 = TREE_SIZE
.equ TREE_TEST_OBJECT_OFFS_VALUE2 = TREE_SIZE+1
.equ TREE_TEST_OBJECT_SIZE        = TREE_SIZE+2



.dseg

testTree_root:   .byte TREE_TEST_OBJECT_SIZE
testTree_child1: .byte TREE_TEST_OBJECT_SIZE
testTree_child2: .byte TREE_TEST_OBJECT_SIZE
testTree_child3: .byte TREE_TEST_OBJECT_SIZE




.cseg


; ---------------------------------------------------------------------------
; @routine treeTest_Object_Init
; @param Y pointer to object to init
; @param r18 value 1
; @param r19 value 2

treeTest_Object_Init:
  bigcall Tree_InitObject                  ; (R16)
  std Y+TREE_TEST_OBJECT_OFFS_VALUE1, r18
  std Y+TREE_TEST_OBJECT_OFFS_VALUE2, r19
  ret
; @end



treeTest1:
  ldi yl, LOW(testTree_root)
  ldi yh, HIGH(testTree_root)
  ldi r18, 1
  ldi r19, 2
  rcall treeTest_Object_Init
  mov xl, yl
  mov xh, yh

  rcall treeTestCreateAndAddChild1
  ldi r16, 1                         ; error code
  brcc treeTest1_error

  rcall treeTestCreateAndAddChild2
  ldi r16, 2                         ; error code
  brcc treeTest1_error

  rcall treeTestCreateAndAddChild3
  ldi r16, 3                         ; error code
  brcc treeTest1_error

  ldi yl, LOW(testTree_root)
  ldi yh, HIGH(testTree_root)
  bigcall Tree_GetFirstChildObject
  ldi r16, 4                    ; error code
  cpi xl, LOW(testTree_child1)
  brne treeTest1_error
  cpi xh, HIGH(testTree_child1)
  brne treeTest1_error

  mov yl, xl
  mov yh, xh
  ldd r16, Y+TREE_TEST_OBJECT_OFFS_VALUE1
  cpi r16, 3
  ldi r16, 5                    ; error code
  brne treeTest1_error
  ldd r16, Y+TREE_TEST_OBJECT_OFFS_VALUE2
  cpi r16, 4
  ldi r16, 6                    ; error code
  brne treeTest1_error

  rcall treeTestNextSiblingIs2
  ldi r16, 7                    ; error code
  brcc treeTest1_error

  rcall treeTestNextSiblingIs3
  ldi r16, 8                    ; error code
  brcc treeTest1_error

  rcall treeTestNextSiblingIsNull
  ldi r16, 9                    ; error code
  brcc treeTest1_error

  sec
  ret

treeTest1_error:
  clc
  ret
; @end



treeTest2:
  ldi yl, LOW(testTree_child2)
  ldi yh, HIGH(testTree_child2)
  bigcall Tree_UnlinkObject

  rcall treeTestCheckParentNull
  ldi r16, 1                   ; error code
  brcc treeTest2_error

  bigcall Tree_GetNextSibling
  mov r16, xl
  or r16, xh
  ldi r16, 2                   ; error code
  brne treeTest2_error

  ldi yl, LOW(testTree_root)
  ldi yh, HIGH(testTree_root)
  bigcall Tree_GetFirstChildObject
  ldi r16, 3                   ; error code
  cpi xl, LOW(testTree_child1)
  brne treeTest2_error
  cpi xh, HIGH(testTree_child1)
  brne treeTest2_error
  mov yl, xl
  mov yh, xh

  rcall treeTestNextSiblingIs3
  ldi r16, 4                   ; error code
  brcc treeTest2_error
  
  sec
  ret
treeTest2_error:
  clc
  ret
; @end



treeTest3:
  ldi yl, LOW(testTree_child1)
  ldi yh, HIGH(testTree_child1)
  bigcall Tree_UnlinkObject

  rcall treeTestCheckParentNull
  ldi r16, 1                   ; error code
  brcc treeTest3_error

  bigcall Tree_GetNextSibling
  mov r16, xl
  or r16, xh
  ldi r16, 2                   ; error code
  brne treeTest3_error

  ldi yl, LOW(testTree_root)
  ldi yh, HIGH(testTree_root)
  bigcall Tree_GetFirstChildObject
  ldi r16, 3                   ; error code
  cpi xl, LOW(testTree_child3)
  brne treeTest3_error
  cpi xh, HIGH(testTree_child3)
  brne treeTest3_error
  mov yl, xl
  mov yh, xh

  rcall treeTestNextSiblingIsNull
  ldi r16, 4                   ; error code
  brcc treeTest3_error
  
  sec
  ret
treeTest3_error:
  clc
  ret
; @end



treeTest4:
  ldi yl, LOW(testTree_child1)
  ldi yh, HIGH(testTree_child1)
  ldi xl, LOW(testTree_root)
  ldi xh, HIGH(testTree_root)
  bigcall Tree_AddChildObject
  
  rcall treeTestCheckParentRoot
  ldi r16, 1                   ; error code
  brcc treeTest4_error

  ldi yl, LOW(testTree_root)
  ldi yh, HIGH(testTree_root)
  bigcall Tree_GetFirstChildObject
  ldi r16, 2                   ; error code
  cpi xl, LOW(testTree_child3)
  brne treeTest4_error
  cpi xh, HIGH(testTree_child3)
  brne treeTest4_error
  mov yl, xl
  mov yh, xh

  rcall treeTestNextSiblingIs1
  ldi r16, 3                   ; error code
  brcc treeTest4_error

  rcall treeTestNextSiblingIsNull
  ldi r16, 4                   ; error code
  brcc treeTest4_error
  
  sec
  ret
treeTest4_error:
  clc
  ret
; @end






treeTestCreateAndAddChild1:
  ldi yl, LOW(testTree_child1)
  ldi yh, HIGH(testTree_child1)
  ldi r18, 3
  ldi r19, 4
  rcall treeTest_Object_Init
  
  ldi xl, LOW(testTree_root)
  ldi xh, HIGH(testTree_root)
  bigcall Tree_AddChildObject
  
  rcall treeTestCheckParentRoot
  brcc treeTestCreateAndAddChild1_error
  sec
  ret
treeTestCreateAndAddChild1_error:
  clc
  ret



treeTestCreateAndAddChild2:
  ldi yl, LOW(testTree_child2)
  ldi yh, HIGH(testTree_child2)
  ldi r18, 5
  ldi r19, 6
  rcall treeTest_Object_Init
  
  ldi xl, LOW(testTree_root)
  ldi xh, HIGH(testTree_root)
  bigcall Tree_AddChildObject
  
  rcall treeTestCheckParentRoot
  brcc treeTestCreateAndAddChild2_error
  sec
  ret
treeTestCreateAndAddChild2_error:
  clc
  ret



treeTestCreateAndAddChild3:
  ldi yl, LOW(testTree_child3)
  ldi yh, HIGH(testTree_child3)
  ldi r18, 7
  ldi r19, 8
  rcall treeTest_Object_Init
  
  ldi xl, LOW(testTree_root)
  ldi xh, HIGH(testTree_root)
  bigcall Tree_AddChildObject
  
  rcall treeTestCheckParentRoot
  brcc treeTestCreateAndAddChild3_error
  sec
  ret
treeTestCreateAndAddChild3_error:
  clc
  ret
  



treeTestCheckParentNull:
  bigcall Tree_GetParentObject
  mov r16, xl
  or r16, xh
  brne treeTestCheckParentNull_error
  sec
  ret
treeTestCheckParentNull_error:
  clc
  ret



treeTestCheckParentRoot:
  bigcall Tree_GetParentObject
  cpi xl, LOW(testTree_root)
  brne treeTestCheckParentRoot_error
  cpi xh, HIGH(testTree_root)
  brne treeTestCheckParentRoot_error
  sec
  ret
treeTestCheckParentRoot_error:
  clc
  ret



treeTestNextSiblingIs1:
  bigcall Tree_GetNextSibling
  cpi xl, LOW(testTree_child1)
  brne treeTestNextSiblingIs1_error
  cpi xh, HIGH(testTree_child1)
  brne treeTestNextSiblingIs1_error
  mov yl, xl
  mov yh, xh
  sec
  ret
treeTestNextSiblingIs1_error:
  clc
  ret




treeTestNextSiblingIs2:
  bigcall Tree_GetNextSibling
  cpi xl, LOW(testTree_child2)
  brne treeTestNextSiblingIs2_error
  cpi xh, HIGH(testTree_child2)
  brne treeTestNextSiblingIs2_error
  mov yl, xl
  mov yh, xh
  sec
  ret
treeTestNextSiblingIs2_error:
  clc
  ret


treeTestNextSiblingIs3:
  bigcall Tree_GetNextSibling
  cpi xl, LOW(testTree_child3)
  brne treeTestNextSiblingIs3_error
  cpi xh, HIGH(testTree_child3)
  brne treeTestNextSiblingIs3_error
  mov yl, xl
  mov yh, xh
  sec
  ret
treeTestNextSiblingIs3_error:
  clc
  ret



treeTestNextSiblingIsNull:
  bigcall Tree_GetNextSibling
  mov r16, xl
  or r16, xh
  brne treeTestNextSiblingIsNull_error
  sec
  ret
treeTestNextSiblingIsNull_error:
  clc
  ret



