Skip to content

Commit 3fc134f

Browse files
committed
[Foundation Tests] Add a unit test to validate the behavior of dropFirst
1 parent b45ed36 commit 3fc134f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/stdlib/TestData.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,50 @@ class TestData : TestDataSuper {
10721072
let actual = d2.map { $0 }
10731073
expectEqual(expected, actual)
10741074
}
1075+
1076+
func test_dropFirst() {
1077+
var data = Data([0, 1, 2, 3, 4, 5])
1078+
let sliced = data.dropFirst()
1079+
expectEqual(data.count - 1, sliced.count)
1080+
expectEqual(UInt8(1), sliced[1])
1081+
expectEqual(UInt8(2), sliced[2])
1082+
expectEqual(UInt8(3), sliced[3])
1083+
expectEqual(UInt8(4), sliced[4])
1084+
expectEqual(UInt8(5), sliced[5])
1085+
}
1086+
1087+
func test_dropFirst2() {
1088+
var data = Data([0, 1, 2, 3, 4, 5])
1089+
let sliced = data.dropFirst(2)
1090+
expectEqual(data.count - 2, sliced.count)
1091+
expectEqual(UInt8(2), sliced[2])
1092+
expectEqual(UInt8(3), sliced[3])
1093+
expectEqual(UInt8(4), sliced[4])
1094+
expectEqual(UInt8(5), sliced[5])
1095+
}
1096+
1097+
func test_copyBytes1() {
1098+
var array: [UInt8] = [0, 1, 2, 3]
1099+
var data = Data(bytes: array)
1100+
1101+
array.withUnsafeMutableBufferPointer {
1102+
data[1..<3].copyBytes(to: $0.baseAddress!, from: 1..<3)
1103+
}
1104+
expectEqual([UInt8(1), UInt8(2), UInt8(2), UInt8(3)], array)
1105+
}
1106+
1107+
func test_copyBytes2() {
1108+
let array: [UInt8] = [0, 1, 2, 3]
1109+
var data = Data(bytes: array)
1110+
1111+
let expectedSlice = array[1..<3]
1112+
1113+
let start = data.index(after: data.startIndex)
1114+
let end = data.index(before: data.endIndex)
1115+
let slice = data[start..<end]
1116+
1117+
expectEqual(expectedSlice[expectedSlice.startIndex], slice[slice.startIndex])
1118+
}
10751119
}
10761120

10771121
#if !FOUNDATION_XCTEST
@@ -1128,6 +1172,10 @@ DataTests.test("test_sliceEquality") { TestData().test_sliceEquality() }
11281172
DataTests.test("test_sliceEquality2") { TestData().test_sliceEquality2() }
11291173
DataTests.test("test_splittingHttp") { TestData().test_splittingHttp() }
11301174
DataTests.test("test_map") { TestData().test_map() }
1175+
DataTests.test("test_dropFirst") { TestData().test_dropFirst() }
1176+
DataTests.test("test_dropFirst2") { TestData().test_dropFirst2() }
1177+
DataTests.test("test_copyBytes1") { TestData().test_copyBytes1() }
1178+
DataTests.test("test_copyBytes2") { TestData().test_copyBytes2() }
11311179

11321180
// XCTest does not have a crash detection, whereas lit does
11331181
DataTests.test("bounding failure subdata") {

0 commit comments

Comments
 (0)