From 66098f51204436d445028d665677c3f2e3fdc475 Mon Sep 17 00:00:00 2001 From: woxtu Date: Sat, 8 Jul 2023 01:37:46 +0000 Subject: [PATCH 1/2] Add byte order utilities --- Foundation.xcodeproj/project.pbxproj | 4 + Sources/Foundation/CMakeLists.txt | 1 + Sources/Foundation/NSByteOrder.swift | 286 +++++++++++++++++++++++++++ 3 files changed, 291 insertions(+) create mode 100644 Sources/Foundation/NSByteOrder.swift diff --git a/Foundation.xcodeproj/project.pbxproj b/Foundation.xcodeproj/project.pbxproj index 4f3e98bd42..34b507f3e8 100644 --- a/Foundation.xcodeproj/project.pbxproj +++ b/Foundation.xcodeproj/project.pbxproj @@ -404,6 +404,7 @@ 90E645DF1E4C89A400D0D47C /* TestNSCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90E645DE1E4C89A400D0D47C /* TestNSCache.swift */; }; 91B668A32252B3C5001487A1 /* FileManager+POSIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B668A22252B3C5001487A1 /* FileManager+POSIX.swift */; }; 91B668A52252B3E7001487A1 /* FileManager+Win32.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B668A42252B3E7001487A1 /* FileManager+Win32.swift */; }; + 9E1A0B0F2A58F2C7008E971B /* NSByteOrder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E1A0B0E2A58F2C7008E971B /* NSByteOrder.swift */; }; 9F0DD3521ECD73D000F68030 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F0041781ECD5962004138BD /* main.swift */; }; 9F0DD3571ECD783500F68030 /* SwiftFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D885D1BBC938800234F36 /* SwiftFoundation.framework */; }; A058C2021E529CF100B07AA1 /* TestMassFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A058C2011E529CF100B07AA1 /* TestMassFormatter.swift */; }; @@ -1134,6 +1135,7 @@ 90E645DE1E4C89A400D0D47C /* TestNSCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSCache.swift; sourceTree = ""; }; 91B668A22252B3C5001487A1 /* FileManager+POSIX.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+POSIX.swift"; sourceTree = ""; }; 91B668A42252B3E7001487A1 /* FileManager+Win32.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+Win32.swift"; sourceTree = ""; }; + 9E1A0B0E2A58F2C7008E971B /* NSByteOrder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSByteOrder.swift; sourceTree = ""; }; 9F0041781ECD5962004138BD /* main.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 9F0DD33F1ECD734200F68030 /* xdgTestHelper.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = xdgTestHelper.app; sourceTree = BUILT_PRODUCTS_DIR; }; 9F0DD34F1ECD737B00F68030 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -2169,6 +2171,7 @@ EADE0B6A1BD15DFF00C49C64 /* NotificationQueue.swift */, 5BDC3F2E1BCC5DCB00ED97BB /* NSArray.swift */, EADE0B4F1BD09E3100C49C64 /* NSAttributedString.swift */, + 9E1A0B0E2A58F2C7008E971B /* NSByteOrder.swift */, EADE0B541BD15DFF00C49C64 /* NSCache.swift */, 5BDC3F301BCC5DCB00ED97BB /* NSCalendar.swift */, 5BDC3FC91BCF176100ED97BB /* NSCFArray.swift */, @@ -3005,6 +3008,7 @@ 5B424C761D0B6E5B007B39C8 /* IndexPath.swift in Sources */, EADE0BB51BD15E0000C49C64 /* Scanner.swift in Sources */, EADE0BA01BD15DFF00C49C64 /* NSIndexPath.swift in Sources */, + 9E1A0B0F2A58F2C7008E971B /* NSByteOrder.swift in Sources */, 5BF7AEB51BCD51F9008F214A /* NSPathUtilities.swift in Sources */, B96C113725BA376D00985A32 /* NSDateComponents.swift in Sources */, EADE0B9D1BD15DFF00C49C64 /* NSGeometry.swift in Sources */, diff --git a/Sources/Foundation/CMakeLists.txt b/Sources/Foundation/CMakeLists.txt index 398fbfc5e9..6790531a9c 100644 --- a/Sources/Foundation/CMakeLists.txt +++ b/Sources/Foundation/CMakeLists.txt @@ -56,6 +56,7 @@ add_library(Foundation NotificationQueue.swift NSArray.swift NSAttributedString.swift + NSByteOrder.swift NSCache.swift NSCalendar.swift NSCFArray.swift diff --git a/Sources/Foundation/NSByteOrder.swift b/Sources/Foundation/NSByteOrder.swift new file mode 100644 index 0000000000..d1e1bd1355 --- /dev/null +++ b/Sources/Foundation/NSByteOrder.swift @@ -0,0 +1,286 @@ +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// + +@_implementationOnly import CoreFoundation + +/// The byte order is unknown. +public let NS_UnknownByteOrder: Int = Int(CFByteOrderUnknown.rawValue) +/// The byte order is little endian. +public let NS_LittleEndian: Int = Int(CFByteOrderLittleEndian.rawValue) +/// The byte order is big endian. +public let NS_BigEndian: Int = Int(CFByteOrderBigEndian.rawValue) + +/// Returns the endian format. +@inline(__always) public func NSHostByteOrder() -> Int { + return CFByteOrderGetCurrent() +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapShort(_ inv: UInt16) -> UInt16 { + return CFSwapInt16(inv) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapInt(_ inv: UInt32) -> UInt32 { + return CFSwapInt32(inv) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLong(_ inv: UInt) -> UInt { +#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + return UInt(CFSwapInt64(UInt64(inv))) +#else + return CFSwapInt32(inv) +#endif +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLongLong(_ inv: UInt64) -> UInt64 { + return CFSwapInt64(inv) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigShortToHost(_ x: UInt16) -> UInt16 { + return CFSwapInt16BigToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigIntToHost(_ x: UInt32) -> UInt32 { + return CFSwapInt32BigToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigLongToHost(_ x: UInt) -> UInt { +#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + return UInt(CFSwapInt64BigToHost(UInt64(x))) +#else + return CFSwapInt32BigToHost(x) +#endif +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigLongLongToHost(_ x: UInt64) -> UInt64 { + return CFSwapInt64BigToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostShortToBig(_ x: UInt16) -> UInt16 { + return CFSwapInt16HostToBig(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostIntToBig(_ x: UInt32) -> UInt32 { + return CFSwapInt32HostToBig(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostLongToBig(_ x: UInt) -> UInt { +#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + return UInt(CFSwapInt64HostToBig(UInt64(x))) +#else + return CFSwapInt32HostToBig(x) +#endif +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostLongLongToBig(_ x: UInt64) -> UInt64 { + return CFSwapInt64HostToBig(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleShortToHost(_ x: UInt16) -> UInt16 { + return CFSwapInt16LittleToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleIntToHost(_ x: UInt32) -> UInt32 { + return CFSwapInt32LittleToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleLongToHost(_ x: UInt) -> UInt { +#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + return UInt(CFSwapInt64LittleToHost(UInt64(x))) +#else + return CFSwapInt32LittleToHost(x) +#endif +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleLongLongToHost(_ x: UInt64) -> UInt64 { + return CFSwapInt64LittleToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostShortToLittle(_ x: UInt16) -> UInt16 { + return CFSwapInt16HostToLittle(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostIntToLittle(_ x: UInt32) -> UInt32 { + return CFSwapInt32HostToLittle(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostLongToLittle(_ x: UInt) -> UInt { +#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + return UInt(CFSwapInt64HostToLittle(UInt64(x))) +#else + return CFSwapInt32HostToLittle(x) +#endif +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostLongLongToLittle(_ x: UInt64) -> UInt64 { + return CFSwapInt64HostToLittle(x) +} + +/// Opaque type containing an endian-independent `float` value. +public struct NSSwappedFloat : @unchecked Sendable { + public var v: UInt32 + + public init() { + self.v = 0 + } + + public init(v: UInt32) { + self.v = v + } +} + +/// Opaque structure containing endian-independent `double` value. +public struct NSSwappedDouble : @unchecked Sendable { + public var v: UInt64 + + public init() { + self.v = 0 + } + + public init(v: UInt64) { + self.v = v + } +} + +/// Performs a type conversion. +@inline(__always) public func NSConvertHostFloatToSwapped(_ x: Float) -> NSSwappedFloat { + return NSSwappedFloat(v: x.bitPattern) +} + +/// Performs a type conversion. +@inline(__always) public func NSConvertSwappedFloatToHost(_ x: NSSwappedFloat) -> Float { + return Float(bitPattern: x.v) +} + +/// Performs a type conversion. +@inline(__always) public func NSConvertHostDoubleToSwapped(_ x: Double) -> NSSwappedDouble { + return NSSwappedDouble(v: x.bitPattern) +} + +/// Performs a type conversion. +@inline(__always) public func NSConvertSwappedDoubleToHost(_ x: NSSwappedDouble) -> Double { + return Double(bitPattern: x.v) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapFloat(_ x: NSSwappedFloat) -> NSSwappedFloat { + return NSSwappedFloat(v: NSSwapInt(x.v)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapDouble(_ x: NSSwappedDouble) -> NSSwappedDouble { + return NSSwappedDouble(v: NSSwapLongLong(x.v)) +} + +#if _endian(big) + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigDoubleToHost(_ x: NSSwappedDouble) -> Double { + return NSConvertSwappedDoubleToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigFloatToHost(_ x: NSSwappedFloat) -> Float { + return NSConvertSwappedFloatToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostDoubleToBig(_ x: Double) -> NSSwappedDouble { + return NSConvertHostDoubleToSwapped(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostFloatToBig(_ x: Float) -> NSSwappedFloat { + return NSConvertHostFloatToSwapped(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleDoubleToHost(_ x: NSSwappedDouble) -> Double { + return NSConvertSwappedDoubleToHost(NSSwapDouble(x)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleFloatToHost(_ x: NSSwappedFloat) -> Float { + return NSConvertSwappedFloatToHost(NSSwapFloat(x)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostDoubleToLittle(_ x: Double) -> NSSwappedDouble { + return NSSwapDouble(NSConvertHostDoubleToSwapped(x)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostFloatToLittle(_ x: Float) -> NSSwappedFloat { + return NSSwapFloat(NSConvertHostFloatToSwapped(x)) +} + +#elseif _endian(little) + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigDoubleToHost(_ x: NSSwappedDouble) -> Double { + return NSConvertSwappedDoubleToHost(NSSwapDouble(x)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapBigFloatToHost(_ x: NSSwappedFloat) -> Float { + return NSConvertSwappedFloatToHost(NSSwapFloat(x)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostDoubleToBig(_ x: Double) -> NSSwappedDouble { + return NSSwapDouble(NSConvertHostDoubleToSwapped(x)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostFloatToBig(_ x: Float) -> NSSwappedFloat { + return NSSwapFloat(NSConvertHostFloatToSwapped(x)) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleDoubleToHost(_ x: NSSwappedDouble) -> Double { + return NSConvertSwappedDoubleToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapLittleFloatToHost(_ x: NSSwappedFloat) -> Float { + return NSConvertSwappedFloatToHost(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostDoubleToLittle(_ x: Double) -> NSSwappedDouble { + return NSConvertHostDoubleToSwapped(x) +} + +/// Swaps the bytes of a number. +@inline(__always) public func NSSwapHostFloatToLittle(_ x: Float) -> NSSwappedFloat { + return NSConvertHostFloatToSwapped(x) +} + +#else +#errors("Do not know the endianess of this architecture") +#endif From a9534eb88b6ea4c0e4a429802446113884d7ae1c Mon Sep 17 00:00:00 2001 From: woxtu Date: Fri, 14 Jul 2023 12:22:37 +0000 Subject: [PATCH 2/2] Add unit tests --- Foundation.xcodeproj/project.pbxproj | 4 + Tests/Foundation/CMakeLists.txt | 1 + Tests/Foundation/Tests/TestNSByteOrder.swift | 317 +++++++++++++++++++ Tests/Foundation/main.swift | 1 + 4 files changed, 323 insertions(+) create mode 100644 Tests/Foundation/Tests/TestNSByteOrder.swift diff --git a/Foundation.xcodeproj/project.pbxproj b/Foundation.xcodeproj/project.pbxproj index 34b507f3e8..c5d9f44586 100644 --- a/Foundation.xcodeproj/project.pbxproj +++ b/Foundation.xcodeproj/project.pbxproj @@ -405,6 +405,7 @@ 91B668A32252B3C5001487A1 /* FileManager+POSIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B668A22252B3C5001487A1 /* FileManager+POSIX.swift */; }; 91B668A52252B3E7001487A1 /* FileManager+Win32.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B668A42252B3E7001487A1 /* FileManager+Win32.swift */; }; 9E1A0B0F2A58F2C7008E971B /* NSByteOrder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E1A0B0E2A58F2C7008E971B /* NSByteOrder.swift */; }; + 9ECDC74B2A61701300F991B5 /* TestNSByteOrder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ECDC74A2A61701300F991B5 /* TestNSByteOrder.swift */; }; 9F0DD3521ECD73D000F68030 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F0041781ECD5962004138BD /* main.swift */; }; 9F0DD3571ECD783500F68030 /* SwiftFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D885D1BBC938800234F36 /* SwiftFoundation.framework */; }; A058C2021E529CF100B07AA1 /* TestMassFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A058C2011E529CF100B07AA1 /* TestMassFormatter.swift */; }; @@ -1136,6 +1137,7 @@ 91B668A22252B3C5001487A1 /* FileManager+POSIX.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+POSIX.swift"; sourceTree = ""; }; 91B668A42252B3E7001487A1 /* FileManager+Win32.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+Win32.swift"; sourceTree = ""; }; 9E1A0B0E2A58F2C7008E971B /* NSByteOrder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSByteOrder.swift; sourceTree = ""; }; + 9ECDC74A2A61701300F991B5 /* TestNSByteOrder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSByteOrder.swift; sourceTree = ""; }; 9F0041781ECD5962004138BD /* main.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 9F0DD33F1ECD734200F68030 /* xdgTestHelper.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = xdgTestHelper.app; sourceTree = BUILT_PRODUCTS_DIR; }; 9F0DD34F1ECD737B00F68030 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -1903,6 +1905,7 @@ 5EF673AB1C28B527006212A3 /* TestNotificationQueue.swift */, EA66F63C1BF1619600136161 /* TestNSArray.swift */, 294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */, + 9ECDC74A2A61701300F991B5 /* TestNSByteOrder.swift */, 90E645DE1E4C89A400D0D47C /* TestNSCache.swift */, 15F10CDB218909BF00D88114 /* TestNSCalendar.swift */, 790043391CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift */, @@ -3281,6 +3284,7 @@ DAA79BD920D42C07004AF044 /* TestURLProtectionSpace.swift in Sources */, 5D0E1BDB237A1FE800C35C5A /* TestUnitVolume.swift in Sources */, 616068F5225DE606004FCC54 /* TestURLSessionFTP.swift in Sources */, + 9ECDC74B2A61701300F991B5 /* TestNSByteOrder.swift in Sources */, 155B77AC22E63D2D00D901DE /* TestURLCredentialStorage.swift in Sources */, B951B5EC1F4E2A2000D8B332 /* TestNSLock.swift in Sources */, 5B13B33A1C582D4C00651CE2 /* TestNSNumber.swift in Sources */, diff --git a/Tests/Foundation/CMakeLists.txt b/Tests/Foundation/CMakeLists.txt index abe9b8b48a..b5d629f018 100644 --- a/Tests/Foundation/CMakeLists.txt +++ b/Tests/Foundation/CMakeLists.txt @@ -49,6 +49,7 @@ target_sources(TestFoundation PRIVATE Tests/TestNotification.swift Tests/TestNSArray.swift Tests/TestNSAttributedString.swift + Tests/TestNSByteOrder.swift Tests/TestNSCache.swift Tests/TestNSCalendar.swift Tests/TestNSCompoundPredicate.swift diff --git a/Tests/Foundation/Tests/TestNSByteOrder.swift b/Tests/Foundation/Tests/TestNSByteOrder.swift new file mode 100644 index 0000000000..5ff145b60a --- /dev/null +++ b/Tests/Foundation/Tests/TestNSByteOrder.swift @@ -0,0 +1,317 @@ +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT + #if canImport(SwiftFoundation) && !DEPLOYMENT_RUNTIME_OBJC + @testable import SwiftFoundation + #else + @testable import Foundation + #endif +#endif + +class TestNSByteOrder: XCTestCase { + func test_NSHostByteOrder() throws { +#if _endian(big) + XCTAssertEqual(NSHostByteOrder(), NS_BigEndian) +#elseif _endian(little) + XCTAssertEqual(NSHostByteOrder(), NS_LittleEndian) +#endif + } + + func test_NSSwapShort() throws { + XCTAssertEqual(NSSwapShort(0x0001), 0x0100) + } + + func test_NSSwapInt() throws { + XCTAssertEqual(NSSwapInt(0x00000001), 0x01000000) + } + + func test_NSSwapLong() throws { +#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapLong(0x0000000000000001), 0x0100000000000000) +#else + XCTAssertEqual(NSSwapLong(0x00000001), 0x01000000) +#endif + } + + func test_NSSwapLongLong() throws { + XCTAssertEqual(NSSwapLongLong(0x0000000000000001), 0x0100000000000000) + } + + func test_NSSwapBigShortToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapBigShortToHost(0x0001), 0x0001) +#elseif _endian(little) + XCTAssertEqual(NSSwapBigShortToHost(0x0001), 0x0100) +#endif + } + + func test_NSSwapBigIntToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapBigIntToHost(0x00000001), 0x00000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapBigIntToHost(0x00000001), 0x01000000) +#endif + } + + func test_NSSwapBigLongToHost() throws { +#if _endian(big) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapBigLongToHost(0x0000000000000001), 0x0000000000000001) +#elseif _endian(big) + XCTAssertEqual(NSSwapBigLongToHost(0x00000001), 0x00000001) +#elseif _endian(little) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapBigLongToHost(0x0000000000000001), 0x0100000000000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapBigLongToHost(0x00000001), 0x01000000) +#endif + } + + func test_NSSwapBigLongLongToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapBigLongLongToHost(0x0000000000000001), 0x0000000000000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapBigLongLongToHost(0x0000000000000001), 0x0100000000000000) +#endif + } + + func test_NSSwapHostShortToBig() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostShortToBig(0x0001), 0x0001) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostShortToBig(0x0001), 0x0100) +#endif + } + + func test_NSSwapHostIntToBig() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostIntToBig(0x00000001), 0x00000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostIntToBig(0x00000001), 0x01000000) +#endif + } + + func test_NSSwapHostLongToBig() throws { +#if _endian(big) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapHostLongToBig(0x0000000000000001), 0x0000000000000001) +#elseif _endian(big) + XCTAssertEqual(NSSwapHostLongToBig(0x00000001), 0x00000001) +#elseif _endian(little) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapHostLongToBig(0x0000000000000001), 0x0100000000000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostLongToBig(0x00000001), 0x01000000) +#endif + } + + func test_NSSwapHostLongLongToBig() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostLongLongToBig(0x0000000000000001), 0x0000000000000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostLongLongToBig(0x0000000000000001), 0x0100000000000000) +#endif + } + + func test_NSSwapLittleShortToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapLittleShortToHost(0x0001), 0x0100) +#elseif _endian(little) + XCTAssertEqual(NSSwapLittleShortToHost(0x0001), 0x0001) +#endif + } + + func test_NSSwapLittleIntToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapLittleIntToHost(0x00000001), 0x01000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapLittleIntToHost(0x00000001), 0x00000001) +#endif + } + + func test_NSSwapLittleLongToHost() throws { +#if _endian(big) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapLittleLongToHost(0x0000000000000001), 0x0100000000000000) +#elseif _endian(big) + XCTAssertEqual(NSSwapLittleLongToHost(0x00000001), 0x01000000) +#elseif _endian(little) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapLittleLongToHost(0x0000000000000001), 0x0000000000000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapLittleLongToHost(0x00000001), 0x00000001) +#endif + } + + func test_NSSwapLittleLongLongToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapLittleLongLongToHost(0x0000000000000001), 0x0100000000000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapLittleLongLongToHost(0x0000000000000001), 0x0000000000000001) +#endif + } + + func test_NSSwapHostShortToLittle() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostShortToLittle(0x0001), 0x0100) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostShortToLittle(0x0001), 0x0001) +#endif + } + + func test_NSSwapHostIntToLittle() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostIntToLittle(0x00000001), 0x01000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostIntToLittle(0x00000001), 0x00000001) +#endif + } + + func test_NSSwapHostLongToLittle() throws { +#if _endian(big) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapHostLongToLittle(0x0000000000000001), 0x0100000000000000) +#elseif _endian(big) + XCTAssertEqual(NSSwapHostLongToLittle(0x00000001), 0x01000000) +#elseif _endian(little) && arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) + XCTAssertEqual(NSSwapHostLongToLittle(0x0000000000000001), 0x0000000000000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostLongToLittle(0x00000001), 0x00000001) +#endif + } + + func test_NSSwapHostLongLongToLittle() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostLongLongToLittle(0x0000000000000001), 0x0100000000000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostLongLongToLittle(0x0000000000000001), 0x0000000000000001) +#endif + } + + func test_NSConvertHostFloatToSwapped() throws { + XCTAssertEqual(NSConvertHostFloatToSwapped(Float(bitPattern: 0x00000001)).v, 0x00000001) + } + + func test_NSConvertSwappedFloatToHost() throws { + XCTAssertEqual(NSConvertSwappedFloatToHost(NSSwappedFloat(v: 0x00000001)), Float(bitPattern: 0x00000001)) + } + + func test_NSConvertHostDoubleToSwapped() throws { + XCTAssertEqual(NSConvertHostDoubleToSwapped(Double(bitPattern: 0x0100000000000000)).v, 0x0100000000000000) + } + + func test_NSConvertSwappedDoubleToHost() throws { + XCTAssertEqual(NSConvertSwappedDoubleToHost(NSSwappedDouble(v: 0x0100000000000000)), Double(bitPattern: 0x0100000000000000)) + } + + func test_NSSwapFloat() throws { + XCTAssertEqual(NSSwapFloat(NSSwappedFloat(v: 0x00000001)).v, 0x01000000) + } + + func test_NSSwapDouble() throws { + XCTAssertEqual(NSSwapDouble(NSSwappedDouble(v: 0x0000000000000001)).v, 0x0100000000000000) + } + + func test_NSSwapBigDoubleToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapBigDoubleToHost(NSSwappedDouble(v: 0x0000000000000001)), Double(bitPattern: 0x0000000000000001)) +#elseif _endian(little) + XCTAssertEqual(NSSwapBigDoubleToHost(NSSwappedDouble(v: 0x0000000000000001)), Double(bitPattern: 0x0100000000000000)) +#endif + } + + func test_NSSwapBigFloatToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapBigFloatToHost(NSSwappedFloat(v: 0x00000001)), Float(bitPattern: 0x00000001)) +#elseif _endian(little) + XCTAssertEqual(NSSwapBigFloatToHost(NSSwappedFloat(v: 0x00000001)), Float(bitPattern: 0x01000000)) +#endif + } + + func test_NSSwapHostDoubleToBig() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostDoubleToBig(Double(bitPattern: 0x0000000000000001)).v, 0x0000000000000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostDoubleToBig(Double(bitPattern: 0x0000000000000001)).v, 0x0100000000000000) +#endif + } + + func test_NSSwapHostFloatToBig() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostFloatToBig(Float(bitPattern: 0x00000001)).v, 0x00000001) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostFloatToBig(Float(bitPattern: 0x00000001)).v, 0x01000000) +#endif + } + + func test_NSSwapLittleDoubleToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapLittleDoubleToHost(NSSwappedDouble(v: 0x0000000000000001)), Double(bitPattern: 0x0100000000000000)) +#elseif _endian(little) + XCTAssertEqual(NSSwapLittleDoubleToHost(NSSwappedDouble(v: 0x0000000000000001)), Double(bitPattern: 0x0000000000000001)) +#endif + } + + func test_NSSwapLittleFloatToHost() throws { +#if _endian(big) + XCTAssertEqual(NSSwapLittleFloatToHost(NSSwappedFloat(v: 0x00000001)), Float(bitPattern: 0x01000000)) +#elseif _endian(little) + XCTAssertEqual(NSSwapLittleFloatToHost(NSSwappedFloat(v: 0x00000001)), Float(bitPattern: 0x00000001)) +#endif + } + + func test_NSSwapHostDoubleToLittle() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostDoubleToLittle(Double(bitPattern: 0x0000000000000001)).v, 0x0100000000000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostDoubleToLittle(Double(bitPattern: 0x0000000000000001)).v, 0x0000000000000001) +#endif + } + + func test_NSSwapHostFloatToLittle() throws { +#if _endian(big) + XCTAssertEqual(NSSwapHostFloatToLittle(Float(bitPattern: 0x00000001)).v, 0x01000000) +#elseif _endian(little) + XCTAssertEqual(NSSwapHostFloatToLittle(Float(bitPattern: 0x00000001)).v, 0x00000001) +#endif + } + + static var allTests: [(String, (TestNSByteOrder) -> () throws -> Void)] { + return [ + ("test_NSHostByteOrder", test_NSHostByteOrder), + ("test_NSSwapShort", test_NSSwapShort), + ("test_NSSwapInt", test_NSSwapInt), + ("test_NSSwapLong", test_NSSwapLong), + ("test_NSSwapLongLong", test_NSSwapLongLong), + ("test_NSSwapBigShortToHost", test_NSSwapBigShortToHost), + ("test_NSSwapBigIntToHost", test_NSSwapBigIntToHost), + ("test_NSSwapBigLongToHost", test_NSSwapBigLongToHost), + ("test_NSSwapBigLongLongToHost", test_NSSwapBigLongLongToHost), + ("test_NSSwapHostShortToBig", test_NSSwapHostShortToBig), + ("test_NSSwapHostIntToBig", test_NSSwapHostIntToBig), + ("test_NSSwapHostLongToBig", test_NSSwapHostLongToBig), + ("test_NSSwapHostLongLongToBig", test_NSSwapHostLongLongToBig), + ("test_NSSwapLittleShortToHost", test_NSSwapLittleShortToHost), + ("test_NSSwapLittleIntToHost", test_NSSwapLittleIntToHost), + ("test_NSSwapLittleLongToHost", test_NSSwapLittleLongToHost), + ("test_NSSwapLittleLongLongToHost", test_NSSwapLittleLongLongToHost), + ("test_NSSwapHostShortToLittle", test_NSSwapHostShortToLittle), + ("test_NSSwapHostIntToLittle", test_NSSwapHostIntToLittle), + ("test_NSSwapHostLongToLittle", test_NSSwapHostLongToLittle), + ("test_NSSwapHostLongLongToLittle", test_NSSwapHostLongLongToLittle), + ("test_NSConvertHostFloatToSwapped", test_NSConvertHostFloatToSwapped), + ("test_NSConvertSwappedFloatToHost", test_NSConvertSwappedFloatToHost), + ("test_NSConvertHostDoubleToSwapped", test_NSConvertHostDoubleToSwapped), + ("test_NSConvertSwappedDoubleToHost", test_NSConvertSwappedDoubleToHost), + ("test_NSSwapFloat", test_NSSwapFloat), + ("test_NSSwapDouble", test_NSSwapDouble), + ("test_NSSwapBigDoubleToHost", test_NSSwapBigDoubleToHost), + ("test_NSSwapBigFloatToHost", test_NSSwapBigFloatToHost), + ("test_NSSwapHostDoubleToBig", test_NSSwapHostDoubleToBig), + ("test_NSSwapHostFloatToBig", test_NSSwapHostFloatToBig), + ("test_NSSwapLittleDoubleToHost", test_NSSwapLittleDoubleToHost), + ("test_NSSwapLittleFloatToHost", test_NSSwapLittleFloatToHost), + ("test_NSSwapHostDoubleToLittle", test_NSSwapHostDoubleToLittle), + ("test_NSSwapHostFloatToLittle", test_NSSwapHostFloatToLittle), + ] + } +} \ No newline at end of file diff --git a/Tests/Foundation/main.swift b/Tests/Foundation/main.swift index e6c7b35bc1..d8e7cb68a5 100644 --- a/Tests/Foundation/main.swift +++ b/Tests/Foundation/main.swift @@ -107,6 +107,7 @@ var allTestCases = [ testCase(TestXMLParser.allTests), testCase(TestXMLDocument.allTests), testCase(TestNSAttributedString.allTests), + testCase(TestNSByteOrder.allTests), testCase(TestNSMutableAttributedString.allTests), testCase(TestFileHandle.allTests), testCase(TestUnitConverter.allTests),