|
4 | 4 | import StdlibUnittest
|
5 | 5 | import StdlibCollectionUnittest
|
6 | 6 |
|
| 7 | +// Tests |
| 8 | + |
| 9 | +struct SubscriptGetTest { |
| 10 | + // SubscriptGetTest operates on a `(end - start)` sized buffer containing |
| 11 | + // monotonically increasing integers from `start` to `end - 1`. |
| 12 | + static let start = 0 |
| 13 | + static let end = 20 |
| 14 | + let rangeSelection: RangeSelection |
| 15 | + /// The values that should be expected by slicing the UBP, or `nil` if the |
| 16 | + /// test is expected to crash. |
| 17 | + let expectedValues: [Int]? |
| 18 | + let loc: SourceLoc |
| 19 | + |
| 20 | + static var elementCount = (end - start) |
| 21 | + |
| 22 | +% for SelfType in ['UnsafeBufferPointer', 'UnsafeMutableBufferPointer']: |
| 23 | + /// Create and populate an `${SelfType}` for use with unit tests. |
| 24 | + /// PRECONDITION: `memory` must be allocated with space for |
| 25 | + /// `SubscriptGetTest.elementCount` elements. |
| 26 | + func create${SelfType}(from memory: UnsafeMutablePointer<OpaqueValue<Int>>) |
| 27 | + -> ${SelfType}<OpaqueValue<Int>> |
| 28 | + { |
| 29 | + for i in SubscriptGetTest.start..<SubscriptGetTest.end { |
| 30 | + memory[i] = OpaqueValue(i) |
| 31 | + } |
| 32 | + return ${SelfType}(start: memory, count: SubscriptGetTest.elementCount) |
| 33 | + } |
| 34 | +% end |
| 35 | + |
| 36 | + init( |
| 37 | + rangeSelection: RangeSelection, expectedValues: [Int]? = nil, |
| 38 | + file: String = #file, line: UInt = #line |
| 39 | + ) { |
| 40 | + self.rangeSelection = rangeSelection |
| 41 | + self.expectedValues = expectedValues |
| 42 | + self.loc = SourceLoc(file, line, comment: "test data") |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +let subscriptGetTests : [SubscriptGetTest] = [ |
| 47 | + // Valid, empty. |
| 48 | + SubscriptGetTest(rangeSelection: .emptyRange, expectedValues: []), |
| 49 | + |
| 50 | + // Valid, edges. |
| 51 | + SubscriptGetTest(rangeSelection: .leftEdge, expectedValues: [0]), |
| 52 | + SubscriptGetTest(rangeSelection: .rightEdge, expectedValues: [19]), |
| 53 | + |
| 54 | + // Valid, internal. |
| 55 | + SubscriptGetTest(rangeSelection: .leftHalf, |
| 56 | + expectedValues: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), |
| 57 | + SubscriptGetTest(rangeSelection: .rightHalf, |
| 58 | + expectedValues: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]), |
| 59 | + SubscriptGetTest(rangeSelection: .middle, |
| 60 | + expectedValues: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]), |
| 61 | + SubscriptGetTest(rangeSelection: .full, |
| 62 | + expectedValues: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]), |
| 63 | + |
| 64 | + // Invalid, bottom out of bounds. |
| 65 | + SubscriptGetTest(rangeSelection: .offsets(-1, -1)), |
| 66 | + SubscriptGetTest(rangeSelection: .offsets(-1, 0)), |
| 67 | + SubscriptGetTest(rangeSelection: .offsets(-100, 5)), |
| 68 | + |
| 69 | + // Invalid, top out of bounds. |
| 70 | + SubscriptGetTest(rangeSelection: .offsets(20, 20)), |
| 71 | + SubscriptGetTest(rangeSelection: .offsets(19, 20)), |
| 72 | + SubscriptGetTest(rangeSelection: .offsets(5, 100)), |
| 73 | + |
| 74 | + // Invalid, both out of bounds. |
| 75 | + SubscriptGetTest(rangeSelection: .offsets(-1, 20)), |
| 76 | + SubscriptGetTest(rangeSelection: .offsets(-100, 100)), |
| 77 | +] |
| 78 | + |
| 79 | + |
| 80 | +// Test Suites |
| 81 | + |
7 | 82 | var UnsafeBufferPointerTestSuite = TestSuite("UnsafeBufferPointer")
|
8 | 83 | var UnsafeMutableBufferPointerTestSuite = TestSuite("UnsafeMutableBufferPointer")
|
9 | 84 |
|
@@ -105,6 +180,34 @@ ${SelfName}TestSuite.test("badNilCount")
|
105 | 180 | _ = buffer
|
106 | 181 | }
|
107 | 182 |
|
| 183 | +% for RangeName in ['range', 'countableRange', 'closedRange', 'countableClosedRange']: |
| 184 | +${SelfName}TestSuite.test("subscript/get").forEach(in: subscriptGetTests) { |
| 185 | + (test) in |
| 186 | + |
| 187 | +% if 'closed' in RangeName.lower(): |
| 188 | + if test.rangeSelection.isEmpty { |
| 189 | + return |
| 190 | + } |
| 191 | +% end |
| 192 | + |
| 193 | + let elementCount = SubscriptGetTest.elementCount |
| 194 | + |
| 195 | + var memory = UnsafeMutablePointer<OpaqueValue<Int>>(allocatingCapacity: elementCount) |
| 196 | + let buffer = test.create${SelfName}(from: memory) |
| 197 | + defer { memory.deallocateCapacity(elementCount) } |
| 198 | + |
| 199 | + let range = test.rangeSelection.${RangeName}(in: buffer) |
| 200 | + |
| 201 | + if test.expectedValues == nil { expectCrashLater() } |
| 202 | + let slice = buffer[range] |
| 203 | + expectEqual( |
| 204 | + test.expectedValues!, |
| 205 | + slice.map { $0.value }, |
| 206 | + stackTrace: SourceLocStack().with(test.loc) |
| 207 | + ) |
| 208 | +} |
| 209 | +% end |
| 210 | + |
108 | 211 | % end
|
109 | 212 |
|
110 | 213 | UnsafeMutableBufferPointerTestSuite.test("changeElementViaBuffer") {
|
|
0 commit comments