Skip to content

Commit

Permalink
reference type check added
Browse files Browse the repository at this point in the history
- added type check
- prepared test case, but not clear to me how to test `assert()`
  • Loading branch information
michaeldorner committed Nov 11, 2016
1 parent e5c5d6f commit 40a59f2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
18 changes: 12 additions & 6 deletions ByteBackpacker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
objects = {

/* Begin PBXBuildFile section */
5F4D819A1DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F4D81991DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift */; };
5F4D819B1DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F4D81991DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift */; };
5F8561D51C415CA700316A22 /* ByteBackpacker.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F8561D41C415CA700316A22 /* ByteBackpacker.h */; settings = {ATTRIBUTES = (Public, ); }; };
5F8561DC1C415CA700316A22 /* ByteBackpacker.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F8561D11C415CA700316A22 /* ByteBackpacker.framework */; };
5F8561E11C415CA700316A22 /* ByteBackpackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F8561E01C415CA700316A22 /* ByteBackpackerTests.swift */; };
5F8561E11C415CA700316A22 /* ByteBackpackerValueTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F8561E01C415CA700316A22 /* ByteBackpackerValueTypeTests.swift */; };
5F8561EC1C415CC300316A22 /* ByteBackpacker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F8561EB1C415CC300316A22 /* ByteBackpacker.swift */; };
5F8561FF1C415D2500316A22 /* ByteBackpacker.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F8561F51C415D2400316A22 /* ByteBackpacker.framework */; };
5F8562151C41670E00316A22 /* ByteBackpackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F8561E01C415CA700316A22 /* ByteBackpackerTests.swift */; };
5F8562151C41670E00316A22 /* ByteBackpackerValueTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F8561E01C415CA700316A22 /* ByteBackpackerValueTypeTests.swift */; };
5F8562161C41677300316A22 /* ByteBackpacker.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F8561D41C415CA700316A22 /* ByteBackpacker.h */; settings = {ATTRIBUTES = (Public, ); }; };
5F8562171C41685300316A22 /* ByteBackpacker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F8561EB1C415CC300316A22 /* ByteBackpacker.swift */; };
/* End PBXBuildFile section */
Expand All @@ -35,12 +37,13 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
5F4D81991DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteBackpackerReferenceTypeTests.swift; sourceTree = "<group>"; };
5F58BA641D956E8600C3B403 /* ByteBackpackerPlayground.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = ByteBackpackerPlayground.playground; sourceTree = "<group>"; };
5F8561D11C415CA700316A22 /* ByteBackpacker.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ByteBackpacker.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5F8561D41C415CA700316A22 /* ByteBackpacker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ByteBackpacker.h; sourceTree = "<group>"; };
5F8561D61C415CA700316A22 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5F8561DB1C415CA700316A22 /* ByteBackpacker iOS Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ByteBackpacker iOS Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
5F8561E01C415CA700316A22 /* ByteBackpackerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ByteBackpackerTests.swift; sourceTree = "<group>"; };
5F8561E01C415CA700316A22 /* ByteBackpackerValueTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ByteBackpackerValueTypeTests.swift; sourceTree = "<group>"; };
5F8561E21C415CA700316A22 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5F8561EB1C415CC300316A22 /* ByteBackpacker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteBackpacker.swift; sourceTree = "<group>"; };
5F8561F51C415D2400316A22 /* ByteBackpacker.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ByteBackpacker.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -115,7 +118,8 @@
5F8561DF1C415CA700316A22 /* ByteBackpackerTests */ = {
isa = PBXGroup;
children = (
5F8561E01C415CA700316A22 /* ByteBackpackerTests.swift */,
5F8561E01C415CA700316A22 /* ByteBackpackerValueTypeTests.swift */,
5F4D81991DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift */,
5F8561E21C415CA700316A22 /* Info.plist */,
);
path = ByteBackpackerTests;
Expand Down Expand Up @@ -305,7 +309,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5F8561E11C415CA700316A22 /* ByteBackpackerTests.swift in Sources */,
5F4D819A1DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift in Sources */,
5F8561E11C415CA700316A22 /* ByteBackpackerValueTypeTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -321,7 +326,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5F8562151C41670E00316A22 /* ByteBackpackerTests.swift in Sources */,
5F4D819B1DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift in Sources */,
5F8562151C41670E00316A22 /* ByteBackpackerValueTypeTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 4 additions & 5 deletions ByteBackpacker/ByteBackpacker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public enum ByteOrder {

open class ByteBackpacker {

fileprivate static let referenceTypeErrorString = "TypeError: Reference Types are not supported."

private static let referenceTypeErrorString = "TypeError: Reference Types are not supported."

open class func unpack<T: Any>(_ valueByteArray: [Byte], byteOrder: ByteOrder = .nativeByteOrder) -> T {
//assert(!(type(of: T.self) is AnyObject), referenceTypeErrorString) // does not work in Swift 3
assert(!(T.self is AnyClass), ByteBackpacker.referenceTypeErrorString)
let bytes = (byteOrder == .littleEndian) ? valueByteArray : valueByteArray.reversed()
return bytes.withUnsafeBufferPointer {
return $0.baseAddress!.withMemoryRebound(to: T.self, capacity: 1) {
Expand All @@ -33,7 +32,7 @@ open class ByteBackpacker {


open class func unpack<T: Any>(_ valueByteArray: [Byte], toType type: T.Type, byteOrder: ByteOrder = .nativeByteOrder) -> T {
//assert(!(T.self is AnyObject), referenceTypeErrorString) // does not work in Swift 3
assert(!(T.self is AnyClass), ByteBackpacker.referenceTypeErrorString)
let bytes = (byteOrder == .littleEndian) ? valueByteArray : valueByteArray.reversed()
return bytes.withUnsafeBufferPointer {
return $0.baseAddress!.withMemoryRebound(to: T.self, capacity: 1) {
Expand All @@ -44,7 +43,7 @@ open class ByteBackpacker {


open class func pack<T: Any>( _ value: T, byteOrder: ByteOrder = .nativeByteOrder) -> [Byte] {
//assert(!(T.self is AnyObject), referenceTypeErrorString) // does not work in Swift 3
assert(!(T.self is AnyClass), ByteBackpacker.referenceTypeErrorString)
var value = value // inout works only for var not let types
let valueByteArray = withUnsafePointer(to: &value) {
Array(UnsafeBufferPointer(start: $0.withMemoryRebound(to: Byte.self, capacity: 1){$0}, count: MemoryLayout<T>.size))
Expand Down
18 changes: 18 additions & 0 deletions ByteBackpackerTests/ByteBackpackerReferenceTypeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
This file is part of ByteBackpacker Project. It is subject to the license terms in the LICENSE file found in the top-level directory of this distribution and at https://github.com/michaeldorner/ByteBackpacker/blob/master/LICENSE. No part of ByteBackpacker Project, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the LICENSE file.
*/

import XCTest
@testable import ByteBackpacker

class ByteBackpackerReferenceTypeTests: XCTestCase {

func testReferenceType() {
/*
//Any ideas how to test assert()?
class C {}
let c = C();
XCTAssertThrowsError(ByteBackpacker.pack(c))
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This file is part of ByteBackpacker Project. It is subject to the license terms
import XCTest
@testable import ByteBackpacker

class ByteBackpackerDoubleTests: XCTestCase {
class ByteBackpackerValueTypeTests: XCTestCase {

fileprivate let testByteArrays: [Double: [UInt8]] = [
-8: [0, 0, 0, 0, 0, 0, 112, 63],
Expand Down

0 comments on commit 40a59f2

Please sign in to comment.