|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Binary Parsing open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +import BinaryParsing |
| 13 | +import Testing |
| 14 | + |
| 15 | +private let buffer: [UInt8] = [ |
| 16 | + 0, 1, 0, 2, 0, 3, 0, 4, |
| 17 | + 0, 5, 0, 6, 0, 7, 0, 0, |
| 18 | +] |
| 19 | + |
| 20 | +private let emptyBuffer: [UInt8] = [] |
| 21 | + |
| 22 | +struct ExtractingTests { |
| 23 | + @Test func extractByteCount() throws { |
| 24 | + try buffer.withParserSpan { input in |
| 25 | + var firstSpan = try input.extract(byteCount: 4) |
| 26 | + #expect(firstSpan.startPosition == 0) |
| 27 | + #expect(firstSpan.count == 4) |
| 28 | + |
| 29 | + // Verify contents of the extracted span |
| 30 | + let firstValue = try UInt16(parsingBigEndian: &firstSpan) |
| 31 | + let secondValue = try UInt16(parsingBigEndian: &firstSpan) |
| 32 | + #expect(firstValue == 1) |
| 33 | + #expect(secondValue == 2) |
| 34 | + #expect(firstSpan.count == 0) |
| 35 | + |
| 36 | + // Input position should advance |
| 37 | + #expect(input.startPosition == 4) |
| 38 | + #expect(input.count == 12) |
| 39 | + |
| 40 | + // Extract another span after advancing the input |
| 41 | + _ = try input.seek(toRelativeOffset: 2) |
| 42 | + var secondSpan = try input.extract(byteCount: 4) |
| 43 | + #expect(secondSpan.startPosition == 0) // Extracted span starts at 0 |
| 44 | + #expect(secondSpan.count == 4) |
| 45 | + |
| 46 | + // Verify the content of the second extracted span |
| 47 | + let thirdValue = try UInt16(parsingBigEndian: &secondSpan) |
| 48 | + let fourthValue = try UInt16(parsingBigEndian: &secondSpan) |
| 49 | + #expect(thirdValue == 4) |
| 50 | + #expect(fourthValue == 5) |
| 51 | + |
| 52 | + // Try extracting with zero byteCount |
| 53 | + let emptySpan = try input.extract(byteCount: 0) |
| 54 | + #expect(emptySpan.count == 0) |
| 55 | + #expect(emptySpan.startPosition == 0) |
| 56 | + |
| 57 | + // Attempt to extract more than available |
| 58 | + #expect(throws: ParsingError.self) { |
| 59 | + _ = try input.extract(byteCount: 11) |
| 60 | + } |
| 61 | + |
| 62 | + // Try with negative byteCount |
| 63 | + #expect(throws: ParsingError.self) { |
| 64 | + _ = try input.extract(byteCount: -1) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Test with empty buffer |
| 69 | + try emptyBuffer.withParserSpan { input in |
| 70 | + // Zero byteCount should succeed |
| 71 | + let emptySpan = try input.extract(byteCount: 0) |
| 72 | + #expect(emptySpan.count == 0) |
| 73 | + #expect(emptySpan.startPosition == 0) |
| 74 | + |
| 75 | + // Any positive byteCount should fail |
| 76 | + #expect(throws: ParsingError.self) { |
| 77 | + _ = try input.extract(byteCount: 1) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test func extractObjectCount() throws { |
| 83 | + try buffer.withParserSpan { input in |
| 84 | + // 2 objects of 2 bytes each |
| 85 | + var firstSpan = try input.extract(objectStride: 2, objectCount: 2) |
| 86 | + #expect(firstSpan.startPosition == 0) |
| 87 | + #expect(firstSpan.count == 4) |
| 88 | + |
| 89 | + // Verify contents of the extracted span |
| 90 | + let firstValue = try UInt16(parsingBigEndian: &firstSpan) |
| 91 | + let secondValue = try UInt16(parsingBigEndian: &firstSpan) |
| 92 | + #expect(firstValue == 1) |
| 93 | + #expect(secondValue == 2) |
| 94 | + #expect(firstSpan.count == 0) |
| 95 | + |
| 96 | + // 1 object of 4 bytes |
| 97 | + var secondSpan = try input.extract(objectStride: 4, objectCount: 1) |
| 98 | + #expect(secondSpan.startPosition == 0) // Extracted spans start at 0 |
| 99 | + #expect(secondSpan.count == 4) |
| 100 | + |
| 101 | + // Verify contents of the second extract |
| 102 | + let thirdValue = try UInt32(parsingBigEndian: &secondSpan) |
| 103 | + #expect(thirdValue == 0x0003_0004) |
| 104 | + #expect(secondSpan.count == 0) |
| 105 | + |
| 106 | + // Input position should advance |
| 107 | + #expect(input.startPosition == 8) |
| 108 | + #expect(input.count == 8) |
| 109 | + |
| 110 | + // objectCount == 0 (should create an empty extracted span) |
| 111 | + let emptySpan = try input.extract(objectStride: 2, objectCount: 0) |
| 112 | + #expect(emptySpan.count == 0) |
| 113 | + #expect(emptySpan.startPosition == 0) |
| 114 | + |
| 115 | + // objectStride == 0 (should create an empty extracted span) |
| 116 | + let emptySpan2 = try input.extract(objectStride: 0, objectCount: 5) |
| 117 | + #expect(emptySpan2.count == 0) |
| 118 | + #expect(emptySpan2.startPosition == 0) |
| 119 | + |
| 120 | + #expect(throws: ParsingError.self) { |
| 121 | + _ = try input.extract(objectStride: 3, objectCount: 3) |
| 122 | + } |
| 123 | + #expect(input.startPosition == 8) |
| 124 | + #expect(throws: ParsingError.self) { |
| 125 | + _ = try input.extract(objectStride: -1, objectCount: 2) |
| 126 | + } |
| 127 | + #expect(throws: ParsingError.self) { |
| 128 | + _ = try input.extract(objectStride: 2, objectCount: -1) |
| 129 | + } |
| 130 | + #expect(throws: ParsingError.self) { |
| 131 | + _ = try input.extract(objectStride: Int.max, objectCount: 2) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Test with empty buffer |
| 136 | + try emptyBuffer.withParserSpan { input in |
| 137 | + let emptySpan = try input.extract(objectStride: 4, objectCount: 0) |
| 138 | + #expect(emptySpan.count == 0) |
| 139 | + #expect(emptySpan.startPosition == 0) |
| 140 | + |
| 141 | + #expect(throws: ParsingError.self) { |
| 142 | + _ = try input.extract(objectStride: 1, objectCount: 1) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test func extractRemaining() throws { |
| 148 | + try buffer.withParserSpan { input in |
| 149 | + // Advance to a position within the buffer |
| 150 | + try input.seek(toRelativeOffset: 6) |
| 151 | + |
| 152 | + var remainingSpan = input.extractRemaining() |
| 153 | + #expect(remainingSpan.startPosition == 0) // Extracted spans start at 0 |
| 154 | + #expect(remainingSpan.count == 10) // 16 - 6 = 10 bytes remaining |
| 155 | + |
| 156 | + // Verify that original input is consumed & reset |
| 157 | + #expect(input.count == 0) |
| 158 | + |
| 159 | + // Verify we can parse the extracted remaining data |
| 160 | + let value1 = try UInt16(parsingBigEndian: &remainingSpan) |
| 161 | + let value2 = try UInt16(parsingBigEndian: &remainingSpan) |
| 162 | + #expect(value1 == 4) |
| 163 | + #expect(value2 == 5) |
| 164 | + #expect(remainingSpan.count == 6) |
| 165 | + |
| 166 | + // Reset to beginning and extract all |
| 167 | + try input.seek(toAbsoluteOffset: 0) |
| 168 | + var fullSpan = input.extractRemaining() |
| 169 | + #expect(fullSpan.startPosition == 0) |
| 170 | + #expect(fullSpan.count == 16) |
| 171 | + #expect(input.count == 0) |
| 172 | + |
| 173 | + // Parse a few values to verify it contains the full buffer data |
| 174 | + let fullValue1 = try UInt16(parsingBigEndian: &fullSpan) |
| 175 | + let fullValue2 = try UInt16(parsingBigEndian: &fullSpan) |
| 176 | + #expect(fullValue1 == 1) |
| 177 | + #expect(fullValue2 == 2) |
| 178 | + } |
| 179 | + |
| 180 | + // Test with empty buffer |
| 181 | + emptyBuffer.withParserSpan { input in |
| 182 | + let emptySpan = input.extractRemaining() |
| 183 | + #expect(emptySpan.startPosition == 0) |
| 184 | + #expect(emptySpan.count == 0) |
| 185 | + #expect(input.count == 0) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Test func extractSliceSemantics() throws { |
| 190 | + try buffer.withParserSpan { input in |
| 191 | + // Create slice and extract of the same data |
| 192 | + try input.seek(toAbsoluteOffset: 4) |
| 193 | + var slicedSpan = try input.sliceSpan(byteCount: 4) |
| 194 | + try input.seek(toAbsoluteOffset: 4) // Go back to same position |
| 195 | + var extractedSpan = try input.extract(byteCount: 4) |
| 196 | + |
| 197 | + // Both should have same count... |
| 198 | + #expect(slicedSpan.count == 4) |
| 199 | + #expect(extractedSpan.count == 4) |
| 200 | + |
| 201 | + // ...but different start positions |
| 202 | + #expect(slicedSpan.startPosition == 4) |
| 203 | + #expect(extractedSpan.startPosition == 0) |
| 204 | + |
| 205 | + // Both should parse the same values |
| 206 | + let sliceValue = try UInt32(parsingBigEndian: &slicedSpan) |
| 207 | + let extractValue = try UInt32(parsingBigEndian: &extractedSpan) |
| 208 | + #expect(sliceValue == 0x0003_0004) |
| 209 | + #expect(sliceValue == extractValue) |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments