1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999-2009 Oracle.  All rights reserved.
4#
5# $Id$
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 &&\
56	    [is_rbtree $method] != 1 && [is_compressed $args] == 0 } {
57		append oflags " -dup"
58	}
59	set db [eval {berkdb_open_noerr} $oflags $testfile]
60	error_check_good dbopen [is_valid_db $db] TRUE
61
62	set nkeys 10
63	puts "\tTest$tnum.b: Fill page with $nkeys small key/data pairs."
64	for { set i 1 } { $i <= $nkeys } { incr i } {
65		if { $txnenv == 1 } {
66			set t [$env txn]
67			error_check_good txn [is_valid_txn $t $env] TRUE
68			set txn "-txn $t"
69		}
70		set ret [eval {$db put} $txn {$key$i $data$i}]
71		error_check_good dbput:$i $ret 0
72		if { $i == 1 } {
73			for {set j 0} { $j < [expr $nkeys / 2]} {incr j} {
74				set ret [eval {$db put} $txn \
75				    {$key$i DUPLICATE$j}]
76				error_check_good dbput:dup:$j $ret 0
77			}
78		}
79		if { $txnenv == 1 } {
80			error_check_good txn [$t commit] 0
81		}
82	}
83
84	# DBC GET
85	if { $txnenv == 1 } {
86		set t [$env txn]
87		error_check_good txn [is_valid_txn $t $env] TRUE
88		set txn "-txn $t"
89	}
90	set dbc_u [eval {$db cursor} $txn]
91	error_check_good db:cursor [is_valid_cursor $dbc_u $db] TRUE
92
93	puts "\tTest$tnum.c: Test dbc->get interfaces..."
94	set i 0
95	foreach flag { current first last next prev nextdup} {
96		puts "\t\t...dbc->get($flag)"
97		catch {$dbc_u get -$flag} ret
98		error_check_good dbc:get:$flag [is_substr $errorCode EINVAL] 1
99	}
100
101	foreach flag { set set_range get_both} {
102		puts "\t\t...dbc->get($flag)"
103		if { [string compare $flag get_both] == 0} {
104			catch {$dbc_u get -$flag $key$i data0} ret
105		} else {
106			catch {$dbc_u get -$flag $key$i} ret
107		}
108		error_check_good dbc:get:$flag [is_substr $errorCode EINVAL] 1
109	}
110
111	puts "\t\t...dbc->get(current, partial)"
112	catch {$dbc_u get -current -partial {0 0}} ret
113	error_check_good dbc:get:partial [is_substr $errorCode EINVAL] 1
114
115	puts "\t\t...dbc->get(current, rmw)"
116	catch {$dbc_u get -rmw -current } ret
117	error_check_good dbc_get:rmw [is_substr $errorCode EINVAL] 1
118
119	puts "\tTest$tnum.d: Test dbc->put interface..."
120	# partial...depends on another
121	foreach flag { after before current keyfirst keylast } {
122		puts "\t\t...dbc->put($flag)"
123		if { [string match key* $flag] == 1 } {
124			if { [is_record_based $method] == 1 ||\
125			   [is_compressed $args] == 1 } {
126				# Keyfirst/keylast not allowed.
127				puts "\t\t...Skipping dbc->put($flag)."
128				continue
129			} else {
130				# keyfirst/last should succeed
131				puts "\t\t...dbc->put($flag)...should succeed."
132				error_check_good dbcput:$flag \
133				    [$dbc_u put -$flag $key$i data0] 0
134
135				# now uninitialize cursor
136				error_check_good dbc_close [$dbc_u close] 0
137				set dbc_u [eval {$db cursor} $txn]
138				error_check_good \
139				    db_cursor [is_substr $dbc_u $db] 1
140			}
141		} elseif { [string compare $flag before ] == 0 ||
142		    [string compare $flag after ] == 0 } {
143			if { [is_record_based $method] == 0 &&\
144			    [is_rbtree $method] == 0 &&\
145			    [is_compressed $args] == 0} {
146				set ret [$dbc_u put -$flag data0]
147				error_check_good "$dbc_u:put:-$flag" $ret 0
148			} elseif { $renum == 1 } {
149				# Renumbering recno will return a record number
150				set currecno \
151				    [lindex [lindex [$dbc_u get -current] 0] 0]
152				set ret [$dbc_u put -$flag data0]
153				if { [string compare $flag after] == 0 } {
154					error_check_good "$dbc_u put $flag" \
155					    $ret [expr $currecno + 1]
156				} else {
157					error_check_good "$dbc_u put $flag" \
158					    $ret $currecno
159				}
160			} else {
161				puts "\t\tSkipping $flag for $method"
162			}
163		} else {
164			set ret [$dbc_u put -$flag data0]
165			error_check_good "$dbc_u:put:-$flag" $ret 0
166		}
167	}
168	# and partial
169	puts "\t\t...dbc->put(partial)"
170	catch {$dbc_u put -partial {0 0} $key$i $data$i} ret
171	error_check_good dbc_put:partial [is_substr $errorCode EINVAL] 1
172
173	# XXX dbc->dup, db->join (dbc->get join_item)
174	# dbc del
175	puts "\tTest$tnum.e: Test dbc->del interface."
176	catch {$dbc_u del} ret
177	error_check_good dbc_del [is_substr $errorCode EINVAL] 1
178
179	error_check_good dbc_close [$dbc_u close] 0
180	if { $txnenv == 1 } {
181		error_check_good txn [$t commit] 0
182	}
183	error_check_good db_close [$db close] 0
184
185	puts "\tTest$tnum complete."
186}
187