robolson (owner)

Revisions

gist: 69461 Download_button fork
public
Public Clone URL: git://gist.github.com/69461.git
Embed All Files: show embed
set.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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