1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	test055
8# TEST	Basic cursor operations.
9# TEST	This test checks basic cursor operations.
10# TEST	There are N different scenarios to tests:
11# TEST	1. (no dups) Set cursor, retrieve current.
12# TEST	2. (no dups) Set cursor, retrieve next.
13# TEST	3. (no dups) Set cursor, retrieve prev.
14proc test055 { method args } {
15	global errorInfo
16	source ./include.tcl
17
18	set args [convert_args $method $args]
19	set omethod [convert_method $method]
20
21	puts "Test055: $method interspersed cursor and normal operations"
22
23	# Create the database and open the dictionary
24	set txnenv 0
25	set eindex [lsearch -exact $args "-env"]
26	#
27	# If we are using an env, then testfile should just be the db name.
28	# Otherwise it is the test directory and the name.
29	if { $eindex == -1 } {
30		set testfile $testdir/test055.db
31		set env NULL
32	} else {
33		set testfile test055.db
34		incr eindex
35		set env [lindex $args $eindex]
36		set txnenv [is_txnenv $env]
37		if { $txnenv == 1 } {
38			append args " -auto_commit "
39		}
40		set testdir [get_home $env]
41	}
42	cleanup $testdir $env
43
44	set flags ""
45	set txn ""
46
47	puts "\tTest055.a: No duplicates"
48	set db [eval {berkdb_open -create -mode 0644 $omethod } \
49	    $args {$testfile}]
50	error_check_good db_open:nodup [is_valid_db $db] TRUE
51
52	# Put three keys in the database
53	for { set key 1 } { $key <= 3 } {incr key} {
54		if { $txnenv == 1 } {
55			set t [$env txn]
56			error_check_good txn [is_valid_txn $t $env] TRUE
57			set txn "-txn $t"
58		}
59		set r [eval {$db put} $txn $flags {$key datum$key}]
60		error_check_good put $r 0
61		if { $txnenv == 1 } {
62			error_check_good txn [$t commit] 0
63		}
64	}
65
66	# Retrieve keys sequentially so we can figure out their order
67	set i 1
68	if { $txnenv == 1 } {
69		set t [$env txn]
70		error_check_good txn [is_valid_txn $t $env] TRUE
71		set txn "-txn $t"
72	}
73	set curs [eval {$db cursor} $txn]
74	error_check_good curs_open:nodup [is_valid_cursor $curs $db] TRUE
75
76	for {set d [$curs get -first] } { [llength $d] != 0 } {\
77		set d [$curs get -next] } {
78		set key_set($i) [lindex [lindex $d 0] 0]
79		incr i
80	}
81
82	# Test case #1.
83	puts "\tTest055.a1: Set cursor, retrieve current"
84
85	# Now set the cursor on the middle on.
86	set r [$curs get -set $key_set(2)]
87	error_check_bad cursor_get:DB_SET [llength $r] 0
88	set k [lindex [lindex $r 0] 0]
89	set d [lindex [lindex $r 0] 1]
90	error_check_good curs_get:DB_SET:key $k $key_set(2)
91	error_check_good \
92	    curs_get:DB_SET:data $d [pad_data $method datum$key_set(2)]
93
94	# Now retrieve current
95	set r [$curs get -current]
96	error_check_bad cursor_get:DB_CURRENT [llength $r] 0
97	set k [lindex [lindex $r 0] 0]
98	set d [lindex [lindex $r 0] 1]
99	error_check_good curs_get:DB_CURRENT:key $k $key_set(2)
100	error_check_good \
101	    curs_get:DB_CURRENT:data $d [pad_data $method datum$key_set(2)]
102
103	# Test case #2.
104	puts "\tTest055.a2: Set cursor, retrieve previous"
105	set r [$curs get -prev]
106	error_check_bad cursor_get:DB_PREV [llength $r] 0
107	set k [lindex [lindex $r 0] 0]
108	set d [lindex [lindex $r 0] 1]
109	error_check_good curs_get:DB_PREV:key $k $key_set(1)
110	error_check_good \
111	    curs_get:DB_PREV:data $d [pad_data $method datum$key_set(1)]
112
113	# Test case #3.
114	puts "\tTest055.a2: Set cursor, retrieve next"
115
116	# Now set the cursor on the middle one.
117	set r [$curs get -set $key_set(2)]
118	error_check_bad cursor_get:DB_SET [llength $r] 0
119	set k [lindex [lindex $r 0] 0]
120	set d [lindex [lindex $r 0] 1]
121	error_check_good curs_get:DB_SET:key $k $key_set(2)
122	error_check_good \
123	    curs_get:DB_SET:data $d [pad_data $method datum$key_set(2)]
124
125	# Now retrieve next
126	set r [$curs get -next]
127	error_check_bad cursor_get:DB_NEXT [llength $r] 0
128	set k [lindex [lindex $r 0] 0]
129	set d [lindex [lindex $r 0] 1]
130	error_check_good curs_get:DB_NEXT:key $k $key_set(3)
131	error_check_good \
132	    curs_get:DB_NEXT:data $d [pad_data $method datum$key_set(3)]
133
134	# Close cursor and database.
135	error_check_good curs_close [$curs close] 0
136	if { $txnenv == 1 } {
137		error_check_good txn [$t commit] 0
138	}
139	error_check_good db_close [$db close] 0
140}
141