|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 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 | +@testable import Algorithms |
| 14 | + |
| 15 | +final class ReplaceSubrangeTests: XCTestCase { |
| 16 | + |
| 17 | + func testAppend() { |
| 18 | + |
| 19 | + // Base: non-empty |
| 20 | + // Appending: non-empty |
| 21 | + do { |
| 22 | + let base = 0..<5 |
| 23 | + let result = base.lazy.replacingSubrange(base.endIndex..<base.endIndex, with: [8, 9, 10]) |
| 24 | + XCTAssertEqualCollections(result, [0, 1, 2, 3, 4, 8, 9, 10]) |
| 25 | + IndexValidator().validate(result, expectedCount: 8) |
| 26 | + } |
| 27 | + |
| 28 | + // Base: non-empty |
| 29 | + // Appending: empty |
| 30 | + do { |
| 31 | + let base = 0..<5 |
| 32 | + let result = base.lazy.replacingSubrange(base.endIndex..<base.endIndex, with: EmptyCollection()) |
| 33 | + XCTAssertEqualCollections(result, [0, 1, 2, 3, 4]) |
| 34 | + IndexValidator().validate(result, expectedCount: 5) |
| 35 | + } |
| 36 | + |
| 37 | + // Base: empty |
| 38 | + // Appending: non-empty |
| 39 | + do { |
| 40 | + let base = EmptyCollection<Int>() |
| 41 | + let result = base.lazy.replacingSubrange(base.endIndex..<base.endIndex, with: 5..<10) |
| 42 | + XCTAssertEqualCollections(result, [5, 6, 7, 8, 9]) |
| 43 | + IndexValidator().validate(result, expectedCount: 5) |
| 44 | + } |
| 45 | + |
| 46 | + // Base: empty |
| 47 | + // Appending: empty |
| 48 | + do { |
| 49 | + let base = EmptyCollection<Int>() |
| 50 | + let result = base.lazy.replacingSubrange(base.endIndex..<base.endIndex, with: EmptyCollection()) |
| 51 | + XCTAssertEqualCollections(result, []) |
| 52 | + IndexValidator().validate(result, expectedCount: 0) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + func testPrepend() { |
| 57 | + |
| 58 | + // Base: non-empty |
| 59 | + // Prepending: non-empty |
| 60 | + do { |
| 61 | + let base = 0..<5 |
| 62 | + let result = base.lazy.replacingSubrange(base.startIndex..<base.startIndex, with: [8, 9, 10]) |
| 63 | + XCTAssertEqualCollections(result, [8, 9, 10, 0, 1, 2, 3, 4]) |
| 64 | + IndexValidator().validate(result, expectedCount: 8) |
| 65 | + } |
| 66 | + |
| 67 | + // Base: non-empty |
| 68 | + // Prepending: empty |
| 69 | + do { |
| 70 | + let base = 0..<5 |
| 71 | + let result = base.lazy.replacingSubrange(base.startIndex..<base.startIndex, with: EmptyCollection()) |
| 72 | + XCTAssertEqualCollections(result, [0, 1, 2, 3, 4]) |
| 73 | + IndexValidator().validate(result, expectedCount: 5) |
| 74 | + } |
| 75 | + |
| 76 | + // Base: empty |
| 77 | + // Prepending: non-empty |
| 78 | + do { |
| 79 | + let base = EmptyCollection<Int>() |
| 80 | + let result = base.lazy.replacingSubrange(base.startIndex..<base.startIndex, with: 5..<10) |
| 81 | + XCTAssertEqualCollections(result, [5, 6, 7, 8, 9]) |
| 82 | + IndexValidator().validate(result, expectedCount: 5) |
| 83 | + } |
| 84 | + |
| 85 | + // Base: empty |
| 86 | + // Prepending: empty |
| 87 | + do { |
| 88 | + let base = EmptyCollection<Int>() |
| 89 | + let result = base.lazy.replacingSubrange(base.startIndex..<base.startIndex, with: EmptyCollection()) |
| 90 | + XCTAssertEqualCollections(result, []) |
| 91 | + IndexValidator().validate(result, expectedCount: 0) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + func testInsert() { |
| 96 | + |
| 97 | + // Inserting: non-empty |
| 98 | + do { |
| 99 | + let base = 0..<10 |
| 100 | + let i = base.index(base.startIndex, offsetBy: 5) |
| 101 | + let result = base.lazy.replacingSubrange(i..<i, with: 20..<25) |
| 102 | + XCTAssertEqualCollections(result, [0, 1, 2, 3, 4, 20, 21, 22, 23, 24, 5, 6, 7, 8, 9]) |
| 103 | + IndexValidator().validate(result, expectedCount: 15) |
| 104 | + } |
| 105 | + |
| 106 | + // Inserting: empty |
| 107 | + do { |
| 108 | + let base = 0..<10 |
| 109 | + let i = base.index(base.startIndex, offsetBy: 5) |
| 110 | + let result = base.lazy.replacingSubrange(i..<i, with: EmptyCollection()) |
| 111 | + XCTAssertEqualCollections(result, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
| 112 | + IndexValidator().validate(result, expectedCount: 10) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + func testReplace() { |
| 117 | + |
| 118 | + // Location: start |
| 119 | + // Replacement: non-empty |
| 120 | + do { |
| 121 | + let base = "hello, world!" |
| 122 | + let i = base.index(base.startIndex, offsetBy: 3) |
| 123 | + let result = base.lazy.replacingSubrange(base.startIndex..<i, with: "goodbye".reversed()) |
| 124 | + XCTAssertEqualCollections(result, "eybdooglo, world!") |
| 125 | + IndexValidator().validate(result, expectedCount: 17) |
| 126 | + } |
| 127 | + |
| 128 | + // Location: start |
| 129 | + // Replacement: empty |
| 130 | + do { |
| 131 | + let base = "hello, world!" |
| 132 | + let i = base.index(base.startIndex, offsetBy: 3) |
| 133 | + let result = base.lazy.replacingSubrange(base.startIndex..<i, with: EmptyCollection()) |
| 134 | + XCTAssertEqualCollections(result, "lo, world!") |
| 135 | + IndexValidator().validate(result, expectedCount: 10) |
| 136 | + } |
| 137 | + |
| 138 | + // Location: middle |
| 139 | + // Replacement: non-empty |
| 140 | + do { |
| 141 | + let base = "hello, world!" |
| 142 | + let start = base.index(base.startIndex, offsetBy: 3) |
| 143 | + let end = base.index(start, offsetBy: 4) |
| 144 | + let result = base.lazy.replacingSubrange(start..<end, with: "goodbye".reversed()) |
| 145 | + XCTAssertEqualCollections(result, "heleybdoogworld!") |
| 146 | + IndexValidator().validate(result, expectedCount: 16) |
| 147 | + } |
| 148 | + |
| 149 | + // Location: middle |
| 150 | + // Replacement: empty |
| 151 | + do { |
| 152 | + let base = "hello, world!" |
| 153 | + let start = base.index(base.startIndex, offsetBy: 3) |
| 154 | + let end = base.index(start, offsetBy: 4) |
| 155 | + let result = base.lazy.replacingSubrange(start..<end, with: EmptyCollection()) |
| 156 | + XCTAssertEqualCollections(result, "helworld!") |
| 157 | + IndexValidator().validate(result, expectedCount: 9) |
| 158 | + } |
| 159 | + |
| 160 | + // Location: end |
| 161 | + // Replacement: non-empty |
| 162 | + do { |
| 163 | + let base = "hello, world!" |
| 164 | + let start = base.index(base.endIndex, offsetBy: -4) |
| 165 | + let result = base.lazy.replacingSubrange(start..<base.endIndex, with: "goodbye".reversed()) |
| 166 | + XCTAssertEqualCollections(result, "hello, woeybdoog") |
| 167 | + IndexValidator().validate(result, expectedCount: 16) |
| 168 | + } |
| 169 | + |
| 170 | + // Location: end |
| 171 | + // Replacement: empty |
| 172 | + do { |
| 173 | + let base = "hello, world!" |
| 174 | + let start = base.index(base.endIndex, offsetBy: -4) |
| 175 | + let result = base.lazy.replacingSubrange(start..<base.endIndex, with: EmptyCollection()) |
| 176 | + XCTAssertEqualCollections(result, "hello, wo") |
| 177 | + IndexValidator().validate(result, expectedCount: 9) |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments