sorted_array_binary 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
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 (0.0.5) 1.179088
40
- array-sorted (1.1.2) 0.076348
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
  ```
@@ -134,7 +134,7 @@ class SortedArrayBinary < Array
134
134
  def replace other_ary
135
135
  self.class._check_for_nil *other_ary
136
136
  super
137
- old_sort!
137
+ old_sort! &@sort_block
138
138
  self
139
139
  end
140
140
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'sorted_array_binary'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = '2014-01-29'
5
5
  s.summary = 'Sorted array'
6
6
  s.description = 'Sorted array using binary search'
@@ -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
- it 'after it, array is sorted' do
85
- @ar.push 'a', 'b', 'c'
86
- @ar.send(method) { |el|
87
- case el
88
- when 'a' then 9
89
- when 'b' then 3
90
- when 'c' then 1
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
- @ar.should == [1, 3, 9]
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
- it '#concat adds another array and everything is sorted' do
106
- @ar.push 'c'
107
- @ar.concat ['a', 'b']
108
- @ar.should == ['a', 'b', 'c']
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
- it 'flattens array' do
114
- @ar.push [1, 2], [4, 3]
115
- @ar.flatten!
116
- @ar.should == [1, 2, 3, 4]
117
- end
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
- it '#replace replaces array, the resulting array is sorted' do
162
- @ar.push 'a'
163
- @ar.replace ['c', 'b']
164
- @ar.should == ['b', 'c']
165
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorted_array_binary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: