Skip to content

Instantly share code, notes, and snippets.

@woodie
Last active December 12, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodie/4737452 to your computer and use it in GitHub Desktop.
Save woodie/4737452 to your computer and use it in GitHub Desktop.
# jruby -S rspec -fs spec/set_spec.rb
describe Set, 'once created' do
before(:each) do
@capacity = 6
@set = Java::cis::Set.new(@capacity)
end
it 'should print an empty set' do
@set.to_string.should eq "{ }"
end
it 'should populated a unique set with insert()' do
[3,9,9,9,6].each { |n| @set.insert(n) }
@set.to_string.should eq "{ 3, 9, 6 }"
end
it 'should report empty' do
[3,9].each { |n| @set.insert(n) }
[3,9].each { |n| @set.remove(n) }
@set.should be_empty
end
it 'should report full' do
1.upto(@capacity) { |n| @set.insert(n) }
@set.should be_full
end
it 'should print a trimmed set after remove()' do
[1,3,5,7,9].each { |n| @set.insert(n) }
@set.should be_remove(3)
@set.should be_remove(7)
@set.should_not be_remove(99)
@set.to_string.should eq "{ 1, 5, 9 }"
end
it 'should raise exception on insert() when Set is full' do
1.upto(@capacity) { |n| @set.insert(n) }
expect { @set.insert(99) }.to raise_error(Java::cis::SetException)
end
it 'should contain item 5 and not 3' do
@set.insert(5)
@set.contains(5).should be_true
@set.contains(3).should_not be_true
end
it 'should unite two sets' do
[1,2,3].each { |n| @set.insert(n) }
aSet = Java::cis::Set.new(6) # Fine
[4,5,6].each { |n| aSet.insert(n) }
@set.union(aSet).to_string.should eq "{ 4, 5, 6, 1, 2, 3 }"
end
it 'should raise exception when union hits capacity' do
[1,2,3].each { |n| @set.insert(n) }
aSet = Java::cis::Set.new(4) # Small
[4,5,6].each { |n| aSet.insert(n) }
expect { @set.union(aSet) }.to raise_error(Java::cis::SetException)
end
it 'should identify the intersection' do
[1,2,3,4,5].each { |n| @set.insert(n) }
aSet = Java::cis::Set.new()
[2,4].each { |n| aSet.insert(n) }
@set.intersection(aSet).to_string.should eq "{ 2, 4 }"
end
it 'should identify valid subset' do
[1,3,5,7,9].each { |n| @set.insert(n) }
aSet = Java::cis::Set.new(3)
[3,5,7].each { |n| aSet.insert(n) }
@set.should be_subset(aSet)
end
it 'should identify invalid subset' do
[1,3,5,7,9].each { |n| @set.insert(n) }
aSet = Java::cis::Set.new(3)
[2,4,6].each { |n| aSet.insert(n) }
@set.should_not be_subset(aSet)
end
it 'should be equal to a similar set' do
[3,6,9].each { |n| @set.insert(n) }
aSet = Java::cis::Set.new(@capacity)
[9,6,3].each { |n| aSet.insert(n) }
@set.should be_equals(aSet)
end
it 'should produce identical clone' do
[3,9,6].each { |n| @set.insert(n) }
aSet = @set.clone();
aSet.to_string.should eq "{ 3, 9, 6 }"
end
end
__END__
$ jruby -S rspec -fs spec/set_spec.rb
Set once created
should print an empty set
should populated a unique set with insert()
should report empty
should report full
should print a trimmed set after remove()
should raise exception on insert() when Set is full
should contain item 5 and not 3
should unite two sets
should raise exception when union hits capacity
should identify the intersection
should identify valid subset
should identify invalid subset
should be equal to a similar set
should produce identical clone
Finished in 0.157 seconds
14 examples, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment