forked from SwifterSwift/SwifterSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectionExtensionsTests.swift
80 lines (68 loc) · 2.11 KB
/
CollectionExtensionsTests.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
//
// CollectionExtensionsTests.swift
// SwifterSwift
//
// Created by Omar Albeik on 09/02/2017.
// Copyright © 2017 SwifterSwift
//
import XCTest
@testable import SwifterSwift
final class CollectionExtensionsTests: XCTestCase {
func testForEachInParallel() {
let collection = [1, 2, 3, 4, 5]
collection.forEachInParallel { item in
XCTAssert(collection.contains(item))
}
}
func testSafeSubscript() {
let collection = [1, 2, 3, 4, 5]
XCTAssertNotNil(collection[safe: 2])
XCTAssertEqual(collection[safe: 2], 3)
XCTAssertNil(collection[safe: 10])
}
func testDivided() {
// Array.divided(by:)
do {
let input = [0, 1, 2, 3, 4, 5]
let (even, odd) = input.divided { $0 % 2 == 0 }
XCTAssertEqual(even, [0, 2, 4])
XCTAssertEqual(odd, [1, 3, 5])
// Parameter names + indexes
let tuple = input.divided { $0 % 2 == 0 }
XCTAssertEqual(tuple.matching, [0, 2, 4])
XCTAssertEqual(tuple.0, [0, 2, 4])
XCTAssertEqual(tuple.nonMatching, [1, 3, 5])
XCTAssertEqual(tuple.1, [1, 3, 5])
}
// String.divided(by:)
do {
let input = "AaBbCc"
let tuple = input.divided { $0.isUppercased }
XCTAssertEqual(String(tuple.matching), "ABC")
XCTAssertEqual(String(tuple.nonMatching), "abc")
}
// Dictionary.divided(by:)
do {
let input: [String: Int] = ["a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5]
let tuple = input.divided { (_, value) in value % 2 == 0 }
/// The tuple contains keyvalue pairs in random order:
///
/// (
/// matching: [(key: "e", value: 4), (key: "a", value: 0), (key: "c", value: 2)],
/// nonMatching: [(key: "b", value: 1), (key: "f", value: 5), (key: "d", value: 3)]
/// )
// Convert keyvalue pairs to dictionary
var matchingDict = [String: Int]()
for (key, value) in tuple.matching {
matchingDict[key] = value
}
XCTAssertEqual(matchingDict, ["a": 0, "c": 2, "e": 4])
// Convert keyvalue pairs to dictionary
var nonMatchingDict = [String: Int]()
for (key, value) in tuple.nonMatching {
nonMatchingDict[key] = value
}
XCTAssertEqual(nonMatchingDict, ["b": 1, "d": 3, "f": 5])
}
}
}