Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Created July 18, 2012 08:16
Show Gist options
  • Select an option

  • Save wojtekmach/3134995 to your computer and use it in GitHub Desktop.

Select an option

Save wojtekmach/3134995 to your computer and use it in GitHub Desktop.
Set
require 'minitest/autorun'
class SetTest < MiniTest::Unit::TestCase
def setup
@set = Set.new
end
def test_size
assert_equal 0, @set.size
@set.add 42
assert_equal 1, @set.size
end
def test_include?
refute @set.include? 42
@set.add 42
assert @set.include? 42
end
def test_add
@set.add 13
@set.add 13
assert_equal 1, @set.size
end
def test_to_a
@set.add 1
@set.add 4
@set.add 2
ary = @set.to_a
assert_equal 3, ary.size
assert ary.include? 1
assert ary.include? 2
assert ary.include? 4
end
end
class Set
include Enumerable
def initialize
@hash = {}
end
def size
@hash.size
end
def add(obj)
@hash[obj] = true
end
def include?(obj)
@hash.include? obj
end
def each(&block)
@hash.keys.each(&block)
end
end
class SortedSetTest < SetTest
def setup
@set = SortedSet.new
end
def test_to_a
super
assert_equal [1, 2, 4], @set.to_a
end
end
class SortedSet < Set
def each(&block)
@hash.keys.sort.each(&block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment