Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 147 lines (121 sloc) 3.257 kb
1174d14 @technoweenie add iteration test
technoweenie authored
1 require 'test/unit'
2 require File.expand_path("../../lib/leveldb", __FILE__)
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
3 require 'fileutils'
1174d14 @technoweenie add iteration test
technoweenie authored
4
5 class IterationTest < Test::Unit::TestCase
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
6 DB_PATH = "/tmp/iteration.db"
7
8 def initialize(*a)
9 super
10 FileUtils.rm_rf DB_PATH
11 @db = LevelDB::DB.new DB_PATH
12 @db.put "a/1", "1"
13 @db.put "b/1", "2"
14 @db.put "b/2", "3"
15 @db.put "b/3", "4"
16 @db.put "c/1", "5"
17 end
1174d14 @technoweenie add iteration test
technoweenie authored
18
19 def test_each
20 expected = %w(a/1 b/1 b/2 b/3 c/1)
21 keys = []
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
22 @db.each do |key, value|
1174d14 @technoweenie add iteration test
technoweenie authored
23 keys << key
24 end
25
26 assert_equal expected, keys
27 end
28
29 def test_each_with_key_from
30 expected = %w(b/1 b/2 b/3 c/1)
31 keys = []
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
32 @db.each(:from => 'b') do |key, value|
1174d14 @technoweenie add iteration test
technoweenie authored
33 keys << key
34 end
35
36 assert_equal expected, keys
37 end
38
39 def test_each_with_key_from_to
4e6fd35 @technoweenie change the order of the limit checking so that `key_to` in #each behaves...
technoweenie authored
40 expected = %w(b/1 b/2 b/3)
1174d14 @technoweenie add iteration test
technoweenie authored
41 keys = []
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
42 @db.each(:from => 'b', :to => 'b/4') do |key, value|
1174d14 @technoweenie add iteration test
technoweenie authored
43 keys << key
44 end
45
46 assert_equal expected, keys
47 end
48
49 def test_reverse_each
50 expected = %w(c/1 b/3 b/2 b/1 a/1)
51 keys = []
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
52 @db.each(:reversed => true) do |key, value|
1174d14 @technoweenie add iteration test
technoweenie authored
53 keys << key
54 end
55
56 assert_equal expected, keys
57 end
58
59 def test_reverse_each_with_key_from
60 expected = %w(b/1 a/1)
61 keys = []
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
62 @db.each(:from => 'b', :reversed => true) do |key, value|
1174d14 @technoweenie add iteration test
technoweenie authored
63 keys << key
64 end
65
66 assert_equal expected, keys
67 end
68
69 def test_reverse_each_with_key_from_to
4e6fd35 @technoweenie change the order of the limit checking so that `key_to` in #each behaves...
technoweenie authored
70 expected = %w(c/1 b/3 b/2 b/1)
1174d14 @technoweenie add iteration test
technoweenie authored
71 keys = []
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
72 @db.each(:from => 'c', :to => 'b', :reversed => true) do |key, value|
1174d14 @technoweenie add iteration test
technoweenie authored
73 keys << key
74 end
75
76 assert_equal expected, keys
77 end
2296ed7 @technoweenie add basic iterator class
technoweenie authored
78
625cc3e @technoweenie finish implementing the iterator
technoweenie authored
79 def test_iterator_reverse_each_with_key_from_to
80 expected_keys = %w(c/1 b/3 b/2 b/1)
81 expected_values = %w(5 4 3 2)
82 keys = []
83 values = []
84
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
85 iter = LevelDB::Iterator.new @db, :from => 'c', :to => 'b', :reversed => true
625cc3e @technoweenie finish implementing the iterator
technoweenie authored
86 iter.each do |key, value|
87 keys << key
88 values << value
89 end
90
91 assert_equal expected_keys, keys
92 assert_equal expected_values, values
93 end
94
95 def test_iterator_each
96 expected_keys = %w(a/1 b/1 b/2 b/3 c/1)
97 expected_values = %w(1 2 3 4 5)
98 keys = []
99 values = []
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
100 iter = LevelDB::Iterator.new @db
625cc3e @technoweenie finish implementing the iterator
technoweenie authored
101 iter.each do |key, value|
102 keys << key
103 values << value
104 end
105
106 assert_equal expected_keys, keys
107 assert_equal expected_values, values
108 end
109
110 def test_iterator_peek
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
111 iter = LevelDB::Iterator.new @db
625cc3e @technoweenie finish implementing the iterator
technoweenie authored
112 assert_equal %w(a/1 1), iter.peek, iter.invalid_reason
113 assert_equal %w(a/1 1), iter.peek, iter.invalid_reason
114 assert_nil iter.scan
115 assert_equal %w(b/1 2), iter.peek, iter.invalid_reason
116 end
117
2296ed7 @technoweenie add basic iterator class
technoweenie authored
118 def test_iterator_init_with_default_options
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
119 iter = LevelDB::Iterator.new @db
120 assert_equal @db, iter.db
2296ed7 @technoweenie add basic iterator class
technoweenie authored
121 assert_nil iter.from
122 assert_nil iter.to
ca3e276 minor style tweaks, and add DB#iterator convenience method
William Morgan authored
123 assert !iter.reversed?
2296ed7 @technoweenie add basic iterator class
technoweenie authored
124 end
125
126 def test_iterator_init_with_options
595e9f0 @wmorgan rejigger tests to use /tmp and add many_key_test
authored
127 iter = LevelDB::Iterator.new @db, :from => 'abc', :to => 'def', :reversed => true
128 assert_equal @db,iter.db
2296ed7 @technoweenie add basic iterator class
technoweenie authored
129 assert_equal 'abc', iter.from
130 assert_equal 'def', iter.to
ca3e276 minor style tweaks, and add DB#iterator convenience method
William Morgan authored
131 assert iter.reversed?
2296ed7 @technoweenie add basic iterator class
technoweenie authored
132 end
133
134 def test_iterator_init_with_no_args
135 assert_raise ArgumentError do
136 LevelDB::Iterator.new
137 end
138 end
139
140 def test_iterator_requires_db
141 assert_raise ArgumentError do
142 LevelDB::Iterator.new 'db'
143 end
144 end
1174d14 @technoweenie add iteration test
technoweenie authored
145 end
146
Something went wrong with that request. Please try again.