File: test029.tcl

package info (click to toggle)
db 2%3A2.4.14-2.7.7.1.c
  • links: PTS
  • area: main
  • in suites: potato
  • size: 12,716 kB
  • ctags: 9,382
  • sloc: ansic: 35,556; tcl: 8,564; cpp: 4,890; sh: 2,075; makefile: 1,723; java: 1,632; sed: 419; awk: 153; asm: 41
file content (168 lines) | stat: -rw-r--r-- 4,924 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# See the file LICENSE for redistribution information.
#
# Copyright (c) 1996, 1997, 1998
#	Sleepycat Software.  All rights reserved.
#
#	@(#)test029.tcl	10.7 (Sleepycat) 4/23/98
#
# DB Test 29 {method nentries}
# Test the Btree and Record number renumbering.

proc test029 { method {nentries 10000} args} {
	set do_renumber [is_rrecno $method]
	set args [convert_args $method $args]
	set method [convert_method $method]
	set args [number_btree $method $args]
	puts "Test029: $method ($args)"

	if { [string compare $method DB_HASH] == 0 } {
		puts "Test029 skipping for method HASH"
		return
	}
	if { [string compare $method DB_RECNO] == 0 && $do_renumber != 1 } {
		puts "Test029 skipping for method RECNO (w/out renumbering)"
		return
	}

	# Get global declarations since tcl doesn't support
	# any useful equivalent to #defines!
	source ./include.tcl

	# Create the database and open the dictionary
	set testfile test029.db
	cleanup $testdir

	# Read the first nentries dictionary elements and reverse them.
	# Keep a list of these (these will be the keys).
	puts "\tTest029.a: initialization"
	set keys ""
	set did [open $dict]
	set count 0
	while { [gets $did str] != -1 && $count < $nentries } {
		lappend keys [reverse $str]
		incr count
	}
	close $did

	# Generate sorted order for the keys
	set sorted_keys [lsort $keys]

	# Save the first and last keys
	set last_key [lindex $sorted_keys end]
	set last_keynum [llength $sorted_keys]

	set first_key [lindex $sorted_keys 0]
	set first_keynum 1

	# Create the database
	set db [eval [concat dbopen \
	    $testfile [expr $DB_CREATE | $DB_TRUNCATE] 0644 $method $args]]
	error_check_good dbopen [is_valid_db $db] TRUE

	set flags 0
	set txn 0
	puts "\tTest029.b: put/get loop"
	foreach k $keys {
		if { [string compare $method DB_RECNO] == 0 } {
			set key [lsearch $sorted_keys $k]
			incr key
		} else {
			set key $k
		}
		set ret [$db put $txn $key $k $flags]
		error_check_good dbput $ret 0

		set ret [$db get $txn $key $flags]
		if { [string compare $ret $k] != 0 } {
			puts "Test029: put key-data $key $k got $ret"
			return
		}
	}

	# Now delete the first key in the database
	puts "\tTest029.c: delete and verify renumber"

	# Delete the first key in the file
	if { [string compare $method DB_RECNO] == 0 } {
		set key $first_keynum
	} else {
		set key $first_key
	}

	set ret [$db del $txn $key $flags]
	error_check_good db_del $ret 0

	# Verify that the keynumber of the last key changed
	if { [string compare $method DB_BTREE] == 0 } {
		set flags $DB_SET_RECNO
	}

	# First try to get the old last key (shouldn't exist)
	set ret [$db getn $txn $last_keynum $flags]
	error_check_good get_after_del [is_substr $ret "not found"] 1

	# Now try to get what we think should be the last key
	set ret [$db getn $txn [expr $last_keynum - 1] $flags]
	error_check_good getn_last_after_del $ret $last_key

	# Create a cursor; we need it for the next test and we
	# need it for recno here.

	set dbc [$db cursor $txn]
	error_check_good cursor [is_valid_widget $dbc $db.cursor] TRUE

	# OK, now re-put the first key and make sure that we
	# renumber the last key appropriately.
	if { [string compare $method DB_BTREE] == 0 } {
		set ret [$db put $txn $key $first_key 0]
		error_check_good db_put $ret 0
	} else {
		# Recno
		set ret [$dbc get 0 $DB_FIRST]
		set ret [$dbc put $key $first_key $DB_BEFORE]
		error_check_good dbc_put:DB_BEFORE $ret 0
	}

	# Now check that the last record matches the last record number
	set ret [$db getn $txn $last_keynum $flags]
	error_check_good getn_last_after_put $ret $last_key

	# Now delete the first key in the database using a cursor
	puts "\tTest029.d: delete with cursor and verify renumber"

	set ret [$dbc get 0 $DB_FIRST]
	error_check_good dbc_first [lindex $ret 0] $key
	error_check_good dbc_first [lindex $ret 1] $first_key

	# Now delete at the cursor
	set ret [$dbc del 0]
	error_check_good dbc_del $ret 0

	# Now check the record numbers of the last keys again.
	# First try to get the old last key (shouldn't exist)
	set ret [$db getn $txn $last_keynum $flags]
	error_check_good get_last_after_cursor_del:$ret \
	    [is_substr $ret "not found"] 1

	# Now try to get what we think should be the last key
	set ret [$db getn $txn [expr $last_keynum - 1] $flags]
	error_check_good getn_after_cursor_del $ret $last_key

	# OK, now re-put the first key and make sure that we
	# renumber the last key appropriately.
	if { [string compare $method DB_BTREE] == 0 } {
		set ret [$dbc put $key $first_key $DB_CURRENT]
		error_check_good dbc_put:DB_CURRENT $ret 0
	} else {
		set ret [$dbc put $key $first_key $DB_BEFORE]
		error_check_good dbc_put:DB_BEFORE $ret 0
	}

	# Now check that the last record matches the last record number
	set ret [$db getn $txn $last_keynum $flags]
	error_check_good get_after_cursor_reput $ret $last_key

	error_check_good dbc_close [$dbc close] 0
	error_check_good db_close [$db close] 0
}