-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_test.rb
166 lines (131 loc) · 3.71 KB
/
linked_list_test.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require "minitest/autorun"
require "minitest/pride"
require "./LinkedList"
require 'pry'
class LinkedListTest < Minitest::Test
def test_empty_list_head_nil
list = LinkedList.new
assert_equal nil, list.head
end
def test_append_returns_node_and_has_corerct_data
list = LinkedList.new
list.append("doop")
result = list.head.class
result_two = list.head.data
assert_equal Node, result
assert_equal "doop", result_two
end
def test_single_element_to_string_returns_data
list = LinkedList.new
list.append("doop")
result = list.to_string
assert_equal "doop", result
end
def test_multiple_nodes_to_string_returns_nodes_with_space
list = LinkedList.new
list.append("doop")
list.append("beep")
list.append("bop")
assert_equal "doop beep bop", list.to_string
end
def test_find_tail_and_append_multiple_nodes
list = LinkedList.new
list.append("doop")
list.append("deep")
list.append("boop")
list.append("beep")
assert_equal "beep", list.find_tail.data
assert_equal "deep", list.head.next_node.data
assert_equal "boop", list.head.next_node.next_node.data
assert_equal "doop", list.head.data
end
def test_counter_one_element
list = LinkedList.new
list.append("boop")
assert_equal 1, list.count
end
def test_counter_multiple_elements
list = LinkedList.new
list.append("beep")
list.append("boop")
list.append("doop")
assert_equal 3, list.count
list.append("sure")
assert_equal 4, list.count
end
def test_prepend_updated_list_data
list = LinkedList.new
list.append("beep")
list.append("boop")
list.prepend("doop")
assert_equal "doop", list.head.data
assert_equal "beep", list.head.next_node.data
assert_equal "boop", list.find_tail.data
assert_equal nil, list.find_tail.next_node
end
def test_insert_into_first_position_becomes_head
list = LinkedList.new
list.insert(0,"beep")
assert_equal "beep", list.head.data
end
def test_insert_into_middle_of_list_updates_count
list = LinkedList.new
list.append("beep")
list.append("boop")
list.append("doop")
list.append("deep")
assert_equal 4, list.count
list.insert(2, "jeep")
assert_equal 5, list.count
end
def test_insert_produces_correct_order
list = LinkedList.new
list.append("beep")
list.append("boop")
list.append("doop")
list.append("deep")
list.insert(1, "jeep")
second_node = list.head.next_node
third_node = list.head.next_node.next_node
assert_equal "beep", list.head.data
assert_equal "jeep", second_node.data
assert_equal "boop", third_node.data
assert_equal "deep", list.find_tail.data
end
def test_includes_method_finds_data
list = LinkedList.new
list.append("beep")
list.append("boop")
list.append("doop")
assert_equal true, list.includes?("boop")
assert_equal false, list.includes?("jeep")
end
def test_find_returns_proper_string
list = LinkedList.new
list.append("beep")
list.append("boop")
list.append("doop")
result = list.find(1,1)
result_two = list.find(2,2)
assert_equal "beep", result
assert_equal "boop doop", result_two
end
def test_pop_returns_tail_data
list = LinkedList.new
list.append("beep")
list.append("boop")
list.append("doop")
result = list.pop
assert_equal "doop", result
end
def test_invalid_word_method_removes_invalid_words
list = LinkedList.new
list.append("beep")
list.append("boop")
assert_equal 2, list.count
assert_equal "beep boop", list.to_string
list.append("Mississippi")
assert_equal 2, list.count
assert_equal "beep boop", list.to_string
end
end