darkhelmet-darkext 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 11
3
- :patch: 0
4
2
  :major: 0
3
+ :minor: 11
4
+ :patch: 1
data/lib/darkext/hash.rb CHANGED
@@ -26,16 +26,13 @@ class Hash
26
26
  self.merge!(second, &merger)
27
27
  end
28
28
 
29
- def nested_hash(array)
30
- node = self
31
- array.each do |i|
32
- node[i]=Hash.new if node[i].nil?
33
- node = node[i]
29
+ def method_missing(m,*a)
30
+ if m.to_s =~ /=$/
31
+ self[$`] = a[0]
32
+ elsif a.empty?
33
+ self[m]
34
+ else
35
+ raise NoMethodError, "#{ m}"
34
36
  end
35
- self
36
- end
37
-
38
- def merge_nested_hash!(nested_hash)
39
- deep_merge!(nested_hash)
40
37
  end
41
38
  end
@@ -18,7 +18,8 @@ class Numeric
18
18
 
19
19
  # Do some other roots
20
20
  def root(n = 2)
21
- return self.sqrt if n == 2
21
+ return self if 1 == n
22
+ return self.sqrt if 2 == n
22
23
  self ** (1 / n.to_f)
23
24
  end
24
25
 
@@ -1,7 +1,7 @@
1
1
  class Object
2
2
  def try(*args, &block)
3
3
  return nil if self.nil?
4
- block.given? ? yield(self) : self.__send__(args.shift, *args)
4
+ block_given? ? yield(self) : self.__send__(args.shift, *args)
5
5
  end
6
6
 
7
7
  def tap
@@ -37,7 +37,7 @@ class Array
37
37
  end
38
38
 
39
39
  # Generates a histogram hash for the array
40
- def histogram(bins = nil)
40
+ def histogram
41
41
  self.sort.inject({}) do |a,x|
42
42
  a[x] = a[x].to_i + 1
43
43
  a
@@ -53,33 +53,17 @@ class Array
53
53
  end
54
54
 
55
55
  # Variance
56
- def population_variance
56
+ def variance
57
57
  raise ArgumentError.new('Array size must be > 0') if self.size.zero?
58
58
  self.sum_of_squares.to_f / (self.size).to_f
59
59
  end
60
60
 
61
- def sample_variance
62
- raise ArgumentError.new('Array size must be > 0') if self.size.zero?
63
- self.sum_of_squares.to_f / (self.size - 1).to_f
64
- end
65
-
66
61
  # Standard deviation
67
- def population_deviation
68
- raise ArgumentError.new('Array size must be > 0') if self.size.zero?
69
- self.population_variance.abs.sqrt
70
- end
71
-
72
- def sample_deviation
73
- raise ArgumentError.new('Array size must be > 0') if self.size.zero?
74
- self.sample_variance.abs.sqrt
75
- end
76
-
77
- def geometric_deviation
62
+ def standard_deviation
78
63
  raise ArgumentError.new('Array size must be > 0') if self.size.zero?
79
- gmean = self.g_mean
80
- Math.exp((self.map { |x| (x.ln - gmean.ln).square }.sum.to_f / self.size.to_f).sqrt)
64
+ self.variance.abs.sqrt
81
65
  end
82
- alias :gstddev :geometric_deviation
66
+ alias :stddev :standard_deviation
83
67
 
84
68
  # Randomly samples n elements
85
69
  def sample(n = 1)
@@ -117,7 +101,7 @@ class Array
117
101
  # Destructive standardize
118
102
  def standardize!
119
103
  m = self.mean.to_f
120
- rho = self.sample_deviation.to_f
104
+ rho = self.standard_deviation.to_f
121
105
  self.map! { |v| (v.to_f - m) / rho }
122
106
  end
123
107
 
@@ -126,17 +110,6 @@ class Array
126
110
  m = self.mean
127
111
  self.map { |v| v - m }.squares.sum
128
112
  end
129
-
130
- # Normalize the array
131
- def normalize
132
- self.clone.normalize!
133
- end
134
-
135
- # Normalize the array destructively
136
- def normalize!
137
- m = self.mean.to_f
138
- self.map! { |v| v / m }
139
- end
140
113
  end
141
114
 
142
115
  module Darkext
@@ -163,6 +136,8 @@ module Darkext
163
136
  end
164
137
 
165
138
  # Finds a two tail p-val for a high/low array
139
+ # can't remember how to use this
140
+ =begin
166
141
  def self.p_val(r, n = 30, rho = 1, mu = r.mean)
167
142
  probs = r.map do |x|
168
143
  (x - mu) / (rho / n.sqrt)
@@ -171,6 +146,7 @@ module Darkext
171
146
  end
172
147
  return 1 - (probs[1] - probs[0])
173
148
  end
149
+ =end
174
150
 
175
151
  module Darkext::Statistics::Regression
176
152
  # Do a least squares linear regression on the two sets of x's and y's
@@ -14,12 +14,12 @@ class String
14
14
  when 3
15
15
  elements = self.split('...')
16
16
  if elements[0] == elements[0].to_i.to_s
17
- return Range.new(elements[0].to_i, (elements[1] - 1).to_i)
17
+ return Range.new(elements[0].to_i, elements[1].to_i, true)
18
18
  else
19
- return Range.new(elements[0], elements[1] - 1)
19
+ return Range.new(elements[0], elements[1], true)
20
20
  end
21
21
  end
22
- return nil
22
+ raise ArgumentError.new('Could not parse range')
23
23
  end
24
24
 
25
25
  # Executes the string with system
data/spec/array_spec.rb CHANGED
@@ -54,4 +54,10 @@ describe Array do
54
54
  it 'should return an Array from randomize' do
55
55
  @a.randomize.should be_a_kind_of(Array)
56
56
  end
57
+
58
+ it 'should return nil from sum, product and random if size == 0' do
59
+ %w(sum product random).each do |method|
60
+ Array.new.send(method.intern).should be_nil
61
+ end
62
+ end
57
63
  end
data/spec/fiber_spec.rb CHANGED
@@ -18,4 +18,8 @@ describe Fiber do
18
18
  it 'should raise an error when it is done' do
19
19
  lambda { 100.times { @fiber.resume } }.should raise_error
20
20
  end
21
+
22
+ it 'should return a string from inspect' do
23
+ @fiber.inspect.should be_a_kind_of(String)
24
+ end
21
25
  end
data/spec/hash_spec.rb CHANGED
@@ -2,10 +2,10 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe Hash do
4
4
  it 'should respond to all the new methods' do
5
- Hash.new.should respond_to(*%w(with_defaults with_defaults! nested_find deep_merge! nested_hash merge_nested_hash!))
5
+ Hash.new.should respond_to(*%w(with_defaults with_defaults! nested_find deep_merge!))
6
6
  end
7
7
 
8
- it 'should return a hash from with_defaults' do
8
+ it 'should return a Hash from with_defaults' do
9
9
  Hash.new.with_defaults(:foo => :bar).should be_a_kind_of(Hash)
10
10
  end
11
11
  end
@@ -6,13 +6,13 @@ describe Array do
6
6
  end
7
7
 
8
8
  it 'should respond to all the new methods' do
9
- Array.new.should respond_to(*%w(mean average ave harmonic_mean h_mean geometric_mean g_mean median histogram mode population_variance sample_variance population_deviation sample_deviation geometric_deviation gstddev sample ci standardize standardize! sum_of_squares normalize normalize!))
10
- Darkext::Statistics.should respond_to(*%w(prob zscore p_val))
9
+ Array.new.should respond_to(*%w(mean average ave harmonic_mean h_mean geometric_mean g_mean median histogram mode standard_deviation stddev standard_deviation sample ci standardize standardize! sum_of_squares))
10
+ Darkext::Statistics.should respond_to(*%w(prob zscore))
11
11
  Darkext::Statistics::Regression.should respond_to(*%w(least_squares))
12
12
  end
13
13
 
14
- it 'should return a Numeric from mean, harmonic_mean, geometric_mean, median, population_variance, sample_variance, population_deviation, sample_deviation, geometric_deviation, and sum_of_squares' do
15
- %w(mean harmonic_mean geometric_mean median population_variance sample_variance population_deviation sample_deviation geometric_deviation sum_of_squares).each do |method|
14
+ it 'should return a Numeric from mean, harmonic_mean, geometric_mean, median, variance, standard_deviation, and sum_of_squares' do
15
+ %w(mean harmonic_mean geometric_mean median variance standard_deviation sum_of_squares).each do |method|
16
16
  @a.send(method.intern).should be_a_kind_of(Numeric)
17
17
  end
18
18
  end
@@ -21,13 +21,13 @@ describe Array do
21
21
  @a.histogram.should be_a_kind_of(Hash)
22
22
  end
23
23
 
24
- it 'should return an array from mode, sample, ci, standardize, and normalize' do
25
- %w(mode sample ci standardize normalize).each do |method|
24
+ it 'should return an array from mode, sample, ci, and standardize' do
25
+ %w(mode sample ci standardize).each do |method|
26
26
  @a.send(method.intern).should be_a_kind_of(Array)
27
27
  end
28
28
  end
29
29
 
30
- it 'should raise and erro from mean, harmonic_mean, geometric_mean, median, mode, population_variance, sample_variance, population_deviation, sample_deviation, geometric_deviation, ci, and sum_of_squares if size is zero' do
30
+ it 'should raise and erro from mean, harmonic_mean, geometric_mean, median, mode, population_variance, sample_variance, standard_deviation, ci, and sum_of_squares if size is zero' do
31
31
  %w(mean harmonic_mean geometric_mean median mode population_variance sample_variance population_deviation sample_deviation geometric_deviation ci sum_of_squares).each do |method|
32
32
  lambda { Array.new.send(method.intern) }.should raise_error
33
33
  end
@@ -38,10 +38,6 @@ describe Array do
38
38
  Darkext::Statistics::zscore(0.9).should be_a_kind_of(Numeric)
39
39
  end
40
40
 
41
- it 'should return a Numeric from p_val' do
42
- Darkext::Statistics::p_val(@a).should be_a_kind_of(Numeric)
43
- end
44
-
45
41
  it 'should return a Hash from least_squares' do
46
42
  Darkext::Statistics::Regression::least_squares([1,2,4,5],[2,3,6,10]).should be_a_kind_of(Hash)
47
43
  end
data/spec/string_spec.rb CHANGED
@@ -24,6 +24,6 @@ describe String do
24
24
  end
25
25
 
26
26
  it 'should return nil if the range is not a valid format' do
27
- 'nipples'.to_range.should be_nil
27
+ lambda { 'nipples'.to_range }.should raise_error
28
28
  end
29
29
  end
@@ -0,0 +1,63 @@
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
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,10 @@
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 ADDED
@@ -0,0 +1,29 @@
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 CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'stringio'
3
3
  require 'test/unit'
4
+
4
5
  require File.dirname(__FILE__) + '/../lib/darkext'
@@ -0,0 +1,10 @@
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 ADDED
@@ -0,0 +1,21 @@
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
@@ -0,0 +1,39 @@
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
+
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,109 @@
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
@@ -0,0 +1,36 @@
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
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class SymbolTest < Test::Unit::TestCase
4
+ def test_to_proc
5
+ assert_equal('abcde'.map(&:upcase).first,'ABCDE')
6
+ assert_equal([1,2,3,4].map(&:square),[1,4,9,16])
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darkhelmet-darkext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Huckstep
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-28 00:00:00 -07:00
12
+ date: 2009-04-06 00:00:00 -07:00
13
13
  default_executable: sinatra-app
14
14
  dependencies: []
15
15
 
@@ -52,7 +52,18 @@ files:
52
52
  - lib/darkext/integer.rb
53
53
  - lib/darkext.rb
54
54
  - test/helper.rb
55
- - test/darkext_test.rb
55
+ - test/array_test.rb
56
+ - test/boolean_test.rb
57
+ - test/fiber_test.rb
58
+ - test/float_test.rb
59
+ - test/hash_test.rb
60
+ - test/integer_test.rb
61
+ - test/io_test.rb
62
+ - test/numeric_test.rb
63
+ - test/object_test.rb
64
+ - test/statistics_test.rb
65
+ - test/string_test.rb
66
+ - test/symbol_test.rb
56
67
  - spec/spec.opts
57
68
  - spec/array_spec.rb
58
69
  - spec/beagle_spec.rb
data/test/darkext_test.rb DELETED
@@ -1,10 +0,0 @@
1
- require File.dirname(__FILE__) + '/helper.rb'
2
-
3
- class TestDarkext < Test::Unit::TestCase
4
- def setup
5
- end
6
-
7
- def test_truth
8
- assert(true)
9
- end
10
- end