Skip to content

Commit 2899369

Browse files
author
Itai Ferber
committed
Clarify new append tests
1 parent 70f5e4c commit 2899369

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

test/stdlib/TestData.swift

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,49 +1189,41 @@ class TestData : TestDataSuper {
11891189

11901190
// d should go from .empty representation to .inline.
11911191
// Appending a small enough sequence to fit in linline should actually be copied.
1192-
d.append(contentsOf: repeatElement(UInt8(0x01), count: 2))
1193-
expectEqual(Data([0x01, 0x01]), d)
1192+
d.append(contentsOf: 0x00...0x01)
1193+
expectEqual(Data([0x00, 0x01]), d)
11941194

11951195
// Appending another small sequence should similarly still work.
1196-
d.append(contentsOf: repeatElement(UInt8(0x02), count: 1))
1197-
expectEqual(Data([0x01, 0x01, 0x02]), d)
1196+
d.append(contentsOf: 0x02...0x02)
1197+
expectEqual(Data([0x00, 0x01, 0x02]), d)
11981198

11991199
// If we append a sequence of elements larger than a single InlineData, the internal append here should buffer.
12001200
// We want to make sure that buffering in this way does not accidentally drop trailing elements on the floor.
1201-
d.append(contentsOf: repeatElement(UInt8(0x03), count: 24))
1202-
expectEqual(Data([0x01, 0x01, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
1203-
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
1204-
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03]), d)
1201+
d.append(contentsOf: 0x03...0x17)
1202+
expectEqual(Data([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1203+
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1204+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17]), d)
12051205
}
12061206

12071207
// This test is like test_appendingNonContiguousSequence_exactCount but uses a sequence which reports 0 for its `.underestimatedCount`.
12081208
// This attempts to hit the worst-case scenario of `Data.append<S>(_:)` -- a discontiguous sequence of unknown length.
12091209
func test_appendingNonContiguousSequence_underestimatedCount() {
1210-
// underestimatedRepeatedElement does the same thing as repeatElement, but reports an `.underestimatedCount` of 0.
1211-
let underestimatedRepeatedElement = { (element: UInt8, count: Int) in
1212-
return sequence(state: count) { (count: inout Int) -> UInt8? in
1213-
defer { count -= 1 }
1214-
return count > 0 ? element : nil
1215-
}
1216-
}
1217-
12181210
var d = Data()
12191211

12201212
// d should go from .empty representation to .inline.
12211213
// Appending a small enough sequence to fit in linline should actually be copied.
1222-
d.append(contentsOf: underestimatedRepeatedElement(UInt8(0x01), 2))
1223-
expectEqual(Data([0x01, 0x01]), d)
1214+
d.append(contentsOf: (0x00...0x01).makeIterator()) // `.makeIterator()` produces a sequence whose `.underestimatedCount` is 0.
1215+
expectEqual(Data([0x00, 0x01]), d)
12241216

12251217
// Appending another small sequence should similarly still work.
1226-
d.append(contentsOf: underestimatedRepeatedElement(UInt8(0x02), 1))
1227-
expectEqual(Data([0x01, 0x01, 0x02]), d)
1218+
d.append(contentsOf: (0x02...0x02).makeIterator()) // `.makeIterator()` produces a sequence whose `.underestimatedCount` is 0.
1219+
expectEqual(Data([0x00, 0x01, 0x02]), d)
12281220

12291221
// If we append a sequence of elements larger than a single InlineData, the internal append here should buffer.
12301222
// We want to make sure that buffering in this way does not accidentally drop trailing elements on the floor.
1231-
d.append(contentsOf: underestimatedRepeatedElement(UInt8(0x03), 24))
1232-
expectEqual(Data([0x01, 0x01, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
1233-
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
1234-
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03]), d)
1223+
d.append(contentsOf: (0x03...0x17).makeIterator()) // `.makeIterator()` produces a sequence whose `.underestimatedCount` is 0.
1224+
expectEqual(Data([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1225+
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1226+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17]), d)
12351227
}
12361228

12371229
func test_sequenceInitializers() {

0 commit comments

Comments
 (0)