1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: test049.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test049
8# TEST	Cursor operations on uninitialized cursors.
9proc test049 { method args } {
10	global errorInfo
11	global errorCode
12	source ./include.tcl
13
14	set tnum 049
15	set renum [is_rrecno $method]
16
17	set args [convert_args $method $args]
18	set omethod [convert_method $method]
19
20	puts "\tTest$tnum: Test of cursor routines with uninitialized cursors."
21
22	set key	"key"
23	set data	"data"
24	set txn ""
25	set flags ""
26	set rflags ""
27
28	if { [is_record_based $method] == 1 } {
29		set key ""
30	}
31
32	puts "\tTest$tnum.a: Create $method database."
33	set txnenv 0
34	set eindex [lsearch -exact $args "-env"]
35	#
36	# If we are using an env, then testfile should just be the db name.
37	# Otherwise it is the test directory and the name.
38	if { $eindex == -1 } {
39		set testfile $testdir/test$tnum.db
40		set env NULL
41	} else {
42		set testfile test$tnum.db
43		incr eindex
44		set env [lindex $args $eindex]
45		set txnenv [is_txnenv $env]
46		if { $txnenv == 1 } {
47			append args " -auto_commit "
48		}
49		set testdir [get_home $env]
50	}
51	set t1 $testdir/t1
52	cleanup $testdir $env
53
54	set oflags "-create -mode 0644 $rflags $omethod $args"
55	if { [is_record_based $method] == 0 && [is_rbtree $method] != 1 } {
56		append oflags " -dup"
57	}
58	set db [eval {berkdb_open_noerr} $oflags $testfile]
59	error_check_good dbopen [is_valid_db $db] TRUE
60
61	set nkeys 10
62	puts "\tTest$tnum.b: Fill page with $nkeys small key/data pairs."
63	for { set i 1 } { $i <= $nkeys } { incr i } {
64		if { $txnenv == 1 } {
65			set t [$env txn]
66			error_check_good txn [is_valid_txn $t $env] TRUE
67			set txn "-txn $t"
68		}
69		set ret [eval {$db put} $txn {$key$i $data$i}]
70		error_check_good dbput:$i $ret 0
71		if { $i == 1 } {
72			for {set j 0} { $j < [expr $nkeys / 2]} {incr j} {
73				set ret [eval {$db put} $txn \
74				    {$key$i DUPLICATE$j}]
75				error_check_good dbput:dup:$j $ret 0
76			}
77		}
78		if { $txnenv == 1 } {
79			error_check_good txn [$t commit] 0
80		}
81	}
82
83	# DBC GET
84	if { $txnenv == 1 } {
85		set t [$env txn]
86		error_check_good txn [is_valid_txn $t $env] TRUE
87		set txn "-txn $t"
88	}
89	set dbc_u [eval {$db cursor} $txn]
90	error_check_good db:cursor [is_valid_cursor $dbc_u $db] TRUE
91
92	puts "\tTest$tnum.c: Test dbc->get interfaces..."
93	set i 0
94	foreach flag { current first last next prev nextdup} {
95		puts "\t\t...dbc->get($flag)"
96		catch {$dbc_u get -$flag} ret
97		error_check_good dbc:get:$flag [is_substr $errorCode EINVAL] 1
98	}
99
100	foreach flag { set set_range get_both} {
101		puts "\t\t...dbc->get($flag)"
102		if { [string compare $flag get_both] == 0} {
103			catch {$dbc_u get -$flag $key$i data0} ret
104		} else {
105			catch {$dbc_u get -$flag $key$i} ret
106		}
107		error_check_good dbc:get:$flag [is_substr $errorCode EINVAL] 1
108	}
109
110	puts "\t\t...dbc->get(current, partial)"
111	catch {$dbc_u get -current -partial {0 0}} ret
112	error_check_good dbc:get:partial [is_substr $errorCode EINVAL] 1
113
114	puts "\t\t...dbc->get(current, rmw)"
115	catch {$dbc_u get -rmw -current } ret
116	error_check_good dbc_get:rmw [is_substr $errorCode EINVAL] 1
117
118	puts "\tTest$tnum.d: Test dbc->put interface..."
119	# partial...depends on another
120	foreach flag { after before current keyfirst keylast } {
121		puts "\t\t...dbc->put($flag)"
122		if { [string match key* $flag] == 1 } {
123			  if { [is_record_based $method] == 1 } {
124				# keyfirst/keylast not allowed in recno
125			puts "\t\t...Skipping dbc->put($flag) for $method."
126				continue
127			  } else {
128				# keyfirst/last should succeed
129		puts "\t\t...dbc->put($flag)...should succeed for $method"
130				error_check_good dbcput:$flag \
131				    [$dbc_u put -$flag $key$i data0] 0
132
133				# now uninitialize cursor
134				error_check_good dbc_close [$dbc_u close] 0
135				set dbc_u [eval {$db cursor} $txn]
136				error_check_good \
137				    db_cursor [is_substr $dbc_u $db] 1
138			}
139		} elseif { [string compare $flag before ] == 0 ||
140		    [string compare $flag after ] == 0 } {
141			if { [is_record_based $method] == 0 &&
142			    [is_rbtree $method] == 0} {
143				set ret [$dbc_u put -$flag data0]
144				error_check_good "$dbc_u:put:-$flag" $ret 0
145			} elseif { $renum == 1 } {
146				# Renumbering recno will return a record number
147				set currecno \
148				    [lindex [lindex [$dbc_u get -current] 0] 0]
149				set ret [$dbc_u put -$flag data0]
150				if { [string compare $flag after] == 0 } {
151					error_check_good "$dbc_u put $flag" \
152					    $ret [expr $currecno + 1]
153				} else {
154					error_check_good "$dbc_u put $flag" \
155					    $ret $currecno
156				}
157			} else {
158				puts "\t\tSkipping $flag for $method"
159			}
160		} else {
161			set ret [$dbc_u put -$flag data0]
162			error_check_good "$dbc_u:put:-$flag" $ret 0
163		}
164	}
165	# and partial
166	puts "\t\t...dbc->put(partial)"
167	catch {$dbc_u put -partial {0 0} $key$i $data$i} ret
168	error_check_good dbc_put:partial [is_substr $errorCode EINVAL] 1
169
170	# XXX dbc->dup, db->join (dbc->get join_item)
171	# dbc del
172	puts "\tTest$tnum.e: Test dbc->del interface."
173	catch {$dbc_u del} ret
174	error_check_good dbc_del [is_substr $errorCode EINVAL] 1
175
176	error_check_good dbc_close [$dbc_u close] 0
177	if { $txnenv == 1 } {
178		error_check_good txn [$t commit] 0
179	}
180	error_check_good db_close [$db close] 0
181
182	puts "\tTest$tnum complete."
183}
184