This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
require 'facets/array/arrange'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :rangify do
|
||||
test 'Array of consecutive integers should return an array made up of a single range.' do
|
||||
[1,2,3,4,5].rangify.assert == [1..5]
|
||||
end
|
||||
|
||||
test 'Array of non-consecutive integers should return the original array.' do
|
||||
[1,3,5,7].rangify.assert == [1,3,5,7]
|
||||
end
|
||||
|
||||
test 'Array of ranges should return the correct ranges.' do
|
||||
arr = [40..45, 1..3, 4..10, 20..30, 24..28, 42..50, 1..6, 1..3, 1..1]
|
||||
arr.rangify.assert == [1..10, 20..30, 40..50]
|
||||
end
|
||||
|
||||
test 'Array of ranges and integers should return the correct ranges.' do
|
||||
arr = [99, 100, 1..3, 101, 4..5, 103, 10..19, 99, 20..20, 31, 32..33, 98, 97]
|
||||
arr.rangify.assert == [1..5, 10..20, 31..33, 97..101, 103]
|
||||
end
|
||||
|
||||
test 'Array of non-consecutive integers should return the correct ranges.' do
|
||||
[1,2,3,6,7,8,10,15].rangify.assert == [1..3, 6..8, 10, 15]
|
||||
end
|
||||
|
||||
test 'Element order should not affect the result.' do
|
||||
[8, 1, 15, 2, 6, 3, 7, 10].rangify.assert == [1..3, 6..8, 10, 15]
|
||||
end
|
||||
|
||||
test 'Duplicate elements should not affect the result.' do
|
||||
[8, 1, 15, 2, 6, 3, 7, 10, 8, 15, 2, 3, 1, 2].rangify.assert == [1..3, 6..8, 10, 15]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
require 'facets/array/average'
|
||||
|
||||
test_case Array do
|
||||
method :average do
|
||||
test 'Empty array returns nil' do
|
||||
[].average.assert == nil
|
||||
end
|
||||
|
||||
test 'Calculate average of array of numerical values' do
|
||||
[1, 2, 3].average.assert == 2
|
||||
[96, 35, 72, 30, 75, 33, 68, 13, 49, 71].average.assert == 54.2
|
||||
[36, -67, -17, 85, -46, -64, -23, -13, 89, -47].average.assert == -6.7
|
||||
[60.7829, 31.2622, 20.626, 78.8907, 61.5328].average.assert == 50.61892
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,48 @@
|
||||
covers 'facets/array/before'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :before do
|
||||
|
||||
setup do
|
||||
@sequence = ['a', 'b', 'c']
|
||||
end
|
||||
|
||||
test "returns the previous element" do
|
||||
@sequence.before('b').assert == 'a'
|
||||
@sequence.before('c').assert == 'b'
|
||||
end
|
||||
|
||||
test "loops around from back to front" do
|
||||
@sequence.before('a').assert == 'c'
|
||||
end
|
||||
|
||||
test "non-member elements return nil" do
|
||||
@sequence.before('d').assert == nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :after do
|
||||
|
||||
setup do
|
||||
@sequence = ['a', 'b', 'c']
|
||||
end
|
||||
|
||||
test "returns the following element" do
|
||||
@sequence.after('a').assert == 'b'
|
||||
@sequence.after('b').assert == 'c'
|
||||
end
|
||||
|
||||
test "loops around from back to front" do
|
||||
@sequence.after('c').assert == 'a'
|
||||
end
|
||||
|
||||
test "non-member elements return nil" do
|
||||
@sequence.after('d').assert == nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
covers 'facets/array/collapse'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :collapse do
|
||||
|
||||
test do
|
||||
[1, nil, [1, 2], nil, [[3]]].collapse.assert == [1, 1, 2, 3]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :collapse! do
|
||||
|
||||
test do
|
||||
a = [1, nil, [1, 2], nil, [[3]]]
|
||||
a.collapse!
|
||||
a.assert == [1, 1, 2, 3]
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
covers 'facets/array/commonality'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :commonality do
|
||||
|
||||
test do
|
||||
a = [1,2,2,3,3,3]
|
||||
e = { 2 => [2,2], 3 => [3,3,3] }
|
||||
r = a.commonality
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
a = [1,2,2,3,3,3]
|
||||
e = {false=>[1, 2, 2], true=>[3, 3, 3]}
|
||||
r = a.commonality{ |x| x > 2 }
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :collisions do
|
||||
|
||||
test "alias for commonality" do
|
||||
a = [1,2,2,3,3,3]
|
||||
e = { 2 => [2,2], 3 => [3,3,3] }
|
||||
r = a.collisions
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
covers 'facets/array/conjoin'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :conjoin do
|
||||
|
||||
setup "any array" do
|
||||
@a = [1,2,3,4]
|
||||
end
|
||||
|
||||
test "like join but spaced" do
|
||||
@a.conjoin.assert == "1 2 3 4"
|
||||
end
|
||||
|
||||
test "comma+space and 'and' on tail" do
|
||||
@a.conjoin(', ', ' and ').assert = "1, 2, 3 and 4"
|
||||
end
|
||||
|
||||
test "comma+space and 'or' on tail using :last option" do
|
||||
@a.conjoin(', ', :last => ' or ').assert == "1, 2, 3 or 4"
|
||||
end
|
||||
|
||||
test "semicolon+space and ampersand on tail using index" do
|
||||
@a.conjoin('; ', -1 => ' & ').assert == "1; 2; 3 & 4"
|
||||
end
|
||||
|
||||
test "with block to determine separator" do
|
||||
r = @a.conjoin{ |i, a, b| i % 2 == 0 ? '.' : '-' }
|
||||
r.assert == "1.2-3.4"
|
||||
end
|
||||
|
||||
test "very esoteric transformation" do
|
||||
r = @a.conjoin{ |i, x, y| "<#{i} #{x} #{y}>" }
|
||||
r.assert == "1<0 1 2>2<1 2 3>3<2 3 4>4"
|
||||
end
|
||||
|
||||
test "with :space option" do
|
||||
r = @a.conjoin(',', '&', :space=>2)
|
||||
r.assert == "1 , 2 , 3 & 4"
|
||||
end
|
||||
|
||||
test ":spacer option can set an alternate spacing string" do
|
||||
r = @a.conjoin('|', '>', :space=>2, :spacer=>'-')
|
||||
r.assert == "1--|--2--|--3-->--4"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
covers 'facets/array/contains'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :contains? do
|
||||
|
||||
test do
|
||||
[1,2,3,4].assert.contains?(2)
|
||||
[1,2,3,4].refute.contains?(9)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
covers 'facets/array/delete'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :delete_unless do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.delete_unless{ |e| e == 2 }
|
||||
a.assert == [2]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :delete_values do
|
||||
|
||||
test do
|
||||
a = [1,2,3,4]
|
||||
a.delete_values(1,2).assert == [1,2]
|
||||
a.assert == [3,4]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :delete_values_at do
|
||||
|
||||
test do
|
||||
a = [1,2,3,4]
|
||||
a.delete_values_at(1,2).assert == [2,3]
|
||||
a.assert == [1,4]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3,4]
|
||||
a.delete_values_at(0..2).assert == [1,2,3]
|
||||
a.assert == [4]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/array/delete_unless'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :delete_unless do
|
||||
|
||||
test 'with an explicit block' do
|
||||
[1,2,3].delete_unless{ |x| x < 2 }.assert == [1]
|
||||
end
|
||||
|
||||
test 'with an implicit block' do
|
||||
[1,2,3].delete_unless(&:even?).assert == [2]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/array/delete_values'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :delete_values do
|
||||
|
||||
test do
|
||||
a = [1,2,3,4]
|
||||
a.delete_values(1,2).assert == [1,2]
|
||||
a.assert == [3,4]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :delete_values_at do
|
||||
|
||||
test do
|
||||
a = [1,2,3,4]
|
||||
a.delete_values_at(1,2).assert == [2,3]
|
||||
a.assert == [1,4]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3,4]
|
||||
a.delete_values_at(0..2).assert == [1,2,3]
|
||||
a.assert == [4]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
covers 'facets/array/divide'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :divide do
|
||||
|
||||
test do
|
||||
r = ['a1','b1','a2','b2'].divide(/^a/)
|
||||
r.assert == [['a1','b1'],['a2','b2']]
|
||||
end
|
||||
|
||||
test do
|
||||
r = ['a1','b1','a2','b2'].divide(/^b/)
|
||||
r.assert == [['a1'],['b1', 'a2'],['b2']]
|
||||
end
|
||||
|
||||
test do
|
||||
r = ['a1','b1','a2','b2'].divide(/c/)
|
||||
r.assert = [['a1','b1','a2','b2']]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
covers 'facets/array/duplicates'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :duplicates do
|
||||
|
||||
test do
|
||||
a = [1,1,2,2,3,4,5]
|
||||
a.duplicates.sort.assert == [1,2]
|
||||
end
|
||||
|
||||
test "with minimum" do
|
||||
a = [0,1,2,3,4,5,
|
||||
0,1,2,3,4,
|
||||
0,1,2,3,
|
||||
0,1,2,
|
||||
0,1,
|
||||
0]
|
||||
(1..6).each do |m|
|
||||
d = a.duplicates(m).sort
|
||||
e = [*0..6-m]
|
||||
d.assert == e
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/array/each_overlap'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :each_overlap do
|
||||
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
r = []
|
||||
a.each_overlap(2,1) { |x| r << x }
|
||||
r.assert == [[1,2],[2,3],[3,4],[4,5]]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3,4,5,6,7]
|
||||
r = []
|
||||
a.each_overlap(3,1) { |x| r << x }
|
||||
r.assert == [[1,2,3],[3,4,5],[5,6,7]]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
r = []
|
||||
a.each_overlap(3,2) { |x| r << x }
|
||||
r.assert == [[1,2,3],[2,3,4],[3,4,5]]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/array/each_pair'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :each_pair do
|
||||
|
||||
test do
|
||||
result = {}
|
||||
a = [:a,:b,:c,:d]
|
||||
a.each_pair{|i,e| result[e] = i + 1}
|
||||
result.assert == {a: 1, b: 2, c: 3, d: 4}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/array/each_value'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :each_value do
|
||||
|
||||
test do
|
||||
a = [ "a", "b", "c" ]
|
||||
out = ''
|
||||
a.each_value {|x| out += "#{x} -- " }
|
||||
out.assert == 'a -- b -- c -- '
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
covers 'facets/array/entropy'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :entropy do
|
||||
|
||||
test do
|
||||
%w{ a b }.entropy.assert == 1.0
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :ideal_entropy do
|
||||
|
||||
test do
|
||||
%w{ a b }.ideal_entropy.assert == 1.0
|
||||
%w{ a a b b }.ideal_entropy.assert == 2.0
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
covers 'facets/array/extract_options'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :extract_options! do
|
||||
|
||||
test do
|
||||
h = { :x=>1, :y=>1 }
|
||||
a = [:a, :b, h]
|
||||
a.extract_options!.assert == h
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test_case Hash do
|
||||
|
||||
method :extractable_options? do
|
||||
|
||||
test do
|
||||
h = { :x=>1, :y=>1 }
|
||||
h.assert.extractable_options?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/array/indexable'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :first! do
|
||||
|
||||
test "alias of shift" do
|
||||
a = [1,2,3]
|
||||
r = a.first!
|
||||
r.assert == 1
|
||||
a.assert == [2,3]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
covers 'facets/array/from'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :from do
|
||||
|
||||
test do
|
||||
%w{W o r l d}.from(3).assert == ['l', 'd']
|
||||
%w{W o r l d}.from(8).assert == []
|
||||
end
|
||||
end
|
||||
|
||||
method :thru do
|
||||
|
||||
test do
|
||||
|
||||
[1, 2, 3, 4, 5].thru(2).assert == [1, 2, 3]
|
||||
[1, 2, 3, 4, 5].thru(7).assert == [1, 2, 3, 4, 5]
|
||||
|
||||
end
|
||||
|
||||
test do
|
||||
|
||||
[1, 2, 3, 4, 5].thru(0, 2).assert == [1, 2, 3]
|
||||
[1, 2, 3, 4, 5].thru(2, 4).assert == [3, 4, 5]
|
||||
|
||||
end
|
||||
|
||||
test do
|
||||
|
||||
[1, 2, 3, 4, 5].thru(3, 7).assert == [4, 5]
|
||||
|
||||
end
|
||||
|
||||
test do
|
||||
|
||||
[1, 2, 3, 4, 5].thru(6, 7).assert = []
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
require 'facets/array/intersection'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :intersection do
|
||||
test { [1, 2].intersection.assert == nil }
|
||||
test { [1..10, 11..20].intersection.assert == nil }
|
||||
test { [1..10, 5..15, 11..20].intersection.assert == nil }
|
||||
test { [8..8, 1..10, 10 ].intersection.assert == nil }
|
||||
test { [10, 1..10].intersection.assert == 10 }
|
||||
test { [1..10, 1].intersection.assert == 1 }
|
||||
test { [1, 1].intersection.assert == 1 }
|
||||
test { [1..10, 5, 5..8, 5..10 ].intersection.assert == 5 }
|
||||
test { [1..10, 5..8, 5..10 ].intersection.assert == (5..8) }
|
||||
test { [5..10, 1..10, 4..8 ].intersection.assert == (5..8) }
|
||||
test { [5..10, 1..10, 4..20, 8].intersection.assert == 8 }
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/array/indexable'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :last! do
|
||||
|
||||
test "alias of pop" do
|
||||
a = [1,2,3]
|
||||
r = a.last!
|
||||
r.assert == 3
|
||||
a.assert == [1,2]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
covers 'facets/array/median'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :median do
|
||||
test "odd sized" do
|
||||
r = %w{e a b c d}.median
|
||||
r.assert == "c"
|
||||
end
|
||||
|
||||
test "even sized" do
|
||||
r = %w{f e a b c d}.median
|
||||
r.assert == "d"
|
||||
end
|
||||
|
||||
test "with positive offset" do
|
||||
r = %w{e a b c d}.median(1)
|
||||
r.assert == "d"
|
||||
end
|
||||
|
||||
test "with negative offset" do
|
||||
r = %w{e a b c d}.median(-1)
|
||||
r.assert == "b"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
covers 'facets/array/merge'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :merge do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
b = [3,4,5]
|
||||
a.merge(b).assert == [1,2,3,4,5]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :merge! do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
b = [3,4,5]
|
||||
a.merge!(b)
|
||||
a.assert == [1,2,3,4,5]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
require 'facets/array/missing'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :missing do
|
||||
test { [1,3..3].missing.assert == [2] }
|
||||
test { [1..5,10..12].missing.assert == [6..9] }
|
||||
test { [100, 9..11, 14, 1..5, 16, 10..12, 17..17].missing.assert == [6..8, 13, 15, 18..99] }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/array/mode'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :mode do
|
||||
|
||||
test do
|
||||
[1, 1, 2, 3].mode.assert == [1]
|
||||
end
|
||||
|
||||
test do
|
||||
[1, 1, 2, 2, 3].mode.sort.assert == [1,2]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
covers 'facets/array/nonuniq'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :nonuniq do
|
||||
|
||||
test do
|
||||
a = [1,1,2,2,3,4,5]
|
||||
a.nonuniq.sort.assert == [1,2]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :nonuniq! do
|
||||
|
||||
test do
|
||||
a = [1,1,2,2,3,4,5]
|
||||
a.nonuniq!
|
||||
a.sort.assert == [1,2]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/array/not_empty'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :not_empty? do
|
||||
|
||||
test do
|
||||
[].refute.not_empty?
|
||||
end
|
||||
|
||||
test do
|
||||
[1,2].assert.not_empty?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
covers 'facets/array/occur'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :occur do
|
||||
|
||||
test "set limit" do
|
||||
a = [1,2,3,4,5,
|
||||
2,3,4,5,
|
||||
3,4,5,
|
||||
4,5,
|
||||
5]
|
||||
(1..5).each do |m|
|
||||
d = a.occur(m)
|
||||
d.assert == [m]
|
||||
end
|
||||
end
|
||||
|
||||
test "range" do
|
||||
a = %w{a b c d e
|
||||
b c d e
|
||||
c d e
|
||||
d e
|
||||
e}
|
||||
a.occur(1..2).sort.assert == ['a','b']
|
||||
a.occur(1..3).sort.assert == ['a','b','c']
|
||||
a.occur(2..3).sort.assert == ['b','c']
|
||||
end
|
||||
|
||||
test do
|
||||
a = [:a,:b,:a]
|
||||
a.occur(1).assert == [:b]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [:a,:b,:a]
|
||||
a.occur(2).assert == [:a]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [:a,:b,:a]
|
||||
a.occur(1..1).assert == [:b]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [:a,:b,:a]
|
||||
a.occur{ |n| n % 2 == 0 }.assert == [:a]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
covers 'facets/array/occurrence'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :occurrence do
|
||||
|
||||
test "no arguments" do
|
||||
r = [:a,:a,:b,:c,:c,:c].occurrence
|
||||
r.assert == { :a => 2, :b => 1, :c => 3 }
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
r = [2,2,3,4,4,4].occurrence{|i| i % 2}
|
||||
r.assert == { 0 => 5, 1 => 1 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/array/only.rb'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :only do
|
||||
|
||||
test do
|
||||
[5].only.assert == 5
|
||||
[nil].only.assert == nil
|
||||
expect(IndexError){ [].only }
|
||||
expect(IndexError){ [1,2,3].only }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
covers 'facets/array/op_pow'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :** do
|
||||
|
||||
test do
|
||||
([1,2] ** [3,4]).assert == [[1, 3], [1, 4], [2, 3], [2, 4]]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
covers 'facets/array/pad'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :pad do
|
||||
|
||||
test do
|
||||
r = [0,1,2,3].pad(7,"x")
|
||||
x = [0,1,2,3,"x","x","x"]
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
test "negative index" do
|
||||
r = [0,1,2,3].pad(-7,"n")
|
||||
x = ["n","n","n",0,1,2,3]
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :pad! do
|
||||
|
||||
test do
|
||||
a = [0,1,2,3]
|
||||
r = a.pad!(6,"y")
|
||||
x = [0,1,2,3,"y","y"]
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
test "negative index" do
|
||||
a = [0,1,2,3]
|
||||
r = a.pad!(-6,"q")
|
||||
x = ["q","q",0,1,2,3]
|
||||
r.assert == x
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
covers 'facets/array/pull'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :peek do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.peek.assert == 3
|
||||
a.assert == [1,2,3]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.peek(1).assert == 2
|
||||
a.assert == [1,2,3]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.peek(-1).assert == 1
|
||||
a.assert == [1,2,3]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
covers 'facets/array/pull'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :poke do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.poke(9)
|
||||
a.assert == [9,1,2,3]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [2,3]
|
||||
a.poke(1).assert == [1,2,3]
|
||||
a.poke(4).assert == [4,1,2,3]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
covers 'facets/array/probability'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :probability do
|
||||
|
||||
test do
|
||||
%w{a b}.probability.assert == {'a'=>0.5, 'b'=>0.5}
|
||||
end
|
||||
|
||||
test do
|
||||
%w{a a a b}.probability.assert == {'a'=>0.75, 'b'=>0.25}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
covers 'facets/array/pull'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :pull do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.pull.assert == 1
|
||||
a.assert == [2,3]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
covers 'facets/array/recurse'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :recurse do
|
||||
|
||||
test do
|
||||
a = ["a", ["b", "c", nil], nil]
|
||||
r = a.recurse{|a| a.compact! }
|
||||
r.assert ["a", ["b", "c"]]
|
||||
end
|
||||
|
||||
test "with Hashes" do
|
||||
require 'facets/hash/recurse'
|
||||
objects = []
|
||||
struct = [1, 2, {:a => 3, :b => [4, 5, {:c=>6}]}, [7, 8]]
|
||||
struct.recurse(Array, Hash){|o| objects << o; o }
|
||||
|
||||
objects.assert.include?( [7, 8] )
|
||||
objects.assert.include?( {:c=>6} )
|
||||
objects.assert.include?( [4, 5, {:c=>6}] )
|
||||
objects.assert.include?( {:a => 3, :b => [4, 5, {:c=>6}]} )
|
||||
objects.assert.include?( struct )
|
||||
|
||||
objects.length.assert == 5
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :recurse! do
|
||||
|
||||
test do
|
||||
a = ["a", ["b", "c", nil], nil]
|
||||
a.recurse!{|a| a.compact! }
|
||||
a.assert ["a", ["b", "c"]]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,33 @@
|
||||
covers 'facets/array/recursively'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :recursively do
|
||||
|
||||
test "each" do
|
||||
r = []
|
||||
[1,2,['a','b']].recursively{ |v| v }.each{ |v| r << v }
|
||||
r.assert == [1,2,'a','b']
|
||||
end
|
||||
|
||||
test "map" do
|
||||
a = [1,2,['a','b']]
|
||||
r = a.recursively{ |v| v.reverse }.map{ |v| v.succ }
|
||||
r.assert == [2,3,['c','b']]
|
||||
end
|
||||
|
||||
test "map!" do
|
||||
a = [1,2,['a','b']]
|
||||
a.recursively{ |v| v }.map!{ |v| v.succ }
|
||||
a.assert == [2,3,['b','c']]
|
||||
end
|
||||
|
||||
test "with block" do
|
||||
a = [1,2,['a','b', nil], nil]
|
||||
r = a.recursively{ |v| v.compact }.map{ |v| v }.compact
|
||||
r.assert == [1,2,['a','b']]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/array/reject_values'
|
||||
|
||||
testcase Array do
|
||||
|
||||
method :reject_values do
|
||||
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
r = a.reject_values(2,4)
|
||||
r.assert == [1,3,5]
|
||||
a.assert = [1,2,3,4,5] # hasn't changed
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
covers 'facets/array/splice'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :splice do
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.splice(1).assert == 2
|
||||
a.assert == [1,3]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3]
|
||||
a.splice(1,4).assert == 4
|
||||
a.assert == [1,4,3]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/array/split'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :split do
|
||||
|
||||
test do
|
||||
['a','b','c'].split('b').assert == [['a'], ['c']]
|
||||
end
|
||||
|
||||
test "empty" do
|
||||
[].split('a').assert == []
|
||||
end
|
||||
|
||||
test "same" do
|
||||
['a'].split('a').assert == []
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/array/squeeze'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :squeeze! do
|
||||
|
||||
test 'default' do
|
||||
a = [1,2,2,3,3,2,1]
|
||||
a.squeeze!
|
||||
a.assert == [1,2,3,2,1]
|
||||
end
|
||||
|
||||
test 'with argument' do
|
||||
a = [1,2,2,3,3,2,1]
|
||||
a.squeeze!(*[3])
|
||||
a.assert == [1,2,2,3,2,1]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
require 'facets/array/standard_deviation'
|
||||
|
||||
test_case Array do
|
||||
method :standard_deviation do
|
||||
test 'Empty array returns nil' do
|
||||
[].standard_deviation.assert == nil
|
||||
end
|
||||
|
||||
test 'Calculate standard_deviation of array of numerical values' do
|
||||
[1, 2, 3].standard_deviation.assert == 0.816496580927726
|
||||
[96, 35, 72, 30, 75, 33, 68, 13, 49, 71].standard_deviation.assert == 24.69331893448104
|
||||
[36, -67, -17, 85, -46, -64, -23, -13, 89, -47].standard_deviation.assert == 54.67183918618432
|
||||
[60.7829, 31.2622, 20.626, 78.8907, 61.5328].standard_deviation.assert == 21.428815505053002
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
covers 'facets/array/step.rb'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :step do
|
||||
|
||||
test "iteration" do
|
||||
a = [:a, :b, :c, :d]
|
||||
r = []
|
||||
|
||||
a.step(2){ |e| r << e }
|
||||
|
||||
r.assert == [:b, :d]
|
||||
end
|
||||
|
||||
test "conversion to array" do
|
||||
a = [:a, :b, :c, :d]
|
||||
a.step(1).to_a.assert == [:a, :b, :c, :d]
|
||||
a.step(2).to_a.assert == [:b, :d]
|
||||
a.step(3).to_a.assert == [:c]
|
||||
a.step(5).to_a.assert == []
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/array/store'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :store do
|
||||
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
a.store(0, :A)
|
||||
a.assert == [:A,2,3,4,5]
|
||||
end
|
||||
|
||||
test do
|
||||
a = [1,2,3,4,5]
|
||||
a.store(-1, :Z)
|
||||
a.assert == [1,2,3,4,:Z]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
covers 'facets/array/to_h'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :to_h do
|
||||
|
||||
test do
|
||||
arr = [[:a, 1], [:b, 2]]
|
||||
arr.to_h.assert == {:a => 1, :b => 2}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
covers 'facets/array/traverse'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :traverse do
|
||||
|
||||
test do
|
||||
a = ["A", "B", ["X", "Y"]]
|
||||
g = a.traverse{ |e| e.downcase }
|
||||
g.assert == ["a", "b", ["x", "y"]]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
method :traverse! do
|
||||
|
||||
test do
|
||||
a = ["A", "B", ["X", "Y"]]
|
||||
a.traverse!{ |e| e.downcase }
|
||||
a.assert == ["a", "b", ["x", "y"]]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
covers 'facets/array/uniq_by'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :uniq_by! do
|
||||
|
||||
test do
|
||||
e = [-5, -4, -3, -2, -1, 0]
|
||||
r = (-5..5).to_a
|
||||
r.uniq_by!{ |i| i*i }
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
test do
|
||||
e = [5, 4, 3, 2, 1, 0]
|
||||
r = (-5..5).to_a.reverse
|
||||
r.uniq_by!{ |i| i*i }
|
||||
r.assert == e
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
covers 'facets/array/unique_permutation'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :unique_permutation do
|
||||
|
||||
test 'example' do
|
||||
[1,1,2,2,3].unique_permutation(2).to_a.assert == [
|
||||
[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2]
|
||||
]
|
||||
end
|
||||
|
||||
test 'not eq permutation.to_a' do
|
||||
[1,1,2,3].unique_permutation.to_a.assert != [1,1,2,3].permutation.to_a
|
||||
end
|
||||
|
||||
test 'eq permutation.to_a.uniq' do
|
||||
[1,1,2,3].unique_permutation.to_a.assert == [1,1,2,3].permutation.to_a.uniq
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
require 'facets/array/variance'
|
||||
|
||||
test_case Array do
|
||||
method :variance do
|
||||
test 'Empty array returns nil' do
|
||||
[].variance.assert == nil
|
||||
end
|
||||
|
||||
test 'Calculate variance of array of numerical values' do
|
||||
[1, 2, 3].variance.assert == 0.6666666666666666
|
||||
[96, 35, 72, 30, 75, 33, 68, 13, 49, 71].variance.assert == 609.76
|
||||
[36, -67, -17, 85, -46, -64, -23, -13, 89, -47].variance.assert == 2989.0099999999993
|
||||
[60.7829, 31.2622, 20.626, 78.8907, 61.5328].variance.assert == 459.1941339495999
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
covers 'facets/array/zip'
|
||||
|
||||
test_case Array do
|
||||
|
||||
method :zip do
|
||||
|
||||
test 'example' do
|
||||
Array.zip([1,2], [3,4]).assert == [[1,3],[2,4]]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user