Created
February 24, 2009 07:26
-
-
Save robolson/69461 to your computer and use it in GitHub Desktop.
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 'test/unit' | |
class Set | |
def initialize(initial=[]) | |
@contents = Hash.new | |
initial.each do |item| | |
@contents[item.to_s] = item | |
end | |
return self | |
end | |
def <<(*items) | |
items.flatten.each do |item| | |
@contents[item.to_s] = item unless @contents.has_key?(item) | |
end | |
return self | |
end | |
def ==(set2) | |
@contents == set2.contents | |
end | |
protected | |
def contents | |
@contents | |
end | |
end | |
class SetTest < Test::Unit::TestCase | |
def setup | |
@s = Set.new([1, 2]) | |
end | |
def test_only_add_once | |
@s << 1 | |
assert_equal Set.new([1, 2]), @s | |
end | |
def test_add_multiple | |
@s << [9, 8] | |
assert_equal Set.new([1, 2, 8, 9]), @s | |
end | |
def test_has_no_order | |
assert_equal Set.new([1, 2]), Set.new([2, 1]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment