Created
September 18, 2011 15:58
-
-
Save clr/1225198 to your computer and use it in GitHub Desktop.
Array Intersection RubyGames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IntegerMath | |
def self.intersection(array_1, array_2) | |
array_1 & array_2 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rspec' | |
if RUBY_VERSION == "1.9.1" | |
require 'spec/autorun' | |
end | |
require 'benchmark' | |
require File.join(File.dirname(__FILE__),'integer_math') | |
describe IntegerMath do | |
before(:all) do | |
@array_1 = Array.new(100000).collect{rand(10000000)}.uniq | |
@array_2 = Array.new(100000).collect{rand(10000000)}.uniq | |
@reports = [] | |
@reports << Benchmark.measure do | |
@correct_answer = (@array_1 & @array_2) | |
end | |
@reports << Benchmark.measure do | |
@my_answer = IntegerMath.intersection(@array_1, @array_2) | |
end | |
end | |
after(:all) do | |
puts '' | |
@reports.each{|r| puts r} | |
end | |
it "should have correct legth" do | |
@correct_answer.length.should == @my_answer.length | |
end | |
it "should have the correct elements" do | |
@correct_answer.sort.should == @my_answer.sort | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require 'benchmark' | |
require File.join(File.dirname(__FILE__),'integer_math') | |
class TestIntegerMath < MiniTest::Unit::TestCase | |
def setup | |
@array_1 = Array.new(100000).collect{rand(10000000)}.uniq | |
@array_2 = Array.new(100000).collect{rand(10000000)}.uniq | |
@reports = [] | |
@reports << Benchmark.measure do | |
@correct_answer = (@array_1 & @array_2) | |
end | |
@reports << Benchmark.measure do | |
@my_answer = IntegerMath.intersection(@array_1, @array_2) | |
end | |
end | |
def teardown | |
@reports.each{|r| puts r} | |
end | |
def test_intersection_answer_length | |
assert_equal @correct_answer.length, @my_answer.length | |
puts '' | |
end | |
def test_intersection_answer_elements | |
assert_equal @correct_answer.sort, @my_answer.sort | |
puts '' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment