darkhelmet-darkext 0.11.2 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/LICENSE +21 -0
- data/README.md +45 -0
- data/Rakefile +45 -0
- data/TODO +4 -0
- data/VERSION.yml +2 -2
- data/darkext.gemspec +105 -0
- data/lib/darkext/array.rb +2 -2
- data/lib/darkext/beagle.rb +1 -2
- data/lib/darkext/hash.rb +10 -10
- data/lib/darkext/io.rb +1 -1
- data/lib/darkext/sinatra.rb +1 -3
- data/lib/darkext/statistics.rb +1 -1
- data/spec/array_spec.rb +52 -3
- data/spec/boolean_spec.rb +22 -0
- data/spec/fiber_spec.rb +11 -1
- data/spec/float_spec.rb +7 -0
- data/spec/hash_spec.rb +18 -3
- data/spec/integer_spec.rb +7 -0
- data/spec/io_spec.rb +15 -1
- data/spec/numeric_spec.rb +35 -0
- data/spec/object_spec.rb +12 -0
- data/spec/spec_helper.rb +4 -1
- data/spec/statistics_spec.rb +110 -0
- data/spec/string_spec.rb +25 -0
- data/spec/symbol_spec.rb +5 -0
- metadata +13 -32
- data/README.rdoc +0 -66
- data/test/array_test.rb +0 -63
- data/test/boolean_test.rb +0 -19
- data/test/fiber_test.rb +0 -15
- data/test/float_test.rb +0 -10
- data/test/hash_test.rb +0 -29
- data/test/helper.rb +0 -5
- data/test/integer_test.rb +0 -10
- data/test/io_test.rb +0 -21
- data/test/numeric_test.rb +0 -39
- data/test/object_test.rb +0 -22
- data/test/statistics_test.rb +0 -109
- data/test/string_test.rb +0 -36
- data/test/symbol_test.rb +0 -8
data/test/array_test.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class ArrayTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@a = [1,2,3,4,5]
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_rotate
|
9
|
-
@a.rotate
|
10
|
-
assert_equal(@a, [2,3,4,5,1])
|
11
|
-
@a.rotate
|
12
|
-
assert_equal(@a, [3,4,5,1,2])
|
13
|
-
@a.rotate(@a.size)
|
14
|
-
assert_equal(@a, [3,4,5,1,2])
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_rotate_reverse
|
18
|
-
@a.rotate_reverse
|
19
|
-
assert_equal(@a, [5,1,2,3,4])
|
20
|
-
@a.rotate_reverse
|
21
|
-
assert_equal(@a, [4,5,1,2,3])
|
22
|
-
@a.rotate_reverse(@a.size)
|
23
|
-
assert_equal(@a, [4,5,1,2,3])
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_sum
|
27
|
-
assert_equal(@a.sum, 15)
|
28
|
-
assert_equal([1].sum, 1)
|
29
|
-
assert_equal(['a','b','c'].sum, 'abc')
|
30
|
-
assert_equal([1.0,0.1,0.01].sum, 1.11)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_product
|
34
|
-
assert_equal(@a.product, 120)
|
35
|
-
assert_equal([1].product, 1)
|
36
|
-
assert_equal(['a',3].product, 'aaa')
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_squares
|
40
|
-
assert_equal(@a.squares, [1,4,9,16,25])
|
41
|
-
assert_equal([1].squares, [1])
|
42
|
-
@a.squares!
|
43
|
-
assert_equal(@a, [1,4,9,16,25])
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_randomize
|
47
|
-
assert_not_equal(@a.randomize, @a)
|
48
|
-
a = @a.clone
|
49
|
-
@a.randomize!
|
50
|
-
assert_not_equal(a, @a)
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_random
|
54
|
-
assert(@a.include?(@a.random))
|
55
|
-
left = Array.new
|
56
|
-
right = Array.new
|
57
|
-
10.times do
|
58
|
-
left << @a.random
|
59
|
-
right << @a.pick
|
60
|
-
end
|
61
|
-
assert_not_equal(left, right)
|
62
|
-
end
|
63
|
-
end
|
data/test/boolean_test.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class BooleanTest < Test::Unit::TestCase
|
4
|
-
def test_is_boolean
|
5
|
-
assert(true.is_boolean?)
|
6
|
-
assert(false.is_boolean?)
|
7
|
-
assert_equal(1.is_boolean?, false)
|
8
|
-
assert_equal('true'.is_boolean?, false)
|
9
|
-
assert_equal(:true.is_boolean?, false)
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_true_intern
|
13
|
-
assert_equal(true.intern, :true)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_false_intern
|
17
|
-
assert_equal(false.intern, :false)
|
18
|
-
end
|
19
|
-
end
|
data/test/fiber_test.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class FiberTest < Test::Unit::TestCase
|
4
|
-
def test_fiber
|
5
|
-
f = Fiber.new do
|
6
|
-
(1..10).each do |i|
|
7
|
-
Fiber.yield(i)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
assert_match(/.*Fiber:0x/,f.inspect)
|
11
|
-
a = Array.new
|
12
|
-
10.times { a << f.resume }
|
13
|
-
assert_equal(a, [1,2,3,4,5,6,7,8,9,10])
|
14
|
-
end
|
15
|
-
end
|
data/test/float_test.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class FloatTest < Test::Unit::TestCase
|
4
|
-
def test_equals
|
5
|
-
assert(1.00000000000000001.equals?(1.00000000000000002))
|
6
|
-
assert(!1.1.equals?(1.2))
|
7
|
-
assert(1.1.equals?(1.2,0.1))
|
8
|
-
assert(1.2.equals?(1.1,0.1))
|
9
|
-
end
|
10
|
-
end
|
data/test/hash_test.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class HashTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@h = { :foo => 1, :bar => 2, :baz => 3 }
|
6
|
-
@nh = { :foo => { :bar => 2 }, :baz => { :biz => 4 } }
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_with_defaults
|
10
|
-
assert_equal(@h.with_defaults(:biz => 4), { :foo => 1, :bar => 2, :baz => 3, :biz => 4 })
|
11
|
-
assert_equal(@h.with_defaults(:foo => 5), { :foo => 1, :bar => 2, :baz => 3 })
|
12
|
-
@h.with_defaults!(:biz => 4)
|
13
|
-
assert_equal(@h, { :foo => 1, :bar => 2, :baz => 3, :biz => 4 })
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_nested_find
|
17
|
-
assert_equal(@nh.nested_find(:baz,:biz), 4)
|
18
|
-
assert_equal(@nh.nested_find(:foo,:bar), 2)
|
19
|
-
assert_nil(@nh.nested_find(:foo,:biz))
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_deep_merge
|
23
|
-
a = { :foo => 1, :bar => { :baz => 10, :biz => { :hello => :world }}}
|
24
|
-
b = { :pickles => true, :sandwich => { :ham => 2, :bread => { :grains => :whole }}}
|
25
|
-
result = { :foo => 1, :pickles => true, :sandwich => { :ham => 2, :bread => { :grains => :whole }}, :bar => { :baz => 10, :biz => { :hello => :world }}}
|
26
|
-
a.deep_merge!(b)
|
27
|
-
assert_equal(a,result)
|
28
|
-
end
|
29
|
-
end
|
data/test/helper.rb
DELETED
data/test/integer_test.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class IntegerTest < Test::Unit::TestCase
|
4
|
-
def test_fact
|
5
|
-
assert_equal(5.fact, 120)
|
6
|
-
assert_equal(1000.fact, 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
|
7
|
-
assert_equal(1.fact, 1)
|
8
|
-
assert_equal(0.fact, 1)
|
9
|
-
end
|
10
|
-
end
|
data/test/io_test.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class TestDarkIO < Test::Unit::TestCase
|
4
|
-
HW = 'Hello, World!'
|
5
|
-
def test_capture_output
|
6
|
-
out = DarkIO::capture_output do
|
7
|
-
HW.print
|
8
|
-
end
|
9
|
-
assert_equal(out, HW)
|
10
|
-
out = DarkIO::capture_output(:stderr => true) do
|
11
|
-
(HW + 'STDOUT').print
|
12
|
-
STDERR.print(HW + 'STDERR')
|
13
|
-
end
|
14
|
-
assert_equal(out.shift, HW + 'STDOUT')
|
15
|
-
assert_equal(out.shift, HW + 'STDERR')
|
16
|
-
out = DarkIO::capture_output(:stderr => true, :stdout => false) do
|
17
|
-
STDERR.print(HW)
|
18
|
-
end
|
19
|
-
assert_equal(out, HW)
|
20
|
-
end
|
21
|
-
end
|
data/test/numeric_test.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class NumericTest < Test::Unit::TestCase
|
4
|
-
def test_square
|
5
|
-
assert_equal(4.square, 16)
|
6
|
-
assert_equal(-4.square, 16)
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_cube
|
10
|
-
assert_equal(3.cube, 27)
|
11
|
-
assert_equal(-3.cube, -27)
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_sqrt
|
15
|
-
assert_equal(9.sqrt, 3)
|
16
|
-
assert_equal(81.sqrt, 9)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_root
|
20
|
-
assert_equal(9.sqrt, 9.root)
|
21
|
-
assert_equal(27.root(3), 3)
|
22
|
-
assert_equal(81.root(4), 3)
|
23
|
-
assert_equal(5.root(1), 5)
|
24
|
-
assert_equal(10.root(-1), 0.1)
|
25
|
-
assert_equal(100.root(-2), 0.1)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_ln
|
29
|
-
assert_equal(Math::E.ln, 1)
|
30
|
-
assert_in_delta(Math::PI.ln, 1.14473, 0.0001)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_log
|
34
|
-
assert_equal(10.log, 1)
|
35
|
-
assert_equal(100.log, 2)
|
36
|
-
assert_in_delta(Math::PI.log, 0.4971, 0.0001)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
data/test/object_test.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class Foo
|
4
|
-
def add(n)
|
5
|
-
42 + n
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
class ObjectTest < Test::Unit::TestCase
|
11
|
-
def test_try
|
12
|
-
assert_nil(nil.try { |me| me.foo })
|
13
|
-
assert(true.try { |me| me })
|
14
|
-
assert_not_nil(true.try(:nil?))
|
15
|
-
assert_equal(Foo.new.try(:add,10),52)
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_tap
|
19
|
-
f = Foo.new
|
20
|
-
assert_equal(f.tap { |me| assert_equal(me,f) }, f)
|
21
|
-
end
|
22
|
-
end
|
data/test/statistics_test.rb
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class StatisticsTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@hist = [1,2,1,1,3,2,1,1,3,3,3,3,3,2,2,1]
|
6
|
-
@a = [5,1,9,12,6,7,8,8,8,1,2,3,4]
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_mean
|
10
|
-
assert_equal([1,2,3,4,5,6].mean, 3.5)
|
11
|
-
assert_equal([1].mean, 1)
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_harmonic_mean
|
15
|
-
assert_in_delta([60,40].harmonic_mean, 48.to_f, 0.0001)
|
16
|
-
assert_in_delta([9].harmonic_mean, 9.to_f, 0.0001)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_geometric_mean
|
20
|
-
assert_equal([2,8].geometric_mean, 4)
|
21
|
-
assert_in_delta([9].geometric_mean, 9.to_f, 0.0001)
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_median
|
25
|
-
assert_equal([1,2,3].median, 2)
|
26
|
-
assert_equal([1].median, 1)
|
27
|
-
assert_equal([2,4,6,8].median, 5)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_histogram
|
31
|
-
assert_equal(@hist.histogram,{ 1 => 6, 2 => 4, 3 => 6 })
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_mode
|
35
|
-
assert_equal(@hist.mode, [1, 3])
|
36
|
-
assert_equal((@hist + [1]).mode, [1])
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_variance
|
40
|
-
assert_in_delta([1,2,3,4,5,6].variance, 35/12, 0.0001)
|
41
|
-
assert_in_delta([1].variance, 0.to_f, 0.0001)
|
42
|
-
assert_in_delta([1,1].variance, 0.to_f, 0.0001)
|
43
|
-
assert_in_delta(@a.variance, 10.5, 0.05)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_standard_deviation
|
47
|
-
assert_in_delta([2,4,4,4,5,5,7,9].stddev, 2, 0.0001)
|
48
|
-
assert_in_delta(@a.stddev, 3.24, 0.01)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_sample
|
52
|
-
assert_equal(@hist.sample(5).size, 5)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_ci
|
56
|
-
ci = @a.ci
|
57
|
-
assert_in_delta(ci.shift, 5.15, 0.01)
|
58
|
-
assert_in_delta(ci.shift, 6.24, 0.01)
|
59
|
-
assert_in_delta(@a.ci(:type => :upper).shift, 6.15, 0.01)
|
60
|
-
assert_in_delta(@a.ci(:type => :lower).shift, 5.24, 0.01)
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_standardize
|
64
|
-
assert_equal([2,2,4,4].standardize, [-1.0,-1.0, 1.0, 1.0])
|
65
|
-
assert_equal(@a.standardize.size, 13)
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_sum_of_squares
|
69
|
-
assert_equal([1,2,3].sum_of_squares, 2.0)
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_prob
|
73
|
-
assert_in_delta(Darkext::Statistics::prob(2), 0.96, 0.02)
|
74
|
-
assert_in_delta(Darkext::Statistics::prob(0.5), 0.69, 0.02)
|
75
|
-
assert_in_delta(Darkext::Statistics::prob(Darkext::Statistics::zscore(1)), 1, 0.0001)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_zscore
|
79
|
-
assert_in_delta(Darkext::Statistics::zscore(0.5), 0, 0.1)
|
80
|
-
assert_in_delta(Darkext::Statistics::prob(0.75), 0.77, 0.02)
|
81
|
-
assert_in_delta(Darkext::Statistics::zscore(Darkext::Statistics::prob(2.5)), 2.5, 0.0001)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_regession
|
85
|
-
xs = [0,1.2,2,2.9,4,5,6]
|
86
|
-
ys = [0.1,1,2.1,3,4.3,4.9,6]
|
87
|
-
results = Darkext::Statistics::Regression::least_squares(xs,ys)
|
88
|
-
assert_equal(results[:n],xs.size)
|
89
|
-
assert_in_delta(results[:b_1], 1, 0.1)
|
90
|
-
assert_in_delta(results[:b_0], 0, 0.1)
|
91
|
-
assert_equal(results[:predicted].size, xs.size)
|
92
|
-
results[:predicted].each_with_index do |pred,index|
|
93
|
-
assert_in_delta(pred, index, 0.3)
|
94
|
-
end
|
95
|
-
assert_equal(results[:residuals].size, xs.size)
|
96
|
-
results[:residuals].each_with_index do |pred,index|
|
97
|
-
assert_in_delta(pred, 0, 0.3)
|
98
|
-
end
|
99
|
-
assert_in_delta(results[:ss_e], 0.15, 0.01)
|
100
|
-
assert_in_delta(results[:ss_t], 27.49, 0.01)
|
101
|
-
assert_in_delta(results[:estimator], 0.03, 0.01)
|
102
|
-
assert_in_delta(results[:r_2], 1, 0.01)
|
103
|
-
assert_in_delta(results[:r], 1, 0.01)
|
104
|
-
eqn = results[:equation]
|
105
|
-
(0..100).each do |i|
|
106
|
-
assert_in_delta(eqn.call(i), i, 0.25)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
data/test/string_test.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class StringTest < Test::Unit::TestCase
|
4
|
-
def test_to_range
|
5
|
-
assert_equal('1..10'.to_range, 1..10)
|
6
|
-
assert_equal('1...10'.to_range, 1...10)
|
7
|
-
assert_equal('a..z'.to_range, 'a'..'z')
|
8
|
-
assert_equal('a...z'.to_range, 'a'...'z')
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_exec
|
12
|
-
assert_equal('whoami'.exec(:capture => true).chomp, ENV['USER'])
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_true
|
16
|
-
assert('true'.true?)
|
17
|
-
assert('TRUE'.true?)
|
18
|
-
assert('True'.true?)
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_false
|
22
|
-
assert('false'.false?)
|
23
|
-
assert('FALSE'.false?)
|
24
|
-
assert('False'.false?)
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_starts_with
|
28
|
-
assert('foobar'.starts_with?('foo'))
|
29
|
-
assert(!'foobar'.starts_with?('bar'))
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_ends_with
|
33
|
-
assert('foobar'.ends_with?('bar'))
|
34
|
-
assert(!'foobar'.ends_with?('foo'))
|
35
|
-
end
|
36
|
-
end
|