summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rake/test_rake_linked_list.rb
blob: 10957fba6d1a7f89fca83a1d71511cfc18c9c5df (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require File.expand_path('../helper', __FILE__)

class TestLinkedList < Rake::TestCase
  include Rake

  def test_empty_list
    empty = LinkedList::EMPTY
    assert empty.empty?, "should be empty"
  end

  def test_list_with_one_item
    list = LinkedList.make(:one)
    assert ! list.empty?, "should not be empty"
    assert_equal :one, list.head
    assert_equal LinkedList::EMPTY, list.tail
  end

  def test_make_with_no_arguments
    empty = LinkedList.make()
    assert_equal LinkedList::EMPTY, empty
  end

  def test_make_with_one_argument
    list = LinkedList.make(:one)
    assert ! list.empty?
    assert_equal :one, list.head
    assert_equal LinkedList::EMPTY, list.tail
  end

  def test_make_with_two_arguments
    list = LinkedList.make(:one, :two)
    assert ! list.empty?
    assert_equal :one, list.head
    assert_equal :two, list.tail.head
    assert_equal LinkedList::EMPTY, list.tail.tail
  end

  def test_list_with_several_items
    list = LinkedList.make(:one, :two, :three)

    assert ! list.empty?, "should not be empty"
    assert_equal :one, list.head
    assert_equal :two, list.tail.head
    assert_equal :three, list.tail.tail.head
    assert_equal LinkedList::EMPTY, list.tail.tail.tail
  end

  def test_lists_are_structurally_equivalent
    list = LinkedList.make(1, 2, 3)
    same = LinkedList.make(1, 2, 3)
    diff = LinkedList.make(1, 2, 4)
    short = LinkedList.make(1, 2)

    assert_equal list, same
    refute_equal list, diff
    refute_equal list, short
    refute_equal short, list
  end

  def test_converstion_to_string
    list = LinkedList.make(:one, :two, :three)
    assert_equal "LL(one, two, three)", list.to_s
    assert_equal "LL()", LinkedList.make().to_s
  end

  def test_converstion_with_inspect
    list = LinkedList.make(:one, :two, :three)
    assert_equal "LL(:one, :two, :three)", list.inspect
    assert_equal "LL()", LinkedList.make().inspect
  end

  def test_lists_are_enumerable
    list = LinkedList.make(1, 2, 3)
    new_list = list.map { |item| item + 10 }
    expected = [11, 12, 13]
    assert_equal expected, new_list
  end

  def test_conjunction
    list = LinkedList.make.conj("C").conj("B").conj("A")
    assert_equal LinkedList.make("A", "B", "C"), list
  end

end