10
10
//===----------------------------------------------------------------------===//
11
11
12
12
import XCTest
13
- import Algorithms
13
+ @ testable import Algorithms
14
14
15
15
final class ChainTests : XCTestCase {
16
+ // intentionally does not depend on `Chain.index(_:offsetBy:)` in order to
17
+ // avoid making assumptions about the code being tested
18
+ func index< A, B> ( atOffset offset: Int , in chain: Chain < A , B > ) -> Chain < A , B > . Index {
19
+ offset < chain. base1. count
20
+ ? . init( first: chain. base1. index ( chain. base1. startIndex, offsetBy: offset) )
21
+ : . init( second: chain. base2. index ( chain. base2. startIndex, offsetBy: offset - chain. base1. count) )
22
+ }
23
+
16
24
func testChainSequences( ) {
17
25
let run = ( 1 ... ) . prefix ( 10 ) . chained ( with: 20 ... )
18
26
XCTAssertEqualSequences ( run. prefix ( 20 ) , Array ( 1 ... 10 ) + ( 20 ..< 30 ) )
@@ -35,5 +43,20 @@ final class ChainTests: XCTestCase {
35
43
XCTAssertEqualSequences ( s1. reversed ( ) . chained ( with: s2) , " JIHGFEDCBAklmnopqrstuv " )
36
44
}
37
45
38
- // TODO: Add tests that check distance and index(offsetBy:)
46
+ // TODO: Add tests that check index(offsetBy:)
47
+
48
+ func testChainDistanceFromTo( ) {
49
+ let s1 = " abcde "
50
+ let s2 = " VWXYZ "
51
+ let chain = s1. chained ( with: s2)
52
+
53
+ XCTAssertEqual ( chain. count, s1. count + s2. count)
54
+
55
+ for (startOffset, endOffset) in product ( 0 ... chain. count, 0 ... chain. count) {
56
+ let start = index ( atOffset: startOffset, in: chain)
57
+ let end = index ( atOffset: endOffset, in: chain)
58
+ let distance = endOffset - startOffset
59
+ XCTAssertEqual ( chain. distance ( from: start, to: end) , distance)
60
+ }
61
+ }
39
62
}
0 commit comments