top_n 1.0.1 → 1.1.0
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.
- checksums.yaml +4 -4
- data/README.rdoc +1 -1
- data/VERSION +1 -1
- data/lib/top_n.rb +39 -66
- data/test/test_adding_bottom.rb +42 -42
- data/test/test_adding_top.rb +39 -39
- data/test/test_creation.rb +19 -14
- data/test/test_data.rb +12 -12
- data/top_n.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 803c6da6c638a4f7c2ae8ac20eb92d1e39703df9
|
4
|
+
data.tar.gz: 57b04413328c9c6c114603a752a188f99abb648e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.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 :
|
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
|
-
|
35
|
-
|
36
|
-
|
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] :
|
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(
|
54
|
+
# topn = TopN.new(maxkeys: 10, direction: :bottom)
|
62
55
|
#
|
63
|
-
def initialize(options = {})
|
56
|
+
def initialize(default = nil, options = {})
|
64
57
|
options = {
|
65
|
-
|
58
|
+
maxkeys: 100,
|
66
59
|
direction: :top,
|
67
60
|
}.merge(options)
|
68
61
|
|
69
62
|
options.keys.each do |opt|
|
70
|
-
unless [:
|
63
|
+
unless [:maxkeys, :direction].include?opt
|
71
64
|
raise ArgumentError.new("invalid option #{opt}")
|
72
65
|
end
|
73
66
|
end
|
74
67
|
|
75
|
-
@
|
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 @
|
86
|
-
raise ArgumentError.new("
|
76
|
+
unless @maxkeys.is_a?Fixnum
|
77
|
+
raise ArgumentError.new("maxkeys must be a Fixnum")
|
87
78
|
end
|
88
79
|
|
89
|
-
if @
|
90
|
-
raise ArgumentError.new("
|
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 [
|
109
|
-
#
|
110
|
-
|
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
|
-
|
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
|
147
|
-
|
123
|
+
if has_key?key
|
124
|
+
fetch(key) << value
|
148
125
|
else
|
149
|
-
if
|
150
|
-
return
|
151
|
-
|
152
|
-
@
|
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
|
-
|
156
|
-
@size += 1
|
131
|
+
super_store(key, [ value ])
|
157
132
|
@threshold_key = key if key < @threshold_key
|
158
133
|
end
|
159
134
|
|
160
|
-
|
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
|
169
|
-
|
143
|
+
if has_key?key
|
144
|
+
fetch(key) << value
|
170
145
|
else
|
171
|
-
if
|
172
|
-
return
|
173
|
-
|
174
|
-
@
|
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
|
-
|
178
|
-
@size += 1
|
151
|
+
super_store(key, [ value ])
|
179
152
|
@threshold_key = key if key > @threshold_key
|
180
153
|
end
|
181
154
|
|
182
|
-
|
155
|
+
value
|
183
156
|
end
|
184
157
|
end
|
data/test/test_adding_bottom.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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,
|
23
|
-
|
24
|
-
|
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,
|
30
|
-
|
31
|
-
|
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,
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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,
|
46
|
-
|
45
|
+
topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
|
46
|
+
topn[5] = 5
|
47
47
|
assert_equal 5, topn.threshold_key
|
48
|
-
|
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,
|
54
|
-
|
53
|
+
topn = TopN.new(nil, direction: :bottom, maxkeys: 10)
|
54
|
+
topn[10] = 10
|
55
55
|
assert_equal 10, topn.threshold_key
|
56
|
-
|
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,
|
62
|
-
|
63
|
-
|
64
|
-
assert_equal [1, 2], topn
|
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,
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
74
|
-
assert_equal [2], topn
|
75
|
-
assert_nil topn
|
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,
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
85
|
-
assert_equal [2], topn
|
86
|
-
assert_nil topn
|
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,
|
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
|
96
|
+
topn[record] = rand(100_000_000)
|
97
97
|
end
|
98
98
|
|
99
99
|
top_records = records.uniq.sort[0..9]
|
data/test/test_adding_top.rb
CHANGED
@@ -8,92 +8,92 @@ class TestAddingTop < Minitest::Test
|
|
8
8
|
|
9
9
|
def test_adding_updates_threshold_key
|
10
10
|
topn = TopN.new
|
11
|
-
|
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
|
-
|
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(
|
23
|
-
|
24
|
-
|
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(
|
30
|
-
|
31
|
-
|
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(
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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(
|
46
|
-
|
45
|
+
topn = TopN.new(nil, maxkeys: 10)
|
46
|
+
topn[5] = 5
|
47
47
|
assert_equal 5, topn.threshold_key
|
48
|
-
|
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(
|
54
|
-
|
53
|
+
topn = TopN.new(nil, maxkeys: 10)
|
54
|
+
topn[10] = 10
|
55
55
|
assert_equal 10, topn.threshold_key
|
56
|
-
|
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(
|
62
|
-
|
63
|
-
|
64
|
-
assert_equal [1, 2], topn
|
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(
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
74
|
-
assert_equal [2], topn
|
75
|
-
assert_equal [3], topn
|
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(
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
85
|
-
assert_equal [2], topn
|
86
|
-
assert_equal [3], topn
|
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(
|
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
|
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]
|
data/test/test_creation.rb
CHANGED
@@ -6,48 +6,53 @@ class TestTopN < Minitest::Test
|
|
6
6
|
assert topn
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
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.
|
16
|
+
assert topn.maxkeys > 0
|
12
17
|
end
|
13
18
|
|
14
|
-
def
|
15
|
-
topn = TopN.new(
|
16
|
-
assert topn.
|
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
|
35
|
+
def test_creation_raises_assertion_with_zero_maxkeys
|
31
36
|
assert_raises(ArgumentError) {
|
32
|
-
TopN.new(
|
37
|
+
TopN.new(nil, maxkeys: 0)
|
33
38
|
}
|
34
39
|
end
|
35
40
|
|
36
|
-
def
|
41
|
+
def test_creation_raises_assertion_with_negative_maxkeys
|
37
42
|
assert_raises(ArgumentError) {
|
38
|
-
TopN.new(
|
43
|
+
TopN.new(nil, maxkeys: -1)
|
39
44
|
}
|
40
45
|
end
|
41
46
|
|
42
|
-
def
|
47
|
+
def test_creation_raises_assertion_with_non_fixnum_maxkeys
|
43
48
|
assert_raises(ArgumentError) {
|
44
|
-
TopN.new(
|
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(
|
6
|
-
assert_equal({}, topn
|
5
|
+
topn = TopN.new(nil, maxkeys: 2)
|
6
|
+
assert_equal({}, topn)
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
topn = TopN.new(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
assert_equal [2, 3], topn.
|
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(
|
19
|
-
|
20
|
-
|
21
|
-
|
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