1
# See the file LICENSE for redistribution information.
3
# Copyright (c) 1996-2002
4
# Sleepycat Software. All rights reserved.
9
# TEST Recno backing file test. Try different patterns of adding
10
# TEST records and making sure that the corresponding file matches.
14
puts "Rsrc001: Basic recno backing file writeback tests"
16
# We run this test essentially twice, once with a db file
17
# and once without (an in-memory database).
18
set rec1 "This is record 1"
19
set rec2 "This is record 2 This is record 2"
20
set rec3 "This is record 3 This is record 3 This is record 3"
21
set rec4 [replicate "This is record 4 " 512]
23
foreach testfile { "$testdir/rsrc001.db" "" } {
27
if { $testfile == "" } {
28
puts "Rsrc001: Testing with in-memory database."
30
puts "Rsrc001: Testing with disk-backed database."
33
# Create backing file for the empty-file test.
34
set oid1 [open $testdir/rsrc.txt w]
37
puts "\tRsrc001.a: Put to empty file."
38
set db [eval {berkdb_open -create -mode 0644\
39
-recno -source $testdir/rsrc.txt} $testfile]
40
error_check_good dbopen [is_valid_db $db] TRUE
43
set ret [eval {$db put} $txn {1 $rec1}]
44
error_check_good put_to_empty $ret 0
45
error_check_good db_close [$db close] 0
47
# Now fill out the backing file and create the check file.
48
set oid1 [open $testdir/rsrc.txt a]
49
set oid2 [open $testdir/check.txt w]
51
# This one was already put into rsrc.txt.
64
puts -nonewline "\tRsrc001.b: Read file, rewrite last record;"
65
puts " write it out and diff"
66
set db [eval {berkdb_open -create -mode 0644\
67
-recno -source $testdir/rsrc.txt} $testfile]
68
error_check_good dbopen [is_valid_db $db] TRUE
70
# Read the last record; replace it (but we won't change it).
71
# Then close the file and diff the two files.
72
set dbc [eval {$db cursor} $txn]
73
error_check_good db_cursor [is_valid_cursor $dbc $db] TRUE
75
set rec [$dbc get -last]
76
error_check_good get_last [llength [lindex $rec 0]] 2
77
set key [lindex [lindex $rec 0] 0]
78
set data [lindex [lindex $rec 0] 1]
80
# Get the last record from the text file
81
set oid [open $testdir/rsrc.txt]
83
while { [gets $oid str] != -1 } {
87
set data [sanitize_record $data]
88
error_check_good getlast $data $laststr
90
set ret [eval {$db put} $txn {$key $data}]
91
error_check_good replace_last $ret 0
93
error_check_good curs_close [$dbc close] 0
94
error_check_good db_sync [$db sync] 0
95
error_check_good db_sync [$db sync] 0
97
Rsrc001:diff($testdir/rsrc.txt,$testdir/check.txt) \
98
[filecmp $testdir/rsrc.txt $testdir/check.txt] 0
100
puts -nonewline "\tRsrc001.c: "
101
puts "Append some records in tree and verify in file."
102
set oid [open $testdir/check.txt a]
103
for {set i 1} {$i < 10} {incr i} {
104
set rec [replicate "New Record $i" $i]
107
set ret [eval {$db put} $txn {-append $rec}]
108
error_check_good put_append $ret $key
110
error_check_good db_sync [$db sync] 0
111
error_check_good db_sync [$db sync] 0
113
set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
115
Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
117
puts "\tRsrc001.d: Append by record number"
118
set oid [open $testdir/check.txt a]
119
for {set i 1} {$i < 10} {incr i} {
120
set rec [replicate "New Record (set 2) $i" $i]
123
set ret [eval {$db put} $txn {$key $rec}]
124
error_check_good put_byno $ret 0
127
error_check_good db_sync [$db sync] 0
128
error_check_good db_sync [$db sync] 0
130
set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
132
Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
134
puts "\tRsrc001.e: Put beyond end of file."
135
set oid [open $testdir/check.txt a]
136
for {set i 1} {$i < 10} {incr i} {
140
set rec "Last Record"
144
set ret [eval {$db put} $txn {$key $rec}]
145
error_check_good put_byno $ret 0
147
puts "\tRsrc001.f: Put beyond end of file, after reopen."
149
error_check_good db_close [$db close] 0
150
set db [eval {berkdb_open -create -mode 0644\
151
-recno -source $testdir/rsrc.txt} $testfile]
152
error_check_good dbopen [is_valid_db $db] TRUE
154
set rec "Last record with reopen"
158
set ret [eval {$db put} $txn {$key $rec}]
159
error_check_good put_byno_with_reopen $ret 0
162
Put several beyond end of file, after reopen with snapshot."
163
error_check_good db_close [$db close] 0
164
set db [eval {berkdb_open -create -mode 0644\
165
-snapshot -recno -source $testdir/rsrc.txt} $testfile]
166
error_check_good dbopen [is_valid_db $db] TRUE
168
set rec "Really really last record with reopen"
179
set ret [eval {$db put} $txn {$key $rec}]
180
error_check_good put_byno_with_reopen $ret 0
182
error_check_good db_sync [$db sync] 0
183
error_check_good db_sync [$db sync] 0
186
set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
188
Rsrc001:diff($testdir/{rsrc.txt,check.txt}) $ret 0
190
puts "\tRsrc001.h: Verify proper syncing of changes on close."
191
error_check_good Rsrc001:db_close [$db close] 0
192
set db [eval {berkdb_open -create -mode 0644 -recno \
193
-source $testdir/rsrc.txt} $testfile]
194
set oid [open $testdir/check.txt a]
195
for {set i 1} {$i < 10} {incr i} {
196
set rec [replicate "New Record $i" $i]
198
set ret [eval {$db put} $txn {-append $rec}]
199
# Don't bother checking return; we don't know what
200
# the key number is, and we'll pick up a failure
203
error_check_good Rsrc001:db_close [$db close] 0
205
set ret [filecmp $testdir/rsrc.txt $testdir/check.txt]
206
error_check_good Rsrc001:diff($testdir/{rsrc,check}.txt) $ret 0
210
# Strip CRs from a record.
211
# Needed on Windows when a file is created as text (with CR/LF)
212
# but read as binary (where CR is read as a separate character)
213
proc sanitize_record { rec } {
216
if { $is_windows_test != 1 } {
219
regsub -all \15 $rec "" data