diff --git a/ByteBackpacker.xcodeproj/project.pbxproj b/ByteBackpacker.xcodeproj/project.pbxproj index d90a812..4b8a3c5 100644 --- a/ByteBackpacker.xcodeproj/project.pbxproj +++ b/ByteBackpacker.xcodeproj/project.pbxproj @@ -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 */ @@ -35,12 +37,13 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 5F4D81991DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteBackpackerReferenceTypeTests.swift; sourceTree = ""; }; 5F58BA641D956E8600C3B403 /* ByteBackpackerPlayground.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = ByteBackpackerPlayground.playground; sourceTree = ""; }; 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 = ""; }; 5F8561D61C415CA700316A22 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 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 = ""; }; + 5F8561E01C415CA700316A22 /* ByteBackpackerValueTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ByteBackpackerValueTypeTests.swift; sourceTree = ""; }; 5F8561E21C415CA700316A22 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 5F8561EB1C415CC300316A22 /* ByteBackpacker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteBackpacker.swift; sourceTree = ""; }; 5F8561F51C415D2400316A22 /* ByteBackpacker.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ByteBackpacker.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -115,7 +118,8 @@ 5F8561DF1C415CA700316A22 /* ByteBackpackerTests */ = { isa = PBXGroup; children = ( - 5F8561E01C415CA700316A22 /* ByteBackpackerTests.swift */, + 5F8561E01C415CA700316A22 /* ByteBackpackerValueTypeTests.swift */, + 5F4D81991DD640A800EF665B /* ByteBackpackerReferenceTypeTests.swift */, 5F8561E21C415CA700316A22 /* Info.plist */, ); path = ByteBackpackerTests; @@ -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; }; @@ -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; }; diff --git a/ByteBackpacker/ByteBackpacker.swift b/ByteBackpacker/ByteBackpacker.swift index 2a1bbca..104080e 100644 --- a/ByteBackpacker/ByteBackpacker.swift +++ b/ByteBackpacker/ByteBackpacker.swift @@ -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(_ 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) { @@ -33,7 +32,7 @@ open class ByteBackpacker { open class func unpack(_ 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) { @@ -44,7 +43,7 @@ open class ByteBackpacker { open class func pack( _ 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.size)) diff --git a/ByteBackpackerTests/ByteBackpackerReferenceTypeTests.swift b/ByteBackpackerTests/ByteBackpackerReferenceTypeTests.swift new file mode 100644 index 0000000..7d6e839 --- /dev/null +++ b/ByteBackpackerTests/ByteBackpackerReferenceTypeTests.swift @@ -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)) + */ + } +} diff --git a/ByteBackpackerTests/ByteBackpackerTests.swift b/ByteBackpackerTests/ByteBackpackerValueTypeTests.swift similarity index 98% rename from ByteBackpackerTests/ByteBackpackerTests.swift rename to ByteBackpackerTests/ByteBackpackerValueTypeTests.swift index bef081b..8489602 100644 --- a/ByteBackpackerTests/ByteBackpackerTests.swift +++ b/ByteBackpackerTests/ByteBackpackerValueTypeTests.swift @@ -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],