puts "==========="
puts "0031918: Application Framework - New binary format for fast reading part of OCAF document"
puts "==========="

set docname ${imagedir}/doc_${casename}.cbfl

NewDocument D0 BinLOcaf
UndoLimit D0 10

NewCommand D0

# set an array 1000 values from 100 to 1099
set values "100 "
for {set i 101} {$i < 1100} {incr i} {set values "$values $i"}

# set 100 arrays to sub-labels of 0:1 0:2 0:3 and 0:4
for {set lab 1} {$lab <= 4} {incr lab} {
  for {set sublab 1} {$sublab <= 100} {incr sublab} {
   set command "SetIntArray D0 0:$lab:$sublab 0 1 1000 $values"
   eval $command
   SetReal D0 0:$lab:$sublab 0.1
  }
}

CommitCommand D0

SaveAs D0 ${docname}
Close D0

set whole_time [lindex [time {
  Open ${docname} D1
  Close D1
} 20] 0]
puts "Whole document open time $whole_time mcs"

set quater_time [lindex [time {
  Open ${docname} D2 -read0:2
  Close D2
} 20] 0]
puts "Quater of document open time $quater_time mcs"

# Check that open of quater of the document is at least twice faster than open of whole.
if { [expr $quater_time * 2] > $whole_time } {
  puts "Error : loading of quater of the document content too slow relatively to the whole document load"
}

set four_quaters_time [lindex [time {
  Open ${docname} D3 -read0:1 -read0:2 -read0:3 -read0:4
  Close D3
} 20] 0]
puts "Four quaters of document open time $four_quaters_time mcs"

# Check that open of four quaters of the document is not too much slower than opening of the whole document.
if { [expr $four_quaters_time * 0.9] > $whole_time } {
  puts "Error : loading of four quaters of the document content too slow relatively to the whole document load"
}

set no_arrays_time [lindex [time {
  Open ${docname} D4 -skipTDataStd_IntegerArray -read0:2
}] 0]
puts "Quater of document without arrays open time $no_arrays_time mcs"

set attrs [Attributes D4 0:2:13]
if {"${attrs}" != "TDataStd_Real "} {
  puts "Error : loading of document skipping arrays contains invalid attributes list '${attrs}'"
}

if {![catch {Attributes D4 0:1:1:13}] || ![catch {Attributes D4 0:1:3:14}] || ![catch {Attributes D4 0:1:4:1}]} {
  puts "Error : loading of document skipping arrays and sub-trees contains invalid attributes list"
}

set append_arrays_time [lindex [time {
  Open ${docname} D4 -append -readTDataStd_IntegerArray -read0:2 -read0:3
}] 0]
puts "Half of document arrays open time $append_arrays_time mcs"

set attrs [Attributes D4 0:2:13]
if {"${attrs}" != "TDataStd_Real TDataStd_IntegerArray "} {
  puts "Error : loading of document reading arrays separately contains invalid attributes list '${attrs}'"
}
set attrs [Attributes D4 0:3:1]
if {"${attrs}" != "TDataStd_IntegerArray "} {
  puts "Error : loading of document reading arrays separately contains invalid attributes list on subtree 3 '${attrs}'"
}
if {![catch {Attributes D4 0:1:1:13}] || ![catch {Attributes D4 0:1:4:1}]} {
  puts "Error : loading of document reading arrays separately contains invalid attributes list on 1 and 4 subtrees"
}

Close D4
