This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 320
/
ExSwiftArrayTests.swift
222 lines (169 loc) · 5.05 KB
/
ExSwiftArrayTests.swift
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// ExtensionsTests.swift
// ExtensionsTests
//
// Created by pNre on 03/06/14.
// Copyright (c) 2014 pNre. All rights reserved.
//
import XCTest
class ExtensionsArrayTests: XCTestCase {
var array: Array<Int> = []
override func setUp() {
super.setUp()
array = [1, 2, 3, 4, 5]
}
func testReject () {
var odd = array.reject({
return $0 % 2 == 0
})
XCTAssert(odd == [1, 3, 5])
}
func testEach() {
var result = Array<Int>()
array.each({
result.append($0)
})
XCTAssert(result == array)
result.removeAll(keepCapacity: true)
array.each({
(index: Int, item: Int) in
result.append(index)
})
XCTAssert(result == array.map( { return $0 - 1 } ))
}
func testRange() {
XCTAssert(Array<Int>.range(0..2) == [0, 1])
XCTAssert(Array<Int>.range(0...2) == [0, 1, 2])
}
func testContains() {
XCTAssertFalse(array.contains("A"))
XCTAssertFalse(array.contains(6))
XCTAssertTrue(array.contains(5))
}
func testFirst() {
XCTAssertEqual(1, array.first()!)
}
func testLast() {
XCTAssertEqual(5, array.last()!)
}
func testDifference() {
var diff1 = array.difference([3, 4])
var diff2 = array - [3, 4]
var diff3 = array.difference([3], [5])
XCTAssert(diff1 == [1, 2, 5])
XCTAssert(diff2 == [1, 2, 5])
XCTAssert(diff3 == [1, 2, 4])
}
func testIndexOf() {
XCTAssertEqual(0, array.indexOf(1))
XCTAssertEqual(-1, array.indexOf(6))
XCTAssertEqual(3, array.indexOf(4))
}
func testIntersection() {
XCTAssert(array.intersection([]) == [])
XCTAssert(array.intersection([1]) == [1])
XCTAssert(array.intersection([1, 2], [1, 2], [1, 3]) == [1])
}
func testUnion() {
XCTAssert(array.union([1]) == array)
XCTAssert(array.union([]) == array)
XCTAssert(array.union([6]) == [1, 2, 3, 4, 5, 6])
}
func testZip() {
var zip1 = [1, 2].zip(["A", "B"])
var a = zip1[0][0] as Int
var b = zip1[0][1] as String
XCTAssertEqual(1, a)
XCTAssertEqual("A", b)
a = zip1[1][0] as Int
b = zip1[1][1] as String
XCTAssertEqual(2, a)
XCTAssertEqual("B", b)
}
func testSample() {
var singleSample = array.sample()
var longerSample = array.sample(size: 2)
XCTAssertEqual(1, singleSample.count)
XCTAssertEqual(2, longerSample.count)
}
func testSubscriptRange() {
XCTAssert(array[0..0] == [])
XCTAssert(array[0..1] == [1])
XCTAssert(array[0..2] == [1, 2])
}
func testShuffled() {
var shuffled = array.shuffled()
XCTAssert(shuffled.difference(array) == [])
}
func testShuffle() {
var toShuffle = array.copy()
toShuffle.shuffle()
XCTAssert(toShuffle.difference(array) == [])
}
func testMax() {
XCTAssertEqual(5, array.max() as Int)
}
func testMin() {
XCTAssertEqual(1, array.min() as Int)
}
func testTake() {
XCTAssert(array.take(3) == [1, 2, 3])
XCTAssert(array.take(0) == [])
}
func testPop() {
XCTAssertEqual(5, array.pop())
XCTAssert(array == [1, 2, 3, 4])
}
func testPush() {
array.push(6)
XCTAssertEqual(6, array.last()!)
}
func testShift() {
XCTAssertEqual(1, array.shift())
XCTAssert(array == [2, 3, 4, 5])
}
func testUnshift() {
array.unshift(0)
XCTAssertEqual(0, array.first()!)
}
func testRemove() {
array.append(array.last()!)
array.remove(array.last()!)
XCTAssert(array == [1, 2, 3, 4])
}
func testUnique() {
let arr = [1, 1, 1, 2, 3]
XCTAssert(arr.unique() as Array<Int> == [1, 2, 3])
}
func testGroupBy() {
let group = array.groupBy(groupingFunction: {
(value: Int) -> Bool in
return value > 3
})
XCTAssert(Array(group.keys) == [false, true])
XCTAssert(Array(group[true]!) == [4, 5])
XCTAssert(Array(group[false]!) == [1, 2, 3])
}
func testReduceRight () {
let list = [[0, 1], [2, 3], [4, 5]];
let flat = list.reduceRight(Array<Int>(), { return $0 + $1 });
XCTAssert(flat == [4, 5, 2, 3, 0, 1])
}
func testImplode () {
XCTAssert(["A", "B", "C"].implode("A") == "AABAC")
}
func testPluck () {
let values = [
["Name": "Bob", "Score": 6],
["Name": "Tim", "Score": 8]
]
let result = values.pluck("Score") as Int[]
println(result)
}
/*
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
}
}*/
}