forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedListTests.m
126 lines (107 loc) · 3.52 KB
/
SinglyLinkedListTests.m
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
//
// SinglyLinkedListTests.m
// SinglyLinkedListTests
//
// Created by Wenru Dong on 2018/10/6.
// Copyright © 2018年 Wenru Dong. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "ListNode.h"
#import "SinglyLinkedList.h"
@interface SinglyLinkedListTests : XCTestCase
@end
@implementation SinglyLinkedListTests
{
SinglyLinkedList* _list;
NSArray* _nodes;
}
- (void)setUp {
[super setUp];
ListNode* node1 = [ListNode nodeWithValue:1];
ListNode* node2 = [ListNode nodeWithValue:2];
ListNode* node3 = [ListNode nodeWithValue:3];
ListNode* node4 = [ListNode nodeWithValue:4];
ListNode* node5 = [ListNode nodeWithValue:5];
ListNode* node6 = [ListNode nodeWithValue:6];
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
_list = [[SinglyLinkedList alloc] init];
_list.head = node1;
_nodes = [NSArray arrayWithObjects:node1, node2, node3, node4, node5, node6, nil];
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testNodeWithValue {
XCTAssertEqualObjects([_list nodeWithValue:1], _list.head);
XCTAssertNil([_list nodeWithValue:10]);
}
- (void)testNodeAtIndex {
XCTAssertEqualObjects([_list nodeAtIndex:4], _nodes[4]);
XCTAssertNil([_list nodeAtIndex:10]);
}
- (void)testInsertNodeWithValue {
[_list insertNodeWithValue:9];
XCTAssertEqual(_list.head.value, 9);
XCTAssertEqual(_list.head.next.value, 1);
}
- (void)testInsertNode {
ListNode* aNode = [ListNode nodeWithValue:7];
[_list insertNode:aNode];
XCTAssertEqualObjects(_list.head, aNode);
}
- (void)testInsertNodeWithValueAfterNode {
[SinglyLinkedList insertNodeWithValue:12 afterNode:_nodes[3]];
XCTAssertEqual([[_list nodeAtIndex:4] value], 12);
}
- (void)testInsertNodeAfterNode {
ListNode* aNode = [ListNode nodeWithValue:28];
[SinglyLinkedList insertNode:aNode afterNode:_nodes[5]];
ListNode* prevNode = (ListNode *)_nodes[5];
XCTAssertEqualObjects(aNode, prevNode.next);
}
- (void)testInsertNodeBeforeNode {
ListNode* aNode = [ListNode nodeWithValue:27];
ListNode* prevNode = (ListNode *)_nodes[3];
[_list insertNode:aNode beforeNode:_nodes[4]];
XCTAssertEqualObjects(aNode, prevNode.next);
}
- (void)testInsertNodeBeforeUnconnectedNode {
ListNode* aNode = [ListNode nodeWithValue:27];
ListNode* floatingNode = [ListNode nodeWithValue:36];
[_list insertNode:aNode beforeNode:floatingNode];
for (NSUInteger i = 0; i < 6; i++) {
XCTAssertEqualObjects([_list nodeAtIndex:i], _nodes[i]);
}
}
- (void)testDeleteNode {
[_list deleteNode:_nodes[0]];
XCTAssertEqual(_list.head.value, 2);
[_list deleteNode:_nodes[5]];
ListNode* lastNode = (ListNode *)_nodes[4];
XCTAssertNil(lastNode.next);
}
- (void)testDeleteNodesWithValue {
ListNode* firstNode = [ListNode nodeWithValue:1];
ListNode* secondNode = [ListNode nodeWithValue:1];
[_list insertNode:firstNode];
[_list insertNode:secondNode];
[_list deleteNodesWithValue:1];
for (NSUInteger i = 1; i < 6; i++) {
XCTAssertEqualObjects([_list nodeAtIndex:i-1], _nodes[i]);
}
}
- (void)testDebugDescription {
XCTAssertEqualObjects(_list.debugDescription, @"1->2->3->4->5->6");
}
//- (void)testPerformanceExample {
// // This is an example of a performance test case.
// [self measureBlock:^{
// // Put the code you want to measure the time of here.
// }];
//}
@end