@@ -13,7 +13,7 @@ import _StringProcessing
1313import XCTest
1414
1515// TODO: Protocol-powered testing
16- class AlgorithmTests : XCTestCase {
16+ class RegexConsumerTests : XCTestCase {
1717
1818}
1919
@@ -32,7 +32,25 @@ func makeSingleUseSequence<T>(element: T, count: Int) -> UnfoldSequence<T, Void>
3232 }
3333}
3434
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+
3654 func testRanges( ) {
3755 func expectRanges(
3856 _ string: String ,
@@ -48,6 +66,9 @@ class RegexConsumerTests: XCTestCase {
4866 // `IndexingIterator` tests the collection conformance
4967 let actualCol : [ Range < Int > ] = string [ ... ] . ranges ( of: regex) [ ... ] . map ( string. offsets ( of: ) )
5068 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)
5172 }
5273
5374 expectRanges ( " " , " " , [ 0 ..< 0 ] )
@@ -68,6 +89,31 @@ class RegexConsumerTests: XCTestCase {
6889 expectRanges ( " abc " , " (a|b)* " , [ 0 ..< 2 , 2 ..< 2 , 3 ..< 3 ] )
6990 expectRanges ( " abc " , " (b|c)+ " , [ 1 ..< 3 ] )
7091 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 ] )
71117 }
72118
73119 func testSplit( ) {
0 commit comments