-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from Bouke/endianness
Clarify endianness in integer byte accessor
- Loading branch information
Showing
4 changed files
with
48 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Foundation | ||
|
||
extension FixedWidthInteger { | ||
/// Returns bytes in Big Endian or Network Byte Ordering. | ||
public var bigEndianBytes: Data { | ||
withUnsafeBytes(of: bigEndian, { Data(bytes: $0.baseAddress!, count: $0.count) }) | ||
} | ||
|
||
/// Returns bytes in Little Endian. | ||
public var littleEndianBytes: Data { | ||
withUnsafeBytes(of: littleEndian, { Data(bytes: $0.baseAddress!, count: $0.count) }) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@testable import HAP | ||
import XCTest | ||
|
||
final class FixedWidthIntegerExtensionTests: XCTestCase { | ||
func testBigEndianBytes() throws { | ||
XCTAssertEqual(UInt8(1).bigEndianBytes, Data([1])) | ||
XCTAssertEqual(UInt16(1).bigEndianBytes, Data([0, 1])) | ||
XCTAssertEqual(UInt32(1).bigEndianBytes, Data([0, 0, 0, 1])) | ||
XCTAssertEqual(UInt64(1).bigEndianBytes, Data([0, 0, 0, 0, 0, 0, 0, 1])) | ||
XCTAssertEqual(UInt16(1 << 8).bigEndianBytes, Data([1, 0])) | ||
XCTAssertEqual(UInt32(1 << 24).bigEndianBytes, Data([1, 0, 0, 0])) | ||
XCTAssertEqual(UInt64(1 << 56).bigEndianBytes, Data([1, 0, 0, 0, 0, 0, 0, 0])) | ||
} | ||
|
||
func testLittleEndianBytes() throws { | ||
XCTAssertEqual(UInt8(1).littleEndianBytes, Data([1])) | ||
XCTAssertEqual(UInt16(1).littleEndianBytes, Data([1, 0])) | ||
XCTAssertEqual(UInt32(1).littleEndianBytes, Data([1, 0, 0, 0])) | ||
XCTAssertEqual(UInt64(1).littleEndianBytes, Data([1, 0, 0, 0, 0, 0, 0, 0])) | ||
XCTAssertEqual(UInt16(1 << 8).littleEndianBytes, Data([0, 1])) | ||
XCTAssertEqual(UInt32(1 << 24).littleEndianBytes, Data([0, 0, 0, 1])) | ||
XCTAssertEqual(UInt64(1 << 56).littleEndianBytes, Data([0, 0, 0, 0, 0, 0, 0, 1])) | ||
} | ||
|
||
func testBE() throws { | ||
try XCTSkipUnless(UInt(16) == UInt(16).bigEndian, "Platform is LE") | ||
} | ||
|
||
func testLE() throws { | ||
try XCTSkipIf(UInt(16) == UInt(16).bigEndian, "Platform is BE") | ||
} | ||
} |