-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_specs.rb
More file actions
86 lines (76 loc) · 2.56 KB
/
linked_list_specs.rb
File metadata and controls
86 lines (76 loc) · 2.56 KB
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
require 'rubygems'
require 'minitest/autorun'
require File.join(File.dirname(__FILE__), "/linked_lists")
describe "Likend lists Exercises" do
it "add values in a proper way" do
linked_list = Node.new(1)
linked_list.values.must_equal [1]
linked_list.add_to_tail(2)
linked_list.add_to_tail(3)
linked_list.add_to_tail(4)
linked_list.values.must_equal [1,2,3,4]
end
describe "#delete" do
it "does nothing when the value is not in the list" do
linked_list = Node.new(1)
linked_list.add_to_tail(2)
linked_list.add_to_tail(3)
linked_list.delete(3)
linked_list.values.must_equal [1,2]
end
it "deletes the element when is in the list" do
linked_list = Node.new(1)
linked_list.add_to_tail(2)
linked_list.add_to_tail(3)
linked_list = linked_list.delete(2)
linked_list.values.must_equal [1,3]
linked_list = linked_list.delete(3)
linked_list.values.must_equal [1]
linked_list = linked_list.delete(1)
linked_list.must_equal nil
end
end
describe "#delete duplicates" do
it "deletes the duplicates element when in the list" do
linked_list = Node.new(1)
linked_list.add_to_tail(2)
linked_list.add_to_tail(2)
linked_list.add_to_tail(3)
linked_list.add_to_tail(2)
linked_list.delete_duplicates
linked_list.values.must_equal [1,2,3]
end
end
describe "#nth_to_last_values" do
it "returns the values from nth to last elemnt of a linked list" do
linked_list = Node.new(1)
linked_list.add_to_tail(2)
linked_list.add_to_tail(3)
linked_list.add_to_tail(4)
linked_list.add_to_tail(5)
linked_list.nth_to_last_values(1).must_equal [2,3,4,5]
linked_list.nth_to_last_values(2).must_equal [3,4,5]
linked_list.nth_to_last_values(3).must_equal [4,5]
end
end
describe "#nth_to_last_values" do
it "returns the values from nth to last elemnt of a linked list" do
linked_list = Node.new(9)
linked_list.add_to_tail(2)
linked_list.add_to_tail(3)
linked_list_b = Node.new(2)
linked_list_b.add_to_tail(2)
linked_list_b.add_to_tail(4)
linked_list.sum_linked_lists(linked_list_b).values.must_equal([7,5,1])
##
linked_list = Node.new(3)
linked_list.add_to_tail(1)
linked_list.add_to_tail(5)
linked_list_b = Node.new(5)
linked_list_b.add_to_tail(9)
linked_list_b.add_to_tail(2)
linked_list_b.add_to_tail(2)
linked_list.sum_linked_lists(linked_list_b).values.must_equal([2,8,0,8])
end
end
end