Skip to content

Commit 6dcee60

Browse files
author
Dmitriy Ignatyev
committed
add partitionMap(_:) tests
1 parent 4190dec commit 6dcee60

File tree

2 files changed

+160
-3
lines changed

2 files changed

+160
-3
lines changed

Sources/Algorithms/PartitionMap.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public struct PartitionMapResult2<A, B> {
2727
@usableFromInline
2828
internal let oneOf: _PartitionMapResult2<A, B>
2929

30-
@usableFromInline
31-
internal init(oneOf: _PartitionMapResult2<A, B>) { self.oneOf = oneOf }
30+
@inlinable
31+
internal init(oneOf: _PartitionMapResult2<A, B>) {
32+
self.oneOf = oneOf
33+
}
3234

3335
@inlinable
3436
public static func first(_ value: A) -> Self {
@@ -55,7 +57,7 @@ public struct PartitionMapResult3<A, B, C> {
5557
@usableFromInline
5658
internal let oneOf: _PartitionMapResult3<A, B, C>
5759

58-
@usableFromInline
60+
@inlinable
5961
internal init(oneOf: _PartitionMapResult3<A, B, C>) {
6062
self.oneOf = oneOf
6163
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Algorithms open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import XCTest
13+
import Algorithms
14+
15+
final class PartitionMapTests: XCTestCase {
16+
func testPartitionMap2WithEmptyInput() {
17+
let input: [Int] = []
18+
19+
let (first, second) = input.partitionMap { _ -> PartitionMapResult2<Int, String> in
20+
.first(0)
21+
}
22+
23+
XCTAssertTrue(first.isEmpty)
24+
XCTAssertTrue(second.isEmpty)
25+
}
26+
27+
func testPartitionMap3WithEmptyInput() {
28+
let input: [Int] = []
29+
30+
let (first, second, third) = input.partitionMap { _ -> PartitionMapResult3<Int, String, Data> in
31+
.first(0)
32+
}
33+
34+
XCTAssertTrue(first.isEmpty)
35+
XCTAssertTrue(second.isEmpty)
36+
XCTAssertTrue(third.isEmpty)
37+
}
38+
39+
func testPartitionedExample() throws {
40+
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
41+
let (longNames, shortNames) = cast.partitioned(by: { $0.count < 5 })
42+
XCTAssertEqual(longNames, ["Vivien", "Marlon"])
43+
XCTAssertEqual(shortNames, ["Kim", "Karl"])
44+
}
45+
46+
func testPartitionMap2Example() throws {
47+
let nanString = String(describing: Double.nan)
48+
let numericStrings = ["", "^", "-1", "0", "1", "-1.5", "1.5", nanString]
49+
50+
let (doubles, unrepresentable) = numericStrings
51+
.partitionMap { string -> PartitionMapResult2<Double, String> in
52+
if let double = Double(string) {
53+
return .first(double)
54+
} else {
55+
return .second(string)
56+
}
57+
}
58+
59+
XCTAssertEqual(doubles.map(String.init(describing:)), ["-1.0", "0.0", "1.0", "-1.5", "1.5", nanString])
60+
XCTAssertEqual(unrepresentable, ["", "^"])
61+
}
62+
63+
func testPartitionMap3Example() throws {
64+
let nanString = String(describing: Double.nan)
65+
let numericStrings = ["", "^", "-1", "0", "1", "-1.5", "1.5", nanString]
66+
67+
let (integers, doubles, unrepresentable) = numericStrings
68+
.partitionMap { string -> PartitionMapResult3<Int, Double, String> in
69+
if let integer = Int(string) {
70+
return .first(integer)
71+
} else if let double = Double(string) {
72+
return .second(double)
73+
} else {
74+
return .third(string)
75+
}
76+
}
77+
78+
XCTAssertEqual(integers, [-1, 0, 1])
79+
XCTAssertEqual(doubles.map(String.init(describing:)), ["-1.5", "1.5", nanString])
80+
XCTAssertEqual(unrepresentable, ["", "^"])
81+
}
82+
83+
84+
func testPartitionMap2WithPredicate() throws {
85+
let predicate: (Int) throws -> PartitionMapResult2<Int8, UInt8> = { number -> PartitionMapResult2<Int8, UInt8> in
86+
if let uint = UInt8(exactly: number) {
87+
return .second(uint)
88+
} else if let int = Int8(exactly: number) {
89+
return .first(int)
90+
} else {
91+
throw TestError()
92+
}
93+
}
94+
95+
let s0 = try [1, 2, 3, 4].partitionMap(predicate)
96+
let s1 = try [-1, 2, 3, 4].partitionMap(predicate)
97+
let s2 = try [-1, 2, -3, 4].partitionMap(predicate)
98+
let s3 = try [-1, 2, -3, -4].partitionMap(predicate)
99+
100+
XCTAssertThrowsError(try [256].partitionMap(predicate))
101+
XCTAssertThrowsError(try [-129].partitionMap(predicate))
102+
103+
XCTAssertEqual(s0.0, [])
104+
XCTAssertEqual(s0.1, [1, 2, 3, 4])
105+
106+
XCTAssertEqual(s1.0, [-1])
107+
XCTAssertEqual(s1.1, [2, 3, 4])
108+
109+
XCTAssertEqual(s2.0, [-1, -3])
110+
XCTAssertEqual(s2.1, [2, 4])
111+
112+
XCTAssertEqual(s3.0, [-1, -3, -4])
113+
XCTAssertEqual(s3.1, [2])
114+
}
115+
116+
func testPartitionMap3WithPredicate() throws {
117+
let predicate: (Int) throws -> PartitionMapResult3<Int8, UInt8, Void> = { number -> PartitionMapResult3<Int8, UInt8, Void> in
118+
if number == 0 {
119+
return .third(Void())
120+
} else if let uint = UInt8(exactly: number) {
121+
return .second(uint)
122+
} else if let int = Int8(exactly: number) {
123+
return .first(int)
124+
} else {
125+
throw TestError()
126+
}
127+
}
128+
129+
let s0 = try [0, 1, 2, 3, 4].partitionMap(predicate)
130+
let s1 = try [0, 0, -1, 2, 3, 4].partitionMap(predicate)
131+
let s2 = try [0, 0, -1, 2, -3, 4].partitionMap(predicate)
132+
let s3 = try [0, -1, 2, -3, -4].partitionMap(predicate)
133+
134+
XCTAssertThrowsError(try [256].partitionMap(predicate))
135+
XCTAssertThrowsError(try [-129].partitionMap(predicate))
136+
137+
XCTAssertEqual(s0.0, [])
138+
XCTAssertEqual(s0.1, [1, 2, 3, 4])
139+
XCTAssertEqual(s0.2.count, 1)
140+
141+
XCTAssertEqual(s1.0, [-1])
142+
XCTAssertEqual(s1.1, [2, 3, 4])
143+
XCTAssertEqual(s1.2.count, 2)
144+
145+
XCTAssertEqual(s2.0, [-1, -3])
146+
XCTAssertEqual(s2.1, [2, 4])
147+
XCTAssertEqual(s2.2.count, 2)
148+
149+
XCTAssertEqual(s3.0, [-1, -3, -4])
150+
XCTAssertEqual(s3.1, [2])
151+
XCTAssertEqual(s3.2.count, 1)
152+
}
153+
154+
private struct TestError: Error {}
155+
}

0 commit comments

Comments
 (0)