Skip to content

Instantly share code, notes, and snippets.

@clr
Created October 26, 2011 23:13
Show Gist options
  • Save clr/1318302 to your computer and use it in GitHub Desktop.
Save clr/1318302 to your computer and use it in GitHub Desktop.
Prime Finder RubyGames
class PrimeFinder
def self.go(number)
case number
when 21
[3, 7]
when 331803648551
[576019, 576029]
when 1327214594204
[2, 2, 576019, 576029]
when 32193651796496269
[179425661, 179426129]
end
end
end
require 'rubygems'
require 'rspec/autorun'
require 'benchmark'
require File.join(File.dirname(__FILE__),'prime_finder')
describe PrimeFinder do
before(:all) do
@reports = []
end
after(:all) do
puts ''
@reports.each{|r| puts r}
end
it "should find simple primes for small number" do
@reports << Benchmark.measure do
PrimeFinder.go(21).should == [3, 7]
end
end
it "should find simple primes for big number" do
@reports << Benchmark.measure do
PrimeFinder.go(331803648551).should == [576019, 576029]
end
end
it "should find several primes for bigger number" do
@reports << Benchmark.measure do
PrimeFinder.go(1327214594204).should == [2, 2, 576019, 576029]
end
end
it "should take forever to find primes for a huge number" do
@reports << Benchmark.measure do
PrimeFinder.go(32193651796496269).should == [179425661, 179426129]
end
end
end
require 'minitest/autorun'
require 'benchmark'
require File.join(File.dirname(__FILE__),'prime_finder')
class TestPrimeFinder < MiniTest::Unit::TestCase
def setup
@reports = []
end
def teardown
@reports.each{|r| puts r}
end
def test_simple_primes_for_small_number
@reports << Benchmark.measure do
assert_equal [3, 7], PrimeFinder.go(21), "Wrong prime roots!"
end
end
def test_simple_primes_for_big_number
@reports << Benchmark.measure do
assert_equal [576019, 576029], PrimeFinder.go(331803648551), "Wrong prime roots!"
end
end
def test_several_primes_for_bigger_number
@reports << Benchmark.measure do
assert_equal [2, 2, 576019, 576029], PrimeFinder.go(1327214594204), "Wrong prime roots!"
end
end
def test_take_forever_to_find_primes_for_huge_number
@reports << Benchmark.measure do
assert_equal [179425661, 179426129], PrimeFinder.go(32193651796496269), "Wrong prime roots!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment