Skip to content

Commit 10a8966

Browse files
committed
Add more tests to testCopyToArray()
1 parent b8f5152 commit 10a8966

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Tests/DequeTests/DequeTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ final class DequeTests: XCTestCase {
8383
buf.reserveCapacity(5)
8484
buf.prepend(0)
8585
XCTAssertEqual(Array(buf), [0,1,2,3])
86+
87+
// also an empty one
88+
buf = []
89+
XCTAssertEqual(Array(buf), [])
90+
// and an empty one with capacity
91+
buf.reserveCapacity(10)
92+
XCTAssertEqual(Array(buf), [])
93+
94+
// Normally, Array(buf) will invoke our _copyContents. It doesn't do this if we're empty
95+
// though, but we'd like to validate that _copyContents works when empty. We'll do that by
96+
// using the other public API for invoking it, which is
97+
// UnsafeMutableBufferPointer.initialize(from:).
98+
let heapBuffer = UnsafeMutableBufferPointer<IntClass>.allocate(capacity: 5)
99+
defer { heapBuffer.deallocate() }
100+
buf = [] // test using shared empty storage
101+
var (iter, idx) = heapBuffer.initialize(from: buf)
102+
XCTAssertNil(iter.next(), "iter.next()")
103+
XCTAssertEqual(idx, heapBuffer.startIndex)
104+
buf.reserveCapacity(10) // test again with some capacity
105+
(iter, idx) = heapBuffer.initialize(from: buf)
106+
XCTAssertNil(iter.next(), "iter.next()")
107+
XCTAssertEqual(idx, heapBuffer.startIndex)
86108
}
87109

88110
func testReserveCapacity() {

0 commit comments

Comments
 (0)