ruby-fizzbuzz 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTllN2IxMDdjZWQ3MmYzZDk3ZTc1YjVkMDUzMGYxMzU2NzlhZDQ2Nw==
4
+ NGMwNWU1NjNiMzhmZGQwZGZlZjUyNTM4ZTE0N2ZiMWUyY2E0YjBkOA==
5
5
  data.tar.gz: !binary |-
6
- ZDRhYTA3ZTdkNDg2ZWM4ODIxN2Y0OWE0M2ZkZGE2YmE4MjlmYzg3Mw==
6
+ MzNkMTQ2MTkxMjUzNTY3M2UxZWQyZjg3NTQ3YzlhMTlmMjYwYjZiYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YmUwMDE4ZWI4OWE3ZDE4NjQ4NTUyYWVjOGUzYzZiYjlhNjcxNDIxMmY5OWMx
10
- ZTZiNzkxOTVlM2ZkNzljMDFlMTNiYzIyNjRmZjQ5YmM0NjU4MmVmZDJkZjk0
11
- YzkzZDU4ODI2MGU4MjZkZjBiMWQxYTk0NDBiYWY1OTJhZmVlMzg=
9
+ NmYwYTYzYzA1MjUyODRhMmVmNTM0NDQ4OGY1NzYwMzAwZDBkZWRlODRjMTNm
10
+ ODQ1YjE5ZjYwMmM1NzQ4MWU5NWE3YzE5ZTkyODIyZDVlODBkNTQ2MjcyMTI5
11
+ ZjRiZTE0YzQzMjkwM2RjYTVkYjUzMjVlMDM5NWIwNjIyMjRmNzY=
12
12
  data.tar.gz: !binary |-
13
- MGI4ZDM2YjlkMTZkYjFiZGU3ZTc1MzQ3M2JiZTVmMDE4YzNhZWIxNzM0NmJj
14
- NTYwODc1MTQ3ZTExMDc2MDhkNmNkNzMyZjYyMmExZjc4ZWQ5OWU0MmJhY2Vm
15
- Njc0OGQ2M2ViYjIwYjAxNWZlYzI3NGNiNmZjNDBkOTNjNTEzZDc=
13
+ MTAxOTE2YjM0NGMxZjVhNjc4YWYyZGUzMzcwMjRiNjNiNDlkMTY4MWMxNGQ4
14
+ Y2RjYzYxYjhkZDM0ZmM5NWE4MDYyZTZmOGY5YTQ2YjBmMWUxOGFkOWFjYmRm
15
+ ZjkyZGVlODdjMzgyMzliYjI3NjE1MTJjMWE3MmI5ZTA5ZWEwMDY=
data/CHANGES CHANGED
@@ -1,3 +1,10 @@
1
+ fizzbuzz (0.0.5)
2
+
3
+ * Added integration with Array and Range types;
4
+ * Added simple benchmark.
5
+
6
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Tue, 26 Nov 2013 12:40:34 +0000
7
+
1
8
  fizzbuzz (0.0.4)
2
9
 
3
10
  * Retired support for Ruby 1.8.x (no support for MRI, Ruby Enterprise Edition and Rubinius);
data/CHANGES.rdoc CHANGED
@@ -1,3 +1,10 @@
1
+ fizzbuzz (0.0.5)
2
+
3
+ * Added integration with Array and Range types;
4
+ * Added simple benchmark.
5
+
6
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Tue, 26 Nov 2013 12:40:34 +0000
7
+
1
8
  fizzbuzz (0.0.4)
2
9
 
3
10
  * Retired support for Ruby 1.8.x (no support for MRI, Ruby Enterprise Edition and Rubinius);
data/Rakefile CHANGED
@@ -72,6 +72,24 @@ end
72
72
 
73
73
  Rake::Task[:test].prerequisites << :compile
74
74
 
75
+ desc 'Run benchmarks'
76
+ task :benchmark, [:first] => :test do |t,argument|
77
+ glob = File.expand_path("../benchmark/*.rb", __FILE__)
78
+ Dir[glob].each do |f|
79
+ process = ['ruby', f]
80
+ process << argument[:first] if argument[:first]
81
+
82
+ STDOUT.sync = true
83
+ STDERR.sync = true
84
+
85
+ IO.popen(process) do |stream|
86
+ stream.each {|line| puts line }
87
+ end
88
+
89
+ Process.waitpid rescue Errno::ECHILD
90
+ end
91
+ end
92
+
75
93
  task :default => :package
76
94
 
77
95
  # vim: set ts=2 sw=2 sts=2 et :
data/TODO CHANGED
@@ -1,23 +1,16 @@
1
- fizzbuzz (1.0.0)
2
-
3
- * Add simple web interface with RESTful backend API.
4
-
5
- -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Mon, 01 Oct 2012 17:57:18 +0100
6
-
7
- fizzbuzz (0.0.6)
1
+ fizzbuzz (0.0.7)
8
2
 
9
3
  * Improve github project page;
10
4
  * Re-factor unit tests to make them more idiomatic (maybe move to Rspec and BDD?).
11
5
 
12
- -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Mon, 18 Nov 2013 11:22:26 +0000
6
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Tue, 26 Nov 2013 12:40:34 +0000
13
7
 
14
- fizzbuzz (0.0.5)
8
+ fizzbuzz (0.0.6)
15
9
 
16
10
  * Move to YARD from RDoc (as RDoc is very bad, really);
17
11
  * Add pure-Ruby implementation;
18
- * Add benchmarks;
19
12
  * Add some examples;
20
13
  * Add Doxygen style comments for other functions in the source code;
21
14
  * Improve documentation i.e. source code commenting, README file, etc.
22
15
 
23
- -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Mon, 18 Nov 2013 11:22:26 +0000
16
+ -- Krzysztof Wilczynski <krzysztof.wilczynski@linux.com> Tue, 26 Nov 2013 12:40:34 +0000
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -0,0 +1,107 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # :enddoc:
4
+
5
+ #
6
+ # benchmark_fizzbuzz.rb
7
+ #
8
+ # Copyright 2012-2013 Krzysztof Wilczynski
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ #
22
+
23
+ $: << File.expand_path('../../lib', __FILE__)
24
+
25
+ require 'time'
26
+ require 'benchmark'
27
+
28
+ begin
29
+ require 'fizzbuzz'
30
+ rescue LoadError
31
+ require 'rubygems'
32
+ require 'fizzbuzz'
33
+ end
34
+
35
+ CAPTION = "%8s%12s%11s%10s\n" % %w(User System Total Real)
36
+
37
+ n = (ARGV.shift || 1_000_000).to_i
38
+
39
+ start = Time.now
40
+ array = (1 .. n).to_a
41
+ reports = []
42
+
43
+ puts "FizzBuzz #{FizzBuzz::VERSION}, n = #{n}\n\n"
44
+
45
+ Benchmark.benchmark(CAPTION, 24) do |bm|
46
+ reports << bm.report('FizzBuzz::fizzbuzz') do
47
+ FizzBuzz.fizzbuzz(1, n)
48
+ end
49
+
50
+ reports << bm.report('FizzBuzz#each') do
51
+ FizzBuzz.new(1, n).each {|i| i }
52
+ end
53
+
54
+ reports << bm.report('FizzBuzz#reverse_each') do
55
+ FizzBuzz.new(1, n).reverse_each {|i| i }
56
+ end
57
+
58
+ reports << bm.report('FizzBuzz#to_a') do
59
+ FizzBuzz.new(1, n).to_a
60
+ end
61
+
62
+ reports << bm.report('FizzBuzz::[]') do
63
+ n.times {|i| FizzBuzz[i] }
64
+ end
65
+
66
+ reports << bm.report('FizzBuzz::is_fizz?') do
67
+ n.times {|i| FizzBuzz.is_fizz?(i) }
68
+ end
69
+
70
+ reports << bm.report('FizzBuzz::is_buzz?') do
71
+ n.times {|i| FizzBuzz.is_buzz?(i) }
72
+ end
73
+
74
+ reports << bm.report('FizzBuzz::is_fizzbuzz?') do
75
+ n.times {|i| FizzBuzz.is_fizzbuzz?(i) }
76
+ end
77
+
78
+ reports << bm.report('Array#fizzbuzz') do
79
+ (1 .. n).fizzbuzz
80
+ end
81
+
82
+ reports << bm.report('Range#fizzbuzz') do
83
+ array.fizzbuzz
84
+ end
85
+
86
+ []
87
+ end
88
+
89
+ reports.sort_by! {|i| i.real }
90
+
91
+ overall = reports.reduce(:+)
92
+ average = overall / reports.size
93
+
94
+ fastest, slowest = reports.shift, reports.pop
95
+
96
+ padding = [fastest.label, slowest.label].max
97
+ padding = padding.size > 16 ? 24 : 16
98
+
99
+ puts "\n%15s%s%16s\n" % ['', CAPTION.strip, 'Name'] +
100
+ "%s%48s [ %-#{padding}s ]\n" % ['Fastest:', fastest.to_s.strip, fastest.label.strip] +
101
+ "%s%48s [ %-#{padding}s ]\n" % ['Slowest:', slowest.to_s.strip, slowest.label.strip] +
102
+ "%s%48s\n" % ['Average:', average.to_s.strip] +
103
+ "%s%48s\n" % ['Overall:', overall.to_s.strip] +
104
+ "\n%s%13.6f\n" % ['Wall Clock Time:', Time.now.to_f - start.to_f]
105
+
106
+ # vim: set ts=2 sw=2 sts=2 et :
107
+ # encoding: utf-8
@@ -66,7 +66,7 @@ static VALUE fizzbuzz_range_error(VALUE klass, VALUE start, VALUE stop,
66
66
  * Will raise a <i>FizzBuzz::TypeError</i> exception if given value of either
67
67
  * +start+ or +stop+ is not an _Integer_ or _Bignum_ type.
68
68
  *
69
- * See also: FizzBuzz::fizzbuzz
69
+ * See also: FizzBuzz::fizzbuzz and FizzBuzz::[]
70
70
  */
71
71
  VALUE
72
72
  rb_fb_initialize(int argc, VALUE *argv, VALUE object)
@@ -182,7 +182,7 @@ rb_fb_set_stop(VALUE object, VALUE value)
182
182
  * call-seq:
183
183
  * fizzbuzz.to_a -> array
184
184
  *
185
- * Returns an array containing results upon calculating an appropriate
185
+ * Returns an +array+ containing results upon calculating an appropriate
186
186
  * values for a given range from +start+ to +stop+.
187
187
  *
188
188
  * Example:
@@ -203,7 +203,7 @@ rb_fb_array(VALUE object)
203
203
  * fizzbuzz.each {|value| block } -> self
204
204
  * fizzbuzz.each -> an Enumerator
205
205
  *
206
- * Calls the block once for each subsequent value for a given range
206
+ * Calls the block _once_ for each subsequent value for a given range
207
207
  * from +start+ to +stop+, passing the value as a parameter to the block.
208
208
  *
209
209
  * If no block is given, an +Enumerator+ is returned instead.
@@ -244,7 +244,7 @@ rb_fb_enumerator(VALUE object)
244
244
  * fizzbuzz.reverse_each {|value| block } -> self
245
245
  * fizzbuzz.reverse_each -> an Enumerator
246
246
  *
247
- * Calls the block once for each subsequent value for a given range
247
+ * Calls the block _once_ for each subsequent value for a given range
248
248
  * from +start+ to +stop+ in an <i>reverse order</i>, passing the value
249
249
  * as a parameter to the block.
250
250
  *
data/lib/fizzbuzz.rb CHANGED
@@ -26,6 +26,8 @@ require 'fizzbuzz/fizzbuzz'
26
26
  require 'fizzbuzz/version'
27
27
  require 'fizzbuzz/integer'
28
28
  require 'fizzbuzz/bignum'
29
+ require 'fizzbuzz/array'
30
+ require 'fizzbuzz/range'
29
31
 
30
32
  #
31
33
  # Yet another _FizzBuzz_ in Ruby.
@@ -0,0 +1,88 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # :stopdoc:
4
+
5
+ #
6
+ # array.rb
7
+ #
8
+ # Copyright 2012-2013 Krzysztof Wilczynski
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ #
22
+
23
+ # :startdoc:
24
+
25
+ #
26
+ # Provides a convenient integration of _FizzBuzz_ with _Array_ class.
27
+ #
28
+ class Array
29
+ #
30
+ # call-seq:
31
+ # array.fizzbuzz( reverse ) -> array
32
+ # array.fizzbuzz( reverse ) {|value| block } -> self
33
+ #
34
+ # Returns either an array or accepts a block if such is given. When a block is given
35
+ # then it will call the block once for each subsequent value for a given array, passing
36
+ # the value as a parameter to the block.
37
+ #
38
+ # Additionally, if the value of +reverse+ is set to be +true+ then the results will
39
+ # be given in an <em>reverse order</em> whether in a resulting array or when passing
40
+ # values to a block given.
41
+ #
42
+ # Example:
43
+ #
44
+ # array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
45
+ # array.fizzbuzz #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
46
+ # array.fizzbuzz(true) #=> ["FizzBuzz", 14, 13, "Fizz", 11, "Buzz", "Fizz", 8, 7, "Fizz", "Buzz", 4, "Fizz", 2, 1]
47
+ #
48
+ # Example:
49
+ #
50
+ # array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
51
+ # array.fizzbuzz {|value| puts "Got #{value}" }
52
+ #
53
+ # Produces:
54
+ #
55
+ # Got 1
56
+ # Got 2
57
+ # Got Fizz
58
+ # Got 4
59
+ # Got Buzz
60
+ # Got Fizz
61
+ # Got 7
62
+ # Got 8
63
+ # Got Fizz
64
+ # Got Buzz
65
+ # Got 11
66
+ # Got Fizz
67
+ # Got 13
68
+ # Got 14
69
+ # Got FizzBuzz
70
+ #
71
+ # See also: FizzBuzz::fizzbuzz, FizzBuzz#to_a, FizzBuzz#each and FizzBuzz#reverse_each
72
+ #
73
+ def fizzbuzz(reverse = false)
74
+ values = self.send(reverse ? :reverse : :entries)
75
+
76
+ if block_given?
77
+ values.each {|i| yield FizzBuzz[i] }
78
+ self
79
+ else
80
+ values.collect {|i| FizzBuzz[i] }
81
+ end
82
+ end
83
+ end
84
+
85
+ # :enddoc:
86
+
87
+ # vim: set ts=2 sw=2 sts=2 et :
88
+ # encoding: utf-8
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # integer.rb
7
7
  #
8
- # Copyright 2012 Krzysztof Wilczynski
8
+ # Copyright 2012-2013 Krzysztof Wilczynski
9
9
  #
10
10
  # Licensed under the Apache License, Version 2.0 (the "License");
11
11
  # you may not use this file except in compliance with the License.
@@ -0,0 +1,86 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # :stopdoc:
4
+
5
+ #
6
+ # range.rb
7
+ #
8
+ # Copyright 2012-2013 Krzysztof Wilczynski
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ #
22
+
23
+ # :startdoc:
24
+
25
+ #
26
+ # Provides a convenient integration of _FizzBuzz_ with _Range_ class.
27
+ #
28
+ class Range
29
+ #
30
+ # call-seq:
31
+ # range.fizzbuzz( reverse ) -> array
32
+ # range.fizzbuzz( reverse ) {|value| block } -> self
33
+ #
34
+ # Returns either an array or accepts a block if such is given. When a block is given
35
+ # then it will call the block once for each subsequent value for a given range, passing
36
+ # the value as a parameter to the block.
37
+ #
38
+ # Additionally, if the value of +reverse+ is set to be +true+ then the results will
39
+ # be given in an <em>reverse order</em> whether in a resulting array or when passing
40
+ # values to a block given.
41
+ #
42
+ # Example:
43
+ #
44
+ # (1..15).fizzbuzz #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
45
+ # (1..15).fizzbuzz(true) #=> ["FizzBuzz", 14, 13, "Fizz", 11, "Buzz", "Fizz", 8, 7, "Fizz", "Buzz", 4, "Fizz", 2, 1]
46
+ #
47
+ # Example:
48
+ #
49
+ # (1..15).fizzbuzz {|value| puts "Got #{value}" }
50
+ #
51
+ # Produces:
52
+ #
53
+ # Got 1
54
+ # Got 2
55
+ # Got Fizz
56
+ # Got 4
57
+ # Got Buzz
58
+ # Got Fizz
59
+ # Got 7
60
+ # Got 8
61
+ # Got Fizz
62
+ # Got Buzz
63
+ # Got 11
64
+ # Got Fizz
65
+ # Got 13
66
+ # Got 14
67
+ # Got FizzBuzz
68
+ #
69
+ # See also: FizzBuzz::fizzbuzz, FizzBuzz#to_a, FizzBuzz#each and FizzBuzz#reverse_each
70
+ #
71
+ def fizzbuzz(reverse = false)
72
+ values = self.collect {|i| FizzBuzz[i] }.send(reverse ? :reverse : :entries)
73
+
74
+ if block_given?
75
+ values.each {|i| yield i }
76
+ self
77
+ else
78
+ values
79
+ end
80
+ end
81
+ end
82
+
83
+ # :enddoc:
84
+
85
+ # vim: set ts=2 sw=2 sts=2 et :
86
+ # encoding: utf-8
@@ -26,7 +26,7 @@ class FizzBuzz
26
26
  #
27
27
  # Current version of _FizzBuzz_.
28
28
  #
29
- VERSION = '0.0.4'
29
+ VERSION = '0.0.5'
30
30
  end
31
31
 
32
32
  # :enddoc:
@@ -0,0 +1,71 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # :enddoc:
4
+
5
+ #
6
+ # ruby-fizzbuzz.gemspec
7
+ #
8
+ # Copyright 2012-2013 Krzysztof Wilczynski
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ #
22
+
23
+ signing_key = File.expand_path('~/.gem/kwilczynski-private.pem')
24
+
25
+ Gem::Specification.new do |s|
26
+ s.name = 'ruby-fizzbuzz'
27
+
28
+ s.description = <<-EOS
29
+ Yet another FizzBuzz in Ruby.
30
+
31
+ Provides simple and fast solution to a popular FizzBuzz problem for Ruby.
32
+
33
+ Written in C as an example of using Ruby's C API - with the support for
34
+ arbitrary large numeric values via the Bignum class.
35
+ EOS
36
+
37
+ s.platform = Gem::Platform::RUBY
38
+ s.version = File.read('VERSION').strip
39
+ s.license = 'Apache License, Version 2.0'
40
+ s.author = 'Krzysztof Wilczynski'
41
+ s.email = 'krzysztof.wilczynski@linux.com'
42
+ s.homepage = 'http://about.me/kwilczynski'
43
+
44
+ s.rubyforge_project = 'ruby-fizzbuzz'
45
+ s.rubygems_version = '~> 1.3.7'
46
+ s.has_rdoc = true
47
+
48
+ s.summary = 'Yet another FizzBuzz in Ruby'
49
+
50
+ s.files = Dir['ext/**/*.{c,h,rb}'] +
51
+ Dir['lib/**/*.rb'] +
52
+ Dir['benchmark/**/*.rb'] +
53
+ Dir['test/**/*.rb'] +
54
+ %w(Rakefile ruby-fizzbuzz.gemspec AUTHORS
55
+ CHANGES CHANGES.rdoc COPYRIGHT LICENSE
56
+ README README.rdoc TODO VERSION)
57
+
58
+ s.executables << 'fizzbuzz'
59
+ s.require_paths << 'lib'
60
+ s.extensions << 'ext/fizzbuzz/extconf.rb'
61
+
62
+ s.add_development_dependency 'rake', '>= 0.8.7'
63
+ s.add_development_dependency 'rdoc', '>= 3.12'
64
+ s.add_development_dependency 'test-unit', '>= 2.5.2'
65
+ s.add_development_dependency 'rake-compiler', '>= 0.7.1'
66
+
67
+ s.signing_key = signing_key if File.exists?(signing_key)
68
+ end
69
+
70
+ # vim: set ts=2 sw=2 sts=2 et :
71
+ # encoding: utf-8
@@ -0,0 +1,514 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # :enddoc:
4
+
5
+ #
6
+ # test_fizzbuzz.rb
7
+ #
8
+ # Copyright 2012-2013 Krzysztof Wilczynski
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ #
22
+
23
+ begin
24
+ require 'coveralls'
25
+ Coveralls.wear!
26
+ rescue LoadError
27
+ STDERR.puts 'The Coveralls gem is not installed, skipping ...'
28
+ end
29
+
30
+ gem 'test-unit', '>= 2.5.2'
31
+
32
+ require 'test/unit'
33
+ require 'fizzbuzz'
34
+
35
+ DEFAULT_START = 1
36
+ DEFAULT_STOP = 15
37
+ DEFAULT_WORDS = ['Fizz', 'Buzz', 'FizzBuzz']
38
+ DEFAULT_EXPECTED = [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
39
+
40
+ DEFAULT_SINGLETON_METHODS = [:fizzbuzz, :is_fizz?, :is_buzz?, :is_fizzbuzz?]
41
+ DEFAULT_INSTANCE_METHODS = [:to_a, :each, :reverse_each]
42
+
43
+ DEFAULT_INSTANCE_METHODS_ADDED = [:fizz?, :buzz?, :fizzbuzz?]
44
+
45
+ class Fixnum
46
+ class << self
47
+ def overflow_size_with_sign
48
+ bits = [''].pack('p').size * 8
49
+ 2 ** (bits - 1) - 1
50
+ end
51
+ end
52
+ end
53
+
54
+ class FizzBuzzTest < Test::Unit::TestCase
55
+ def setup
56
+ @integer = 1
57
+ @bignum = 1_000_000_000_000
58
+ @large_bignum = 1_000_000_000_000_000
59
+
60
+ if Fixnum::overflow_size_with_sign + 1 > 2147483647
61
+ @bignum = @bignum ** 2
62
+ @large_bignum = @large_bignum ** 2
63
+ end
64
+ end
65
+
66
+ def test_fizzbuzz_alias
67
+ assert(FB == FizzBuzz)
68
+ end
69
+
70
+ def test_fizzbuzz_singleton_methods
71
+ fb = FizzBuzz
72
+
73
+ assert_block do
74
+ DEFAULT_SINGLETON_METHODS.all? {|i| fb.respond_to?(i) }
75
+ end
76
+ end
77
+
78
+ def test_fizzbuzz_new_instance
79
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
80
+ assert(fb.class == FizzBuzz)
81
+ end
82
+
83
+ def test_fizzbuzz_instance_methods
84
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
85
+
86
+ assert_block do
87
+ DEFAULT_INSTANCE_METHODS.all? {|i| fb.respond_to?(i) }
88
+ end
89
+ end
90
+
91
+ def test_integer_integration
92
+ assert_block do
93
+ DEFAULT_INSTANCE_METHODS_ADDED.all? {|i| @integer.respond_to?(i) }
94
+ end
95
+ end
96
+
97
+ def test_integer_is_fizz
98
+ assert_equal(3.fizz?, true)
99
+ assert_equal(15.fizz?, false)
100
+ end
101
+
102
+ def test_integer_is_buzz
103
+ assert_equal(5.buzz?, true)
104
+ assert_equal(15.buzz?, false)
105
+ end
106
+
107
+ def test_integer_is_fizzbuzz
108
+ assert_equal(15.fizzbuzz?, true)
109
+ assert_equal(3.fizzbuzz?, false)
110
+ assert_equal(5.fizzbuzz?, false)
111
+ end
112
+
113
+ def test_bignum_integration
114
+ assert_block do
115
+ DEFAULT_INSTANCE_METHODS_ADDED.all? {|i| @bignum.respond_to?(i) }
116
+ end
117
+ end
118
+
119
+ def test_bignum_is_fizz
120
+ assert_equal((@bignum + 2).fizz?, true)
121
+ assert_equal((@bignum + 15).fizz?, false)
122
+ end
123
+
124
+ def test_bignum_is_buzz
125
+ assert_equal((@bignum + 15).buzz?, true)
126
+ assert_equal((@bignum + 5).buzz?, false)
127
+ end
128
+
129
+ def test_bignum_is_fizzbuzz
130
+ assert_equal((@bignum + 5).fizzbuzz?, true)
131
+ assert_equal((@bignum + 2).fizzbuzz?, false)
132
+ assert_equal((@bignum + 15).fizzbuzz?, false)
133
+ end
134
+
135
+ def test_array_integration
136
+ assert([].respond_to?(:fizzbuzz))
137
+ end
138
+
139
+ def test_array_integration_fizzbuzz
140
+ obtainted = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].fizzbuzz
141
+ assert_equal(obtainted, DEFAULT_EXPECTED)
142
+ end
143
+
144
+ def test_array_integration_fizzbuzz_reverse
145
+ obtainted = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].fizzbuzz(true)
146
+ assert_equal(obtainted, DEFAULT_EXPECTED.reverse)
147
+ end
148
+
149
+ def test_array_integration_fizzbuzz_block
150
+ obtainted = []
151
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].fizzbuzz {|i| obtainted << i }
152
+ assert_equal(obtainted, DEFAULT_EXPECTED)
153
+ end
154
+
155
+ def test_array_integration_fizzbuzz_block_reverse
156
+ obtainted = []
157
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].fizzbuzz(true) {|i| obtainted << i }
158
+ assert_equal(obtainted, DEFAULT_EXPECTED.reverse)
159
+ end
160
+
161
+ def test_range_integration
162
+ assert((0..0).respond_to?(:fizzbuzz))
163
+ end
164
+
165
+ def test_range_integration_fizzbuzz
166
+ obtainted = (DEFAULT_START .. DEFAULT_STOP).fizzbuzz
167
+ assert_equal(obtainted, DEFAULT_EXPECTED)
168
+ end
169
+
170
+ def test_range_integration_fizzbuzz_reverse
171
+ obtainted = (DEFAULT_START .. DEFAULT_STOP).fizzbuzz(true)
172
+ assert_equal(obtainted, DEFAULT_EXPECTED.reverse)
173
+ end
174
+
175
+ def test_range_integration_fizzbuzz_block
176
+ obtainted = []
177
+ (DEFAULT_START .. DEFAULT_STOP).fizzbuzz {|i| obtainted << i }
178
+ assert_equal(obtainted, DEFAULT_EXPECTED)
179
+ end
180
+
181
+ def test_range_integration_fizzbuzz_block_reverse
182
+ obtainted = []
183
+ (DEFAULT_START .. DEFAULT_STOP).fizzbuzz(true) {|i| obtainted << i }
184
+ assert_equal(obtainted, DEFAULT_EXPECTED.reverse)
185
+ end
186
+
187
+ def test_singleton_fizzbuzz_incorrect_range_error
188
+ assert_raise FizzBuzz::RangeError do
189
+ FizzBuzz.fizzbuzz(2, 1)
190
+ end
191
+ end
192
+
193
+ def test_singleton_fizzbuzz_incorrect_type_error
194
+ assert_raise FizzBuzz::TypeError do
195
+ FizzBuzz.fizzbuzz('', '')
196
+ end
197
+ end
198
+
199
+ def test_singleton_fizzbuzz_integer
200
+ integer = @integer + 14
201
+ obtainted = FizzBuzz.fizzbuzz(integer, integer)
202
+ assert_equal(obtainted, ['FizzBuzz'])
203
+ end
204
+
205
+ def test_singleton_fizzbuzz_bignum
206
+ bignum = @bignum + 5
207
+ obtainted = FizzBuzz.fizzbuzz(bignum, bignum)
208
+ assert_equal(obtainted, ['FizzBuzz'])
209
+ end
210
+
211
+ def test_singleton_fizzbuzz_large_bignum
212
+ bignum = @large_bignum + 5
213
+ obtainted = FizzBuzz.fizzbuzz(bignum, bignum)
214
+ assert_equal(obtainted, ['FizzBuzz'])
215
+ end
216
+
217
+ def test_singleton_fizzbuzz_array
218
+ obtained = FizzBuzz.fizzbuzz(DEFAULT_START, DEFAULT_STOP)
219
+ assert_equal(obtained, DEFAULT_EXPECTED)
220
+ end
221
+
222
+ def test_singleton_fizzbuzz_array_reverse
223
+ obtained = FizzBuzz.fizzbuzz(DEFAULT_START, DEFAULT_STOP, true)
224
+ assert_equal(obtained, DEFAULT_EXPECTED.reverse)
225
+ end
226
+
227
+ def test_singleton_fizzbuzz_block
228
+ obtainted = []
229
+ FizzBuzz.fizzbuzz(DEFAULT_START, DEFAULT_STOP) {|i| obtainted << i }
230
+ assert_equal(obtainted, DEFAULT_EXPECTED)
231
+ end
232
+
233
+ def test_singleton_fizzbuzz_block_reverse
234
+ obtainted = []
235
+ FizzBuzz.fizzbuzz(DEFAULT_START, DEFAULT_STOP, true) {|i| obtainted << i }
236
+ assert_equal(obtainted, DEFAULT_EXPECTED.reverse)
237
+ end
238
+
239
+ def test_singleton_squre
240
+ obtained_fizz = FizzBuzz[3]
241
+ obtained_buzz = FizzBuzz[5]
242
+ obtained_fizzbuzz = FizzBuzz[15]
243
+
244
+ obtained = [obtained_fizz, obtained_buzz, obtained_fizzbuzz]
245
+
246
+ assert(FizzBuzz[@integer].class == Fixnum)
247
+ assert_block do
248
+ obtained.each_with_index.all? do |v,i|
249
+ v.is_a?(String) && obtained[i] == DEFAULT_WORDS[i]
250
+ end
251
+ end
252
+ end
253
+
254
+ def test_singleton_squre_bignum
255
+ obtained_fizz = FizzBuzz[@bignum + 2]
256
+ obtained_buzz = FizzBuzz[@bignum + 15]
257
+ obtained_fizzbuzz = FizzBuzz[@bignum + 5]
258
+
259
+ obtained = [obtained_fizz, obtained_buzz, obtained_fizzbuzz]
260
+
261
+ assert(FizzBuzz[@bignum + 3].class == Bignum)
262
+ assert_block do
263
+ obtained.each_with_index.all? do |v,i|
264
+ v.is_a?(String) && obtained[i] == DEFAULT_WORDS[i]
265
+ end
266
+ end
267
+ end
268
+
269
+ def test_singleton_squre_large_bignum
270
+ obtained_fizz = FizzBuzz[@large_bignum + 2]
271
+ obtained_buzz = FizzBuzz[@large_bignum + 15]
272
+ obtained_fizzbuzz = FizzBuzz[@large_bignum + 5]
273
+
274
+ obtained = [obtained_fizz, obtained_buzz, obtained_fizzbuzz]
275
+
276
+ assert(FizzBuzz[@large_bignum + 3].class == Bignum)
277
+ assert_block do
278
+ obtained.each_with_index.all? do |v,i|
279
+ v.is_a?(String) && obtained[i] == DEFAULT_WORDS[i]
280
+ end
281
+ end
282
+ end
283
+
284
+ def test_singleton_is_fizz
285
+ assert_equal(FizzBuzz.is_fizz?(3), true)
286
+ assert_equal(FizzBuzz.is_fizz?(15), false)
287
+ end
288
+
289
+ def test_singleton_is_buzz
290
+ assert_equal(FizzBuzz.is_buzz?(5), true)
291
+ assert_equal(FizzBuzz.is_buzz?(15), false)
292
+ end
293
+
294
+ def test_singleton_is_fizzbuzz
295
+ assert_equal(FizzBuzz.is_fizzbuzz?(15), true)
296
+ assert_equal(FizzBuzz.is_fizzbuzz?(3), false)
297
+ assert_equal(FizzBuzz.is_fizzbuzz?(5), false)
298
+ end
299
+
300
+ def test_fizzbuzz_for_0
301
+ obtained_square = FizzBuzz[0]
302
+ obtained_is_fizzbuzz = FizzBuzz.is_fizzbuzz?(0)
303
+
304
+ assert(obtained_square.is_a?(Integer) && obtained_square == 0)
305
+ assert(obtained_is_fizzbuzz.is_a?(FalseClass) && obtained_is_fizzbuzz == false)
306
+ end
307
+
308
+ def test_to_a
309
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
310
+ assert_equal(fb.to_a, DEFAULT_EXPECTED)
311
+ end
312
+
313
+ def test_each
314
+ obtainted = []
315
+
316
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
317
+ fb.each {|i| obtainted << i }
318
+
319
+ assert_equal(obtainted, DEFAULT_EXPECTED)
320
+ end
321
+
322
+ def test_reverse_each
323
+ obtainted = []
324
+
325
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
326
+ fb.reverse_each {|i| obtainted << i }
327
+
328
+ assert_equal(obtainted, DEFAULT_EXPECTED.reverse)
329
+ end
330
+
331
+ def test_for_fizzbuzz_integer
332
+ fb = FizzBuzz.new(DEFAULT_START, 15)
333
+ assert_equal(fb.to_a[14], 'FizzBuzz')
334
+ end
335
+
336
+ def test_for_fizzbuzz_bignum
337
+ fb = FizzBuzz.new(@bignum + 5, @bignum + 5)
338
+ assert_equal(fb.to_a[0], 'FizzBuzz')
339
+ end
340
+
341
+ def test_for_fizzbuzz_large_bignum
342
+ fb = FizzBuzz.new(@large_bignum + 5, @large_bignum + 5)
343
+ assert_equal(fb.to_a[0], 'FizzBuzz')
344
+ end
345
+
346
+ def test_correct_start_integer
347
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
348
+
349
+ assert(fb.start.is_a?(Integer) && fb.start == 1)
350
+ fb.start = 2
351
+ assert(fb.start.is_a?(Integer) && fb.start == 2)
352
+ end
353
+
354
+ def test_correct_start_bignum
355
+ fb = FizzBuzz.new(@bignum, @bignum)
356
+
357
+ assert_block do
358
+ fb.start.is_a?(Bignum) && fb.start == @bignum
359
+ fb.start = @bignum - 5
360
+ fb.start.is_a?(Bignum) && fb.start == (@bignum - 5)
361
+ end
362
+ end
363
+
364
+ def test_correct_start_large_bignum
365
+ fb = FizzBuzz.new(@large_bignum, @large_bignum)
366
+
367
+ assert(fb.start.is_a?(Bignum) && fb.start == @large_bignum)
368
+ fb.start = @large_bignum - 5
369
+ assert(fb.start.is_a?(Bignum) && fb.start == (@large_bignum - 5))
370
+ end
371
+
372
+ def test_correct_stop_integer
373
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
374
+
375
+ assert(fb.stop.is_a?(Integer) && fb.stop == 15)
376
+ fb.stop = 5
377
+ assert(fb.stop.is_a?(Integer) && fb.stop == 5)
378
+ end
379
+
380
+ def test_correct_stop_bignum
381
+ fb = FizzBuzz.new(@bignum, @bignum)
382
+
383
+ assert(fb.stop.is_a?(Bignum) && fb.stop == @bignum)
384
+ fb.stop = @bignum + 5
385
+ assert(fb.stop.is_a?(Bignum) && fb.stop == (@bignum + 5))
386
+ end
387
+
388
+ def test_correct_stop_large_bignum
389
+ fb = FizzBuzz.new(@large_bignum, @large_bignum)
390
+
391
+ assert(fb.stop.is_a?(Bignum) && fb.stop == @large_bignum)
392
+ fb.stop = @large_bignum + 5
393
+ assert(fb.stop.is_a?(Bignum) && fb.stop == (@large_bignum + 5))
394
+ end
395
+
396
+ def test_missing_arguments
397
+ assert_raise ArgumentError do
398
+ FizzBuzz.new
399
+ end
400
+ end
401
+
402
+ def test_arguments_type_error_strings
403
+ assert_raise FizzBuzz::TypeError do
404
+ FizzBuzz.new('', '')
405
+ end
406
+ end
407
+
408
+ def test_arguments_type_error_nils
409
+ assert_raise FizzBuzz::TypeError do
410
+ FizzBuzz.new(nil, nil)
411
+ end
412
+ end
413
+
414
+ def test_incorrect_range_error
415
+ assert_raise FizzBuzz::RangeError do
416
+ FizzBuzz.new(2, 1)
417
+ end
418
+ end
419
+
420
+ def test_start_type_error
421
+ assert_raise FizzBuzz::TypeError do
422
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
423
+ fb.start = ''
424
+ end
425
+ end
426
+
427
+ def test_stop_type_error
428
+ assert_raise FizzBuzz::TypeError do
429
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
430
+ fb.stop = ''
431
+ end
432
+ end
433
+
434
+ def test_correct_start_stop
435
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
436
+
437
+ fb.start = 2
438
+ fb.stop = 3
439
+
440
+ assert(fb.start.is_a?(Integer) && fb.start == 2)
441
+ assert(fb.stop.is_a?(Integer) && fb.stop == 3)
442
+ end
443
+
444
+ def test_start_range_error
445
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
446
+
447
+ assert_raise FizzBuzz::RangeError do
448
+ fb.start = 16
449
+ end
450
+ end
451
+
452
+ def test_stop_range_error
453
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
454
+
455
+ assert_raise FizzBuzz::RangeError do
456
+ fb.stop = -1
457
+ end
458
+ end
459
+
460
+ def test_for_range_error
461
+ assert_nothing_raised do
462
+ FizzBuzz.new(DEFAULT_START, @bignum ** 2)
463
+ FizzBuzz.new(-@large_bignum, DEFAULT_STOP)
464
+ end
465
+ end
466
+
467
+ def test_error_attributes_not_nil
468
+ fb = FizzBuzz.new(DEFAULT_START, DEFAULT_STOP)
469
+
470
+ begin
471
+ fb.start = 1
472
+ fb.stop = 0
473
+ rescue FizzBuzz::RangeError => error
474
+ assert_equal(error.start, 1)
475
+ assert_equal(error.stop, 0)
476
+ end
477
+ end
478
+
479
+ def test_error_attributes_nil
480
+ begin
481
+ FizzBuzz.new('', '')
482
+ rescue FizzBuzz::TypeError => error
483
+ assert_equal(error.start, nil)
484
+ assert_equal(error.stop, nil)
485
+ end
486
+ end
487
+
488
+ def test_start_type_error_message
489
+ begin
490
+ FizzBuzz.new('', DEFAULT_STOP)
491
+ rescue FizzBuzz::TypeError => error
492
+ assert_equal(error.message, 'must be an Integer or Bignum type for start')
493
+ end
494
+ end
495
+
496
+ def test_stop_type_error_message
497
+ begin
498
+ FizzBuzz.new(DEFAULT_START, '')
499
+ rescue FizzBuzz::TypeError => error
500
+ assert_equal(error.message, 'must be an Integer or Bignum type for stop')
501
+ end
502
+ end
503
+
504
+ def test_range_error_message
505
+ begin
506
+ FizzBuzz.new(DEFAULT_STOP, DEFAULT_START)
507
+ rescue FizzBuzz::RangeError => error
508
+ assert_equal(error.message, 'start value is higher than stop value')
509
+ end
510
+ end
511
+ end
512
+
513
+ # vim: set ts=2 sw=2 sts=2 et :
514
+ # encoding: utf-8
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-fizzbuzz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Wilczynski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-18 00:00:00.000000000 Z
11
+ date: 2013-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -89,10 +89,15 @@ files:
89
89
  - ext/fizzbuzz/fizzbuzz.h
90
90
  - ext/fizzbuzz/extconf.rb
91
91
  - lib/fizzbuzz/bignum.rb
92
+ - lib/fizzbuzz/array.rb
92
93
  - lib/fizzbuzz/integer.rb
94
+ - lib/fizzbuzz/range.rb
93
95
  - lib/fizzbuzz/version.rb
94
96
  - lib/fizzbuzz.rb
97
+ - benchmark/benchmark_fizzbuzz.rb
98
+ - test/test_fizzbuzz.rb
95
99
  - Rakefile
100
+ - ruby-fizzbuzz.gemspec
96
101
  - AUTHORS
97
102
  - CHANGES
98
103
  - CHANGES.rdoc