/tags/SN-6.0/db4/test/test051.tcl

https://gitlab.com/OpenSourceMirror/sourcenav · TCL · 221 lines · 172 code · 20 blank · 29 comment · 35 complexity · a256d4419172e144b59cf65de2cddadb MD5 · raw file

  1. # See the file LICENSE for redistribution information.
  2. #
  3. # Copyright (c) 1999,2007 Oracle. All rights reserved.
  4. #
  5. # $Id: test051.tcl,v 12.7 2007/05/17 15:15:56 bostic Exp $
  6. #
  7. # TEST test051
  8. # TEST Fixed-length record Recno test.
  9. # TEST 0. Test various flags (legal and illegal) to open
  10. # TEST 1. Test partial puts where dlen != size (should fail)
  11. # TEST 2. Partial puts for existent record -- replaces at beg, mid, and
  12. # TEST end of record, as well as full replace
  13. proc test051 { method { args "" } } {
  14. global fixed_len
  15. global errorInfo
  16. global errorCode
  17. source ./include.tcl
  18. set args [convert_args $method $args]
  19. set omethod [convert_method $method]
  20. puts "Test051 ($method): Test of the fixed length records."
  21. if { [is_fixed_length $method] != 1 } {
  22. puts "Test051: skipping for method $method"
  23. return
  24. }
  25. # Create the database and open the dictionary
  26. set txnenv 0
  27. set eindex [lsearch -exact $args "-env"]
  28. #
  29. # If we are using an env, then testfile should just be the db name.
  30. # Otherwise it is the test directory and the name.
  31. if { $eindex == -1 } {
  32. set testfile $testdir/test051.db
  33. set testfile1 $testdir/test051a.db
  34. set env NULL
  35. } else {
  36. set testfile test051.db
  37. set testfile1 test051a.db
  38. incr eindex
  39. set env [lindex $args $eindex]
  40. set txnenv [is_txnenv $env]
  41. if { $txnenv == 1 } {
  42. append args " -auto_commit "
  43. }
  44. set testdir [get_home $env]
  45. }
  46. cleanup $testdir $env
  47. set oflags "-create -mode 0644 $args"
  48. # Test various flags (legal and illegal) to open
  49. puts "\tTest051.a: Test correct flag behavior on open."
  50. set errorCode NONE
  51. foreach f { "-dup" "-dup -dupsort" "-recnum" } {
  52. puts "\t\tTest051.a: Test flag $f"
  53. set stat [catch {eval {berkdb_open_noerr} $oflags $f $omethod \
  54. $testfile} ret]
  55. error_check_good dbopen:flagtest:catch $stat 1
  56. error_check_good \
  57. dbopen:flagtest:$f [is_substr $errorCode EINVAL] 1
  58. set errorCode NONE
  59. }
  60. set f "-renumber"
  61. puts "\t\tTest051.a: Test $f"
  62. if { [is_frecno $method] == 1 } {
  63. set db [eval {berkdb_open} $oflags $f $omethod $testfile]
  64. error_check_good dbopen:flagtest:$f [is_valid_db $db] TRUE
  65. $db close
  66. } else {
  67. error_check_good \
  68. dbopen:flagtest:catch [catch {eval {berkdb_open_noerr}\
  69. $oflags $f $omethod $testfile} ret] 1
  70. error_check_good \
  71. dbopen:flagtest:$f [is_substr $errorCode EINVAL] 1
  72. }
  73. # Test partial puts where dlen != size (should fail)
  74. # it is an error to specify a partial put w/ different
  75. # dlen and size in fixed length recno/queue
  76. set key 1
  77. set data ""
  78. set txn ""
  79. set test_char "a"
  80. set db [eval {berkdb_open_noerr} $oflags $omethod $testfile1]
  81. error_check_good dbopen [is_valid_db $db] TRUE
  82. if { $txnenv == 1 } {
  83. set t [$env txn]
  84. error_check_good txn [is_valid_txn $t $env] TRUE
  85. set txn "-txn $t"
  86. }
  87. puts "\tTest051.b: Partial puts with dlen != size."
  88. foreach dlen { 1 16 20 32 } {
  89. foreach doff { 0 10 20 32 } {
  90. # dlen < size
  91. puts "\t\tTest051.e: dlen: $dlen, doff: $doff, \
  92. size: [expr $dlen+1]"
  93. set data [repeat $test_char [expr $dlen + 1]]
  94. error_check_good \
  95. catch:put 1 [catch {eval {$db put -partial \
  96. [list $doff $dlen]} $txn {$key $data}} ret]
  97. # We don't get back the server error string just
  98. # the result.
  99. if { $eindex == -1 } {
  100. error_check_good "dbput:partial: dlen < size" \
  101. [is_substr \
  102. $errorInfo "ecord length"] 1
  103. } else {
  104. error_check_good "dbput:partial: dlen < size" \
  105. [is_substr $errorCode "EINVAL"] 1
  106. }
  107. # dlen > size
  108. puts "\t\tTest051.e: dlen: $dlen, doff: $doff, \
  109. size: [expr $dlen-1]"
  110. set data [repeat $test_char [expr $dlen - 1]]
  111. error_check_good \
  112. catch:put 1 [catch {eval {$db put -partial \
  113. [list $doff $dlen]} $txn {$key $data}} ret]
  114. if { $eindex == -1 } {
  115. error_check_good "dbput:partial: dlen > size" \
  116. [is_substr \
  117. $errorInfo "ecord length"] 1
  118. } else {
  119. error_check_good "dbput:partial: dlen < size" \
  120. [is_substr $errorCode "EINVAL"] 1
  121. }
  122. }
  123. }
  124. if { $txnenv == 1 } {
  125. error_check_good txn [$t commit] 0
  126. }
  127. $db close
  128. # Partial puts for existent record -- replaces at beg, mid, and
  129. # end of record, as well as full replace
  130. puts "\tTest051.f: Partial puts within existent record."
  131. set db [eval {berkdb_open} $oflags $omethod $testfile]
  132. error_check_good dbopen [is_valid_db $db] TRUE
  133. puts "\t\tTest051.f: First try a put and then a full replace."
  134. set data [repeat "a" $fixed_len]
  135. if { $txnenv == 1 } {
  136. set t [$env txn]
  137. error_check_good txn [is_valid_txn $t $env] TRUE
  138. set txn "-txn $t"
  139. }
  140. set ret [eval {$db put} $txn {1 $data}]
  141. error_check_good dbput $ret 0
  142. set ret [eval {$db get} $txn {-recno 1}]
  143. error_check_good dbget $data [lindex [lindex $ret 0] 1]
  144. set data [repeat "b" $fixed_len]
  145. set ret [eval {$db put -partial [list 0 $fixed_len]} $txn {1 $data}]
  146. error_check_good dbput $ret 0
  147. set ret [eval {$db get} $txn {-recno 1}]
  148. error_check_good dbget $data [lindex [lindex $ret 0] 1]
  149. if { $txnenv == 1 } {
  150. error_check_good txn [$t commit] 0
  151. }
  152. set data "InitialData"
  153. set pdata "PUT"
  154. set dlen [string length $pdata]
  155. set ilen [string length $data]
  156. set mid [expr $ilen/2]
  157. # put initial data
  158. set key 0
  159. set offlist [list 0 $mid [expr $ilen -1] [expr $fixed_len - $dlen]]
  160. puts "\t\tTest051.g: Now replace at different offsets ($offlist)."
  161. foreach doff $offlist {
  162. incr key
  163. if { $txnenv == 1 } {
  164. set t [$env txn]
  165. error_check_good txn [is_valid_txn $t $env] TRUE
  166. set txn "-txn $t"
  167. }
  168. set ret [eval {$db put} $txn {$key $data}]
  169. error_check_good dbput:init $ret 0
  170. puts "\t\tTest051.g: Replace at offset $doff."
  171. set ret [eval {$db put -partial [list $doff $dlen]} $txn \
  172. {$key $pdata}]
  173. error_check_good dbput:partial $ret 0
  174. if { $txnenv == 1 } {
  175. error_check_good txn [$t commit] 0
  176. }
  177. if { $doff == 0} {
  178. set beg ""
  179. set end [string range $data $dlen $ilen]
  180. } else {
  181. set beg [string range $data 0 [expr $doff - 1]]
  182. set end [string range $data [expr $doff + $dlen] $ilen]
  183. }
  184. if { $doff > $ilen } {
  185. # have to put padding between record and inserted
  186. # string
  187. set newdata [format %s%s $beg $end]
  188. set diff [expr $doff - $ilen]
  189. set nlen [string length $newdata]
  190. set newdata [binary \
  191. format a[set nlen]x[set diff]a$dlen $newdata $pdata]
  192. } else {
  193. set newdata [make_fixed_length \
  194. frecno [format %s%s%s $beg $pdata $end]]
  195. }
  196. set ret [$db get -recno $key]
  197. error_check_good compare($newdata,$ret) \
  198. [binary_compare [lindex [lindex $ret 0] 1] $newdata] 0
  199. }
  200. $db close
  201. }