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.
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  describe Fiber do
4
4
  before(:each) do
5
5
  @fiber = Fiber.new do
6
- (1..10).each { |i| Fiber.yield }
6
+ (1..10).each { |i| Fiber.yield(i) }
7
7
  end
8
8
  end
9
9
 
@@ -22,4 +22,14 @@ describe Fiber do
22
22
  it 'should return a string from inspect' do
23
23
  @fiber.inspect.should be_a_kind_of(String)
24
24
  end
25
+
26
+ it 'should inspect properly' do
27
+ @fiber.inspect.should match(/.*Fiber:0x/)
28
+ end
29
+
30
+ it 'should, um, work' do
31
+ a = Array.new
32
+ 10.times { a << @fiber.resume }
33
+ a.should == (1..10).to_a
34
+ end
25
35
  end
@@ -8,4 +8,11 @@ describe Float do
8
8
  it 'should return a boolean from equals?' do
9
9
  1.0.equals?(1.0000001).is_boolean?.should be_true
10
10
  end
11
+
12
+ it 'should properly equals' do
13
+ 1.00000000000000001.equals?(1.00000000000000002).should be_true
14
+ 1.1.equals?(1.2).should be_false
15
+ 1.1.equals?(1.2,0.1).should be_true
16
+ 1.2.equals?(1.1,0.1).should be_true
17
+ end
11
18
  end
@@ -1,11 +1,26 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe Hash do
4
+ before(:each) do
5
+ @h = { :foo => 1, :bar => 2, :baz => 3 }
6
+ @nh = { :foo => { :bar => 2 }, :baz => { :biz => 4 } }
7
+ end
8
+
4
9
  it 'should respond to all the new methods' do
5
- Hash.new.should respond_to(*%w(with_defaults with_defaults! nested_find deep_merge!))
10
+ Hash.new.should respond_to(*%w(nested_find deep_merge!))
11
+ end
12
+
13
+ it 'should deep merge' do
14
+ a = { :foo => 1, :bar => { :baz => 10, :biz => { :hello => :world }}}
15
+ b = { :pickles => true, :sandwich => { :ham => 2, :bread => { :grains => :whole }}}
16
+ result = { :foo => 1, :pickles => true, :sandwich => { :ham => 2, :bread => { :grains => :whole }}, :bar => { :baz => 10, :biz => { :hello => :world }}}
17
+ a.deep_merge!(b)
18
+ a.should == result
6
19
  end
7
20
 
8
- it 'should return a Hash from with_defaults' do
9
- Hash.new.with_defaults(:foo => :bar).should be_a_kind_of(Hash)
21
+ it 'should find nested things' do
22
+ @nh.nested_find(:baz,:biz).should == 4
23
+ @nh.nested_find(:foo,:bar).should == 2
24
+ @nh.nested_find(:foo,:biz).should be_nil
10
25
  end
11
26
  end
@@ -12,4 +12,11 @@ describe Integer do
12
12
  it 'should return an Integer from factorial' do
13
13
  10.fact.should be_a_kind_of(Integer)
14
14
  end
15
+
16
+ it 'should factorial properly' do
17
+ 5.fact.should == 120
18
+ 1.fact.should == 1
19
+ 0.fact.should == 1
20
+ 1000.fact.should == 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
21
+ end
15
22
  end
@@ -25,6 +25,20 @@ describe DarkIO do
25
25
  DarkIO::capture_output(:stderr => false, :stdout => false) do
26
26
  STDOUT.print('Hello, World!')
27
27
  STDERR.print('Hello, World!')
28
- end.nil?.should == true
28
+ end.nil?.should be_true
29
+ end
30
+
31
+ it 'should capture output' do
32
+ HW = 'Hello, World!'
33
+ out = DarkIO::capture_output { HW.print }
34
+ out.should == HW
35
+ out = DarkIO::capture_output(:stderr => true) do
36
+ (HW + 'STDOUT').print
37
+ STDERR.print(HW + 'STDERR')
38
+ end
39
+ out.shift.should == HW + 'STDOUT'
40
+ out.shift.should == HW + 'STDERR'
41
+ out = DarkIO::capture_output(:stderr => true, :stdout => false) { STDERR.print(HW) }
42
+ out.should == HW
29
43
  end
30
44
  end
@@ -14,4 +14,39 @@ describe Numeric do
14
14
  end
15
15
  end
16
16
  end
17
+
18
+ it 'should do squares' do
19
+ 4.square.should == 16
20
+ -4.square.should == 16
21
+ end
22
+
23
+ it 'should do cubes' do
24
+ 3.cube.should == 27
25
+ -3.cube.should == -27
26
+ end
27
+
28
+ it 'should do square roots' do
29
+ 9.sqrt.should == 3
30
+ 81.sqrt.should == 9
31
+ end
32
+
33
+ it 'should do roots' do
34
+ 9.sqrt.should == 9.root
35
+ 27.root(3).should == 3
36
+ 81.root(4).should == 3
37
+ 5.root(1).should == 5
38
+ 10.root(-1).should == 0.1
39
+ 100.root(-2).should == 0.1
40
+ end
41
+
42
+ it 'should do ln' do
43
+ Math::E.ln.should == 1
44
+ Math::PI.ln.should be_close(1.14473, 0.001)
45
+ end
46
+
47
+ it 'should do log' do
48
+ 10.log.should == 1
49
+ 100.log.should == 2
50
+ Math::PI.log.should be_close(0.4971, 0.0001)
51
+ end
17
52
  end
@@ -13,4 +13,16 @@ describe Object do
13
13
  it 'should return nil from try if called on nil' do
14
14
  nil.try(1) { |a| }.should be_nil
15
15
  end
16
+
17
+ it 'should try...like really try' do
18
+ nil.try { |me| me.foo }.should be_nil
19
+ true.try { |me| me }.should be_true
20
+ true.try(:nil?).should_not be_nil
21
+ end
22
+
23
+ it 'should tap dance' do
24
+ 10.tap { |m| m.should == 10 }.should == 10
25
+ o = Object.new
26
+ o.tap { |m| m.should == o }.should == o
27
+ end
16
28
  end
@@ -2,7 +2,10 @@ require 'rubygems'
2
2
  require 'spec'
3
3
 
4
4
  # gem install redgreen for colored test output
5
- begin require 'redgreen' unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
5
+ begin
6
+ require 'redgreen' unless ENV['TM_CURRENT_LINE'];
7
+ rescue LoadError
8
+ end
6
9
 
7
10
  path = File.expand_path(File.dirname(__FILE__) + '/../lib/')
8
11
  $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
@@ -3,6 +3,8 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  describe Array do
4
4
  before(:each) do
5
5
  @a = [1,2,3,4,5,6,7,8,9,10]
6
+ @hist = [1,2,1,1,3,2,1,1,3,3,3,3,3,2,2,1]
7
+ @b = [5,1,9,12,6,7,8,8,8,1,2,3,4]
6
8
  end
7
9
 
8
10
  it 'should respond to all the new methods' do
@@ -49,4 +51,112 @@ describe Array do
49
51
  it 'should raise an error if either argument has size.zero?' do
50
52
  lambda { Darkext::Statistics::Regression::least_squares(@a,Array.new) }.should raise_error
51
53
  end
54
+
55
+ it 'should handle mean' do
56
+ (1..6).to_a.mean.should == 3.5
57
+ [1].mean.should == 1
58
+ end
59
+
60
+ it 'should handle harmonic mean' do
61
+ [60,40].harmonic_mean.should be_close(48, 0.0001)
62
+ [9].harmonic_mean.should be_close(9, 0.0001)
63
+ end
64
+
65
+ it 'should handle geometric mean' do
66
+ [2,8].geometric_mean.should == 4
67
+ [9].geometric_mean.should be_close(9, 0.0001)
68
+ end
69
+
70
+ it 'should handle median' do
71
+ (1..3).to_a.median.should == 2
72
+ [1].median.should == 1
73
+ [2,4,6,8].median.should == 5
74
+ end
75
+
76
+ it 'should make histograms' do
77
+ @hist.histogram.should == { 1 => 6, 2 => 4, 3 => 6 }
78
+ end
79
+
80
+ it 'should handle mode' do
81
+ @hist.mode.should == [1,3]
82
+ (@hist + [1]).mode.should == [1]
83
+ end
84
+
85
+ it 'should handle variance' do
86
+ (1..6).to_a.variance.should be_close(35/12, 0.0001)
87
+ [1].variance.should be_close(0, 0.0001)
88
+ [1,1].variance.should be_close(0, 0.0001)
89
+ @b.variance.should be_close(10.5, 0.05)
90
+ end
91
+
92
+ it 'should handle std dev' do
93
+ [2,4,4,4,5,5,7,9].stddev.should be_close(2, 0.0001)
94
+ @b.stddev.should be_close(3.24, 0.01)
95
+ end
96
+
97
+ it 'should sample' do
98
+ @hist.sample(5).size.should == 5
99
+ end
100
+
101
+ it 'should handle confidence intervals' do
102
+ ci = @b.ci
103
+ ci.shift.should be_close(5.15, 0.01)
104
+ ci.shift.should be_close(6.24, 0.01)
105
+ @b.ci(:type => :upper).shift.should be_close(6.15, 0.01)
106
+ @b.ci(:type => :lower).shift.should be_close(5.24, 0.01)
107
+ end
108
+
109
+ it 'should standardize' do
110
+ [2,2,4,4].standardize.should == [-1,-1,1,1].map(&:to_f)
111
+ @b.standardize.size.should == 13
112
+ end
113
+
114
+ it 'should do sum of squares' do
115
+ (1..3).to_a.sum_of_squares.should == 2.0
116
+ end
117
+
118
+ it 'should do probability' do
119
+ Darkext::Statistics::prob(2).should be_close(0.96, 0.02)
120
+ Darkext::Statistics::prob(0.5).should be_close(0.69, 0.02)
121
+ Darkext::Statistics::prob(Darkext::Statistics::zscore(1)).should be_close(1, 0.0001)
122
+ end
123
+
124
+ it 'should do zscore' do
125
+ Darkext::Statistics::zscore(0.5).should be_close(0, 0.1)
126
+ Darkext::Statistics::prob(0.75).should be_close(0.77, 0.02)
127
+ Darkext::Statistics::zscore(Darkext::Statistics::prob(2.5)).should be_close(2.5, 0.0001)
128
+ end
129
+
130
+ it 'should handle regressions' do
131
+ xs = [0,1.2,2,2.9,4,5,6]
132
+ ys = [0.1,1,2.1,3,4.3,4.9,6]
133
+
134
+ results = Darkext::Statistics::Regression::least_squares(xs,ys)
135
+
136
+ results[:n].should == xs.size
137
+ results[:b_1].should be_close(1, 0.1)
138
+ results[:b_0].should be_close(0, 0.1)
139
+ results[:predicted].size.should == xs.size
140
+
141
+ results[:predicted].each_with_index do |pred,index|
142
+ pred.should be_close(index, 0.3)
143
+ end
144
+
145
+ results[:residuals].size.should == xs.size
146
+
147
+ results[:residuals].each_with_index do |pred,index|
148
+ pred.should be_close(0, 0.3)
149
+ end
150
+
151
+ results[:ss_e].should be_close(0.15, 0.01)
152
+ results[:ss_t].should be_close(27.49, 0.01)
153
+ results[:estimator].should be_close(0.03, 0.01)
154
+ results[:r_2].should be_close(1, 0.01)
155
+ results[:r].should be_close(1, 0.01)
156
+
157
+ eqn = results[:equation]
158
+ (0..100).each do |i|
159
+ eqn.call(i).should be_close(i, 0.25)
160
+ end
161
+ end
52
162
  end
@@ -26,4 +26,29 @@ describe String do
26
26
  it 'should return nil if the range is not a valid format' do
27
27
  lambda { 'nipples'.to_range }.should raise_error
28
28
  end
29
+
30
+ it 'should convert to range' do
31
+ '1..10'.to_range.should == (1..10)
32
+ '1...10'.to_range.should == (1...10)
33
+ 'a..z'.to_range.should == ('a'..'z')
34
+ 'a...z'.to_range.should == ('a'...'z')
35
+ end
36
+
37
+ it 'should be true' do
38
+ %w(true TRUE True).each { |t| t.true?.should be_true }
39
+ end
40
+
41
+ it 'should be false' do
42
+ %w(false FALSE False).each { |f| f.false?.should be_true }
43
+ end
44
+
45
+ it 'should start with' do
46
+ 'foobar'.starts_with?('foo').should be_true
47
+ 'foobar'.starts_with?('bar').should be_false
48
+ end
49
+
50
+ it 'should end with' do
51
+ 'foobar'.ends_with?('bar').should be_true
52
+ 'foobar'.ends_with?('foo').should be_false
53
+ end
29
54
  end
@@ -8,4 +8,9 @@ describe Symbol do
8
8
  it 'should return a Proc from to_proc' do
9
9
  :foo.to_proc.should be_a_kind_of(Proc)
10
10
  end
11
+
12
+ it 'should properly to do to_proc' do
13
+ 'abc'.map(&:upcase).first.should == 'ABC'
14
+ [1,2,3,4].map(&:square).should == [1,4,9,16]
15
+ end
11
16
  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.2
4
+ version: 0.12.0
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-04-28 00:00:00 -07:00
12
+ date: 2009-08-11 00:00:00 -07:00
13
13
  default_executable: sinatra-app
14
14
  dependencies: []
15
15
 
@@ -20,10 +20,15 @@ executables:
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README.rdoc
23
+ - LICENSE
24
+ - README.md
24
25
  files:
26
+ - .gitignore
25
27
  - History.txt
26
- - README.rdoc
28
+ - LICENSE
29
+ - README.md
30
+ - Rakefile
31
+ - TODO
27
32
  - VERSION.yml
28
33
  - app_generators/sinatra_app_generator.rb
29
34
  - app_generators/templates/app.rb
@@ -34,6 +39,7 @@ files:
34
39
  - app_generators/templates/http_method.rb
35
40
  - app_generators/templates/options.rb
36
41
  - bin/sinatra-app
42
+ - darkext.gemspec
37
43
  - lib/darkext.rb
38
44
  - lib/darkext/array.rb
39
45
  - lib/darkext/beagle.rb
@@ -67,21 +73,9 @@ files:
67
73
  - spec/statistics_spec.rb
68
74
  - spec/string_spec.rb
69
75
  - spec/symbol_spec.rb
70
- - test/array_test.rb
71
- - test/boolean_test.rb
72
- - test/fiber_test.rb
73
- - test/float_test.rb
74
- - test/hash_test.rb
75
- - test/helper.rb
76
- - test/integer_test.rb
77
- - test/io_test.rb
78
- - test/numeric_test.rb
79
- - test/object_test.rb
80
- - test/statistics_test.rb
81
- - test/string_test.rb
82
- - test/symbol_test.rb
83
- has_rdoc: true
76
+ has_rdoc: false
84
77
  homepage: http://github.com/darkhelmet/darkext
78
+ licenses:
85
79
  post_install_message:
86
80
  rdoc_options:
87
81
  - --charset=UTF-8
@@ -102,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
96
  requirements: []
103
97
 
104
98
  rubyforge_project:
105
- rubygems_version: 1.2.0
99
+ rubygems_version: 1.3.5
106
100
  signing_key:
107
101
  specification_version: 3
108
102
  summary: Just some useful Ruby functionality
@@ -122,16 +116,3 @@ test_files:
122
116
  - spec/string_spec.rb
123
117
  - spec/symbol_spec.rb
124
118
  - spec/spec_helper.rb
125
- - test/helper.rb
126
- - test/array_test.rb
127
- - test/boolean_test.rb
128
- - test/fiber_test.rb
129
- - test/float_test.rb
130
- - test/hash_test.rb
131
- - test/integer_test.rb
132
- - test/io_test.rb
133
- - test/numeric_test.rb
134
- - test/object_test.rb
135
- - test/statistics_test.rb
136
- - test/string_test.rb
137
- - test/symbol_test.rb
@@ -1,66 +0,0 @@
1
- = darkext
2
-
3
- == DESCRIPTION:
4
-
5
- Just some useful Ruby functionality. No particular focus, except usefulness
6
-
7
- == FEATURES:
8
-
9
- * Adds to Array
10
- * rotate, rotate_reverse
11
- * sum, product
12
- * squares, squares!
13
- * random, randomize, randomize!
14
- * Adds to TrueClass and FalseClass
15
- * intern
16
- * Adds to Hash
17
- * with_defaults, with_defaults!
18
- * Adds to Integer
19
- * fact
20
- * Adds to Numeric
21
- * square, cube
22
- * sqrt
23
- * ln, log
24
- * Adds to Object
25
- * try
26
- * is_boolean?
27
- * Adds to String
28
- * to_range
29
- * exec
30
- * print
31
- * Adds IO package
32
- * Adds statistical package
33
-
34
- == USAGE:
35
-
36
- $ require 'darkext'
37
-
38
- == INSTALLATION:
39
-
40
- * $ gem sources -a http://gems.github.com # unless you already have done this
41
- * $ gem install darkhelmet-darkext
42
-
43
- == LICENSE:
44
-
45
- (The MIT License)
46
-
47
- Copyright (c) 2008 Daniel Huckstep
48
-
49
- Permission is hereby granted, free of charge, to any person obtaining
50
- a copy of this software and associated documentation files (the
51
- 'Software'), to deal in the Software without restriction, including
52
- without limitation the rights to use, copy, modify, merge, publish,
53
- distribute, sublicense, and/or sell copies of the Software, and to
54
- permit persons to whom the Software is furnished to do so, subject to
55
- the following conditions:
56
-
57
- The above copyright notice and this permission notice shall be
58
- included in all copies or substantial portions of the Software.
59
-
60
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.