top_n 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cda9c5805aaeda70a3ea35e8a01314a96e071cd5
4
- data.tar.gz: f581488f6929408afe40e98567b076fc1d09f42f
3
+ metadata.gz: 803c6da6c638a4f7c2ae8ac20eb92d1e39703df9
4
+ data.tar.gz: 57b04413328c9c6c114603a752a188f99abb648e
5
5
  SHA512:
6
- metadata.gz: f55b87b0711b17b8fee03294baffa347c1df8fb92f010198418870ed5a337a2559358e76cd4a92ded26c424da9e9a9abfea7ef9bea9c744723a3cd3a62b8d2b4
7
- data.tar.gz: dab46fa2befd85a4edc51c5c7bef1f4f41b43e1e57ee2aaae0ff5031cf1a0e6fb458f58261e6ca7b20dd5de288edd6bf625d57acc5f0bc942d5a06249f6e19e3
6
+ metadata.gz: 4cf9a41e670df85e675638d9fa6078889113bcd7dfc957287b5082522969251ae6f2c36489b1650c23856846a551fb50ccc2a5b3035a7af47787404e4bc56d50
7
+ data.tar.gz: 0f4bd0478afba479f1a10a443b4786f9b0d32f7599efe7cbdd356557502c5a00987c7fc38e8c7af7cf7035653d714a58219f80b9000b3b13cdd01eb7b92eeb4e
data/README.rdoc CHANGED
@@ -3,7 +3,7 @@
3
3
  {<img src="https://travis-ci.org/skandragon/top_n.png?branch=master" alt="Build Status" />}[https://travis-ci.org/skandragon/top_n]
4
4
 
5
5
  This gem allows tracking of the top N (or bottom N) keys added to a list.
6
- Each key can have an optional value, which is then added to a list of values for that key.
6
+ Each key can have an optional value, which is then added to a list of values for that key. It acts a lot like a norman Ruby Hash type, but with a limited number of keys based on sorting order.
7
7
 
8
8
  Key types must respond to the usual comparison operators: <, <=, >, >=, and <=>. Values may be any object type, and need not be unique. Values are not examined in any way, simply added to an internal list.
9
9
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
data/lib/top_n.rb CHANGED
@@ -14,36 +14,29 @@
14
14
  # This can cause memory issues if many values are added to the same key.
15
15
  # If this is the type of data you are tracking, you may need a different
16
16
  # solution.
17
- class TopN
17
+ class TopN < Hash
18
18
  # The maxinum number of keys which will be tracked.
19
19
  # @return [Fixnum] the configured maximum number of keys to be tracked.
20
- attr_reader :maxsize
20
+ attr_reader :maxkeys
21
21
 
22
22
  # The configured direction.
23
23
  # @return [Symbol] either :top or :bottom.
24
24
  attr_reader :direction
25
25
 
26
- # The current number of keys we are tracking.
27
- # @return [FixNum] the count, which will be 0 up to :maxsize.
28
- attr_reader :size
29
-
30
26
  # The current value of the minimum (:top) or maximum (:bottom) key.
31
27
  # @return [Object] the threshold key.
32
28
  attr_reader :threshold_key
33
29
 
34
- # The currently tracked data as one blob. This is tied to the
35
- # current implementation, so its use is not recommended.
36
- # @return [Hash] the internal data structure containing the keys and
37
- # values. The keys to the returned Hash are the tracked keys, and
38
- # the values at those keys is the list of values.
39
- attr_reader :data
30
+ alias_method :super_store, :store
31
+ alias_method :super_bracket_assign, :[]=
32
+ alias_method :super_bracket, :[]
40
33
 
41
34
  ##
42
35
  # Create a new TopN object. Options available:
43
36
  #
44
37
  # @param [Hash] options the options used to configure the TopN object.
45
38
  #
46
- # @option options [Fixnum] :maxsize The maximum number of keys to track.
39
+ # @option options [Fixnum] :maxkeys The maximum number of keys to track.
47
40
  # Must be a positive Fixnum. Defaults to 100.
48
41
  #
49
42
  # @option options [Symbol] :direction Configure the direction.
@@ -58,37 +51,37 @@ class TopN
58
51
  # topn = TopN.new
59
52
  #
60
53
  # @example Create with a maximum size of 10, and track smaller values
61
- # topn = TopN.new(maxsize: 10, direction: :bottom)
54
+ # topn = TopN.new(maxkeys: 10, direction: :bottom)
62
55
  #
63
- def initialize(options = {})
56
+ def initialize(default = nil, options = {})
64
57
  options = {
65
- maxsize: 100,
58
+ maxkeys: 100,
66
59
  direction: :top,
67
60
  }.merge(options)
68
61
 
69
62
  options.keys.each do |opt|
70
- unless [:maxsize, :direction].include?opt
63
+ unless [:maxkeys, :direction].include?opt
71
64
  raise ArgumentError.new("invalid option #{opt}")
72
65
  end
73
66
  end
74
67
 
75
- @maxsize = options[:maxsize]
68
+ @maxkeys = options[:maxkeys]
76
69
  @direction = options[:direction]
77
- @data = {}
78
- @size = 0
79
70
  @threshold_key = nil
80
71
 
81
72
  unless [:top, :bottom].include?(@direction)
82
73
  raise ArgumentError.new("direction must be :top or :bottom")
83
74
  end
84
75
 
85
- unless @maxsize.is_a?Fixnum
86
- raise ArgumentError.new("maxsize must be a Fixnum")
76
+ unless @maxkeys.is_a?Fixnum
77
+ raise ArgumentError.new("maxkeys must be a Fixnum")
87
78
  end
88
79
 
89
- if @maxsize <= 0
90
- raise ArgumentError.new("maxsize must be >= 1")
80
+ if @maxkeys <= 0
81
+ raise ArgumentError.new("maxkeys must be >= 1")
91
82
  end
83
+
84
+ super(default)
92
85
  end
93
86
 
94
87
  ##
@@ -105,11 +98,9 @@ class TopN
105
98
  # If an existing (key, value) is permitted, and will result in the list of
106
99
  # values at that key having the same value multiple times.
107
100
  #
108
- # @return [Array] if the value was added to the key's list.
109
- #
110
- # @return [nil] if the value was not added because the key is too small or
111
- # large to be tracked.
112
- def add(key, value)
101
+ # @return [Object] the value passed in. This will be returned even if
102
+ # the value is not added because there are too many keys already present.
103
+ def store(key, value)
113
104
  if @direction == :top
114
105
  add_top(key, value)
115
106
  else
@@ -117,23 +108,9 @@ class TopN
117
108
  end
118
109
  end
119
110
 
120
- ##
121
- # Find and return the list of values for a key.
122
- #
123
- # @return [Array<Object>] the list of values for 'key'.
124
- #
125
- # @return [nil] if the key does not exist.
126
- def find(key)
127
- @data[key]
128
- end
129
-
130
- ##
131
- # Return the list of currently tracked keys.
132
- #
133
- # @return [Array<Object>] the list of values for this key.
134
- # Order is not guaranteed to match the oder which they were added.
135
- def keys
136
- @data.keys
111
+ # Behave like #store, with the same semantics.
112
+ def []=(key, value)
113
+ store(key, value)
137
114
  end
138
115
 
139
116
  private
@@ -143,21 +120,19 @@ class TopN
143
120
  def add_top(key, value)
144
121
  @threshold_key ||= key
145
122
 
146
- if @data.has_key?key
147
- @data[key] << value
123
+ if has_key?key
124
+ fetch(key) << value
148
125
  else
149
- if @size >= @maxsize
150
- return nil if key < @threshold_key
151
- @data.delete(@threshold_key)
152
- @size -= 1
153
- @threshold_key = @data.keys.min
126
+ if size >= @maxkeys
127
+ return value if key < @threshold_key
128
+ delete(@threshold_key)
129
+ @threshold_key = keys.min
154
130
  end
155
- @data[key] = [ value ]
156
- @size += 1
131
+ super_store(key, [ value ])
157
132
  @threshold_key = key if key < @threshold_key
158
133
  end
159
134
 
160
- @data[key]
135
+ value
161
136
  end
162
137
 
163
138
  ##
@@ -165,20 +140,18 @@ class TopN
165
140
  def add_bottom(key, value)
166
141
  @threshold_key ||= key
167
142
 
168
- if @data.has_key?key
169
- @data[key] << value
143
+ if has_key?key
144
+ fetch(key) << value
170
145
  else
171
- if @size >= @maxsize
172
- return nil if key > @threshold_key
173
- @data.delete(@threshold_key)
174
- @size -= 1
175
- @threshold_key = @data.keys.max
146
+ if size >= @maxkeys
147
+ return value if key > @threshold_key
148
+ delete(@threshold_key)
149
+ @threshold_key = keys.max
176
150
  end
177
- @data[key] = [ value ]
178
- @size += 1
151
+ super_store(key, [ value ])
179
152
  @threshold_key = key if key > @threshold_key
180
153
  end
181
154
 
182
- @data[key]
155
+ value
183
156
  end
184
157
  end
@@ -2,98 +2,98 @@ require 'helper'
2
2
 
3
3
  class TestAddingBottom < Minitest::Test
4
4
  def test_threshold_key_is_nil_on_create
5
- topn = TopN.new(direction: :bottom)
5
+ topn = TopN.new(nil, direction: :bottom)
6
6
  assert_nil topn.threshold_key
7
7
  end
8
8
 
9
9
  def test_adding_updates_threshold_key
10
- topn = TopN.new(direction: :bottom)
11
- assert topn.add(5, 5), "add(5, 5) failed"
10
+ topn = TopN.new(nil, direction: :bottom)
11
+ topn[5] = 5
12
12
  assert_equal 5, topn.threshold_key
13
13
  end
14
14
 
15
15
  def test_adding_increments_size
16
- topn = TopN.new(direction: :bottom)
17
- assert topn.add(1, 1), "add(1, 1) failed"
16
+ topn = TopN.new(nil, direction: :bottom)
17
+ topn[1] = 1
18
18
  assert topn.size == 1, "size == 1 failed"
19
19
  end
20
20
 
21
21
  def test_adding_without_exceeding_adds
22
- topn = TopN.new(direction: :bottom, maxsize: 10)
23
- assert topn.add(5, 5), "add(5, 5) failed"
24
- assert topn.find(5) == [5], "find(5) failed"
22
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
23
+ topn[5] = 5
24
+ assert_equal [5], topn[5]
25
25
  assert_equal 5, topn.threshold_key
26
26
  end
27
27
 
28
28
  def test_adding_sets_threshold_key
29
- topn = TopN.new(direction: :bottom, maxsize: 10)
30
- assert topn.add(5, 5), "add(5, 5) failed"
31
- assert topn.find(5) == [5], "find(5) failed"
29
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
30
+ topn[5] = 5
31
+ assert_equal [5], topn[5]
32
32
  assert_equal 5, topn.threshold_key
33
33
  end
34
34
 
35
35
  def test_adding_without_exceeding
36
- topn = TopN.new(direction: :bottom, maxsize: 10)
37
- assert topn.add(5, 5), "add(5, 5) failed"
38
- assert topn.add(10, 10), "add(10, 10) failed"
39
- assert topn.find(5) == [5], "find(5) failed"
40
- assert topn.find(10) == [10], "find(5) failed"
36
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
37
+ topn[5] = 5
38
+ topn[10] = 10
39
+ assert_equal [5], topn[5]
40
+ assert_equal [10], topn[10]
41
41
  assert_equal 10, topn.threshold_key
42
42
  end
43
43
 
44
44
  def test_adding_larger_without_exceeding_sets_threshold_key
45
- topn = TopN.new(direction: :bottom, maxsize: 10)
46
- assert topn.add(5, 5), "add(5, 5) failed"
45
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
46
+ topn[5] = 5
47
47
  assert_equal 5, topn.threshold_key
48
- assert topn.add(10, 10), "add(10, 10) failed"
48
+ topn[10] = 10
49
49
  assert_equal 10, topn.threshold_key
50
50
  end
51
51
 
52
52
  def test_adding_smaller_without_exceeding_sets_threshold_key
53
- topn = TopN.new(direction: :bottom, maxsize: 10)
54
- assert topn.add(10, 10), "add(10, 10) failed"
53
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
54
+ topn[10] = 10
55
55
  assert_equal 10, topn.threshold_key
56
- assert topn.add(5, 5), "add(5, 5) failed"
56
+ topn[5] = 5
57
57
  assert_equal 10, topn.threshold_key
58
58
  end
59
59
 
60
60
  def test_adding_two_values_to_the_same_key
61
- topn = TopN.new(direction: :bottom, maxsize: 10)
62
- assert topn.add(1, 1), "add(1, 1) failed"
63
- assert topn.add(1, 2), "add(1, 2) failed"
64
- assert_equal [1, 2], topn.find(1).sort
61
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
62
+ topn[1] = 1
63
+ topn[1] = 2
64
+ assert_equal [1, 2], topn[1].sort
65
65
  end
66
66
 
67
67
  def test_adding_smaller_key_when_limit_will_exceed
68
- topn = TopN.new(direction: :bottom, maxsize: 2)
69
- assert topn.add(3, 3), "add(3, 3) failed"
70
- assert topn.add(2, 2), "add(2, 2) failed"
71
- assert topn.add(1, 1), "add(1, 1) failed"
68
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 2)
69
+ topn[3] = 3
70
+ topn[2] = 2
71
+ topn[1] = 1
72
72
  assert_equal 2, topn.size
73
- assert_equal [1], topn.find(1)
74
- assert_equal [2], topn.find(2)
75
- assert_nil topn.find(3)
73
+ assert_equal [1], topn[1]
74
+ assert_equal [2], topn[2]
75
+ assert_nil topn[3]
76
76
  end
77
77
 
78
78
  def test_adding_larger_key_when_limit_will_exceed
79
- topn = TopN.new(direction: :bottom, maxsize: 2)
80
- assert topn.add(1, 1), "add(1, 1) failed"
81
- assert topn.add(2, 2), "add(2, 2) failed"
82
- assert_nil topn.add(3, 3)
79
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 2)
80
+ topn[1] = 1
81
+ topn[2] = 2
82
+ topn[3] = 3
83
83
  assert_equal 2, topn.size
84
- assert_equal [1], topn.find(1)
85
- assert_equal [2], topn.find(2)
86
- assert_nil topn.find(3)
84
+ assert_equal [1], topn[1]
85
+ assert_equal [2], topn[2]
86
+ assert_nil topn[3]
87
87
  end
88
88
 
89
89
  def test_torture
90
- topn = TopN.new(direction: :bottom, maxsize: 10)
90
+ topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
91
91
 
92
92
  records = []
93
93
  300_000.times do
94
94
  record = rand(4000)
95
95
  records << record
96
- topn.add(record, rand(100_000_000))
96
+ topn[record] = rand(100_000_000)
97
97
  end
98
98
 
99
99
  top_records = records.uniq.sort[0..9]
@@ -8,92 +8,92 @@ class TestAddingTop < Minitest::Test
8
8
 
9
9
  def test_adding_updates_threshold_key
10
10
  topn = TopN.new
11
- assert topn.add(5, 5), "add(5, 5) failed"
11
+ topn[5] = 5
12
12
  assert_equal 5, topn.threshold_key
13
13
  end
14
14
 
15
15
  def test_adding_increments_size
16
16
  topn = TopN.new
17
- assert topn.add(1, 1), "add(1, 1) failed"
17
+ topn[1] = 1
18
18
  assert topn.size == 1, "size == 1 failed"
19
19
  end
20
20
 
21
21
  def test_adding_without_exceeding_adds
22
- topn = TopN.new(maxsize: 10)
23
- assert topn.add(5, 5), "add(5, 5) failed"
24
- assert topn.find(5) == [5], "find(5) failed"
22
+ topn = TopN.new(nil, maxkeys: 10)
23
+ topn[5] = 5
24
+ assert_equal [5], topn[5]
25
25
  assert_equal 5, topn.threshold_key
26
26
  end
27
27
 
28
28
  def test_adding_sets_threshold_key
29
- topn = TopN.new(maxsize: 10)
30
- assert topn.add(5, 5), "add(5, 5) failed"
31
- assert topn.find(5) == [5], "find(5) failed"
29
+ topn = TopN.new(nil, maxkeys: 10)
30
+ topn[5] = 5
31
+ assert_equal [5], topn[5]
32
32
  assert_equal 5, topn.threshold_key
33
33
  end
34
34
 
35
35
  def test_adding_without_exceeding
36
- topn = TopN.new(maxsize: 10)
37
- assert topn.add(5, 5), "add(5, 5) failed"
38
- assert topn.add(10, 10), "add(10, 10) failed"
39
- assert topn.find(5) == [5], "find(5) failed"
40
- assert topn.find(10) == [10], "find(5) failed"
36
+ topn = TopN.new(nil, maxkeys: 10)
37
+ topn[5] = 5
38
+ topn[10] = 10
39
+ assert_equal [5], topn[5]
40
+ assert_equal [10], topn[10]
41
41
  assert_equal 5, topn.threshold_key
42
42
  end
43
43
 
44
44
  def test_adding_larger_without_exceeding_sets_threshold_key
45
- topn = TopN.new(maxsize: 10)
46
- assert topn.add(5, 5), "add(5, 5) failed"
45
+ topn = TopN.new(nil, maxkeys: 10)
46
+ topn[5] = 5
47
47
  assert_equal 5, topn.threshold_key
48
- assert topn.add(10, 10), "add(10, 10) failed"
48
+ topn[10] = 10
49
49
  assert_equal 5, topn.threshold_key
50
50
  end
51
51
 
52
52
  def test_adding_smaller_without_exceeding_sets_threshold_key
53
- topn = TopN.new(maxsize: 10)
54
- assert topn.add(10, 10), "add(10, 10) failed"
53
+ topn = TopN.new(nil, maxkeys: 10)
54
+ topn[10] = 10
55
55
  assert_equal 10, topn.threshold_key
56
- assert topn.add(5, 5), "add(5, 5) failed"
56
+ topn[5] = 5
57
57
  assert_equal 5, topn.threshold_key
58
58
  end
59
59
 
60
60
  def test_adding_two_values_to_the_same_key
61
- topn = TopN.new(maxsize: 10)
62
- assert topn.add(1, 1), "add(1, 1) failed"
63
- assert topn.add(1, 2), "add(1, 2) failed"
64
- assert_equal [1, 2], topn.find(1).sort
61
+ topn = TopN.new(nil, maxkeys: 10)
62
+ topn[1] = 1
63
+ topn[1] = 2
64
+ assert_equal [1, 2], topn[1].sort
65
65
  end
66
66
 
67
67
  def test_adding_larger_key_when_limit_will_exceed
68
- topn = TopN.new(maxsize: 2)
69
- assert topn.add(1, 1), "add(1, 1) failed"
70
- assert topn.add(2, 2), "add(1, 1) failed"
71
- assert topn.add(3, 3), "add(1, 1) failed"
68
+ topn = TopN.new(nil, maxkeys: 2)
69
+ topn[1] = 1
70
+ topn[2] = 2
71
+ topn[3] = 3
72
72
  assert_equal 2, topn.size
73
- assert_nil topn.find(1)
74
- assert_equal [2], topn.find(2)
75
- assert_equal [3], topn.find(3)
73
+ assert_nil topn[1]
74
+ assert_equal [2], topn[2]
75
+ assert_equal [3], topn[3]
76
76
  end
77
77
 
78
78
  def test_adding_smaller_key_when_limit_will_exceed
79
- topn = TopN.new(maxsize: 2)
80
- assert topn.add(3, 3), "add(1, 1) failed"
81
- assert topn.add(2, 2), "add(1, 1) failed"
82
- assert_nil topn.add(1, 1)
79
+ topn = TopN.new(nil, maxkeys: 2)
80
+ topn[3] = 3
81
+ topn[2] = 2
82
+ topn[1] = 1
83
83
  assert_equal 2, topn.size
84
- assert_nil topn.find(1)
85
- assert_equal [2], topn.find(2)
86
- assert_equal [3], topn.find(3)
84
+ assert_nil topn[1]
85
+ assert_equal [2], topn[2]
86
+ assert_equal [3], topn[3]
87
87
  end
88
88
 
89
89
  def test_torture
90
- topn = TopN.new(maxsize: 1000)
90
+ topn = TopN.new(nil, maxkeys: 1000)
91
91
 
92
92
  records = []
93
93
  300_000.times do
94
94
  record = rand(4000)
95
95
  records << record
96
- topn.add(record, rand(100_000_000))
96
+ topn[record] = rand(100_000_000)
97
97
  end
98
98
 
99
99
  top_records = records.uniq.sort { |a, b| b <=> a }[0..999]
@@ -6,48 +6,53 @@ class TestTopN < Minitest::Test
6
6
  assert topn
7
7
  end
8
8
 
9
- def test_creation_without_arguments_sets_maxsize_to_some_positive_default
9
+ def test_creation_with_default_value
10
+ topn = TopN.new(:flarg)
11
+ assert_equal :flarg, topn[5]
12
+ end
13
+
14
+ def test_creation_without_arguments_sets_maxkeys_to_some_positive_default
10
15
  topn = TopN.new
11
- assert topn.maxsize > 0
16
+ assert topn.maxkeys > 0
12
17
  end
13
18
 
14
- def test_creation_with_maxsize
15
- topn = TopN.new(maxsize: 100)
16
- assert topn.maxsize == 100
19
+ def test_creation_with_maxkeys
20
+ topn = TopN.new(nil, maxkeys: 100)
21
+ assert topn.maxkeys == 100
17
22
  end
18
23
 
19
24
  def test_creation_with_direction
20
- topn = TopN.new(direction: :bottom)
25
+ topn = TopN.new(nil, direction: :bottom)
21
26
  assert topn.direction == :bottom
22
27
  end
23
28
 
24
29
  def test_creation_raises_assertion_with_bad_direction
25
30
  assert_raises(ArgumentError) {
26
- TopN.new(direction: :flarg)
31
+ TopN.new(nil, direction: :flarg)
27
32
  }
28
33
  end
29
34
 
30
- def test_creation_raises_assertion_with_zero_maxsize
35
+ def test_creation_raises_assertion_with_zero_maxkeys
31
36
  assert_raises(ArgumentError) {
32
- TopN.new(maxsize: 0)
37
+ TopN.new(nil, maxkeys: 0)
33
38
  }
34
39
  end
35
40
 
36
- def test_creation_raises_assertion_with_negative_maxsize
41
+ def test_creation_raises_assertion_with_negative_maxkeys
37
42
  assert_raises(ArgumentError) {
38
- TopN.new(maxsize: -1)
43
+ TopN.new(nil, maxkeys: -1)
39
44
  }
40
45
  end
41
46
 
42
- def test_creation_raises_assertion_with_non_fixnum_maxsize
47
+ def test_creation_raises_assertion_with_non_fixnum_maxkeys
43
48
  assert_raises(ArgumentError) {
44
- TopN.new(maxsize: 'foo')
49
+ TopN.new(nil, maxkeys: 'foo')
45
50
  }
46
51
  end
47
52
 
48
53
  def test_creation_raises_assertion_for_invalid_argument_names
49
54
  assert_raises(ArgumentError) {
50
- TopN.new(flarg: 1)
55
+ TopN.new(nil, flarg: 1)
51
56
  }
52
57
  end
53
58
  end
data/test/test_data.rb CHANGED
@@ -2,23 +2,23 @@ require 'helper'
2
2
 
3
3
  class TestData < Minitest::Test
4
4
  def test_data_is_empty_on_create
5
- topn = TopN.new(maxsize: 2)
6
- assert_equal({}, topn.data)
5
+ topn = TopN.new(nil, maxkeys: 2)
6
+ assert_equal({}, topn)
7
7
  end
8
8
 
9
- def test_data_has_keys
10
- topn = TopN.new(maxsize: 2)
11
- assert topn.add(1, 1), "add(1, 1) failed"
12
- assert topn.add(2, 2), "add(1, 1) failed"
13
- assert topn.add(3, 3), "add(1, 1) failed"
14
- assert_equal [2, 3], topn.data.keys.sort
9
+ def test_data
10
+ topn = TopN.new(nil, maxkeys: 2)
11
+ topn[1] = 1
12
+ topn[2] = 2
13
+ topn[3] = 3
14
+ assert_equal([ [2, [2]], [3, [ 3 ]] ], topn.sort)
15
15
  end
16
16
 
17
17
  def test_keys
18
- topn = TopN.new(maxsize: 2)
19
- assert topn.add(1, 1), "add(1, 1) failed"
20
- assert topn.add(2, 2), "add(1, 1) failed"
21
- assert topn.add(3, 3), "add(1, 1) failed"
18
+ topn = TopN.new(nil, maxkeys: 2)
19
+ topn[1] = 1
20
+ topn[2] = 2
21
+ topn[3] = 3
22
22
  assert_equal [2, 3], topn.keys.sort
23
23
  end
24
24
  end
data/top_n.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "top_n"
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Graff"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: top_n
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Graff