Skip to content

Commit 68e8d78

Browse files
committed
Update uninitialized array tests.
1 parent 659cf1d commit 68e8d78

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

test/stdlib/Inputs/CommonArrayTests.gyb

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,6 @@ ${Suite}.test("${ArrayType}/withUnsafeMutableBytes")
462462
expectEqual(10, b[0])
463463
}
464464

465-
// FIXME: Implement these changes for ArraySlice and ContiguousArray
466-
467-
%if ArrayType == 'Array':
468-
469465
//===----------------------------------------------------------------------===//
470466
// reserveCapacity semantics
471467
//===----------------------------------------------------------------------===//
@@ -478,36 +474,39 @@ ${Suite}.test("${ArrayType}/reserveCapacity") {
478474
}
479475
}
480476

477+
%if ArrayType in ['Array', 'ContiguousArray']:
478+
481479
//===----------------------------------------------------------------------===//
482-
// init(_unsafeUninitializedCapacity:initializingWith:)
480+
// init(unsafeUninitializedCapacity:initializingWith:)
483481
//===----------------------------------------------------------------------===//
484482

485483
extension Collection {
486484
func stablyPartitioned(
487485
by belongsInFirstPartition: (Element) -> Bool
488486
) -> ${ArrayType}<Element> {
489-
let result = ${ArrayType}<Element>(_unsafeUninitializedCapacity: self.count) {
487+
let result = ${ArrayType}<Element>(unsafeUninitializedCapacity: self.count) {
490488
buffer, initializedCount in
491-
var lowIndex = 0
492-
var highIndex = buffer.count
489+
var low = buffer.baseAddress!
490+
var high = low + buffer.count
493491
for element in self {
494492
if belongsInFirstPartition(element) {
495-
buffer[lowIndex] = element
496-
lowIndex += 1
493+
low.initialize(to: element)
494+
low += 1
497495
} else {
498-
highIndex -= 1
499-
buffer[highIndex] = element
496+
high -= 1
497+
high.initialize(to: element)
500498
}
501499
}
502-
500+
501+
let highIndex = high - buffer.baseAddress!
503502
buffer[highIndex...].reverse()
504503
initializedCount = buffer.count
505504
}
506505
return result
507506
}
508507
}
509508

510-
${Suite}.test("${ArrayType}/init(_unsafeUninitializedCapacity:...:)") {
509+
${Suite}.test("${ArrayType}/init(unsafeUninitializedCapacity:...:)") {
511510
var a = ${ArrayType}(0..<300)
512511
let p = a.stablyPartitioned(by: { $0 % 2 == 0 })
513512
expectEqualSequence(
@@ -516,7 +515,7 @@ ${Suite}.test("${ArrayType}/init(_unsafeUninitializedCapacity:...:)") {
516515
)
517516
}
518517

519-
${Suite}.test("${ArrayType}/init(_unsafeUninitializedCapacity:...:)/throwing") {
518+
${Suite}.test("${ArrayType}/init(unsafeUninitializedCapacity:...:)/throwing") {
520519
final class InstanceCountedClass {
521520
static var instanceCounter = 0
522521

@@ -526,7 +525,8 @@ ${Suite}.test("${ArrayType}/init(_unsafeUninitializedCapacity:...:)/throwing") {
526525
enum E: Error { case error }
527526

528527
do {
529-
var a = Array<InstanceCountedClass>(_unsafeUninitializedCapacity: 10) { buffer, c in
528+
var a = ${ArrayType}<InstanceCountedClass>(unsafeUninitializedCapacity: 10) {
529+
buffer, c in
530530
let p = buffer.baseAddress!
531531
for i in 0..<5 {
532532
(p + i).initialize(to: InstanceCountedClass())
@@ -539,7 +539,7 @@ ${Suite}.test("${ArrayType}/init(_unsafeUninitializedCapacity:...:)/throwing") {
539539
a = []
540540
expectEqual(0, InstanceCountedClass.instanceCounter)
541541

542-
a = try Array(_unsafeUninitializedCapacity: 10) { buffer, c in
542+
a = try ${ArrayType}(unsafeUninitializedCapacity: 10) { buffer, c in
543543
let p = buffer.baseAddress!
544544
for i in 0..<5 {
545545
(p + i).initialize(to: InstanceCountedClass())
@@ -548,10 +548,7 @@ ${Suite}.test("${ArrayType}/init(_unsafeUninitializedCapacity:...:)/throwing") {
548548
throw E.error
549549
}
550550

551-
// The throw above should prevent reaching here, which should mean the
552-
// instances created in the closure should get deallocated before the final
553-
// expectation outside the do/catch block.
554-
expectTrue(false)
551+
expectUnreachable()
555552
} catch {}
556553
expectEqual(0, InstanceCountedClass.instanceCounter)
557554
}

0 commit comments

Comments
 (0)