-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathArrayTests.swift
More file actions
96 lines (81 loc) · 3.13 KB
/
Copy pathArrayTests.swift
File metadata and controls
96 lines (81 loc) · 3.13 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
87
88
89
90
91
92
93
94
95
96
import XCTest
@testable import WordPressShared
class ArrayTests: XCTestCase {
func testRepeatLast() {
let array = [1, 2, 3]
let repeated = Array(array.repeatingLast().prefix(5))
let expected = [1, 2, 3, 3, 3]
XCTAssertEqual(expected, repeated)
}
func testRepeatLastWithEmptyArray() {
let array = [Int]()
let repeated = Array(array.repeatingLast().prefix(5))
let expected = [Int]()
XCTAssertEqual(expected, repeated)
}
func testDifferentIndicesReturnsEmptyWithSameArray() {
let test = [1, 2, 3]
let result = test.differentIndices(test)
XCTAssert(result.isEmpty)
}
func testDifferentIndicesReturnsDifferencesWhenOtherSmaller() {
let test = [1, 2, 3]
let other = [1]
let result = test.differentIndices(other)
XCTAssertEqual(result, [1, 2])
}
func testDifferentIndicesReturnsDifferencesWhenOtherLarger() {
let test = [1, 2, 3]
let other = [1, 0, 3, 4]
let result = test.differentIndices(other)
XCTAssertEqual(result, [1])
}
func testSortedGroupEmptyArray() {
let test = [TestElement]()
let result = test.sortedGroup(keyPath: \TestElement.key)
XCTAssertEqual(result.count, 0)
}
func testSortedGroupCase1() {
let test: [TestElement] = [TestElement(key: "a", value: 1), TestElement(key: "a", value: 2)]
let result = test.sortedGroup(keyPath: \TestElement.key)
XCTAssertEqual(result[0].0, test[0].key)
XCTAssertEqual(result[0].1[0], test[0])
XCTAssertEqual(result[0].1[1], test[1])
}
func testSortedGroupCase2() {
let test: [TestElement] = [TestElement(key: "a", value: 1), TestElement(key: "b", value: 1)]
let result = test.sortedGroup(keyPath: \TestElement.key)
XCTAssertEqual(result[0].0, test[0].key)
XCTAssertEqual(result[1].0, test[1].key)
XCTAssertEqual(result[0].1[0], test[0])
XCTAssertEqual(result[1].1[0], test[1])
}
func testSortedGroupCase3() {
let test: [TestElement] = [TestElement(key: "a", value: 1), TestElement(key: "b", value: 1),
TestElement(key: "b", value: 2), TestElement(key: "c", value: 1)]
let result = test.sortedGroup(keyPath: \TestElement.key)
XCTAssertEqual(result[0].0, test[0].key)
XCTAssertEqual(result[1].0, test[1].key)
XCTAssertEqual(result[2].0, test[3].key)
XCTAssertEqual(result[0].1[0], test[0])
XCTAssertEqual(result[1].1[0], test[1])
XCTAssertEqual(result[1].1[1], test[2])
XCTAssertEqual(result[2].1[0], test[3])
}
func testUniqueRemovesDuplicates() {
let test = ["🦄", "🦄", "🌈"]
let result = test.unique
XCTAssertTrue(result.count == 2 && result.contains("🦄") && result.contains("🌈"))
}
}
class TestElement: Equatable {
var key: String
var value: Int
init(key: String, value: Int) {
self.key = key
self.value = value
}
}
func ==(lhs: TestElement, rhs: TestElement) -> Bool {
return lhs.key == rhs.key && lhs.value == rhs.value
}