@@ -13,7 +13,7 @@ import _StringProcessing
13
13
import XCTest
14
14
15
15
// TODO: Protocol-powered testing
16
- class AlgorithmTests : XCTestCase {
16
+ class RegexConsumerTests : XCTestCase {
17
17
18
18
}
19
19
@@ -32,7 +32,25 @@ func makeSingleUseSequence<T>(element: T, count: Int) -> UnfoldSequence<T, Void>
32
32
}
33
33
}
34
34
35
- class RegexConsumerTests : XCTestCase {
35
+ class AlgorithmTests : XCTestCase {
36
+ func testContains( ) {
37
+ XCTAssertTrue ( " " . contains ( " " ) )
38
+ XCTAssertTrue ( " abcde " . contains ( " " ) )
39
+ XCTAssertTrue ( " abcde " . contains ( " abcd " ) )
40
+ XCTAssertTrue ( " abcde " . contains ( " bcde " ) )
41
+ XCTAssertTrue ( " abcde " . contains ( " bcd " ) )
42
+ XCTAssertTrue ( " ababacabababa " . contains ( " abababa " ) )
43
+
44
+ XCTAssertFalse ( " " . contains ( " abcd " ) )
45
+
46
+ for start in 0 ..< 9 {
47
+ for end in start..< 9 {
48
+ XCTAssertTrue ( ( 0 ..< 10 ) . contains ( start... end) )
49
+ XCTAssertFalse ( ( 0 ..< 10 ) . contains ( start... 10 ) )
50
+ }
51
+ }
52
+ }
53
+
36
54
func testRanges( ) {
37
55
func expectRanges(
38
56
_ string: String ,
@@ -48,6 +66,9 @@ class RegexConsumerTests: XCTestCase {
48
66
// `IndexingIterator` tests the collection conformance
49
67
let actualCol : [ Range < Int > ] = string [ ... ] . ranges ( of: regex) [ ... ] . map ( string. offsets ( of: ) )
50
68
XCTAssertEqual ( actualCol, expected, file: file, line: line)
69
+
70
+ let firstRange = string. firstRange ( of: regex) . map ( string. offsets ( of: ) )
71
+ XCTAssertEqual ( firstRange, expected. first, file: file, line: line)
51
72
}
52
73
53
74
expectRanges ( " " , " " , [ 0 ..< 0 ] )
@@ -68,6 +89,31 @@ class RegexConsumerTests: XCTestCase {
68
89
expectRanges ( " abc " , " (a|b)* " , [ 0 ..< 2 , 2 ..< 2 , 3 ..< 3 ] )
69
90
expectRanges ( " abc " , " (b|c)+ " , [ 1 ..< 3 ] )
70
91
expectRanges ( " abc " , " (b|c)* " , [ 0 ..< 0 , 1 ..< 3 , 3 ..< 3 ] )
92
+
93
+ func expectStringRanges(
94
+ _ input: String ,
95
+ _ pattern: String ,
96
+ _ expected: [ Range < Int > ] ,
97
+ file: StaticString = #file, line: UInt = #line
98
+ ) {
99
+ let actualSeq : [ Range < Int > ] = input. ranges ( of: pattern) . map ( input. offsets ( of: ) )
100
+ XCTAssertEqual ( actualSeq, expected, file: file, line: line)
101
+
102
+ // `IndexingIterator` tests the collection conformance
103
+ let actualCol : [ Range < Int > ] = input. ranges ( of: pattern) [ ... ] . map ( input. offsets ( of: ) )
104
+ XCTAssertEqual ( actualCol, expected, file: file, line: line)
105
+
106
+ let firstRange = input. firstRange ( of: pattern) . map ( input. offsets ( of: ) )
107
+ XCTAssertEqual ( firstRange, expected. first, file: file, line: line)
108
+ }
109
+
110
+ expectStringRanges ( " " , " " , [ 0 ..< 0 ] )
111
+ expectStringRanges ( " abcde " , " " , [ 0 ..< 0 , 1 ..< 1 , 2 ..< 2 , 3 ..< 3 , 4 ..< 4 , 5 ..< 5 ] )
112
+ expectStringRanges ( " abcde " , " abcd " , [ 0 ..< 4 ] )
113
+ expectStringRanges ( " abcde " , " bcde " , [ 1 ..< 5 ] )
114
+ expectStringRanges ( " abcde " , " bcd " , [ 1 ..< 4 ] )
115
+ expectStringRanges ( " ababacabababa " , " abababa " , [ 6 ..< 13 ] )
116
+ expectStringRanges ( " ababacabababa " , " aba " , [ 0 ..< 3 , 6 ..< 9 , 10 ..< 13 ] )
71
117
}
72
118
73
119
func testSplit( ) {
0 commit comments