sorted_array_binary 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +5 -2
- data/lib/sorted_array_binary.rb +1 -1
- data/sorted_array_binary.gemspec +1 -1
- data/spec/sorted_array_binary_spec.rb +60 -24
- metadata +1 -1
data/README.md
CHANGED
@@ -30,13 +30,16 @@ array.push 'b', 'a' #=> ['a', 'b']
|
|
30
30
|
# Use custom sorting block.
|
31
31
|
array = SortedArrayBinary.new { |a, b| b <=> a }
|
32
32
|
array.push 'a', 'b' #=> ['b', 'a']
|
33
|
+
|
34
|
+
# Nils not allowed.
|
35
|
+
array.push nil #=> ArgumentError is raised
|
33
36
|
```
|
34
37
|
|
35
38
|
## Performance
|
36
39
|
|
37
40
|
When #push'ing 1000 random numbers into an array:
|
38
41
|
```
|
39
|
-
sorted_array
|
40
|
-
array-sorted
|
42
|
+
sorted_array (0.0.5) 1.179088
|
43
|
+
array-sorted (1.1.2) 0.076348
|
41
44
|
sorted_array_binary (0.0.1) 0.015969
|
42
45
|
```
|
data/lib/sorted_array_binary.rb
CHANGED
data/sorted_array_binary.gemspec
CHANGED
@@ -2,6 +2,11 @@ require 'rspec'
|
|
2
2
|
require 'spec_helper'
|
3
3
|
require 'sorted_array_binary'
|
4
4
|
|
5
|
+
def new_array_with_block
|
6
|
+
SortedArrayBinary.new { |a, b| b <=> a }
|
7
|
+
end
|
8
|
+
|
9
|
+
# {{{1 SortedArrayBinary
|
5
10
|
describe SortedArrayBinary do
|
6
11
|
before :each do
|
7
12
|
@ar = SortedArrayBinary.new
|
@@ -81,17 +86,24 @@ describe SortedArrayBinary do
|
|
81
86
|
# {{{2 #collect!
|
82
87
|
[:collect!, :map!].each { |method|
|
83
88
|
context "##{method}" do
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
89
|
+
[false, true].each { |sort_block_given|
|
90
|
+
it 'after it, array is sorted' do
|
91
|
+
@ar = new_array_with_block if sort_block_given
|
92
|
+
@ar.push 'a', 'b', 'c'
|
93
|
+
@ar.send(method) { |el|
|
94
|
+
case el
|
95
|
+
when 'a' then 9
|
96
|
+
when 'b' then 3
|
97
|
+
when 'c' then 1
|
98
|
+
end
|
99
|
+
}
|
100
|
+
if sort_block_given
|
101
|
+
@ar.should == [9, 3, 1]
|
102
|
+
else
|
103
|
+
@ar.should == [1, 3, 9]
|
91
104
|
end
|
92
|
-
|
93
|
-
|
94
|
-
end
|
105
|
+
end
|
106
|
+
}
|
95
107
|
|
96
108
|
it 'raises exception if one of resulting elements is nil' do
|
97
109
|
@ar.push 'a'
|
@@ -102,19 +114,35 @@ describe SortedArrayBinary do
|
|
102
114
|
}
|
103
115
|
|
104
116
|
# {{{2 #concat
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
117
|
+
context '#concat' do
|
118
|
+
[false, true].each { |sort_block_given|
|
119
|
+
it '#concat adds another array and everything is sorted' do
|
120
|
+
@ar = new_array_with_block if sort_block_given
|
121
|
+
@ar.push 'c'
|
122
|
+
@ar.concat ['a', 'b']
|
123
|
+
if sort_block_given
|
124
|
+
@ar.should == ['c', 'b', 'a']
|
125
|
+
else
|
126
|
+
@ar.should == ['a', 'b', 'c']
|
127
|
+
end
|
128
|
+
end
|
129
|
+
}
|
109
130
|
end
|
110
131
|
|
111
132
|
# {{{2 #flatten!
|
112
133
|
context '#flatten!' do
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
134
|
+
[false, true].each { |sort_block_given|
|
135
|
+
it 'flattens array and the result is sorted' do
|
136
|
+
@ar = new_array_with_block if sort_block_given
|
137
|
+
@ar.push [1, 2], [4, 3]
|
138
|
+
@ar.flatten!
|
139
|
+
if sort_block_given
|
140
|
+
@ar.should == [4, 3, 2, 1]
|
141
|
+
else
|
142
|
+
@ar.should == [1, 2, 3, 4]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
}
|
118
146
|
|
119
147
|
it 'raises exception if resulting array contains nil' do
|
120
148
|
@ar.push [nil, 1]
|
@@ -158,11 +186,18 @@ describe SortedArrayBinary do
|
|
158
186
|
|
159
187
|
# {{{2 #replace
|
160
188
|
context '#replace' do
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
189
|
+
[false, true].each { |sort_block_given|
|
190
|
+
it '#replace replaces array, the resulting array is sorted' do
|
191
|
+
@ar = new_array_with_block if sort_block_given
|
192
|
+
@ar.push 'a'
|
193
|
+
@ar.replace ['c', 'b']
|
194
|
+
if sort_block_given
|
195
|
+
@ar.should == ['c', 'b']
|
196
|
+
else
|
197
|
+
@ar.should == ['b', 'c']
|
198
|
+
end
|
199
|
+
end
|
200
|
+
}
|
166
201
|
|
167
202
|
it "doesn't allow nils" do
|
168
203
|
expect { @ar.replace [nil] }.to raise_error ArgumentError
|
@@ -320,6 +355,7 @@ describe SortedArrayBinary do
|
|
320
355
|
# }}}2
|
321
356
|
end
|
322
357
|
|
358
|
+
# {{{1 ComparisonState
|
323
359
|
describe SortedArrayBinary::ComparisonState do
|
324
360
|
[nil, -2, 2].each { |state|
|
325
361
|
it "allows only valid state in" do
|