Skip to content

Commit 2f417aa

Browse files
committed
[test] add MutableSpan slicing tests
1 parent c59888f commit 2f417aa

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

test/stdlib/Span/MutableSpanTests.swift

+127
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,130 @@ suite.test("swapAt")
572572

573573
expectEqual(array, (0..<count).reversed())
574574
}
575+
576+
suite.test("_extracting()")
577+
.skip(.custom(
578+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
579+
reason: "Requires Swift 6.2's standard library"
580+
))
581+
.code {
582+
guard #available(SwiftStdlib 6.2, *) else { return }
583+
584+
let capacity = 4
585+
var b = (0..<capacity).map(Int8.init)
586+
b.withUnsafeMutableBufferPointer {
587+
var span = MutableSpan(_unsafeElements: $0)
588+
589+
var sub = span._extracting(0..<2)
590+
expectEqual(sub.count, 2)
591+
expectEqual(sub[0], 0)
592+
593+
sub = span._extracting(..<2)
594+
expectEqual(sub.count, 2)
595+
expectEqual(sub[0], 0)
596+
597+
sub = span._extracting(...)
598+
expectEqual(sub.count, 4)
599+
expectEqual(sub[0], 0)
600+
601+
sub = span._extracting(2...)
602+
expectEqual(sub.count, 2)
603+
expectEqual(sub[0], 2)
604+
}
605+
}
606+
607+
suite.test("_extracting(unchecked:)")
608+
.skip(.custom(
609+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
610+
reason: "Requires Swift 6.2's standard library"
611+
))
612+
.code {
613+
guard #available(SwiftStdlib 6.2, *) else { return }
614+
615+
let capacity = 32
616+
var b = (0..<capacity).map(UInt8.init)
617+
b.withUnsafeMutableBufferPointer {
618+
var span = MutableSpan(_unsafeElements: $0.prefix(8))
619+
let beyond = span._extracting(unchecked: 16...23)
620+
expectEqual(beyond.count, 8)
621+
let fromBeyond = beyond[0]
622+
expectEqual(fromBeyond, 16)
623+
}
624+
}
625+
626+
suite.test("_extracting prefixes")
627+
.skip(.custom(
628+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
629+
reason: "Requires Swift 6.2's standard library"
630+
))
631+
.code {
632+
guard #available(SwiftStdlib 6.2, *) else { return }
633+
634+
let capacity = 4
635+
var a = Array(0..<UInt8(capacity))
636+
a.withUnsafeMutableBufferPointer {
637+
var prefix: MutableSpan<UInt8>
638+
var span = MutableSpan(_unsafeElements: $0)
639+
expectEqual(span.count, capacity)
640+
641+
prefix = span._extracting(first: 1)
642+
expectEqual(prefix[0], 0)
643+
644+
prefix = span._extracting(first: capacity)
645+
expectEqual(prefix[capacity-1], UInt8(capacity-1))
646+
647+
prefix = span._extracting(droppingLast: capacity)
648+
expectEqual(prefix.isEmpty, true)
649+
650+
prefix = span._extracting(droppingLast: 1)
651+
expectEqual(prefix[capacity-2], UInt8(capacity-2))
652+
}
653+
654+
do {
655+
let b = UnsafeMutableBufferPointer<Int>(start: nil, count: 0)
656+
var span = MutableSpan(_unsafeElements: b)
657+
expectEqual(span.count, b.count)
658+
expectEqual(span._extracting(first: 1).count, b.count)
659+
expectEqual(span._extracting(droppingLast: 1).count, b.count)
660+
}
661+
}
662+
663+
suite.test("_extracting suffixes")
664+
.skip(.custom(
665+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
666+
reason: "Requires Swift 6.2's standard library"
667+
))
668+
.code {
669+
guard #available(SwiftStdlib 6.2, *) else { return }
670+
671+
let capacity = 4
672+
var a = Array(0..<UInt8(capacity))
673+
a.withUnsafeMutableBufferPointer {
674+
var suffix: MutableSpan<UInt8>
675+
var span = MutableSpan(_unsafeElements: $0)
676+
expectEqual(span.count, capacity)
677+
678+
suffix = span._extracting(last: capacity)
679+
expectEqual(suffix[0], 0)
680+
681+
suffix = span._extracting(last: capacity-1)
682+
expectEqual(suffix[0], 1)
683+
684+
suffix = span._extracting(last: 1)
685+
expectEqual(suffix[0], UInt8(capacity-1))
686+
687+
suffix = span._extracting(droppingFirst: capacity)
688+
expectTrue(suffix.isEmpty)
689+
690+
suffix = span._extracting(droppingFirst: 1)
691+
expectEqual(suffix[0], 1)
692+
}
693+
694+
do {
695+
let b = UnsafeMutableBufferPointer<ObjectIdentifier>(start: nil, count: 0)
696+
var span = MutableSpan(_unsafeElements: b)
697+
expectEqual(span.count, b.count)
698+
expectEqual(span._extracting(last: 1).count, b.count)
699+
expectEqual(span._extracting(droppingFirst: 1).count, b.count)
700+
}
701+
}

0 commit comments

Comments
 (0)