Skip to content

Instantly share code, notes, and snippets.

@Sheeo
Created December 7, 2010 18:01
Show Gist options
  • Save Sheeo/732149 to your computer and use it in GitHub Desktop.
Save Sheeo/732149 to your computer and use it in GitHub Desktop.
Awesomeness
# Ruby-RSpec klasse brugt til at beskrive MultiSet-java klassen med
#
# For at køre:
# make spec
#
# Kræver:
# jruby (http://jruby.org)
# rspec (gem install rspec)
#
include Java
require 'rubygems'
require 'rspec'
import 'MultiSet'
include_class Java::MultiSet
describe MultiSet do
let(:multiset) { MultiSet.new }
describe "MultiSet()" do
it "should initialize an empty multiset" do
multiset.size.should equal(0)
end
it "should initialize a non-empty multiset" do
multiset = MultiSet.new([5,2,3])
multiset.size.should equal(3)
end
end
describe "add()" do
it "should add an item to the multiset" do
multiset.add(2)
multiset.size.should equal(1)
end
end
describe "remove()" do
it "should remove an item from the multiset" do
multiset.add(2)
multiset.remove(2)
multiset.size.should equal(0)
end
end
describe "addAll()" do
it "should add all elements from a collection" do
multiset.addAll([5,2,3,2])
multiset.size.should equal(4)
end
end
describe "hashCode()" do
it "should return a hashcode of the multiset" do
multiset.addAll([1,1,1,1])
multiset.hashCode.should_not equal(nil)
end
it "should return the same hashcode for identical objects" do
multiset.addAll([1,2,3,4])
multiset.hashCode.should == MultiSet.new([1,2,3,4]).hashCode
end
it "should not return the same hashcode for different objects" do
multiset.addAll([1,2,3,4])
multiset.hashCode.should_not == MultiSet.new([1,1,2]).hashCode
end
end
describe "toString()" do
{
[5,1,2,5] => "{1=1, 2=1, 5=2}",
[5,2,1] => "{1=1, 2=1, 5=1}",
[5,5,4,4,1,1,1,1,2] => "{1=4, 2=1, 4=2, 5=2}",
["foo", "bar", "goat"] => "{goat=1, foo=1, bar=1}",
["foo", "foo", "goat"] => "{goat=1, foo=2}"
}.each do |inputCollection, toStringValue|
context " with #{inputCollection.join(" ")}" do
let(:multiset) {MultiSet.new(inputCollection)}
it "prints #{toStringValue}" do
multiset.toString.should == toStringValue
end
end
end
end
describe "iterator()" do
let(:multiset) {MultiSet.new([5,4,3,2,2,1,0])}
it "should return an iterator for MultiSet" do
iterator = multiset.iterator
iterator.hasNext.should == true
iterator.next.should == 0 # apparently the array is reversed
iterator.next.should == 1
end
describe "MultiIterator" do
describe "remove()" do
it "should remove elements from the MultiSet" do
pending "MultiIterator is weird"
iterator = multiset.iterator
iterator.next
iterator.remove
multiset.size.should == 6
end
end
end
end
end
@Zatnosk
Copy link

Zatnosk commented Dec 8, 2010

Det ser lækkert ud! Jeg undrer mig bare over en enkelt ting. Der hvor du tester hashCode() og siger den skal være lig med en fast værdi, er det ikke en ret implementations specifik test?

Jeg ved ikke om det er fornuftigt, men man kunne lave en test udfra specifikationen af hashCode, der siger noget i retning af at to ens objekter, skal have ens hashcode, to forskellige burde have forskellig hashcode, og hashcoden for et objekt skal være konstant gennem et program.

Det er udelukkende for at udfordre dig lidt ekstra :P

@Sheeo
Copy link
Author

Sheeo commented Dec 8, 2010

Hehe, du har ret--jeg har faktisk også rettet spec'en til hashCode.should_not equal(nil).

Jeg har også lige læst spec'en--skriver lige som du foreslår :)

@Sheeo
Copy link
Author

Sheeo commented Dec 8, 2010

Er lidt inkonsistent i min brug af == og equal().

Det er tilsyneladende omvendt her--equal() tester objekt-identitet (hvilket med integer osv stadig er det samme), == teste værdierne.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment