@@ -4,67 +4,70 @@ class TestArrayStats < Test::Unit::TestCase
4
4
context "median" do
5
5
should "return the middle of the set if array length is odd" do
6
6
assert_equal 15 , [ 1 , 2 , 15 , 22 , 38 ] . median
7
+ assert_equal 15 , [ 1 , 2 , 15 , 22 , 38 ] . fast_median
7
8
end
8
-
9
+
9
10
should "return the average of the middle of the set if array length is even" do
10
11
assert_equal 10 , [ 1 , 6 , 14 , 22 ] . median
12
+ assert_equal 10 , [ 1 , 6 , 14 , 22 ] . fast_median
11
13
end
12
-
14
+
13
15
should "return nil if the array is empty" do
14
16
assert_nil [ ] . median
17
+ assert_nil [ ] . fast_median
15
18
end
16
-
19
+
17
20
should "sort an array before deriving the median" do
18
21
assert_equal 20 , [ 1 , 20 , 50 , 60 , 10 ] . median
22
+ assert_equal 20.0 , [ 1 , 20 , 50 , 60 , 10 ] . fast_median
19
23
end
20
24
end
21
-
22
- context "percentile" do
25
+
26
+ context "percentile" do
23
27
should "choose the number at a particular rank if array divides cleanly" do
24
28
assert_equal 36 , [ 12 , 14 , 16 , 18 , 20 , 22 , 24 , 26 , 28 , 30 , 32 , 34 , 36 , 38 , 40 , 42 , 44 , 46 , 48 ] . percentile ( 65 )
29
+ assert_equal 36 , [ 12 , 14 , 16 , 18 , 20 , 22 , 24 , 26 , 28 , 30 , 32 , 34 , 36 , 38 , 40 , 42 , 44 , 46 , 48 ] . fast_percentile ( 65 )
25
30
end
26
-
31
+
27
32
should "interpolate according to algorithm if array does not divide cleanly" do
28
33
assert_equal 5.5 , [ 3 , 5 , 7 , 8 , 9 , 11 , 13 , 15 ] . percentile ( 25 )
34
+ assert_equal 5.5 , [ 3 , 5 , 7 , 8 , 9 , 11 , 13 , 15 ] . fast_percentile ( 25 )
29
35
assert_equal 95.1981 , [ 95.1772 , 95.1567 , 95.1937 , 95.1959 , 95.1442 , 95.0610 , 95.1591 , 95.1195 , 95.1065 , 95.0925 , 95.1990 , 95.1682 ] . percentile ( 90 ) . round_to ( 0.0001 )
36
+ assert_equal 95.1981 , [ 95.1772 , 95.1567 , 95.1937 , 95.1959 , 95.1442 , 95.0610 , 95.1591 , 95.1195 , 95.1065 , 95.0925 , 95.1990 , 95.1682 ] . fast_percentile ( 90 ) . round_to ( 0.0001 )
30
37
end
31
-
38
+
32
39
should "return nil if the array is empty" do
33
40
assert_nil [ ] . percentile ( 30 )
41
+ assert_nil [ ] . fast_percentile ( 30 )
34
42
end
35
43
end
36
-
44
+
37
45
context "total_sum" do
38
46
should "return the sum of all elements in the array" do
39
47
assert_equal 12 , [ 2 , 4 , 6 ] . total_sum
40
48
end
41
-
49
+
42
50
should "return an integer if all elements are ints" do
43
51
assert_kind_of Integer , [ 2 , 4 , 6 ] . total_sum
44
52
end
45
-
53
+
46
54
should "return a float if at least some of the elements are floats" do
47
55
assert_kind_of Float , [ 2.5 , 3.5 , 6 ] . total_sum
48
56
end
49
-
57
+
50
58
should "return 0 if the array is empty" do
51
59
assert_equal 0 , [ ] . total_sum
52
60
end
53
61
end
54
-
62
+
55
63
context "mean" do
56
64
should "return the mean for the array " do
57
65
assert_equal 7 , [ 2 , 4 , 6 , 8 , 10 , 12 ] . mean
58
66
assert_equal 25 , [ 48 , 29 , 26 , 19 , 3 ] . mean
59
67
end
60
-
68
+
61
69
should "return nil if the array is empty" do
62
70
assert_nil [ ] . mean
63
71
end
64
72
end
65
-
66
-
67
-
68
73
end
69
-
70
-
0 commit comments