Skip to content

Commit 391c7cb

Browse files
committed
Add Data extension tests. Fix bug around reading last byte in data range
1 parent 93b090d commit 391c7cb

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

FastImage.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
27B63D652249AB5A004BCC5B /* DataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27B63D642249AB5A004BCC5B /* DataTests.swift */; };
1011
27CF9392223352940093543E /* FastImage.h in Headers */ = {isa = PBXBuildFile; fileRef = 27CF9390223352940093543E /* FastImage.h */; settings = {ATTRIBUTES = (Public, ); }; };
1112
27CF9397223352C20093543E /* FastImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27CF9396223352C20093543E /* FastImage.swift */; };
1213
27CF939922335E490093543E /* FastImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27CF939822335E490093543E /* FastImageTests.swift */; };
@@ -25,6 +26,7 @@
2526
/* End PBXBuildFile section */
2627

2728
/* Begin PBXFileReference section */
29+
27B63D642249AB5A004BCC5B /* DataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataTests.swift; sourceTree = "<group>"; };
2830
27CF938E223352940093543E /* FastImage.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FastImage.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2931
27CF9390223352940093543E /* FastImage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FastImage.h; sourceTree = "<group>"; };
3032
27CF9391223352940093543E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -123,6 +125,7 @@
123125
isa = PBXGroup;
124126
children = (
125127
27CF939822335E490093543E /* FastImageTests.swift */,
128+
27B63D642249AB5A004BCC5B /* DataTests.swift */,
126129
A1B48FDA1A0DD89F001BB364 /* Supporting Files */,
127130
);
128131
path = FastImageTests;
@@ -265,6 +268,7 @@
265268
buildActionMask = 2147483647;
266269
files = (
267270
27CF939922335E490093543E /* FastImageTests.swift in Sources */,
271+
27B63D652249AB5A004BCC5B /* DataTests.swift in Sources */,
268272
);
269273
runOnlyForDeploymentPostprocessing = 0;
270274
};

FastImage/DataExtensions.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111
extension Data {
12-
struct DataAccessError: Error {
12+
struct AccessError: Error {
1313
let range: Range<Data.Index>
1414
let data: Data
1515
}
@@ -20,18 +20,18 @@ extension Data {
2020
return try read(index..<index+1) as UInt8
2121
}
2222
func read<ResultType>(_ range: Range<Data.Index>) throws -> ResultType {
23-
if range.upperBound < count {
23+
if range.upperBound <= count {
2424
return subdata(in: range).withUnsafeBytes({ $0.pointee })
2525
}
26-
throw DataAccessError(range: range, data: self)
26+
throw AccessError(range: range, data: self)
2727
}
2828
func readBytes(_ range: Range<Data.Index>) throws -> [UInt8] {
29-
if range.upperBound < count {
29+
if range.upperBound <= count {
3030
return subdata(in: range).withUnsafeBytes { bytes in
3131
Array(UnsafeBufferPointer(start: bytes, count: range.count / MemoryLayout<UInt8>.stride))
3232
}
3333
}
34-
throw DataAccessError(range: range, data: self)
34+
throw AccessError(range: range, data: self)
3535
}
3636
}
3737

FastImage/Size Decoders/ImageSizeDecoder.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ protocol ImageSizeDecoder {
2020
///
2121
/// For example, a GIF size decoder will check the first 3 bytes to see if they match "GIF"
2222
///
23-
/// - throws: DataAccessError
23+
/// - throws: Data.AccessError
2424
static func isDecoder(for data: Data) throws -> Bool
2525
/// Get the size of the image given the data available
2626
///
27-
/// - throws: SizeNotFoundError, DataAccessError
27+
/// - throws: SizeNotFoundError, Data.AccessError
2828
static func size(for data: Data) throws -> CGSize
2929
}

FastImageTests/DataTests.swift

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// DataTests.swift
3+
// FastImageTests
4+
//
5+
// Created by Kyle Hickinson on 2019-03-25.
6+
// Copyright © 2019 Kyle Hickinson. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import FastImage
11+
12+
class DataTests: XCTestCase {
13+
14+
func testSingleByteRead() {
15+
let data = Data([0x01, 0x02])
16+
do {
17+
XCTAssertEqual(try data.read(0), 0x01)
18+
XCTAssertEqual(try data.read(1), 0x02)
19+
}
20+
}
21+
22+
func testDataAccessError() {
23+
let data = Data([0x01, 0x02])
24+
do {
25+
_ = try data.read(3)
26+
XCTFail("Should have failed to read from 3")
27+
} catch {
28+
XCTAssertTrue(error is Data.AccessError)
29+
}
30+
}
31+
32+
func testReadBytes() {
33+
let bytes: [UInt8] = [0xdd, 0xee, 0xff]
34+
let data = Data(bytes)
35+
XCTAssertEqual(try data.readBytes(0..<3), bytes)
36+
}
37+
38+
func testReadToType() {
39+
let value: UInt16 = 4096
40+
let data = Data(withUnsafeBytes(of: value, { Array($0) }))
41+
XCTAssertEqual(data.count, 2)
42+
XCTAssertEqual(try data.read(0..<2) as UInt16, value)
43+
}
44+
45+
func testReadToStruct() {
46+
struct TestType: Equatable {
47+
let a: UInt16
48+
let b: UInt16
49+
let c: UInt32
50+
}
51+
let value = TestType(a: 0x800, b: 0x1000, c: 0x10000)
52+
let data = Data(withUnsafeBytes(of: value, { Array($0) }))
53+
let structSize = MemoryLayout<TestType>.size
54+
XCTAssertEqual(data.count, structSize)
55+
XCTAssertEqual(try data.read(0..<structSize) as TestType, value)
56+
XCTAssertEqual(try data.read(2..<4) as UInt16, value.b)
57+
XCTAssertEqual(try data.read(4..<8) as UInt32, value.c)
58+
}
59+
}

0 commit comments

Comments
 (0)