diff --git a/Package.swift b/Package.swift index e4008e4..bc18dda 100644 --- a/Package.swift +++ b/Package.swift @@ -3,29 +3,29 @@ import PackageDescription let package = Package( - - name: "SwiftRadix", - - platforms: [.macOS(.v10_10), .iOS(.v8), .tvOS(.v9), .watchOS(.v2)], - - products: [ - .library( - name: "SwiftRadix", - targets: ["SwiftRadix"]) - ], - - dependencies: [ - // none - ], - - targets: [ - .target( - name: "SwiftRadix", - dependencies: []), - - .testTarget( - name: "SwiftRadixTests", - dependencies: ["SwiftRadix"]) - ] - + + name: "SwiftRadix", + + platforms: [.macOS(.v10_10), .iOS(.v8), .tvOS(.v9), .watchOS(.v2)], + + products: [ + .library( + name: "SwiftRadix", + targets: ["SwiftRadix"]) + ], + + dependencies: [ + // none + ], + + targets: [ + .target( + name: "SwiftRadix", + dependencies: []), + + .testTarget( + name: "SwiftRadixTests", + dependencies: ["SwiftRadix"]) + ] + ) diff --git a/Sources/SwiftRadix/Radix/Binary/Binary Type Extensions.swift b/Sources/SwiftRadix/Radix/Binary/Binary Type Extensions.swift index 3486634..8856967 100644 --- a/Sources/SwiftRadix/Radix/Binary/Binary Type Extensions.swift +++ b/Sources/SwiftRadix/Radix/Binary/Binary Type Extensions.swift @@ -1,10 +1,6 @@ // // Binary Type Extensions.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -12,82 +8,80 @@ import Foundation // MARK: - Constructors extension BinaryInteger { - - /// Returns a new `Radix` base-2 struct from an integer, preserving the integer type. - @inline(__always) public var binary: Radix { - - Radix(self, base: 2) - - } - + + /// Returns a new `Radix` base-2 struct from an integer, preserving the integer type. + @inline(__always) public var binary: Radix { + + Radix(self, base: 2) + + } + } extension String { - - /// Returns a new `Radix?` base-2 struct from a binary string. - @inlinable public var binary: Radix? { - - Radix(self, base: 2) - - } - - /// Returns a new `Radix?` base-2 struct from a binary string. - /// - /// Example usage: - /// - /// ``` - /// "1010".binary(as: Int16.self) - /// ``` - @inlinable public func binary(as type: T.Type) -> Radix? { - - Radix(self, base: 2) - - } - + + /// Returns a new `Radix?` base-2 struct from a binary string. + @inlinable public var binary: Radix? { + + Radix(self, base: 2) + + } + + /// Returns a new `Radix?` base-2 struct from a binary string. + /// + /// Example usage: + /// + /// "1010".binary(as: Int16.self) + /// + @inlinable public func binary(as type: T.Type) -> Radix? { + + Radix(self, base: 2) + + } + } extension Array where Element == String { - - /// Returns an array of `Radix?` base-2 structs constructed from an array of binary strings. - public var binary: [Radix?] { - - self.map { Radix($0, base: 2) } - - } - - /// Returns an array of `Radix?` base-2 structs constructed from an array of binary strings. - /// - /// Example usage: - /// - /// ``` - /// ["1010", "1111"].binary(as: Int16.self) - /// ``` - public func binary(as type: T.Type) -> [Radix?] { - - self.map { Radix($0, base: 2) } - - } - + + /// Returns an array of `Radix?` base-2 structs constructed from an array of binary strings. + public var binary: [Radix?] { + + self.map { Radix($0, base: 2) } + + } + + /// Returns an array of `Radix?` base-2 structs constructed from an array of binary strings. + /// + /// Example usage: + /// + /// ["1010", "1111"].binary(as: Int16.self) + /// + public func binary(as type: T.Type) -> [Radix?] { + + self.map { Radix($0, base: 2) } + + } + } extension Collection where Element: BinaryInteger { - - /// Returns an array of `Radix` base-2 structs built from an integer array, preserving the integer type. - @inlinable public var binary: [Radix] { - - self.map { Radix($0, base: 2) } - - } - + + /// Returns an array of `Radix` base-2 structs built from an integer array, preserving the integer type. + @inlinable public var binary: [Radix] { + + self.map { Radix($0, base: 2) } + + } + } extension Data { - - /// Returns an array of `Radix` base-2 structs built from Data bytes. - public var binary: [Radix] { - - self.map { Radix($0, base: 2) } - - } - + + /// Returns an array of `Radix` base-2 structs built from `Data` bytes. + public var binary: [Radix] { + + self.map { Radix($0, base: 2) } + + } + } diff --git a/Sources/SwiftRadix/Radix/Binary/Binary.swift b/Sources/SwiftRadix/Radix/Binary/Binary.swift index 48ffe42..c895ff7 100644 --- a/Sources/SwiftRadix/Radix/Binary/Binary.swift +++ b/Sources/SwiftRadix/Radix/Binary/Binary.swift @@ -1,37 +1,32 @@ // // Binary.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation /// Convenience constructor for `Radix` with a radix of 2 (binary). @inlinable public func Binary(_ number: T) -> Radix { - - Radix(number, base: 2) - + + Radix(number, base: 2) + } /// Convenience constructor for `Radix` with a radix of 2 (binary). @inlinable public func Binary(_ string: String) -> Radix? { - - Radix(string, base: 2) - + + Radix(string, base: 2) + } /// Convenience constructor for `Radix` with a radix of 2 (binary). /// /// Example usage: /// -/// ``` -/// Binary("1010", as: Int16.self) -/// ``` +/// Binary("1010", as: Int16.self) +/// @inlinable public func Binary(_ string: String, as type: T.Type) -> Radix? { - - Radix(string, base: 2) - + + Radix(string, base: 2) + } diff --git a/Sources/SwiftRadix/Radix/Hex/Hex Type Extensions.swift b/Sources/SwiftRadix/Radix/Hex/Hex Type Extensions.swift index fc1c8d5..ab1a099 100644 --- a/Sources/SwiftRadix/Radix/Hex/Hex Type Extensions.swift +++ b/Sources/SwiftRadix/Radix/Hex/Hex Type Extensions.swift @@ -1,10 +1,6 @@ // // Hex Type Extensions.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -12,82 +8,80 @@ import Foundation // MARK: - Constructors extension BinaryInteger { - - /// Returns a new `Radix` base-16 struct from an integer, preserving the integer type. - @inline(__always) public var hex: Radix { - - Radix(self, base: 16) - - } - + + /// Returns a new `Radix` base-16 struct from an integer, preserving the integer type. + @inline(__always) public var hex: Radix { + + Radix(self, base: 16) + + } + } extension String { - - /// Returns a new `Radix?` base-16 struct from a hex string. - @inlinable public var hex: Radix? { - - Radix(self, base: 16) - - } - - /// Returns a new `Radix?` base-16 struct from a hex string. - /// - /// Example usage: - /// - /// ``` - /// "FF".hex(as: Int16.self) - /// ``` - @inlinable public func hex(as type: T.Type) -> Radix? { - - Radix(self, base: 16) - - } - + + /// Returns a new `Radix?` base-16 struct from a hex string. + @inlinable public var hex: Radix? { + + Radix(self, base: 16) + + } + + /// Returns a new `Radix?` base-16 struct from a hex string. + /// + /// Example usage: + /// + /// "FF".hex(as: Int16.self) + /// + @inlinable public func hex(as type: T.Type) -> Radix? { + + Radix(self, base: 16) + + } + } extension Array where Element == String { - - /// Returns an array of `Radix?` base-16 structs constructed from an array of hex strings. - public var hex: [Radix?] { - - self.map { Radix($0, base: 16) } - - } - - /// Returns an array of `Radix` base-16 structs constructed from an array of hex strings. - /// - /// Example usage: - /// - /// ``` - /// ["0F", "FF"].hex(as: Int16.self) - /// ``` - public func hex(as type: T.Type) -> [Radix?] { - - self.map { Radix($0, base: 16) } - - } - + + /// Returns an array of `Radix?` base-16 structs constructed from an array of hex strings. + public var hex: [Radix?] { + + self.map { Radix($0, base: 16) } + + } + + /// Returns an array of `Radix` base-16 structs constructed from an array of hex strings. + /// + /// Example usage: + /// + /// ["0F", "FF"].hex(as: Int16.self) + /// + public func hex(as type: T.Type) -> [Radix?] { + + self.map { Radix($0, base: 16) } + + } + } extension Collection where Element: BinaryInteger { - - /// Returns an array of `Radix` base-16 structs built from an integer array, preserving the integer type. - @inlinable public var hex: [Radix] { - - self.map { Radix($0, base: 16) } - - } - + + /// Returns an array of `Radix` base-16 structs built from an integer array, preserving the integer type. + @inlinable public var hex: [Radix] { + + self.map { Radix($0, base: 16) } + + } + } extension Data { - - /// Returns an array of `Radix` base-16 structs built from Data bytes. - public var hex: [Radix] { - - self.map { Radix($0, base: 16) } - - } - + + /// Returns an array of `Radix` base-16 structs built from Data bytes. + public var hex: [Radix] { + + self.map { Radix($0, base: 16) } + + } + } diff --git a/Sources/SwiftRadix/Radix/Hex/Hex.swift b/Sources/SwiftRadix/Radix/Hex/Hex.swift index 91938a2..2daaac2 100644 --- a/Sources/SwiftRadix/Radix/Hex/Hex.swift +++ b/Sources/SwiftRadix/Radix/Hex/Hex.swift @@ -1,37 +1,32 @@ // // Hex.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation /// Convenience constructor for `Radix` with a radix of 16 (hex). @inlinable public func Hex(_ number: T) -> Radix { - - Radix(number, base: 16) - + + Radix(number, base: 16) + } /// Convenience constructor for `Radix` with a radix of 16 (hex). @inlinable public func Hex(_ string: String) -> Radix? { - - Radix(string, base: 16) - + + Radix(string, base: 16) + } /// Convenience constructor for `Radix` with a radix of 16 (hex). /// /// Example usage: /// -/// ``` -/// Hex("FF", as: Int16.self) -/// ``` +/// Hex("FF", as: Int16.self) +/// @inlinable public func Hex(_ string: String, as type: T.Type) -> Radix? { - - Radix(string, base: 16) - + + Radix(string, base: 16) + } diff --git a/Sources/SwiftRadix/Radix/Octal/Octal Type Extensions.swift b/Sources/SwiftRadix/Radix/Octal/Octal Type Extensions.swift index b05dc13..621b00d 100644 --- a/Sources/SwiftRadix/Radix/Octal/Octal Type Extensions.swift +++ b/Sources/SwiftRadix/Radix/Octal/Octal Type Extensions.swift @@ -1,10 +1,6 @@ // // Octal Type Extensions.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -12,82 +8,80 @@ import Foundation // MARK: - Constructors extension BinaryInteger { - - /// Returns a new `Radix` base-8 struct from an integer, preserving the integer type. - @inline(__always) public var octal: Radix { - - Radix(self, base: 8) - - } - + + /// Returns a new `Radix` base-8 struct from an integer, preserving the integer type. + @inline(__always) public var octal: Radix { + + Radix(self, base: 8) + + } + } extension String { - - /// Returns a new `Radix?` base-8 struct from an octal string. - @inlinable public var octal: Radix? { - - Radix(self, base: 8) - - } - - /// Returns a new `Radix?` base-8 struct from a octal string. - /// - /// Example usage: - /// - /// ``` - /// "123".octal(as: Int16.self) - /// ``` - @inlinable public func octal(as type: T.Type) -> Radix? { - - Radix(self, base: 8) - - } - + + /// Returns a new `Radix?` base-8 struct from an octal string. + @inlinable public var octal: Radix? { + + Radix(self, base: 8) + + } + + /// Returns a new `Radix?` base-8 struct from a octal string. + /// + /// Example usage: + /// + /// "123".octal(as: Int16.self) + /// + @inlinable public func octal(as type: T.Type) -> Radix? { + + Radix(self, base: 8) + + } + } extension Array where Element == String { - - /// Returns an array of `Radix?` base-8 structs constructed from an array of octal strings. - public var octal: [Radix?] { - - self.map { Radix($0, base: 8) } - - } - - /// Returns an array of `Radix?` base-8 structs constructed from an array of octal strings. - /// - /// Example usage: - /// - /// ``` - /// ["47", "123"].octal(as: Int16.self) - /// ``` - public func octal(as type: T.Type) -> [Radix?] { - - self.map { Radix($0, base: 8) } - - } - + + /// Returns an array of `Radix?` base-8 structs constructed from an array of octal strings. + public var octal: [Radix?] { + + self.map { Radix($0, base: 8) } + + } + + /// Returns an array of `Radix?` base-8 structs constructed from an array of octal strings. + /// + /// Example usage: + /// + /// ["47", "123"].octal(as: Int16.self) + /// + public func octal(as type: T.Type) -> [Radix?] { + + self.map { Radix($0, base: 8) } + + } + } extension Collection where Element: BinaryInteger { - - /// Returns an array of `Radix` base-8 structs built from an integer array, preserving the integer type. - @inlinable public var octal: [Radix] { - - self.map { Radix($0, base: 8) } - - } - + + /// Returns an array of `Radix` base-8 structs built from an integer array, preserving the integer type. + @inlinable public var octal: [Radix] { + + self.map { Radix($0, base: 8) } + + } + } extension Data { - - /// Returns an array of `Radix` base-8 structs built from Data bytes. - public var octal: [Radix] { - - self.map { Radix($0, base: 8) } - - } - + + /// Returns an array of `Radix` base-8 structs built from Data bytes. + public var octal: [Radix] { + + self.map { Radix($0, base: 8) } + + } + } diff --git a/Sources/SwiftRadix/Radix/Octal/Octal.swift b/Sources/SwiftRadix/Radix/Octal/Octal.swift index a695f19..df143cd 100644 --- a/Sources/SwiftRadix/Radix/Octal/Octal.swift +++ b/Sources/SwiftRadix/Radix/Octal/Octal.swift @@ -1,37 +1,32 @@ // // Octal.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation /// Convenience constructor for `Radix` with a radix of 8 (octal). @inlinable public func Octal(_ number: T) -> Radix { - - Radix(number, base: 8) - + + Radix(number, base: 8) + } /// Convenience constructor for `Radix` with a radix of 8 (octal). @inlinable public func Octal(_ string: String) -> Radix? { - - Radix(string, base: 8) - + + Radix(string, base: 8) + } /// Convenience constructor for `Radix` with a radix of 8 (octal). /// /// Example usage: /// -/// ``` -/// Octal("123", as: Int16.self) -/// ``` +/// Octal("123", as: Int16.self) +/// @inlinable public func Octal(_ string: String, as type: T.Type) -> Radix? { - - Radix(string, base: 8) - + + Radix(string, base: 8) + } diff --git a/Sources/SwiftRadix/Radix/Radix Bit.swift b/Sources/SwiftRadix/Radix/Radix Bit.swift index 4111ae5..c896879 100644 --- a/Sources/SwiftRadix/Radix/Radix Bit.swift +++ b/Sources/SwiftRadix/Radix/Radix Bit.swift @@ -1,49 +1,45 @@ // // Radix Bit.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation extension Radix { - - /// Access binary bits from right-to-left. `position` is zero-based. - /// - /// Bits can also be get or set directly via the `[bit: Int]` mutating subscript. - @inline(__always) public func bit(_ position: Int) -> Radix { - - Radix(self[bit: position], base: base) - - } - - /// Access binary bits from right-to-left. `position` is zero-based. - @inline(__always) public subscript(bit position: Int) -> NumberType { - - get { - - (value & (0b1 << position)) >> position - - } - - set { - - // ensure bit is valid - if newValue < 0b0 || newValue > 0b1 { return } - - // obtain old bit value - let mask = value & (0b1 << NumberType(position)) - - // subtract old value and add new bit value - let setValue = (value - mask) + (newValue << position) - - value = setValue - - } - - } - + + /// Access binary bits from right-to-left. `position` is zero-based. + /// + /// Bits can also be get or set directly via the `[bit: Int]` mutating subscript. + @inline(__always) public func bit(_ position: Int) -> Radix { + + Radix(self[bit: position], base: base) + + } + + /// Access binary bits from right-to-left. `position` is zero-based. + @inline(__always) public subscript(bit position: Int) -> NumberType { + + get { + + (value & (0b1 << position)) >> position + + } + + set { + + // ensure bit is valid + if newValue < 0b0 || newValue > 0b1 { return } + + // obtain old bit value + let mask = value & (0b1 << NumberType(position)) + + // subtract old value and add new bit value + let setValue = (value - mask) + (newValue << position) + + value = setValue + + } + + } + } diff --git a/Sources/SwiftRadix/Radix/Radix Bytes.swift b/Sources/SwiftRadix/Radix/Radix Bytes.swift index 70cafee..f3e9e6b 100644 --- a/Sources/SwiftRadix/Radix/Radix Bytes.swift +++ b/Sources/SwiftRadix/Radix/Radix Bytes.swift @@ -1,32 +1,27 @@ // // Radix Bytes.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation extension Radix { - - /// Returns a raw byte array of the value (agnostic of radix base). - /// - /// All bytes for the integer are returned, since they are fixed-width numbers. - /// (ie: `UInt8` returns one byte, but `Int` will return 8 bytes on 64-bit systems.) - /// - /// ``` - /// UInt8(0xFF).hex.bytes // [0xFF] - /// UInt16(0xFF).hex.bytes // [0xFF,0x00] - /// Int(0xFF).hex.bytes // [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00] - /// ``` - @inlinable public var bytes: [UInt8] { - - var mutableValueCopy = value - - return withUnsafeBytes(of: &mutableValueCopy) { Array($0) } - - } - + + /// Returns a raw byte array of the value (agnostic of radix base). + /// + /// All bytes for the integer are returned, since they are fixed-width numbers. + /// (ie: `UInt8` returns one byte, but `Int` will return 8 bytes on 64-bit systems.) + /// + /// UInt8(0xFF).hex.bytes // [0xFF] + /// UInt16(0xFF).hex.bytes // [0xFF,0x00] + /// Int(0xFF).hex.bytes // [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00] + /// + @inlinable public var bytes: [UInt8] { + + var mutableValueCopy = value + + return withUnsafeBytes(of: &mutableValueCopy) { Array($0) } + + } + } diff --git a/Sources/SwiftRadix/Radix/Radix Collection Methods.swift b/Sources/SwiftRadix/Radix/Radix Collection Methods.swift index 15c16f1..cf6c285 100644 --- a/Sources/SwiftRadix/Radix/Radix Collection Methods.swift +++ b/Sources/SwiftRadix/Radix/Radix Collection Methods.swift @@ -1,10 +1,6 @@ // // Radix Collection Methods.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -13,117 +9,117 @@ import Foundation // MARK: - .stringValue extension Collection where Element : RadixProtocol { - - /// Convert an array of `Radix` to a concatenated String of radix string values. - public var stringValue: String { - - self - .map { $0.stringValue } - .joined(separator: " ") - - } - - /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to the number of characters specified. - public func stringValue(prefix: Bool) -> String { - - self - .map { $0.stringValue(prefix: prefix) } - .joined(separator: " ") - - } - - /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to the number of characters specified. - public func stringValue(padTo: Int, - prefix: Bool = false) -> String { - - self - .map { $0.stringValue(padTo: padTo, - splitEvery: 0, - prefix: prefix) } - .joined(separator: " ") - - } - - /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to multiples of the number of characters specified. - public func stringValue(padToEvery: Int, - prefix: Bool = false) -> String { - - self - .map { $0.stringValue(padToEvery: padToEvery, - splitEvery: 0, - prefix: prefix) } - .joined(separator: " ") - - } - + + /// Convert an array of `Radix` to a concatenated String of radix string values. + public var stringValue: String { + + self + .map { $0.stringValue } + .joined(separator: " ") + + } + + /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to the number of characters specified. + public func stringValue(prefix: Bool) -> String { + + self + .map { $0.stringValue(prefix: prefix) } + .joined(separator: " ") + + } + + /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to the number of characters specified. + public func stringValue(padTo: Int, + prefix: Bool = false) -> String { + + self + .map { $0.stringValue(padTo: padTo, + splitEvery: 0, + prefix: prefix) } + .joined(separator: " ") + + } + + /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to multiples of the number of characters specified. + public func stringValue(padToEvery: Int, + prefix: Bool = false) -> String { + + self + .map { $0.stringValue(padToEvery: padToEvery, + splitEvery: 0, + prefix: prefix) } + .joined(separator: " ") + + } + } // MARK: - .stringValueArrayLiteral extension Collection where Element : RadixProtocol { - - /// Convert an array of `Radix` to a Swift array literal, useful for generating Swift array declarations. - public var stringValueArrayLiteral: String { - - stringValueArrayLiteral(padToEvery: 0) - - } - - /// Convert an array of `Radix` to a Swift array literal, useful for generating Swift array declarations, each value padded to the number of characters specified. - public func stringValueArrayLiteral(padTo: Int) -> String { - - "[" - + self.stringValues(padTo: padTo, prefixes: true) - .joined(separator: ", " ) - + "]" - - } - - /// Convert an array of `Radix` to a Swift array literal, useful for generating Swift array declarations, each value padded to multiples of the number of characters specified. - public func stringValueArrayLiteral(padToEvery: Int) -> String { - - "[" - + self.stringValues(padToEvery: padToEvery, prefixes: true) - .joined(separator: ", " ) - + "]" - - } - + + /// Convert an array of `Radix` to a Swift array literal, useful for generating Swift array declarations. + public var stringValueArrayLiteral: String { + + stringValueArrayLiteral(padToEvery: 0) + + } + + /// Convert an array of `Radix` to a Swift array literal, useful for generating Swift array declarations, each value padded to the number of characters specified. + public func stringValueArrayLiteral(padTo: Int) -> String { + + "[" + + self.stringValues(padTo: padTo, prefixes: true) + .joined(separator: ", " ) + + "]" + + } + + /// Convert an array of `Radix` to a Swift array literal, useful for generating Swift array declarations, each value padded to multiples of the number of characters specified. + public func stringValueArrayLiteral(padToEvery: Int) -> String { + + "[" + + self.stringValues(padToEvery: padToEvery, prefixes: true) + .joined(separator: ", " ) + + "]" + + } + } // MARK: - .stringValues extension Collection where Element : RadixProtocol { - - /// Convert an array of `Radix` to an Array of radix string values. - public var stringValues: [String] { - - self.stringValues(padTo: 0) - - } - - /// Convert an array of `Radix` to an Array of radix string values, each value padded to the number of characters specified. - public func stringValues(padTo: Int, - prefixes: Bool = false) -> [String] { - - self.map { $0.stringValue(padTo: padTo, - splitEvery: 0, - prefix: prefixes) } - - } - - /// Convert an array of `Radix` to an Array of radix string values, each value padded to multiples of the number of characters specified. - public func stringValues(padToEvery: Int, - prefixes: Bool = false) -> [String] { - - self.map { $0.stringValue(padToEvery: padToEvery, - splitEvery: 0, - prefix: prefixes) } - - } - + + /// Convert an array of `Radix` to an Array of radix string values. + public var stringValues: [String] { + + self.stringValues(padTo: 0) + + } + + /// Convert an array of `Radix` to an Array of radix string values, each value padded to the number of characters specified. + public func stringValues(padTo: Int, + prefixes: Bool = false) -> [String] { + + self.map { $0.stringValue(padTo: padTo, + splitEvery: 0, + prefix: prefixes) } + + } + + /// Convert an array of `Radix` to an Array of radix string values, each value padded to multiples of the number of characters specified. + public func stringValues(padToEvery: Int, + prefixes: Bool = false) -> [String] { + + self.map { $0.stringValue(padToEvery: padToEvery, + splitEvery: 0, + prefix: prefixes) } + + } + } @@ -131,24 +127,24 @@ extension Collection where Element : RadixProtocol { /// Extension on [RadixProtocol] extension Collection where Element : RadixProtocol { - - /// Returns an array of extracted values. - @inlinable public var values: [Element.NumberType] { - - self.map { $0.value } - - } - + + /// Returns an array of extracted values. + @inlinable public var values: [Element.NumberType] { + + self.map { $0.value } + + } + } /// Extension on [RadixProtocol?] extension Collection where Element: OptionalType, Element.Wrapped : RadixProtocol { - - /// Returns an array of extracted values. - @inlinable public var values: [Element.Wrapped.NumberType?] { - - self.map { $0.optional?.value } - - } - + + /// Returns an array of extracted values. + @inlinable public var values: [Element.Wrapped.NumberType?] { + + self.map { $0.optional?.value } + + } + } diff --git a/Sources/SwiftRadix/Radix/Radix Nibble.swift b/Sources/SwiftRadix/Radix/Radix Nibble.swift index b0b03f9..7f0367f 100644 --- a/Sources/SwiftRadix/Radix/Radix Nibble.swift +++ b/Sources/SwiftRadix/Radix/Radix Nibble.swift @@ -1,10 +1,6 @@ // // Radix Nibble.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -13,41 +9,41 @@ import Foundation // MARK: - Nibble extension Radix { - - /// Returns the value of the 4-bit nibble from right-to-left. `position` is zero-based. - /// - /// Nibbles can also be get or set directly via the `[nibble: Int]` mutating subscript. - @inline(__always) public func nibble(_ position: Int) -> Radix { - - Radix(self[nibble: position], base: base) - - } - - /// Access binary nibbles from right-to-left. `position` is zero-based. - @inline(__always) public subscript(nibble position: Int) -> NumberType { - - get { - - (value & (0xF << (4 * NumberType(position)))) - >> (4 * NumberType(position)) - - } - - set { - - // ensure nibble is valid - if newValue < 0x0 || newValue > 0xF { return } - - // obtain old nibble value - let mask = value & (0b1111 << (4 * NumberType(position))) - - // subtract old value and add new nibble value - let setValue = (value - mask) + (newValue << (4 * position)) - - value = setValue - - } - - } - + + /// Returns the value of the 4-bit nibble from right-to-left. `position` is zero-based. + /// + /// Nibbles can also be get or set directly via the `[nibble: Int]` mutating subscript. + @inline(__always) public func nibble(_ position: Int) -> Radix { + + Radix(self[nibble: position], base: base) + + } + + /// Access binary nibbles from right-to-left. `position` is zero-based. + @inline(__always) public subscript(nibble position: Int) -> NumberType { + + get { + + (value & (0xF << (4 * NumberType(position)))) + >> (4 * NumberType(position)) + + } + + set { + + // ensure nibble is valid + if newValue < 0x0 || newValue > 0xF { return } + + // obtain old nibble value + let mask = value & (0b1111 << (4 * NumberType(position))) + + // subtract old value and add new nibble value + let setValue = (value - mask) + (newValue << (4 * position)) + + value = setValue + + } + + } + } diff --git a/Sources/SwiftRadix/Radix/Radix Operators.swift b/Sources/SwiftRadix/Radix/Radix Operators.swift index f474e0b..1674e65 100644 --- a/Sources/SwiftRadix/Radix/Radix Operators.swift +++ b/Sources/SwiftRadix/Radix/Radix Operators.swift @@ -1,10 +1,6 @@ // // Radix Operators.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -13,260 +9,260 @@ import Foundation // MARK: - Equatable extension Radix: Equatable { - - // also gives != for free - @inlinable static public func == (lhs: Radix, rhs: Radix) -> Bool { - lhs.value == rhs.value - } - - @inlinable static public func == (lhs: Radix, rhs: T) -> Bool { - lhs.value == rhs - } - - @inlinable static public func == (lhs: T, rhs: Radix) -> Bool { - lhs == rhs.value - } - - @inlinable static public func != (lhs: Radix, rhs: T) -> Bool { - lhs.value != rhs - } - - @inlinable static public func != (lhs: T, rhs: Radix) -> Bool { - lhs != rhs.value - } - + + // also gives != for free + @inlinable static public func == (lhs: Radix, rhs: Radix) -> Bool { + lhs.value == rhs.value + } + + @inlinable static public func == (lhs: Radix, rhs: T) -> Bool { + lhs.value == rhs + } + + @inlinable static public func == (lhs: T, rhs: Radix) -> Bool { + lhs == rhs.value + } + + @inlinable static public func != (lhs: Radix, rhs: T) -> Bool { + lhs.value != rhs + } + + @inlinable static public func != (lhs: T, rhs: Radix) -> Bool { + lhs != rhs.value + } + } // MARK: - Equatable - Optionals extension Radix { - - @inlinable static public func == (lhs: Radix?, rhs: Radix) -> Bool { - lhs?.value == rhs.value - } - - @inlinable static public func == (lhs: Radix, rhs: Radix?) -> Bool { - lhs.value == rhs?.value - } - + + @inlinable static public func == (lhs: Radix?, rhs: Radix) -> Bool { + lhs?.value == rhs.value + } + + @inlinable static public func == (lhs: Radix, rhs: Radix?) -> Bool { + lhs.value == rhs?.value + } + } @inlinable public func == (lhs: Radix?, rhs: Other) -> Bool where Other : BinaryInteger { - if lhs == nil { return false } - return lhs!.value == rhs + if lhs == nil { return false } + return lhs!.value == rhs } @inlinable public func == (lhs: Other, rhs: Radix?) -> Bool where Other : BinaryInteger { - if rhs == nil { return false } - return lhs == rhs!.value + if rhs == nil { return false } + return lhs == rhs!.value } @inlinable public func != (lhs: Radix?, rhs: Other) -> Bool where Other : BinaryInteger { - if lhs == nil { return false } - return lhs!.value != rhs + if lhs == nil { return false } + return lhs!.value != rhs } @inlinable public func != (lhs: Other, rhs: Radix?) -> Bool where Other : BinaryInteger { - if rhs == nil { return false } - return lhs != rhs!.value + if rhs == nil { return false } + return lhs != rhs!.value } // MARK: - Comparable extension Radix: Comparable { - - @inlinable static public func > (lhs: Radix, rhs: Radix) -> Bool { - lhs.value > rhs.value - } - - @inlinable static public func > (lhs: Radix, rhs: T) -> Bool { - lhs.value > rhs - } - - @inlinable static public func > (lhs: T, rhs: Radix) -> Bool { - lhs > rhs.value - } - - @inlinable static public func < (lhs: Radix, rhs: Radix) -> Bool { - lhs.value < rhs.value - } - - @inlinable static public func < (lhs: Radix, rhs: T) -> Bool { - lhs.value < rhs - } - - @inlinable static public func < (lhs: T, rhs: Radix) -> Bool { - lhs < rhs.value - } - - @inlinable static public func >= (lhs: Radix, rhs: Radix) -> Bool { - lhs.value >= rhs.value - } - - @inlinable static public func >= (lhs: Radix, rhs: T) -> Bool { - lhs.value >= rhs - } - - @inlinable static public func >= (lhs: T, rhs: Radix) -> Bool { - lhs >= rhs.value - } - - @inlinable static public func <= (lhs: Radix, rhs: Radix) -> Bool { - lhs.value <= rhs.value - } - - @inlinable static public func <= (lhs: Radix, rhs: T) -> Bool { - lhs.value <= rhs - } - - @inlinable static public func <= (lhs: T, rhs: Radix) -> Bool { - lhs <= rhs.value - } - + + @inlinable static public func > (lhs: Radix, rhs: Radix) -> Bool { + lhs.value > rhs.value + } + + @inlinable static public func > (lhs: Radix, rhs: T) -> Bool { + lhs.value > rhs + } + + @inlinable static public func > (lhs: T, rhs: Radix) -> Bool { + lhs > rhs.value + } + + @inlinable static public func < (lhs: Radix, rhs: Radix) -> Bool { + lhs.value < rhs.value + } + + @inlinable static public func < (lhs: Radix, rhs: T) -> Bool { + lhs.value < rhs + } + + @inlinable static public func < (lhs: T, rhs: Radix) -> Bool { + lhs < rhs.value + } + + @inlinable static public func >= (lhs: Radix, rhs: Radix) -> Bool { + lhs.value >= rhs.value + } + + @inlinable static public func >= (lhs: Radix, rhs: T) -> Bool { + lhs.value >= rhs + } + + @inlinable static public func >= (lhs: T, rhs: Radix) -> Bool { + lhs >= rhs.value + } + + @inlinable static public func <= (lhs: Radix, rhs: Radix) -> Bool { + lhs.value <= rhs.value + } + + @inlinable static public func <= (lhs: Radix, rhs: T) -> Bool { + lhs.value <= rhs + } + + @inlinable static public func <= (lhs: T, rhs: Radix) -> Bool { + lhs <= rhs.value + } + } // MARK: - Math operators @inlinable public func + (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value + rhs.value, base: lhs.base) + Radix(lhs.value + rhs.value, base: lhs.base) } @inlinable public func + (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value + rhs, base: lhs.base) + Radix(lhs.value + rhs, base: lhs.base) } @inlinable public func + (lhs: T, rhs: Radix) -> T { - lhs + rhs.value + lhs + rhs.value } @inlinable public func - (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value - rhs.value, base: lhs.base) + Radix(lhs.value - rhs.value, base: lhs.base) } @inlinable public func - (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value - rhs, base: lhs.base) + Radix(lhs.value - rhs, base: lhs.base) } @inlinable public func - (lhs: T, rhs: Radix) -> T { - lhs - rhs.value + lhs - rhs.value } @inlinable public func * (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value * rhs.value, base: lhs.base) + Radix(lhs.value * rhs.value, base: lhs.base) } @inlinable public func * (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value * rhs, base: lhs.base) + Radix(lhs.value * rhs, base: lhs.base) } @inlinable public func * (lhs: T, rhs: Radix) -> T { - lhs * rhs.value + lhs * rhs.value } @inlinable public func / (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value / rhs.value, base: lhs.base) + Radix(lhs.value / rhs.value, base: lhs.base) } @inlinable public func / (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value / rhs, base: lhs.base) + Radix(lhs.value / rhs, base: lhs.base) } @inlinable public func / (lhs: T, rhs: Radix) -> T { - lhs / rhs.value + lhs / rhs.value } // MARK: - Mutating math operators @inlinable public func += (lhs: inout Radix, rhs: Radix) { - lhs.value += rhs.value + lhs.value += rhs.value } @inlinable public func += (lhs: inout Radix, rhs: T) { - lhs.value += rhs + lhs.value += rhs } @inlinable public func +=< T>(lhs: inout T, rhs: Radix) { - lhs += rhs.value + lhs += rhs.value } @inlinable public func -= (lhs: inout Radix, rhs: Radix) { - lhs.value -= rhs.value + lhs.value -= rhs.value } @inlinable public func -= (lhs: inout Radix, rhs: T) { - lhs.value -= rhs + lhs.value -= rhs } @inlinable public func -= (lhs: inout T, rhs: Radix) { - lhs -= rhs.value + lhs -= rhs.value } @inlinable public func *= (lhs: inout Radix, rhs: Radix) { - lhs.value *= rhs.value + lhs.value *= rhs.value } @inlinable public func *=< T>(lhs: inout Radix, rhs: T) { - lhs.value *= rhs + lhs.value *= rhs } @inlinable public func *= (lhs: inout T, rhs: Radix) { - lhs *= rhs.value + lhs *= rhs.value } @inlinable public func /= (lhs: inout Radix, rhs: Radix) { - lhs.value /= rhs.value + lhs.value /= rhs.value } @inlinable public func /= (lhs: inout Radix, rhs: T) { - lhs.value /= rhs + lhs.value /= rhs } @inlinable public func /= (lhs: inout T, rhs: Radix) { - lhs /= rhs.value + lhs /= rhs.value } // MARK: Bitwise bit shift extension Radix { - - @inlinable static public func >> (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value >> rhs.value, base: lhs.base) - } - - @inlinable static public func >> (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value >> rhs, base: lhs.base) - } - - @inlinable static public func >> (lhs: T, rhs: Radix) -> T { - lhs >> rhs.value - } - - @inlinable static public func << (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value << rhs.value, base: lhs.base) - } - - @inlinable static public func << (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value << rhs, base: lhs.base) - } - - @inlinable static public func << (lhs: T, rhs: Radix) -> T { - lhs << rhs.value - } - + + @inlinable static public func >> (lhs: Radix, rhs: Radix) -> Radix { + Radix(lhs.value >> rhs.value, base: lhs.base) + } + + @inlinable static public func >> (lhs: Radix, rhs: T) -> Radix { + Radix(lhs.value >> rhs, base: lhs.base) + } + + @inlinable static public func >> (lhs: T, rhs: Radix) -> T { + lhs >> rhs.value + } + + @inlinable static public func << (lhs: Radix, rhs: Radix) -> Radix { + Radix(lhs.value << rhs.value, base: lhs.base) + } + + @inlinable static public func << (lhs: Radix, rhs: T) -> Radix { + Radix(lhs.value << rhs, base: lhs.base) + } + + @inlinable static public func << (lhs: T, rhs: Radix) -> T { + lhs << rhs.value + } + } @@ -276,48 +272,48 @@ infix operator >>>>: BitwiseShiftPrecedence infix operator <<<<: BitwiseShiftPrecedence extension Radix { - - @inlinable static public func >>>> (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value >> (rhs.value * 4), base: lhs.base) - } - - @inlinable static public func >>>> (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value >> (rhs * 4), base: lhs.base) - } - - @inlinable static public func >>>> (lhs: T, rhs: Radix) -> T { - lhs >> (rhs.value * 4) - } - - @inlinable static public func <<<< (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value << (rhs.value * 4), base: lhs.base) - } - - @inlinable static public func <<<< (lhs: Radix, rhs: T) -> Radix { - Radix(lhs.value << (rhs * 4), base: lhs.base) - } - - @inlinable static public func <<<< (lhs: T, rhs: Radix) -> T { - lhs << (rhs.value * 4) - } - + + @inlinable static public func >>>> (lhs: Radix, rhs: Radix) -> Radix { + Radix(lhs.value >> (rhs.value * 4), base: lhs.base) + } + + @inlinable static public func >>>> (lhs: Radix, rhs: T) -> Radix { + Radix(lhs.value >> (rhs * 4), base: lhs.base) + } + + @inlinable static public func >>>> (lhs: T, rhs: Radix) -> T { + lhs >> (rhs.value * 4) + } + + @inlinable static public func <<<< (lhs: Radix, rhs: Radix) -> Radix { + Radix(lhs.value << (rhs.value * 4), base: lhs.base) + } + + @inlinable static public func <<<< (lhs: Radix, rhs: T) -> Radix { + Radix(lhs.value << (rhs * 4), base: lhs.base) + } + + @inlinable static public func <<<< (lhs: T, rhs: Radix) -> T { + lhs << (rhs.value * 4) + } + } // MARK: Bitwise AND extension Radix { - - @inlinable static public func & (lhs: Radix, rhs: Radix) -> Radix { - Radix(lhs.value & rhs.value, base: lhs.base) - } - - @inlinable static public func & (lhs: Radix, rhs: T) -> Radix { - Radix(T(lhs.value) & rhs, base: lhs.base) - } - - @inlinable static public func & (lhs: T, rhs: Radix) -> T { - lhs & T(rhs.value) - } - + + @inlinable static public func & (lhs: Radix, rhs: Radix) -> Radix { + Radix(lhs.value & rhs.value, base: lhs.base) + } + + @inlinable static public func & (lhs: Radix, rhs: T) -> Radix { + Radix(T(lhs.value) & rhs, base: lhs.base) + } + + @inlinable static public func & (lhs: T, rhs: Radix) -> T { + lhs & T(rhs.value) + } + } diff --git a/Sources/SwiftRadix/Radix/Radix Protocol Adoptions.swift b/Sources/SwiftRadix/Radix/Radix Protocol Adoptions.swift index abf0fc2..f2983b2 100644 --- a/Sources/SwiftRadix/Radix/Radix Protocol Adoptions.swift +++ b/Sources/SwiftRadix/Radix/Radix Protocol Adoptions.swift @@ -1,10 +1,6 @@ // // Radix Protocol Adoptions.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -13,30 +9,30 @@ import Foundation // MARK: String Convertible extension Radix: CustomStringConvertible, CustomDebugStringConvertible { - - public var description: String { - - "Radix<\(String(describing: NumberType.self))>(\(stringPrefix)\(stringValue))" - - } - - public var debugDescription: String { - - "Radix<\(String(describing: NumberType.self))>(\(stringPrefix)\(stringValue))" - - } - + + public var description: String { + + "Radix<\(String(describing: NumberType.self))>(\(stringPrefix)\(stringValue))" + + } + + public var debugDescription: String { + + "Radix<\(String(describing: NumberType.self))>(\(stringPrefix)\(stringValue))" + + } + } // MARK: Hashable extension Radix: Hashable { - - public func hash(into hasher: inout Hasher) { - - hasher.combine(value) - - } - + + public func hash(into hasher: inout Hasher) { + + hasher.combine(value) + + } + } diff --git a/Sources/SwiftRadix/Radix/Radix Strings.swift b/Sources/SwiftRadix/Radix/Radix Strings.swift index d4793ba..90de98b 100644 --- a/Sources/SwiftRadix/Radix/Radix Strings.swift +++ b/Sources/SwiftRadix/Radix/Radix Strings.swift @@ -1,155 +1,151 @@ // // Radix Strings.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation extension Radix { - - /// Computed property: - /// Getter returns radix String representation of `value` without prefix. - /// Setter assigns `value` to value equivalent of radix String, if it is valid. - public var stringValue: String { - - get { - - String(value, radix: base, uppercase: true) - - } - - set { - - guard let convertedValue = valueFrom(radixString: newValue) - else { return } - - value = convertedValue - - } - - } - - /// Computed property: - /// Returns radix String representation of `value`, optionally including `prefix`. - public func stringValue(prefix: Bool) -> String { - - prefix - ? stringPrefix + stringValue - : stringValue - - } - - /// Computed property: - /// Returns radix String representation of `value`, padding zeros to number of places passed. - /// - /// If `splitEvery` > 0, a spacer is inserted every nth characters in the resulting string; the spacer will be a space if `prefix` is false and an underscore if `prefix` is true. - public func stringValue(padTo: Int, - splitEvery: Int = 0, - prefix: Bool = false) -> String { - - var radixString = stringValue - - var padCount = (padTo - radixString.count) - if padCount < 0 { padCount = 0 } - - radixString = String(repeatElement("0", count: padCount)) + radixString - - if splitEvery > 0 { - let splitter = prefix ? "_" : " " - - radixString = radixString - .split(every: splitEvery, backwards: true) - .joined(separator: splitter) - } - - return prefix - ? stringPrefix + radixString - : radixString - - } - - /// Computed property: - /// Returns radix String representation of `value`, padded to multiples of specified number of characters. - /// - /// If `splitEvery` > 0, a space is inserted every nth characters in the resulting string; the spacer will be a space if `prefix` is false and an underscore if `prefix` is true. - public func stringValue(padToEvery: Int, - splitEvery: Int = 0, - prefix: Bool = false) -> String { - - var radixString = stringValue - - var padCount = radixString.count - .roundedUp(toMultiplesOf: padToEvery) - radixString.count - if padCount < 0 { padCount = 0 } - - radixString = String(repeatElement("0", count: padCount)) + radixString - - if splitEvery > 0 { - let splitter = prefix ? "_" : " " - - radixString = radixString - .split(every: splitEvery, backwards: true) - .joined(separator: splitter) - } - - return prefix - ? stringPrefix + radixString - : radixString - - } - + + /// Computed property: + /// Getter returns radix String representation of `value` without prefix. + /// Setter assigns `value` to value equivalent of radix String, if it is valid. + public var stringValue: String { + + get { + + String(value, radix: base, uppercase: true) + + } + + set { + + guard let convertedValue = valueFrom(radixString: newValue) + else { return } + + value = convertedValue + + } + + } + + /// Computed property: + /// Returns radix String representation of `value`, optionally including `prefix`. + public func stringValue(prefix: Bool) -> String { + + prefix + ? stringPrefix + stringValue + : stringValue + + } + + /// Computed property: + /// Returns radix String representation of `value`, padding zeros to number of places passed. + /// + /// If `splitEvery` > 0, a spacer is inserted every nth characters in the resulting string; the spacer will be a space if `prefix` is false and an underscore if `prefix` is true. + public func stringValue(padTo: Int, + splitEvery: Int = 0, + prefix: Bool = false) -> String { + + var radixString = stringValue + + var padCount = (padTo - radixString.count) + if padCount < 0 { padCount = 0 } + + radixString = String(repeatElement("0", count: padCount)) + radixString + + if splitEvery > 0 { + let splitter = prefix ? "_" : " " + + radixString = radixString + .split(every: splitEvery, backwards: true) + .joined(separator: splitter) + } + + return prefix + ? stringPrefix + radixString + : radixString + + } + + /// Computed property: + /// Returns radix String representation of `value`, padded to multiples of specified number of characters. + /// + /// If `splitEvery` > 0, a space is inserted every nth characters in the resulting string; the spacer will be a space if `prefix` is false and an underscore if `prefix` is true. + public func stringValue(padToEvery: Int, + splitEvery: Int = 0, + prefix: Bool = false) -> String { + + var radixString = stringValue + + var padCount = radixString.count + .roundedUp(toMultiplesOf: padToEvery) - radixString.count + if padCount < 0 { padCount = 0 } + + radixString = String(repeatElement("0", count: padCount)) + radixString + + if splitEvery > 0 { + let splitter = prefix ? "_" : " " + + radixString = radixString + .split(every: splitEvery, backwards: true) + .joined(separator: splitter) + } + + return prefix + ? stringPrefix + radixString + : radixString + + } + } // MARK: - Helpers extension Radix { - - /// Internal function to convert a radix String to a value. - /// Fails with nil if not successful or if the String is malformed. - @usableFromInline internal func valueFrom(radixString: String) -> NumberType? { - - var parseString: String - - // treat string prefix as case-sensitive - if radixString.starts(with: stringPrefix), - stringPrefix != "" - { - parseString = String(radixString.dropFirst(2)) - } else { - parseString = radixString - } - - var castValue: NumberType? = nil - - switch NumberType.self { - case is UInt.Type: - // Handle UInt case separately, since it can overflow into Int(_: radix:) - - // fails if non-conformant to the radix (a malformed string) - guard let convertedValue = UInt(parseString, radix: base) - else { return nil } - - // nil if the number overflows the integer type - castValue = NumberType(exactly: convertedValue) - - default: - // fails if non-conformant to the radix (a malformed string) - guard let convertedValue = Int(parseString, radix: base) - else { return nil } - - // nil if the number overflows the integer type - castValue = NumberType(exactly: convertedValue) - - } - - return castValue - - } - + + /// Internal function to convert a radix String to a value. + /// Fails with nil if not successful or if the String is malformed. + @usableFromInline internal func valueFrom(radixString: String) -> NumberType? { + + var parseString: String + + // treat string prefix as case-sensitive + if radixString.starts(with: stringPrefix), + stringPrefix != "" + { + parseString = String(radixString.dropFirst(2)) + } else { + parseString = radixString + } + + var castValue: NumberType? = nil + + switch NumberType.self { + case is UInt.Type: + // Handle UInt case separately, since it can overflow into Int(_: radix:) + + // fails if non-conformant to the radix (a malformed string) + guard let convertedValue = UInt(parseString, radix: base) + else { return nil } + + // nil if the number overflows the integer type + castValue = NumberType(exactly: convertedValue) + + default: + // fails if non-conformant to the radix (a malformed string) + guard let convertedValue = Int(parseString, radix: base) + else { return nil } + + // nil if the number overflows the integer type + castValue = NumberType(exactly: convertedValue) + + } + + return castValue + + } + } diff --git a/Sources/SwiftRadix/Radix/Radix Type Extensions.swift b/Sources/SwiftRadix/Radix/Radix Type Extensions.swift index 2583972..d334daa 100644 --- a/Sources/SwiftRadix/Radix/Radix Type Extensions.swift +++ b/Sources/SwiftRadix/Radix/Radix Type Extensions.swift @@ -1,10 +1,6 @@ // // Radix Type Extensions.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-14. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -12,82 +8,80 @@ import Foundation // MARK: - Constructors extension BinaryInteger { - - /// Returns a new `Radix` struct from an integer, preserving the integer type. - @inline(__always) public func radix(base: Int) -> Radix { - - Radix(self, base: base) - - } - + + /// Returns a new `Radix` struct from an integer, preserving the integer type. + @inline(__always) public func radix(base: Int) -> Radix { + + Radix(self, base: base) + + } + } extension String { - - /// Returns a new `Radix?` struct from a radix string. - @inlinable public func radix(base: Int) -> Radix? { - - Radix(self, base: base) - - } - - /// Returns a new `Radix?` struct from a radix string. - /// - /// Example usage: - /// - /// ``` - /// "123".radix(base: 4, as: Int16.self) - /// ``` - @inlinable public func radix(base: Int, as type: T.Type) -> Radix? { - - Radix(self, base: base) - - } - + + /// Returns a new `Radix?` struct from a radix string. + @inlinable public func radix(base: Int) -> Radix? { + + Radix(self, base: base) + + } + + /// Returns a new `Radix?` struct from a radix string. + /// + /// Example usage: + /// + /// "123".radix(base: 4, as: Int16.self) + /// + @inlinable public func radix(base: Int, as type: T.Type) -> Radix? { + + Radix(self, base: base) + + } + } extension Array where Element == String { - - /// Returns an array of `Radix?` structs constructed from an array of hex strings. - public func radix(base: Int) -> [Radix?] { - - self.map { Radix($0, base: base) } - - } - - /// Returns an array of `Radix?` structs constructed from an array of radix strings. - /// - /// Example usage: - /// - /// ``` - /// ["20", "123"].radix(base: 4, as: Int16.self) - /// ``` - public func radix(base: Int, as type: T.Type) -> [Radix?] { - - self.map { Radix($0, base: base) } - - } - + + /// Returns an array of `Radix?` structs constructed from an array of hex strings. + public func radix(base: Int) -> [Radix?] { + + self.map { Radix($0, base: base) } + + } + + /// Returns an array of `Radix?` structs constructed from an array of radix strings. + /// + /// Example usage: + /// + /// ["20", "123"].radix(base: 4, as: Int16.self) + /// + public func radix(base: Int, as type: T.Type) -> [Radix?] { + + self.map { Radix($0, base: base) } + + } + } extension Collection where Element: BinaryInteger { - - /// Returns an array of `Radix` structs built from an integer array, preserving the integer type. - @inlinable public func radix(base: Int) -> [Radix] { - - self.map { Radix($0, base: base) } - - } - + + /// Returns an array of `Radix` structs built from an integer array, preserving the integer type. + @inlinable public func radix(base: Int) -> [Radix] { + + self.map { Radix($0, base: base) } + + } + } extension Data { - - /// Returns an array of `Radix` structs built from Data bytes. - public func radix(base: Int) -> [Radix] { - - self.map { Radix($0, base: base) } - - } - + + /// Returns an array of `Radix` structs built from Data bytes. + public func radix(base: Int) -> [Radix] { + + self.map { Radix($0, base: base) } + + } + } diff --git a/Sources/SwiftRadix/Radix/Radix.swift b/Sources/SwiftRadix/Radix/Radix.swift index 67e8516..99d8afe 100644 --- a/Sources/SwiftRadix/Radix/Radix.swift +++ b/Sources/SwiftRadix/Radix/Radix.swift @@ -1,127 +1,114 @@ // // Radix.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation - // MARK: - Radix /// A lightweight type useful for translating integers to radix strings and vice-versa, with a suite of convenient constructors and accessors. public struct Radix: RadixProtocol { - - // MARK: - Base Properties - - /// Stored value type - public typealias NumberType = T - - /// Stored value - public var value: NumberType = 0 - - /// Base (radix) - public var base: Int - - /// String prefix specific to the radix - @inline(__always) public var stringPrefix: String { - - switch base { - case 2: return "0b" - case 8: return "0o" - case 16: return "0x" - default: return "" - } - - } - - - // MARK: - Constructors - - /// Construct from a number, preserving the number's type in `value` variable. - /// - /// Valid radix is between 2 and 36. - /// - /// Note: convenience methods exist for common radices: - /// - /// ``` - /// Binary(0b1100) - /// 0b1100.binary - /// - /// Octal(0o123) - /// 0o123.octal - /// - /// Hex(0xFF) - /// 0xFF.hex - /// - /// ``` - @inline(__always) public init(_ number: NumberType, base: Int) { - - self.base = base - - // radix validity check - if base < 2 || base > 36 { self.base = 10 } - - self.value = number - - } - - /// Construct from a radix string. - /// - /// Valid radix is between 2 and 36. - /// - /// ie: - /// ``` - /// // Binary: - /// Radix("1100", radix: 2) - /// Radix("0b1100", radix: 2) - /// - /// // Octal: - /// Radix("12345670", radix: 8) - /// Radix("0o12345670", radix: 8) - /// - /// // Hex: - /// Radix("FF", radix: 16) - /// Radix("0xFF", radix: 16) - /// ``` - /// - /// Stores `value` as `Int` type by default. - /// - /// To specify number type: - /// - /// ``` - /// Radix("FF", radix: 16) - /// Radix("97FE", radix: 16) - /// ``` - /// - /// Note: convenience methods also exist for common radices: - /// - /// ``` - /// Binary("1100") - /// "1100".binary - /// - /// Octal("123") - /// "123".octal - /// - /// Hex("FF") - /// "FF".hex - /// - /// ``` - @inlinable public init?(_ string: String, base: Int) { - - self.base = base - - // radix validity check - if base < 2 || base > 36 { return nil } - - guard let convertedValue = valueFrom(radixString: string) - else { return nil } - - value = convertedValue - - } - + + // MARK: - Base Properties + + /// Stored value type + public typealias NumberType = T + + /// Stored value + public var value: NumberType = 0 + + /// Base (radix) + public var base: Int + + /// String prefix specific to the radix + @inline(__always) public var stringPrefix: String { + + switch base { + case 2: return "0b" + case 8: return "0o" + case 16: return "0x" + default: return "" + } + + } + + // MARK: - Constructors + + /// Construct from a number, preserving the number's type in `value` variable. + /// + /// Valid radix is between 2 and 36. + /// + /// Note: convenience methods exist for common radices: + /// + /// Binary(0b1100) + /// 0b1100.binary + /// + /// Octal(0o123) + /// 0o123.octal + /// + /// Hex(0xFF) + /// 0xFF.hex + /// + @inline(__always) public init(_ number: NumberType, base: Int) { + + self.base = base + + // radix validity check + if base < 2 || base > 36 { self.base = 10 } + + self.value = number + + } + + /// Construct from a radix string. + /// + /// Valid radix is between 2 and 36. + /// + /// ie: + /// + /// // Binary: + /// Radix("1100", radix: 2) + /// Radix("0b1100", radix: 2) + /// + /// // Octal: + /// Radix("12345670", radix: 8) + /// Radix("0o12345670", radix: 8) + /// + /// // Hex: + /// Radix("FF", radix: 16) + /// Radix("0xFF", radix: 16) + /// + /// Stores `value` as `Int` type by default. + /// + /// To specify number type: + /// + /// Radix("FF", radix: 16) + /// Radix("97FE", radix: 16) + /// + /// Note: convenience methods also exist for common radices: + /// + /// Binary("1100") + /// "1100".binary + /// + /// Octal("123") + /// "123".octal + /// + /// Hex("FF") + /// "FF".hex + /// + @inlinable public init?(_ string: String, base: Int) { + + self.base = base + + // radix validity check + if base < 2 || base > 36 { return nil } + + guard let convertedValue = valueFrom(radixString: string) + else { return nil } + + value = convertedValue + + } + } diff --git a/Sources/SwiftRadix/Radix/RadixProtocol.swift b/Sources/SwiftRadix/Radix/RadixProtocol.swift index 97a872c..a59eea9 100644 --- a/Sources/SwiftRadix/Radix/RadixProtocol.swift +++ b/Sources/SwiftRadix/Radix/RadixProtocol.swift @@ -1,10 +1,6 @@ // // RadixProtocol.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -14,20 +10,20 @@ import Foundation // extension Collection where Element : RadixProtocol { } public protocol RadixProtocol { - - associatedtype NumberType: BinaryInteger - - var value: NumberType { get set } - - var stringPrefix: String { get } - var base: Int { get set } - - init(_ number: NumberType, base: Int) - init?(_ string: String, base: Int) - - var stringValue: String { get set } - func stringValue(prefix: Bool) -> String - func stringValue(padTo: Int, splitEvery: Int, prefix: Bool) -> String - func stringValue(padToEvery: Int, splitEvery: Int, prefix: Bool) -> String - + + associatedtype NumberType: BinaryInteger + + var value: NumberType { get set } + + var stringPrefix: String { get } + var base: Int { get set } + + init(_ number: NumberType, base: Int) + init?(_ string: String, base: Int) + + var stringValue: String { get set } + func stringValue(prefix: Bool) -> String + func stringValue(padTo: Int, splitEvery: Int, prefix: Bool) -> String + func stringValue(padToEvery: Int, splitEvery: Int, prefix: Bool) -> String + } diff --git a/Sources/SwiftRadix/Utilities.swift b/Sources/SwiftRadix/Utilities.swift index b486aa9..f7ba693 100644 --- a/Sources/SwiftRadix/Utilities.swift +++ b/Sources/SwiftRadix/Utilities.swift @@ -1,10 +1,6 @@ // // Utilities.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-12. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import Foundation @@ -17,100 +13,101 @@ import Foundation // Workaround useful to enable extensions on types such as `Struct?` or `[Struct?]`. // // ie: -// ``` -// extension Collection -// where Element: OptionalType, -// Element.Wrapped: RadixProtocol -// { ... } -// ``` +// +// extension Collection +// where Element: OptionalType, +// Element.Wrapped: RadixProtocol +// { ... } +// public protocol OptionalType { - - associatedtype Wrapped - - /// Return an object as an optional. - var optional: Wrapped? { get } - + + associatedtype Wrapped + + /// Return an object as an optional. + var optional: Wrapped? { get } + } extension Optional: OptionalType { - - @inline(__always) public var optional: Wrapped? { - - self - - } - + + @inline(__always) public var optional: Wrapped? { + + self + + } + } // MARK: - BinaryInteger extension BinaryInteger { - - /// Returns the value rounded up to multiples of a given value. - internal func roundedUp(toMultiplesOf: Self) -> Self { - - if toMultiplesOf < 1 { return self } - - let source: Self = - self >= 0 - ? self - : 0 - self - - let isNegative: Bool = self < 0 - - let rem = source % toMultiplesOf - - let divisions = - rem == 0 - ? self - : self + (isNegative ? rem : toMultiplesOf - rem ) - - return divisions - - } - + + /// Returns the value rounded up to multiples of a given value. + internal func roundedUp(toMultiplesOf: Self) -> Self { + + if toMultiplesOf < 1 { return self } + + let source: Self = + self >= 0 + ? self + : 0 - self + + let isNegative: Bool = self < 0 + + let rem = source % toMultiplesOf + + let divisions = + rem == 0 + ? self + : self + (isNegative ? rem : toMultiplesOf - rem ) + + return divisions + + } + } // MARK: - String extension StringProtocol { - - /// Splits a string into groups of `length` characters, grouping from left-to-right. If `backwards` is true, right-to-left. - internal func split(every: Int, - backwards: Bool = false) -> [Self.SubSequence] { - - var result: [Self.SubSequence] = [] - - for i in stride(from: 0, to: self.count, by: every) { - - switch backwards { - case true: - let endIndex = self.index(self.endIndex, offsetBy: -i) - let startIndex = self.index(endIndex, - offsetBy: -every, - limitedBy: self.startIndex) - ?? self.startIndex - - result.insert(self[startIndex.. [Self.SubSequence] { + + var result: [Self.SubSequence] = [] + + for i in stride(from: 0, to: self.count, by: every) { + + switch backwards { + case true: + let endIndex = self.index(self.endIndex, offsetBy: -i) + let startIndex = self.index(endIndex, + offsetBy: -every, + limitedBy: self.startIndex) + ?? self.startIndex + + result.insert(self[startIndex.. - - // binary - - radix = 0b1100_1100.binary - - radix[bit: 0] = 1 - - XCTAssertEqual(radix.value, 0b1100_1101) - - radix[bit: 1] = 1 - - XCTAssertEqual(radix.value, 0b1100_1111) - - // hex - - radix = 0b1100_1100.hex - - radix[bit: 0] = 1 - - XCTAssertEqual(radix.value, 0b1100_1101) - - radix[bit: 1] = 1 - - XCTAssertEqual(radix.value, 0b1100_1111) - - // octal - - radix = 0b1100_1100.octal - - radix[bit: 0] = 1 - - XCTAssertEqual(radix.value, 0b1100_1101) - - radix[bit: 1] = 1 - - XCTAssertEqual(radix.value, 0b1100_1111) - - // edge cases - - radix = 0b1100_1100.hex - - radix[bit: 0] = 3 // invalid value, fails silently - - XCTAssertEqual(radix.value, 0b1100_1100) - - radix = 0b1100_1100.hex - - radix[bit: -1] = 1 // invalid index, fails silently - - XCTAssertEqual(radix.value, 0b1100_1100) - - } - + + func testRadix_Bit_Get() { + + let source = 0b1100_1100 + + // binary + + XCTAssertTrue(source.binary.bit(0) == 0b0) + XCTAssertTrue(source.binary.bit(1) == 0b0) + XCTAssertTrue(source.binary.bit(2) == 0b1) + XCTAssertTrue(source.binary.bit(3) == 0b1) + + XCTAssertEqual(source.binary[bit: 0], 0b0) + XCTAssertEqual(source.binary[bit: 1], 0b0) + XCTAssertEqual(source.binary[bit: 2], 0b1) + XCTAssertEqual(source.binary[bit: 3], 0b1) + + // hex + + XCTAssertTrue(source.hex.bit(0) == 0b0) + XCTAssertTrue(source.hex.bit(1) == 0b0) + XCTAssertTrue(source.hex.bit(2) == 0b1) + XCTAssertTrue(source.hex.bit(3) == 0b1) + + XCTAssertEqual(source.hex[bit: 0], 0b0) + XCTAssertEqual(source.hex[bit: 1], 0b0) + XCTAssertEqual(source.hex[bit: 2], 0b1) + XCTAssertEqual(source.hex[bit: 3], 0b1) + + // octal + + XCTAssertTrue(source.octal.bit(0) == 0b0) + XCTAssertTrue(source.octal.bit(1) == 0b0) + XCTAssertTrue(source.octal.bit(2) == 0b1) + XCTAssertTrue(source.octal.bit(3) == 0b1) + + XCTAssertEqual(source.octal[bit: 0], 0b0) + XCTAssertEqual(source.octal[bit: 1], 0b0) + XCTAssertEqual(source.octal[bit: 2], 0b1) + XCTAssertEqual(source.octal[bit: 3], 0b1) + + // edge cases + + XCTAssertTrue(source.hex.bit(-1) == 0) // out of bounds, default 0 + XCTAssertTrue(source.hex.bit(9) == 0) // out of bounds, default 0 + + } + + func testRadix_Bit_Subscript_Set() { + + var radix: Radix + + // binary + + radix = 0b1100_1100.binary + + radix[bit: 0] = 1 + + XCTAssertEqual(radix.value, 0b1100_1101) + + radix[bit: 1] = 1 + + XCTAssertEqual(radix.value, 0b1100_1111) + + // hex + + radix = 0b1100_1100.hex + + radix[bit: 0] = 1 + + XCTAssertEqual(radix.value, 0b1100_1101) + + radix[bit: 1] = 1 + + XCTAssertEqual(radix.value, 0b1100_1111) + + // octal + + radix = 0b1100_1100.octal + + radix[bit: 0] = 1 + + XCTAssertEqual(radix.value, 0b1100_1101) + + radix[bit: 1] = 1 + + XCTAssertEqual(radix.value, 0b1100_1111) + + // edge cases + + radix = 0b1100_1100.hex + + radix[bit: 0] = 3 // invalid value, fails silently + + XCTAssertEqual(radix.value, 0b1100_1100) + + radix = 0b1100_1100.hex + + radix[bit: -1] = 1 // invalid index, fails silently + + XCTAssertEqual(radix.value, 0b1100_1100) + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Bytes Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Bytes Tests.swift index c522b6f..8e58b23 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Bytes Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Bytes Tests.swift @@ -1,52 +1,48 @@ // // Radix Bytes Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_Bytes() { - - // binary - - XCTAssertEqual(UInt8(0xFF).binary.bytes, - [0xFF]) - - XCTAssertEqual(UInt16(0xFF).binary.bytes, - [0xFF, 0x00]) - - XCTAssertEqual(Int(0xFF).binary.bytes, - [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) - - // hex - - XCTAssertEqual(UInt8(0xFF).hex.bytes, - [0xFF]) - - XCTAssertEqual(UInt16(0xFF).hex.bytes, - [0xFF, 0x00]) - - XCTAssertEqual(Int(0xFF).hex.bytes, - [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) - - // octal - - XCTAssertEqual(UInt8(0xFF).octal.bytes, - [0xFF]) - - XCTAssertEqual(UInt16(0xFF).octal.bytes, - [0xFF, 0x00]) - - XCTAssertEqual(Int(0xFF).octal.bytes, - [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) - - } - + + func testRadix_Bytes() { + + // binary + + XCTAssertEqual(UInt8(0xFF).binary.bytes, + [0xFF]) + + XCTAssertEqual(UInt16(0xFF).binary.bytes, + [0xFF, 0x00]) + + XCTAssertEqual(Int(0xFF).binary.bytes, + [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) + + // hex + + XCTAssertEqual(UInt8(0xFF).hex.bytes, + [0xFF]) + + XCTAssertEqual(UInt16(0xFF).hex.bytes, + [0xFF, 0x00]) + + XCTAssertEqual(Int(0xFF).hex.bytes, + [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) + + // octal + + XCTAssertEqual(UInt8(0xFF).octal.bytes, + [0xFF]) + + XCTAssertEqual(UInt16(0xFF).octal.bytes, + [0xFF, 0x00]) + + XCTAssertEqual(Int(0xFF).octal.bytes, + [0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift index cf64741..e962beb 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift @@ -1,436 +1,432 @@ // // Radix Collection Methods Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_CollectionExtension_stringValue() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValue, - "0 11111111") - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValue, - "0 FF") - - // octal - - let source3 = [0x000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValue, - "0 123") - - } - - func testRadix_CollectionExtension_stringValue_Prefix() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValue(prefix: true), - "0b0 0b11111111") - - XCTAssertEqual(source1.stringValue(prefix: false), - "0 11111111") - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValue(prefix: true), - "0x0 0xFF") - - XCTAssertEqual(source2.stringValue(prefix: false), - "0 FF") - - // octal - - let source3 = [0x000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValue(prefix: true), - "0o0 0o123") - - XCTAssertEqual(source3.stringValue(prefix: false), - "0 123") - - } - - func testRadix_CollectionExtension_stringValue_PadTo() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValue(padTo: 2), - "00 11111111") - - XCTAssertEqual(source1.stringValue(padTo: 2, prefix: true), - "0b00 0b11111111") - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValue(padTo: 2), - "00 FF") - - XCTAssertEqual(source2.stringValue(padTo: 2, prefix: true), - "0x00 0xFF") - - // octal - - let source3 = [0x000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValue(padTo: 2), - "00 123") - - XCTAssertEqual(source3.stringValue(padTo: 2, prefix: true), - "0o00 0o123") - - // edge cases - - let source4 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source4.stringValue(padTo: -1), - "0 FF") - - } - - func testRadix_CollectionExtension_stringValue_PadToEvery() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValue(padToEvery: 0), - "0 11111111") - - XCTAssertEqual(source1.stringValue(padToEvery: 2), - "00 11111111") - - XCTAssertEqual(source1.stringValue(padToEvery: 3), - "000 011111111") - - XCTAssertEqual(source1.stringValue(padToEvery: 2, prefix: true), - "0b00 0b11111111") - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValue(padToEvery: 0), - "0 FF") - - XCTAssertEqual(source2.stringValue(padToEvery: 2), - "00 FF") - - XCTAssertEqual(source2.stringValue(padToEvery: 3), - "000 0FF") - - XCTAssertEqual(source2.stringValue(padToEvery: 2, prefix: true), - "0x00 0xFF") - - // octal - - let source3 = [0x000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValue(padToEvery: 0), - "0 123") - - XCTAssertEqual(source3.stringValue(padToEvery: 2), - "00 0123") - - XCTAssertEqual(source3.stringValue(padToEvery: 3), - "000 123") - - XCTAssertEqual(source3.stringValue(padToEvery: 5), - "00000 00123") - - XCTAssertEqual(source3.stringValue(padToEvery: 2, prefix: true), - "0o00 0o0123") - - // edge cases - - let source4 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source4.stringValue(padToEvery: -1), - "0 FF") - - } - - func testRadix_CollectionExtension_stringValueArrayLiteral() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValueArrayLiteral, - "[0b0, 0b11111111]") - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValueArrayLiteral, - "[0x0, 0xFF]") - - // octal - - let source3 = [0o000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValueArrayLiteral, - "[0o0, 0o123]") - - } - - func testRadix_CollectionExtension_stringValueArrayLiteral_PadTo() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValueArrayLiteral(padTo: 4), - "[0b0000, 0b11111111]") - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValueArrayLiteral(padTo: 4), - "[0x0000, 0x00FF]") - - // octal - - let source3 = [0o000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValueArrayLiteral(padTo: 4), - "[0o0000, 0o0123]") - - } - - func testRadix_CollectionExtension_stringValueArrayLiteral_PadToEvery() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValueArrayLiteral(padToEvery: 2), - "[0b00, 0b11111111]") - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValueArrayLiteral(padToEvery: 2), - "[0x00, 0xFF]") - - // octal - - let source3 = [0o000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValueArrayLiteral(padToEvery: 2), - "[0o00, 0o0123]") - - } - - func testRadix_CollectionExtension_stringValues() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValues, - ["0", "11111111"]) - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValues, - ["0", "FF"]) - - // octal - - let source3 = [0o000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValues, - ["0", "123"]) - - } - - func testRadix_CollectionExtension_stringValues_PadTo() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValues(padTo: 0), - ["0", "11111111"]) - - XCTAssertEqual(source1.stringValues(padTo: 2), - ["00", "11111111"]) - - XCTAssertEqual(source1.stringValues(padTo: 3), - ["000", "11111111"]) - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValues(padTo: 0), - ["0", "FF"]) - - XCTAssertEqual(source2.stringValues(padTo: 2), - ["00", "FF"]) - - XCTAssertEqual(source2.stringValues(padTo: 3), - ["000", "0FF"]) - - // octal - - let source3 = [0o000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValues(padTo: 0), - ["0", "123"]) - - XCTAssertEqual(source3.stringValues(padTo: 2), - ["00", "123"]) - - XCTAssertEqual(source3.stringValues(padTo: 3), - ["000", "123"]) - - XCTAssertEqual(source3.stringValues(padTo: 5), - ["00000", "00123"]) - - } - - func testRadix_CollectionExtension_stringValues_PadToEvery() { - - // binary - - let source1 = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.stringValues(padToEvery: 0), - ["0", "11111111"]) - - XCTAssertEqual(source1.stringValues(padToEvery: 2), - ["00", "11111111"]) - - XCTAssertEqual(source1.stringValues(padToEvery: 3), - ["000", "011111111"]) - - // hex - - let source2 = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.stringValues(padToEvery: 0), - ["0", "FF"]) - - XCTAssertEqual(source2.stringValues(padToEvery: 2), - ["00", "FF"]) - - XCTAssertEqual(source2.stringValues(padToEvery: 3), - ["000", "0FF"]) - - XCTAssertEqual(source2.stringValues(padToEvery: 2, prefixes: true), - ["0x00", "0xFF"]) - - let source2B = [0x00.hex, 0xFFFF.hex] - - XCTAssertEqual(source2B.stringValues(padToEvery: 0), - ["0", "FFFF"]) - - XCTAssertEqual(source2B.stringValues(padToEvery: 2), - ["00", "FFFF"]) - - XCTAssertEqual(source2B.stringValues(padToEvery: 3), - ["000", "00FFFF"]) - - XCTAssertEqual(source2B.stringValues(padToEvery: 3, prefixes: true), - ["0x000", "0x00FFFF"]) - - // octal - - let source3 = [0o000.octal, 0o123.octal] - - XCTAssertEqual(source3.stringValues(padToEvery: 0), - ["0", "123"]) - - XCTAssertEqual(source3.stringValues(padToEvery: 2), - ["00", "0123"]) - - XCTAssertEqual(source3.stringValues(padToEvery: 3), - ["000", "123"]) - - XCTAssertEqual(source3.stringValues(padToEvery: 5), - ["00000", "00123"]) - - } - - func testRadix_CollectionExtension_values() { - - // binary - - let source1: [Radix] - = [0x00.binary, 0xFF.binary] - - XCTAssertEqual(source1.values, [0x00, 0xFF]) - - // hex - - let source2: [Radix] - = [0x00.hex, 0xFF.hex] - - XCTAssertEqual(source2.values, [0x00, 0xFF]) - - // octal - - let source3: [Radix] - = [0x00.octal, 0xFF.octal] - - XCTAssertEqual(source3.values, [0x00, 0xFF]) - - } - - func testRadix_CollectionExtension_Optional_values() { - - // binary - - let source1: [Radix?] - = [0x00.binary, 0xFF.binary, nil] - - XCTAssertEqual(source1.values, [0x00, 0xFF, nil]) - - // hex - - let source2: [Radix?] - = [0x00.hex, 0xFF.hex, nil] - - XCTAssertEqual(source2.values, [0x00, 0xFF, nil]) - - // octal - - let source3: [Radix?] - = [0x00.octal, 0xFF.octal, nil] - - XCTAssertEqual(source3.values, [0x00, 0xFF, nil]) - - } - + + func testRadix_CollectionExtension_stringValue() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValue, + "0 11111111") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValue, + "0 FF") + + // octal + + let source3 = [0x000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValue, + "0 123") + + } + + func testRadix_CollectionExtension_stringValue_Prefix() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValue(prefix: true), + "0b0 0b11111111") + + XCTAssertEqual(source1.stringValue(prefix: false), + "0 11111111") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValue(prefix: true), + "0x0 0xFF") + + XCTAssertEqual(source2.stringValue(prefix: false), + "0 FF") + + // octal + + let source3 = [0x000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValue(prefix: true), + "0o0 0o123") + + XCTAssertEqual(source3.stringValue(prefix: false), + "0 123") + + } + + func testRadix_CollectionExtension_stringValue_PadTo() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValue(padTo: 2), + "00 11111111") + + XCTAssertEqual(source1.stringValue(padTo: 2, prefix: true), + "0b00 0b11111111") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValue(padTo: 2), + "00 FF") + + XCTAssertEqual(source2.stringValue(padTo: 2, prefix: true), + "0x00 0xFF") + + // octal + + let source3 = [0x000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValue(padTo: 2), + "00 123") + + XCTAssertEqual(source3.stringValue(padTo: 2, prefix: true), + "0o00 0o123") + + // edge cases + + let source4 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source4.stringValue(padTo: -1), + "0 FF") + + } + + func testRadix_CollectionExtension_stringValue_PadToEvery() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValue(padToEvery: 0), + "0 11111111") + + XCTAssertEqual(source1.stringValue(padToEvery: 2), + "00 11111111") + + XCTAssertEqual(source1.stringValue(padToEvery: 3), + "000 011111111") + + XCTAssertEqual(source1.stringValue(padToEvery: 2, prefix: true), + "0b00 0b11111111") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValue(padToEvery: 0), + "0 FF") + + XCTAssertEqual(source2.stringValue(padToEvery: 2), + "00 FF") + + XCTAssertEqual(source2.stringValue(padToEvery: 3), + "000 0FF") + + XCTAssertEqual(source2.stringValue(padToEvery: 2, prefix: true), + "0x00 0xFF") + + // octal + + let source3 = [0x000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValue(padToEvery: 0), + "0 123") + + XCTAssertEqual(source3.stringValue(padToEvery: 2), + "00 0123") + + XCTAssertEqual(source3.stringValue(padToEvery: 3), + "000 123") + + XCTAssertEqual(source3.stringValue(padToEvery: 5), + "00000 00123") + + XCTAssertEqual(source3.stringValue(padToEvery: 2, prefix: true), + "0o00 0o0123") + + // edge cases + + let source4 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source4.stringValue(padToEvery: -1), + "0 FF") + + } + + func testRadix_CollectionExtension_stringValueArrayLiteral() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValueArrayLiteral, + "[0b0, 0b11111111]") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValueArrayLiteral, + "[0x0, 0xFF]") + + // octal + + let source3 = [0o000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValueArrayLiteral, + "[0o0, 0o123]") + + } + + func testRadix_CollectionExtension_stringValueArrayLiteral_PadTo() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValueArrayLiteral(padTo: 4), + "[0b0000, 0b11111111]") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValueArrayLiteral(padTo: 4), + "[0x0000, 0x00FF]") + + // octal + + let source3 = [0o000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValueArrayLiteral(padTo: 4), + "[0o0000, 0o0123]") + + } + + func testRadix_CollectionExtension_stringValueArrayLiteral_PadToEvery() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValueArrayLiteral(padToEvery: 2), + "[0b00, 0b11111111]") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValueArrayLiteral(padToEvery: 2), + "[0x00, 0xFF]") + + // octal + + let source3 = [0o000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValueArrayLiteral(padToEvery: 2), + "[0o00, 0o0123]") + + } + + func testRadix_CollectionExtension_stringValues() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValues, + ["0", "11111111"]) + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValues, + ["0", "FF"]) + + // octal + + let source3 = [0o000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValues, + ["0", "123"]) + + } + + func testRadix_CollectionExtension_stringValues_PadTo() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValues(padTo: 0), + ["0", "11111111"]) + + XCTAssertEqual(source1.stringValues(padTo: 2), + ["00", "11111111"]) + + XCTAssertEqual(source1.stringValues(padTo: 3), + ["000", "11111111"]) + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValues(padTo: 0), + ["0", "FF"]) + + XCTAssertEqual(source2.stringValues(padTo: 2), + ["00", "FF"]) + + XCTAssertEqual(source2.stringValues(padTo: 3), + ["000", "0FF"]) + + // octal + + let source3 = [0o000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValues(padTo: 0), + ["0", "123"]) + + XCTAssertEqual(source3.stringValues(padTo: 2), + ["00", "123"]) + + XCTAssertEqual(source3.stringValues(padTo: 3), + ["000", "123"]) + + XCTAssertEqual(source3.stringValues(padTo: 5), + ["00000", "00123"]) + + } + + func testRadix_CollectionExtension_stringValues_PadToEvery() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValues(padToEvery: 0), + ["0", "11111111"]) + + XCTAssertEqual(source1.stringValues(padToEvery: 2), + ["00", "11111111"]) + + XCTAssertEqual(source1.stringValues(padToEvery: 3), + ["000", "011111111"]) + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValues(padToEvery: 0), + ["0", "FF"]) + + XCTAssertEqual(source2.stringValues(padToEvery: 2), + ["00", "FF"]) + + XCTAssertEqual(source2.stringValues(padToEvery: 3), + ["000", "0FF"]) + + XCTAssertEqual(source2.stringValues(padToEvery: 2, prefixes: true), + ["0x00", "0xFF"]) + + let source2B = [0x00.hex, 0xFFFF.hex] + + XCTAssertEqual(source2B.stringValues(padToEvery: 0), + ["0", "FFFF"]) + + XCTAssertEqual(source2B.stringValues(padToEvery: 2), + ["00", "FFFF"]) + + XCTAssertEqual(source2B.stringValues(padToEvery: 3), + ["000", "00FFFF"]) + + XCTAssertEqual(source2B.stringValues(padToEvery: 3, prefixes: true), + ["0x000", "0x00FFFF"]) + + // octal + + let source3 = [0o000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValues(padToEvery: 0), + ["0", "123"]) + + XCTAssertEqual(source3.stringValues(padToEvery: 2), + ["00", "0123"]) + + XCTAssertEqual(source3.stringValues(padToEvery: 3), + ["000", "123"]) + + XCTAssertEqual(source3.stringValues(padToEvery: 5), + ["00000", "00123"]) + + } + + func testRadix_CollectionExtension_values() { + + // binary + + let source1: [Radix] + = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.values, [0x00, 0xFF]) + + // hex + + let source2: [Radix] + = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.values, [0x00, 0xFF]) + + // octal + + let source3: [Radix] + = [0x00.octal, 0xFF.octal] + + XCTAssertEqual(source3.values, [0x00, 0xFF]) + + } + + func testRadix_CollectionExtension_Optional_values() { + + // binary + + let source1: [Radix?] + = [0x00.binary, 0xFF.binary, nil] + + XCTAssertEqual(source1.values, [0x00, 0xFF, nil]) + + // hex + + let source2: [Radix?] + = [0x00.hex, 0xFF.hex, nil] + + XCTAssertEqual(source2.values, [0x00, 0xFF, nil]) + + // octal + + let source3: [Radix?] + = [0x00.octal, 0xFF.octal, nil] + + XCTAssertEqual(source3.values, [0x00, 0xFF, nil]) + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Nibble Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Nibble Tests.swift index 4bd929d..c38097a 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Nibble Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Nibble Tests.swift @@ -1,104 +1,100 @@ // // Radix Nibble Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_Nibble_Get() { - - let source = 0b0101_1100 - - // binary - - XCTAssertTrue(source.binary.nibble(0) == 0b1100) - XCTAssertTrue(source.binary.nibble(1) == 0b0101) - - XCTAssertTrue(source.binary[nibble: 0] == 0b1100) - XCTAssertTrue(source.binary[nibble: 1] == 0b0101) - - // hex - - XCTAssertTrue(source.hex.nibble(0) == 0b1100) - XCTAssertTrue(source.hex.nibble(1) == 0b0101) - - XCTAssertTrue(source.hex[nibble: 0] == 0b1100) - XCTAssertTrue(source.hex[nibble: 1] == 0b0101) - - // octal - - XCTAssertTrue(source.octal.nibble(0) == 0b1100) - XCTAssertTrue(source.octal.nibble(1) == 0b0101) - - XCTAssertTrue(source.octal[nibble: 0] == 0b1100) - XCTAssertTrue(source.octal[nibble: 1] == 0b0101) - - // edge cases - - XCTAssertTrue(source.binary.nibble(-1) == 0) // out of bounds, default 0 - XCTAssertTrue(source.binary.nibble(2) == 0) // out of bounds, default 0 - - } - - func testRadix_Nibble_Subscript_Set() { - - var radix: Radix - - // binary - - radix = 0b0101_1100.binary - - radix[nibble: 0] = 0b0110 - - XCTAssertEqual(radix.value, 0b0101_0110) - - radix[nibble: 1] = 0b0110 - - XCTAssertEqual(radix.value, 0b0110_0110) - - // hex - - radix = 0b0101_1100.hex - - radix[nibble: 0] = 0b0110 - - XCTAssertEqual(radix.value, 0b0101_0110) - - radix[nibble: 1] = 0b0110 - - XCTAssertEqual(radix.value, 0b0110_0110) - - // octal - - radix = 0b0101_1100.octal - - radix[nibble: 0] = 0b0110 - - XCTAssertEqual(radix.value, 0b0101_0110) - - radix[nibble: 1] = 0b0110 - - XCTAssertEqual(radix.value, 0b0110_0110) - - // edge cases - - radix = 0b0101_1100.hex - - radix[nibble: 0] = 20 // invalid value, fails silently - - XCTAssertEqual(radix.value, 0b0101_1100) - - radix[nibble: -1] = 1 // invalid index, fails silently - - XCTAssertEqual(radix.value, 0b0101_1100) - - } - + + func testRadix_Nibble_Get() { + + let source = 0b0101_1100 + + // binary + + XCTAssertTrue(source.binary.nibble(0) == 0b1100) + XCTAssertTrue(source.binary.nibble(1) == 0b0101) + + XCTAssertTrue(source.binary[nibble: 0] == 0b1100) + XCTAssertTrue(source.binary[nibble: 1] == 0b0101) + + // hex + + XCTAssertTrue(source.hex.nibble(0) == 0b1100) + XCTAssertTrue(source.hex.nibble(1) == 0b0101) + + XCTAssertTrue(source.hex[nibble: 0] == 0b1100) + XCTAssertTrue(source.hex[nibble: 1] == 0b0101) + + // octal + + XCTAssertTrue(source.octal.nibble(0) == 0b1100) + XCTAssertTrue(source.octal.nibble(1) == 0b0101) + + XCTAssertTrue(source.octal[nibble: 0] == 0b1100) + XCTAssertTrue(source.octal[nibble: 1] == 0b0101) + + // edge cases + + XCTAssertTrue(source.binary.nibble(-1) == 0) // out of bounds, default 0 + XCTAssertTrue(source.binary.nibble(2) == 0) // out of bounds, default 0 + + } + + func testRadix_Nibble_Subscript_Set() { + + var radix: Radix + + // binary + + radix = 0b0101_1100.binary + + radix[nibble: 0] = 0b0110 + + XCTAssertEqual(radix.value, 0b0101_0110) + + radix[nibble: 1] = 0b0110 + + XCTAssertEqual(radix.value, 0b0110_0110) + + // hex + + radix = 0b0101_1100.hex + + radix[nibble: 0] = 0b0110 + + XCTAssertEqual(radix.value, 0b0101_0110) + + radix[nibble: 1] = 0b0110 + + XCTAssertEqual(radix.value, 0b0110_0110) + + // octal + + radix = 0b0101_1100.octal + + radix[nibble: 0] = 0b0110 + + XCTAssertEqual(radix.value, 0b0101_0110) + + radix[nibble: 1] = 0b0110 + + XCTAssertEqual(radix.value, 0b0110_0110) + + // edge cases + + radix = 0b0101_1100.hex + + radix[nibble: 0] = 20 // invalid value, fails silently + + XCTAssertEqual(radix.value, 0b0101_1100) + + radix[nibble: -1] = 1 // invalid index, fails silently + + XCTAssertEqual(radix.value, 0b0101_1100) + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Operators Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Operators Tests.swift index e989927..e322e91 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Operators Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Operators Tests.swift @@ -1,197 +1,193 @@ // // Radix Operators Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2017-12-29. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_Operators() { - - // just test hex (base-16) - // (since all operators operate on the underlying value, agnostic of radix) - - // Equatable - - XCTAssert( 1.hex == 1.hex) - XCTAssert( 1.hex != 2.hex) - - XCTAssert( 1.hex == 1) - XCTAssert( 1.hex == UInt8(1)) - XCTAssert( UInt8(1) == 1.hex) - XCTAssert( 1.hex != 2) - XCTAssert( 1.hex != UInt8(2)) - XCTAssert( UInt8(2) != 1.hex) - - // Equatable - Optionals - - XCTAssert( "1".hex == "1".hex) - XCTAssert( "1".hex != "2".hex) - - XCTAssert( "1".hex == 1) - XCTAssert( "1".hex == UInt8(1)) - XCTAssert( UInt8(1) == 1.hex) - XCTAssert( "1".hex != 2) - XCTAssert( "1".hex != UInt8(2)) - XCTAssert( UInt8(2) != "1".hex) - - // Comparable - - XCTAssert( 2.hex > 1.hex) - XCTAssertFalse(1.hex > 2.hex) - - XCTAssert( 2.hex > UInt8(1)) - XCTAssert( UInt8(2) > 1.hex) - XCTAssertFalse(1.hex > UInt8(2)) - XCTAssertFalse(UInt8(1) > 2.hex) - - XCTAssert( 1.hex < 2.hex) - XCTAssertFalse(2.hex < 1.hex) - - XCTAssert( 1.hex < UInt8(2)) - XCTAssert( UInt8(1) < 2.hex) - XCTAssertFalse(2.hex < Int8(1)) - XCTAssertFalse(UInt8(2) < 1.hex) - - XCTAssert( 1.hex >= 1.hex) - XCTAssert( 2.hex >= 1.hex) - - XCTAssert( 1.hex >= UInt8(1)) - XCTAssert( UInt8(2) >= 1.hex) - - XCTAssert( 1.hex <= 2.hex) - XCTAssert( 2.hex <= 2.hex) - - XCTAssert( 1.hex <= UInt8(2)) - XCTAssert( UInt8(2) <= 2.hex) - - // Math operators - - XCTAssertEqual(2.hex + 2.hex , 4.hex) - XCTAssertEqual(2.hex + 2 , 4.hex) - XCTAssertEqual(2 + 2.hex , 4) - XCTAssertEqual(UInt8(2) + UInt8(2).hex, UInt8(4)) - - XCTAssertEqual(4.hex - 2.hex , 2.hex) - XCTAssertEqual(4.hex - 2 , 2.hex) - XCTAssertEqual(4 - 2.hex , 2) - XCTAssertEqual(UInt8(4) - UInt8(2).hex, UInt8(2)) - - XCTAssertEqual(4.hex * 2.hex , 8.hex) - XCTAssertEqual(4.hex * 2 , 8.hex) - XCTAssertEqual(4 * 2.hex , 8) - XCTAssertEqual(UInt8(4) * UInt8(2).hex, UInt8(8)) - - XCTAssertEqual(8.hex / 2.hex , 4.hex) - XCTAssertEqual(8.hex / 2 , 4.hex) - XCTAssertEqual(8 / 2.hex , 4) - XCTAssertEqual(UInt8(8) / UInt8(2).hex, UInt8(4)) - - // Mutating math operators - - var hexVal: Radix - var intVal: Int - - hexVal = Radix(0, base: 16) - hexVal += 1 - XCTAssert(hexVal == 1) - hexVal += 1.hex - XCTAssert(hexVal == 2) - hexVal -= 1 - XCTAssert(hexVal == 1) - hexVal -= 1.hex - XCTAssert(hexVal == 0) - - intVal = 0 - intVal += 1.hex - XCTAssert(intVal == 1) - intVal -= 1.hex - XCTAssert(intVal == 0) - - hexVal = Radix(2, base: 16) - hexVal *= 2 - XCTAssert(hexVal == 4) - hexVal *= 2.hex - XCTAssert(hexVal == 8) - hexVal /= 2 - XCTAssert(hexVal == 4) - hexVal /= 2.hex - XCTAssert(hexVal == 2) - - intVal = 2 - intVal *= 2.hex - XCTAssert(intVal == 4) - intVal /= 2.hex - XCTAssert(intVal == 2) - - // Bitwise bit shift operators - - XCTAssertEqual(0b010.hex >> 1 , 0b001.hex) - XCTAssertEqual(0b010.hex >> 1.hex , 0b001.hex) - XCTAssertEqual(0b010 >> 1.hex , 0b001) - - XCTAssertEqual(0b010.hex << 1 , 0b100.hex) - XCTAssertEqual(0b010.hex << 1.hex , 0b100.hex) - XCTAssertEqual(0b010 << 1.hex , 0b100) - - // -- mixed types - - XCTAssertEqual(UInt8(0b010).hex >> 1 , UInt8(0b001).hex) - XCTAssertEqual( 0b010 .hex >> UInt8(1) , 0b001 .hex) - XCTAssertEqual(UInt8(0b010).hex >> 1 .hex , UInt8(0b001).hex) - XCTAssertEqual( 0b010 .hex >> UInt8(1).hex , 0b001 .hex) - XCTAssertEqual(UInt8(0b010) >> 1 .hex , 0b001) - XCTAssertEqual( 0b010 >> UInt8(1) , 0b001) - - XCTAssertEqual(UInt8(0b010).hex << 1 , UInt8(0b100).hex) - XCTAssertEqual( 0b010 .hex << UInt8(1) , 0b100 .hex) - XCTAssertEqual(UInt8(0b010).hex << 1 .hex , UInt8(0b100).hex) - XCTAssertEqual( 0b010 .hex << UInt8(1).hex , 0b100 .hex) - XCTAssertEqual(UInt8(0b010) << 1 .hex , UInt8(0b100)) - XCTAssertEqual( 0b010 << UInt8(1).hex , 0b100) - - // Bitwise nibble shift operators - - XCTAssertEqual(0x0F0.hex >>>> 1 , 0x00F.hex) - XCTAssertEqual(0x0F0.hex >>>> 1.hex , 0x00F.hex) - XCTAssertEqual(0x0F0 >>>> 1.hex , 0x00F) - - XCTAssertEqual(0x0F0.hex <<<< 1 , 0xF00.hex) - XCTAssertEqual(0x0F0.hex <<<< 1.hex , 0xF00.hex) - XCTAssertEqual(0x0F0 <<<< 1.hex , 0xF00) - - // -- mixed types - - XCTAssertEqual(UInt8(0x0F0).hex >>>> 1 , UInt8(0x00F).hex) - XCTAssertEqual( 0x0F0 .hex >>>> UInt8(1) , 0x00F.hex) - XCTAssertEqual(UInt8(0x0F0).hex >>>> 1 .hex , UInt8(0x00F).hex) - XCTAssertEqual( 0x0F0 .hex >>>> UInt8(1).hex , 0x00F.hex) - XCTAssertEqual(UInt8(0x0F0) >>>> 1 .hex , 0x00F) - XCTAssertEqual( 0x0F0 >>>> UInt8(1).hex , 0x00F) - - XCTAssertEqual(UInt8(0x00F).hex <<<< 1 , UInt8(0x0F0).hex) - XCTAssertEqual( 0x00F .hex <<<< UInt8(1) , 0x0F0 .hex) - XCTAssertEqual(UInt8(0x00F).hex <<<< 1 .hex , UInt8(0x0F0).hex) - XCTAssertEqual( 0x00F .hex <<<< UInt8(1).hex , 0x0F0 .hex) - XCTAssertEqual(UInt8(0x00F) <<<< 1 .hex , 0x0F0) - XCTAssertEqual( 0x00F <<<< UInt8(1).hex , 0x0F0) - - // Bitwise AND - // & operator only supports integers of the same type - - XCTAssertEqual(0b1111_1111.hex & 0b1000_1000.hex, 0b1000_1000.hex) - XCTAssertEqual(0b1111_1111.hex & 0b1000_1000 , 0b1000_1000.hex) - XCTAssertEqual(0b1111_1111 & 0b1000_1000.hex, 0b1000_1000) - - XCTAssertEqual(UInt8(0b1111_1111).hex & UInt8(0b1000_1000).hex, - UInt8(0b1000_1000).hex) - - } - + + func testRadix_Operators() { + + // just test hex (base-16) + // (since all operators operate on the underlying value, agnostic of radix) + + // Equatable + + XCTAssert( 1.hex == 1.hex) + XCTAssert( 1.hex != 2.hex) + + XCTAssert( 1.hex == 1) + XCTAssert( 1.hex == UInt8(1)) + XCTAssert( UInt8(1) == 1.hex) + XCTAssert( 1.hex != 2) + XCTAssert( 1.hex != UInt8(2)) + XCTAssert( UInt8(2) != 1.hex) + + // Equatable - Optionals + + XCTAssert( "1".hex == "1".hex) + XCTAssert( "1".hex != "2".hex) + + XCTAssert( "1".hex == 1) + XCTAssert( "1".hex == UInt8(1)) + XCTAssert( UInt8(1) == 1.hex) + XCTAssert( "1".hex != 2) + XCTAssert( "1".hex != UInt8(2)) + XCTAssert( UInt8(2) != "1".hex) + + // Comparable + + XCTAssert( 2.hex > 1.hex) + XCTAssertFalse(1.hex > 2.hex) + + XCTAssert( 2.hex > UInt8(1)) + XCTAssert( UInt8(2) > 1.hex) + XCTAssertFalse(1.hex > UInt8(2)) + XCTAssertFalse(UInt8(1) > 2.hex) + + XCTAssert( 1.hex < 2.hex) + XCTAssertFalse(2.hex < 1.hex) + + XCTAssert( 1.hex < UInt8(2)) + XCTAssert( UInt8(1) < 2.hex) + XCTAssertFalse(2.hex < Int8(1)) + XCTAssertFalse(UInt8(2) < 1.hex) + + XCTAssert( 1.hex >= 1.hex) + XCTAssert( 2.hex >= 1.hex) + + XCTAssert( 1.hex >= UInt8(1)) + XCTAssert( UInt8(2) >= 1.hex) + + XCTAssert( 1.hex <= 2.hex) + XCTAssert( 2.hex <= 2.hex) + + XCTAssert( 1.hex <= UInt8(2)) + XCTAssert( UInt8(2) <= 2.hex) + + // Math operators + + XCTAssertEqual(2.hex + 2.hex , 4.hex) + XCTAssertEqual(2.hex + 2 , 4.hex) + XCTAssertEqual(2 + 2.hex , 4) + XCTAssertEqual(UInt8(2) + UInt8(2).hex, UInt8(4)) + + XCTAssertEqual(4.hex - 2.hex , 2.hex) + XCTAssertEqual(4.hex - 2 , 2.hex) + XCTAssertEqual(4 - 2.hex , 2) + XCTAssertEqual(UInt8(4) - UInt8(2).hex, UInt8(2)) + + XCTAssertEqual(4.hex * 2.hex , 8.hex) + XCTAssertEqual(4.hex * 2 , 8.hex) + XCTAssertEqual(4 * 2.hex , 8) + XCTAssertEqual(UInt8(4) * UInt8(2).hex, UInt8(8)) + + XCTAssertEqual(8.hex / 2.hex , 4.hex) + XCTAssertEqual(8.hex / 2 , 4.hex) + XCTAssertEqual(8 / 2.hex , 4) + XCTAssertEqual(UInt8(8) / UInt8(2).hex, UInt8(4)) + + // Mutating math operators + + var hexVal: Radix + var intVal: Int + + hexVal = Radix(0, base: 16) + hexVal += 1 + XCTAssert(hexVal == 1) + hexVal += 1.hex + XCTAssert(hexVal == 2) + hexVal -= 1 + XCTAssert(hexVal == 1) + hexVal -= 1.hex + XCTAssert(hexVal == 0) + + intVal = 0 + intVal += 1.hex + XCTAssert(intVal == 1) + intVal -= 1.hex + XCTAssert(intVal == 0) + + hexVal = Radix(2, base: 16) + hexVal *= 2 + XCTAssert(hexVal == 4) + hexVal *= 2.hex + XCTAssert(hexVal == 8) + hexVal /= 2 + XCTAssert(hexVal == 4) + hexVal /= 2.hex + XCTAssert(hexVal == 2) + + intVal = 2 + intVal *= 2.hex + XCTAssert(intVal == 4) + intVal /= 2.hex + XCTAssert(intVal == 2) + + // Bitwise bit shift operators + + XCTAssertEqual(0b010.hex >> 1 , 0b001.hex) + XCTAssertEqual(0b010.hex >> 1.hex , 0b001.hex) + XCTAssertEqual(0b010 >> 1.hex , 0b001) + + XCTAssertEqual(0b010.hex << 1 , 0b100.hex) + XCTAssertEqual(0b010.hex << 1.hex , 0b100.hex) + XCTAssertEqual(0b010 << 1.hex , 0b100) + + // -- mixed types + + XCTAssertEqual(UInt8(0b010).hex >> 1 , UInt8(0b001).hex) + XCTAssertEqual( 0b010 .hex >> UInt8(1) , 0b001 .hex) + XCTAssertEqual(UInt8(0b010).hex >> 1 .hex , UInt8(0b001).hex) + XCTAssertEqual( 0b010 .hex >> UInt8(1).hex , 0b001 .hex) + XCTAssertEqual(UInt8(0b010) >> 1 .hex , 0b001) + XCTAssertEqual( 0b010 >> UInt8(1) , 0b001) + + XCTAssertEqual(UInt8(0b010).hex << 1 , UInt8(0b100).hex) + XCTAssertEqual( 0b010 .hex << UInt8(1) , 0b100 .hex) + XCTAssertEqual(UInt8(0b010).hex << 1 .hex , UInt8(0b100).hex) + XCTAssertEqual( 0b010 .hex << UInt8(1).hex , 0b100 .hex) + XCTAssertEqual(UInt8(0b010) << 1 .hex , UInt8(0b100)) + XCTAssertEqual( 0b010 << UInt8(1).hex , 0b100) + + // Bitwise nibble shift operators + + XCTAssertEqual(0x0F0.hex >>>> 1 , 0x00F.hex) + XCTAssertEqual(0x0F0.hex >>>> 1.hex , 0x00F.hex) + XCTAssertEqual(0x0F0 >>>> 1.hex , 0x00F) + + XCTAssertEqual(0x0F0.hex <<<< 1 , 0xF00.hex) + XCTAssertEqual(0x0F0.hex <<<< 1.hex , 0xF00.hex) + XCTAssertEqual(0x0F0 <<<< 1.hex , 0xF00) + + // -- mixed types + + XCTAssertEqual(UInt8(0x0F0).hex >>>> 1 , UInt8(0x00F).hex) + XCTAssertEqual( 0x0F0 .hex >>>> UInt8(1) , 0x00F.hex) + XCTAssertEqual(UInt8(0x0F0).hex >>>> 1 .hex , UInt8(0x00F).hex) + XCTAssertEqual( 0x0F0 .hex >>>> UInt8(1).hex , 0x00F.hex) + XCTAssertEqual(UInt8(0x0F0) >>>> 1 .hex , 0x00F) + XCTAssertEqual( 0x0F0 >>>> UInt8(1).hex , 0x00F) + + XCTAssertEqual(UInt8(0x00F).hex <<<< 1 , UInt8(0x0F0).hex) + XCTAssertEqual( 0x00F .hex <<<< UInt8(1) , 0x0F0 .hex) + XCTAssertEqual(UInt8(0x00F).hex <<<< 1 .hex , UInt8(0x0F0).hex) + XCTAssertEqual( 0x00F .hex <<<< UInt8(1).hex , 0x0F0 .hex) + XCTAssertEqual(UInt8(0x00F) <<<< 1 .hex , 0x0F0) + XCTAssertEqual( 0x00F <<<< UInt8(1).hex , 0x0F0) + + // Bitwise AND + // & operator only supports integers of the same type + + XCTAssertEqual(0b1111_1111.hex & 0b1000_1000.hex, 0b1000_1000.hex) + XCTAssertEqual(0b1111_1111.hex & 0b1000_1000 , 0b1000_1000.hex) + XCTAssertEqual(0b1111_1111 & 0b1000_1000.hex, 0b1000_1000) + + XCTAssertEqual(UInt8(0b1111_1111).hex & UInt8(0b1000_1000).hex, + UInt8(0b1000_1000).hex) + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Protocol Adoptions Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Protocol Adoptions Tests.swift index bbab920..8ca8c1f 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Protocol Adoptions Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Protocol Adoptions Tests.swift @@ -1,83 +1,79 @@ // // Radix Protocol Adoptions Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-13. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_CustomStringConvertible() { - - // CustomStringConvertible - - // binary - - XCTAssertEqual(String(describing: 1.binary), - "Radix(0b1)") - XCTAssertEqual(1.binary.description, - "Radix(0b1)") - - // hex - - XCTAssertEqual(String(describing: 1.hex), - "Radix(0x1)") - XCTAssertEqual(1.hex.description, - "Radix(0x1)") - - // octal - - XCTAssertEqual(String(describing: 1.octal), - "Radix(0o1)") - XCTAssertEqual(1.octal.description, - "Radix(0o1)") - - // CustomDebugStringConvertible - - // binary - - XCTAssertEqual(1.binary.debugDescription, - "Radix(0b1)") - - // hex - - XCTAssertEqual(1.hex.debugDescription, - "Radix(0x1)") - - // octal - - XCTAssertEqual(1.octal.debugDescription, - "Radix(0o1)") - - } - - func testRadix_Hashable() { - - // Dictionary - [Key : Hashable] - - var dict: [String : Radix] = [:] - - dict.updateValue(1.hex, forKey: "value1") - - XCTAssertEqual(dict["value1"]!.value, 1) - - // Set - Hashable - - var set: Set = [1.hex] - - set.update(with: 1.hex) - - XCTAssertEqual(set.count, 1) // should recognize the dupe was not added - - set.update(with: 2.hex) - - XCTAssertEqual(set.count, 2) - - } - + + func testRadix_CustomStringConvertible() { + + // CustomStringConvertible + + // binary + + XCTAssertEqual(String(describing: 1.binary), + "Radix(0b1)") + XCTAssertEqual(1.binary.description, + "Radix(0b1)") + + // hex + + XCTAssertEqual(String(describing: 1.hex), + "Radix(0x1)") + XCTAssertEqual(1.hex.description, + "Radix(0x1)") + + // octal + + XCTAssertEqual(String(describing: 1.octal), + "Radix(0o1)") + XCTAssertEqual(1.octal.description, + "Radix(0o1)") + + // CustomDebugStringConvertible + + // binary + + XCTAssertEqual(1.binary.debugDescription, + "Radix(0b1)") + + // hex + + XCTAssertEqual(1.hex.debugDescription, + "Radix(0x1)") + + // octal + + XCTAssertEqual(1.octal.debugDescription, + "Radix(0o1)") + + } + + func testRadix_Hashable() { + + // Dictionary - [Key : Hashable] + + var dict: [String : Radix] = [:] + + dict.updateValue(1.hex, forKey: "value1") + + XCTAssertEqual(dict["value1"]!.value, 1) + + // Set - Hashable + + var set: Set = [1.hex] + + set.update(with: 1.hex) + + XCTAssertEqual(set.count, 1) // should recognize the dupe was not added + + set.update(with: 2.hex) + + XCTAssertEqual(set.count, 2) + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift index fa80b72..e632faa 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift @@ -1,384 +1,380 @@ // // Radix Strings Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-14. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_stringValue_Get() { - - // binary - - XCTAssertEqual(0b0.binary.stringValue, "0") - XCTAssertEqual(0b1100_1100.binary.stringValue, "11001100") - - // hex - - XCTAssertEqual(0x00.hex.stringValue, "0") - XCTAssertEqual(0xFF.hex.stringValue, "FF") - - // octal - - XCTAssertEqual(0x000.octal.stringValue, "0") - XCTAssertEqual(0o123.octal.stringValue, "123") - - } - - func testRadix_stringValue_Get_Extents() { - - // binary - - XCTAssertEqual(UInt.max.binary.stringValue, - "1111111111111111111111111111111111111111111111111111111111111111") - - // hex - - XCTAssertEqual(UInt.max.hex.stringValue, - "FFFFFFFFFFFFFFFF") - - // octal - - XCTAssertEqual(UInt.max.octal.stringValue, - "1777777777777777777777") - - } - - func testRadix_stringValue_Set() { - - // binary - - var source1 = 0b0.binary - - source1.stringValue = "11001100" // no prefix - XCTAssertEqual(source1.value, 0b1100_1100) - - source1.stringValue = "0b00110011" // with prefix - XCTAssertEqual(source1.value, 0b0011_0011) - - // hex - - var source2 = 0x00.hex - - source2.stringValue = "FF" // no prefix - XCTAssertEqual(source2.value, 0xFF) - - source2.stringValue = "0xFFFF" // with prefix - XCTAssertEqual(source2.value, 0xFFFF) - - // octal - - var source3 = 0o0.octal - - source3.stringValue = "123" // no prefix - XCTAssertEqual(source3.value, 0o123) - - source3.stringValue = "0o1234" // with prefix - XCTAssertEqual(source3.value, 0o1234) - - } - - func testRadix_stringValue_Set_Extents() { - - // binary - - var source1 = UInt(0b0).binary - - source1.stringValue = "1111111111111111111111111111111111111111111111111111111111111111" - - XCTAssertEqual(source1.value, UInt.max) - - // hex - - var source2 = UInt(0x00).hex - - source2.stringValue = "FFFFFFFFFFFFFFFF" - - XCTAssertEqual(source2.value, UInt.max) - - // octal - - var source3 = UInt(0o0).octal - - source3.stringValue = "1777777777777777777777" - - XCTAssertEqual(source3.value, UInt.max) - - } - - func testRadix_stringValue_Set_Extents_EdgeCases() { - - // binary - - var source1 = 0b0.binary - - source1.stringValue = "1111111111111111111111111111111111111111111111111111111111111111" - - XCTAssertEqual(source1.value, 0) // string overflowed Int, set silently fails - - // hex - - var source2 = 0x00.hex - - source2.stringValue = "FFFFFFFFFFFFFFFF" - - XCTAssertEqual(source2.value, 0) // string overflowed Int, set silently fails - - // octal - - var source3 = 0o0.octal - - source3.stringValue = "1777777777777777777777" - - XCTAssertEqual(source3.value, 0) // string overflowed Int, set silently fails - - } - - func testRadix_stringValue_Prefix() { - - // binary - - XCTAssertEqual(0b0.binary.stringValue(prefix: true), "0b0") - XCTAssertEqual(0b0.binary.stringValue(prefix: false), "0") - - // hex - - XCTAssertEqual(0b0.hex.stringValue(prefix: true), "0x0") - XCTAssertEqual(0b0.hex.stringValue(prefix: false), "0") - - // octal - - XCTAssertEqual(0b0.octal.stringValue(prefix: true), "0o0") - XCTAssertEqual(0b0.octal.stringValue(prefix: false), "0") - - } - - func testRadix_stringValue_PadTo() { - - // binary - - let source1 = 0b0000_0111.binary - - XCTAssertEqual(source1.stringValue(padTo: 0), "111") - XCTAssertEqual(source1.stringValue(padTo: 1), "111") - XCTAssertEqual(source1.stringValue(padTo: 2), "111") - XCTAssertEqual(source1.stringValue(padTo: 6), "000111") - - XCTAssertEqual(source1.stringValue(padTo: 2, prefix: true), "0b111") - XCTAssertEqual(source1.stringValue(padTo: 6, prefix: true), "0b000111") - - // edge cases - XCTAssertEqual(source1.stringValue(padTo: -1), "111") - - - // hex - - let source2 = 0x0F.hex - - XCTAssertEqual(source2.stringValue(padTo: 0), "F") - XCTAssertEqual(source2.stringValue(padTo: 1), "F") - XCTAssertEqual(source2.stringValue(padTo: 2), "0F") - XCTAssertEqual(source2.stringValue(padTo: 6), "00000F") - - XCTAssertEqual(source2.stringValue(padTo: 2, prefix: true), "0x0F") - - // edge cases - XCTAssertEqual(source2.stringValue(padTo: -1), "F") - - - // octal - - let source3 = 0o17.octal - - XCTAssertEqual(source3.stringValue(padTo: 0), "17") - XCTAssertEqual(source3.stringValue(padTo: 1), "17") - XCTAssertEqual(source3.stringValue(padTo: 2), "17") - XCTAssertEqual(source3.stringValue(padTo: 6), "000017") - - XCTAssertEqual(source3.stringValue(padTo: 2, prefix: true), "0o17") - - // edge cases - XCTAssertEqual(source3.stringValue(padTo: -1), "17") - - } - - func testRadix_stringValue_PadTo_EdgeCases() { - - // ensure padding of 0 or a negative value defaults to an internal padding of 1 - - // binary - - XCTAssertEqual(0b0.binary.stringValue(padTo: -1), "0") - XCTAssertEqual(0b0.binary.stringValue(padTo: 0), "0") - - // hex - - XCTAssertEqual(0x0.hex.stringValue(padTo: -1), "0") - XCTAssertEqual(0x0.hex.stringValue(padTo: 0), "0") - - // octal - - XCTAssertEqual(0o0.hex.stringValue(padTo: -1), "0") - XCTAssertEqual(0o0.hex.stringValue(padTo: 0), "0") - - } - - func testRadix_stringValue_PadTo_SplitEvery() { - - // binary - - let source1 = 0b0000_0111.binary - - XCTAssertEqual(source1.stringValue(padTo: 0, splitEvery: 0), "111") - XCTAssertEqual(source1.stringValue(padTo: 1, splitEvery: 1), "1 1 1") - XCTAssertEqual(source1.stringValue(padTo: 1, splitEvery: 2), "1 11") - XCTAssertEqual(source1.stringValue(padTo: 2, splitEvery: 2), "1 11") - XCTAssertEqual(source1.stringValue(padTo: 4, splitEvery: 3), "0 111") - - // prefix treats it as a Swift Integer Literal, inserting underscores for splits - XCTAssertEqual(source1.stringValue(padTo: 8, splitEvery: 2, prefix: true), "0b00_00_01_11") - - // edge cases - XCTAssertEqual(source1.stringValue(padTo: 2, splitEvery: -1), "111") - - - // hex - - let source2 = 0xF0F.hex - - XCTAssertEqual(source2.stringValue(padTo: 0, splitEvery: 0), "F0F") - XCTAssertEqual(source2.stringValue(padTo: 1, splitEvery: 1), "F 0 F") - XCTAssertEqual(source2.stringValue(padTo: 2, splitEvery: 2), "F 0F") - XCTAssertEqual(source2.stringValue(padTo: 8, splitEvery: 3), "00 000 F0F") - - // prefix treats it as a Swift Integer Literal, inserting underscores for splits - XCTAssertEqual(source2.stringValue(padTo: 8, splitEvery: 2, prefix: true), "0x00_00_0F_0F") - - // edge cases - XCTAssertEqual(source2.stringValue(padTo: 2, splitEvery: -1), "F0F") - - - // octal - - let source3 = 0o7.octal - - XCTAssertEqual(source3.stringValue(padTo: 0, splitEvery: 0), "7") - XCTAssertEqual(source3.stringValue(padTo: 1, splitEvery: 1), "7") - XCTAssertEqual(source3.stringValue(padTo: 2, splitEvery: 2), "07") - XCTAssertEqual(source3.stringValue(padTo: 4, splitEvery: 3), "0 007") - - // prefix treats it as a Swift Integer Literal, inserting underscores for splits - XCTAssertEqual(source3.stringValue(padTo: 2, splitEvery: 2, prefix: true), "0o07") - - // edge cases - XCTAssertEqual(source3.stringValue(padTo: 2, splitEvery: -1), "07") - - } - - func testRadix_stringValue_PadToEvery() { - - // binary - - let source1 = 0b0001_1111.binary - - XCTAssertEqual(source1.stringValue(padToEvery: 0), "11111") - XCTAssertEqual(source1.stringValue(padToEvery: 1), "11111") - XCTAssertEqual(source1.stringValue(padToEvery: 2), "011111") - XCTAssertEqual(source1.stringValue(padToEvery: 4), "00011111") - - XCTAssertEqual(source1.stringValue(padToEvery: 2, prefix: true), "0b011111") - - // edge cases - XCTAssertEqual(source1.stringValue(padToEvery: -1), "11111") - - - // hex - - let source2 = 0xFFF.hex - - XCTAssertEqual(source2.stringValue(padToEvery: 0), "FFF") - XCTAssertEqual(source2.stringValue(padToEvery: 1), "FFF") - XCTAssertEqual(source2.stringValue(padToEvery: 2), "0FFF") - XCTAssertEqual(source2.stringValue(padToEvery: 6), "000FFF") - - XCTAssertEqual(source2.stringValue(padToEvery: 2, prefix: true), "0x0FFF") - - // edge cases - XCTAssertEqual(source2.stringValue(padToEvery: -1), "FFF") - - - // octal - - let source3 = 0o123456.octal - - XCTAssertEqual(source3.stringValue(padToEvery: 0), "123456") - XCTAssertEqual(source3.stringValue(padToEvery: 1), "123456") - XCTAssertEqual(source3.stringValue(padToEvery: 2), "123456") - XCTAssertEqual(source3.stringValue(padToEvery: 4), "00123456") - - XCTAssertEqual(source3.stringValue(padToEvery: 2, prefix: true), "0o123456") - - // edge cases - XCTAssertEqual(source3.stringValue(padToEvery: -1), "123456") - - } - - func testRadix_stringValue_PadToEvery_SplitEvery() { - - // binary - - let source1 = 0b0001_1111.binary - - XCTAssertEqual(source1.stringValue(padToEvery: 0, splitEvery: 0), "11111") - XCTAssertEqual(source1.stringValue(padToEvery: 1, splitEvery: 1), "1 1 1 1 1") - XCTAssertEqual(source1.stringValue(padToEvery: 1, splitEvery: 2), "1 11 11") - XCTAssertEqual(source1.stringValue(padToEvery: 2, splitEvery: 2), "01 11 11") - XCTAssertEqual(source1.stringValue(padToEvery: 4, splitEvery: 3), "00 011 111") - - // prefix treats it as a Swift Integer Literal, inserting underscores for splits - XCTAssertEqual(source1.stringValue(padToEvery: 2, splitEvery: 2, prefix: true), "0b01_11_11") - - // edge cases - XCTAssertEqual(source1.stringValue(padToEvery: 2, splitEvery: -1), "011111") - - - // hex - - let source2 = 0xFFF.hex - - XCTAssertEqual(source2.stringValue(padToEvery: 0, splitEvery: 0), "FFF") - XCTAssertEqual(source2.stringValue(padToEvery: 1, splitEvery: 1), "F F F") - XCTAssertEqual(source2.stringValue(padToEvery: 2, splitEvery: 2), "0F FF") - XCTAssertEqual(source2.stringValue(padToEvery: 8, splitEvery: 3), "00 000 FFF") - - // prefix treats it as a Swift Integer Literal, inserting underscores for splits - XCTAssertEqual(source2.stringValue(padToEvery: 2, splitEvery: 2, prefix: true), "0x0F_FF") - - // edge cases - XCTAssertEqual(source2.stringValue(padToEvery: 2, splitEvery: -1), "0FFF") - - - // octal - - let source3 = 0o123456.octal - - XCTAssertEqual(source3.stringValue(padToEvery: 0, splitEvery: 0), "123456") - XCTAssertEqual(source3.stringValue(padToEvery: 1, splitEvery: 1), "1 2 3 4 5 6") - XCTAssertEqual(source3.stringValue(padToEvery: 2, splitEvery: 2), "12 34 56") - XCTAssertEqual(source3.stringValue(padToEvery: 4, splitEvery: 3), "00 123 456") - - // prefix treats it as a Swift Integer Literal, inserting underscores for splits - XCTAssertEqual(source3.stringValue(padToEvery: 2, splitEvery: 2, prefix: true), "0o12_34_56") - - // edge cases - XCTAssertEqual(source3.stringValue(padToEvery: 2, splitEvery: -1), "123456") - - } - + + func testRadix_stringValue_Get() { + + // binary + + XCTAssertEqual(0b0.binary.stringValue, "0") + XCTAssertEqual(0b1100_1100.binary.stringValue, "11001100") + + // hex + + XCTAssertEqual(0x00.hex.stringValue, "0") + XCTAssertEqual(0xFF.hex.stringValue, "FF") + + // octal + + XCTAssertEqual(0x000.octal.stringValue, "0") + XCTAssertEqual(0o123.octal.stringValue, "123") + + } + + func testRadix_stringValue_Get_Extents() { + + // binary + + XCTAssertEqual(UInt.max.binary.stringValue, + "1111111111111111111111111111111111111111111111111111111111111111") + + // hex + + XCTAssertEqual(UInt.max.hex.stringValue, + "FFFFFFFFFFFFFFFF") + + // octal + + XCTAssertEqual(UInt.max.octal.stringValue, + "1777777777777777777777") + + } + + func testRadix_stringValue_Set() { + + // binary + + var source1 = 0b0.binary + + source1.stringValue = "11001100" // no prefix + XCTAssertEqual(source1.value, 0b1100_1100) + + source1.stringValue = "0b00110011" // with prefix + XCTAssertEqual(source1.value, 0b0011_0011) + + // hex + + var source2 = 0x00.hex + + source2.stringValue = "FF" // no prefix + XCTAssertEqual(source2.value, 0xFF) + + source2.stringValue = "0xFFFF" // with prefix + XCTAssertEqual(source2.value, 0xFFFF) + + // octal + + var source3 = 0o0.octal + + source3.stringValue = "123" // no prefix + XCTAssertEqual(source3.value, 0o123) + + source3.stringValue = "0o1234" // with prefix + XCTAssertEqual(source3.value, 0o1234) + + } + + func testRadix_stringValue_Set_Extents() { + + // binary + + var source1 = UInt(0b0).binary + + source1.stringValue = "1111111111111111111111111111111111111111111111111111111111111111" + + XCTAssertEqual(source1.value, UInt.max) + + // hex + + var source2 = UInt(0x00).hex + + source2.stringValue = "FFFFFFFFFFFFFFFF" + + XCTAssertEqual(source2.value, UInt.max) + + // octal + + var source3 = UInt(0o0).octal + + source3.stringValue = "1777777777777777777777" + + XCTAssertEqual(source3.value, UInt.max) + + } + + func testRadix_stringValue_Set_Extents_EdgeCases() { + + // binary + + var source1 = 0b0.binary + + source1.stringValue = "1111111111111111111111111111111111111111111111111111111111111111" + + XCTAssertEqual(source1.value, 0) // string overflowed Int, set silently fails + + // hex + + var source2 = 0x00.hex + + source2.stringValue = "FFFFFFFFFFFFFFFF" + + XCTAssertEqual(source2.value, 0) // string overflowed Int, set silently fails + + // octal + + var source3 = 0o0.octal + + source3.stringValue = "1777777777777777777777" + + XCTAssertEqual(source3.value, 0) // string overflowed Int, set silently fails + + } + + func testRadix_stringValue_Prefix() { + + // binary + + XCTAssertEqual(0b0.binary.stringValue(prefix: true), "0b0") + XCTAssertEqual(0b0.binary.stringValue(prefix: false), "0") + + // hex + + XCTAssertEqual(0b0.hex.stringValue(prefix: true), "0x0") + XCTAssertEqual(0b0.hex.stringValue(prefix: false), "0") + + // octal + + XCTAssertEqual(0b0.octal.stringValue(prefix: true), "0o0") + XCTAssertEqual(0b0.octal.stringValue(prefix: false), "0") + + } + + func testRadix_stringValue_PadTo() { + + // binary + + let source1 = 0b0000_0111.binary + + XCTAssertEqual(source1.stringValue(padTo: 0), "111") + XCTAssertEqual(source1.stringValue(padTo: 1), "111") + XCTAssertEqual(source1.stringValue(padTo: 2), "111") + XCTAssertEqual(source1.stringValue(padTo: 6), "000111") + + XCTAssertEqual(source1.stringValue(padTo: 2, prefix: true), "0b111") + XCTAssertEqual(source1.stringValue(padTo: 6, prefix: true), "0b000111") + + // edge cases + XCTAssertEqual(source1.stringValue(padTo: -1), "111") + + + // hex + + let source2 = 0x0F.hex + + XCTAssertEqual(source2.stringValue(padTo: 0), "F") + XCTAssertEqual(source2.stringValue(padTo: 1), "F") + XCTAssertEqual(source2.stringValue(padTo: 2), "0F") + XCTAssertEqual(source2.stringValue(padTo: 6), "00000F") + + XCTAssertEqual(source2.stringValue(padTo: 2, prefix: true), "0x0F") + + // edge cases + XCTAssertEqual(source2.stringValue(padTo: -1), "F") + + + // octal + + let source3 = 0o17.octal + + XCTAssertEqual(source3.stringValue(padTo: 0), "17") + XCTAssertEqual(source3.stringValue(padTo: 1), "17") + XCTAssertEqual(source3.stringValue(padTo: 2), "17") + XCTAssertEqual(source3.stringValue(padTo: 6), "000017") + + XCTAssertEqual(source3.stringValue(padTo: 2, prefix: true), "0o17") + + // edge cases + XCTAssertEqual(source3.stringValue(padTo: -1), "17") + + } + + func testRadix_stringValue_PadTo_EdgeCases() { + + // ensure padding of 0 or a negative value defaults to an internal padding of 1 + + // binary + + XCTAssertEqual(0b0.binary.stringValue(padTo: -1), "0") + XCTAssertEqual(0b0.binary.stringValue(padTo: 0), "0") + + // hex + + XCTAssertEqual(0x0.hex.stringValue(padTo: -1), "0") + XCTAssertEqual(0x0.hex.stringValue(padTo: 0), "0") + + // octal + + XCTAssertEqual(0o0.hex.stringValue(padTo: -1), "0") + XCTAssertEqual(0o0.hex.stringValue(padTo: 0), "0") + + } + + func testRadix_stringValue_PadTo_SplitEvery() { + + // binary + + let source1 = 0b0000_0111.binary + + XCTAssertEqual(source1.stringValue(padTo: 0, splitEvery: 0), "111") + XCTAssertEqual(source1.stringValue(padTo: 1, splitEvery: 1), "1 1 1") + XCTAssertEqual(source1.stringValue(padTo: 1, splitEvery: 2), "1 11") + XCTAssertEqual(source1.stringValue(padTo: 2, splitEvery: 2), "1 11") + XCTAssertEqual(source1.stringValue(padTo: 4, splitEvery: 3), "0 111") + + // prefix treats it as a Swift Integer Literal, inserting underscores for splits + XCTAssertEqual(source1.stringValue(padTo: 8, splitEvery: 2, prefix: true), "0b00_00_01_11") + + // edge cases + XCTAssertEqual(source1.stringValue(padTo: 2, splitEvery: -1), "111") + + + // hex + + let source2 = 0xF0F.hex + + XCTAssertEqual(source2.stringValue(padTo: 0, splitEvery: 0), "F0F") + XCTAssertEqual(source2.stringValue(padTo: 1, splitEvery: 1), "F 0 F") + XCTAssertEqual(source2.stringValue(padTo: 2, splitEvery: 2), "F 0F") + XCTAssertEqual(source2.stringValue(padTo: 8, splitEvery: 3), "00 000 F0F") + + // prefix treats it as a Swift Integer Literal, inserting underscores for splits + XCTAssertEqual(source2.stringValue(padTo: 8, splitEvery: 2, prefix: true), "0x00_00_0F_0F") + + // edge cases + XCTAssertEqual(source2.stringValue(padTo: 2, splitEvery: -1), "F0F") + + + // octal + + let source3 = 0o7.octal + + XCTAssertEqual(source3.stringValue(padTo: 0, splitEvery: 0), "7") + XCTAssertEqual(source3.stringValue(padTo: 1, splitEvery: 1), "7") + XCTAssertEqual(source3.stringValue(padTo: 2, splitEvery: 2), "07") + XCTAssertEqual(source3.stringValue(padTo: 4, splitEvery: 3), "0 007") + + // prefix treats it as a Swift Integer Literal, inserting underscores for splits + XCTAssertEqual(source3.stringValue(padTo: 2, splitEvery: 2, prefix: true), "0o07") + + // edge cases + XCTAssertEqual(source3.stringValue(padTo: 2, splitEvery: -1), "07") + + } + + func testRadix_stringValue_PadToEvery() { + + // binary + + let source1 = 0b0001_1111.binary + + XCTAssertEqual(source1.stringValue(padToEvery: 0), "11111") + XCTAssertEqual(source1.stringValue(padToEvery: 1), "11111") + XCTAssertEqual(source1.stringValue(padToEvery: 2), "011111") + XCTAssertEqual(source1.stringValue(padToEvery: 4), "00011111") + + XCTAssertEqual(source1.stringValue(padToEvery: 2, prefix: true), "0b011111") + + // edge cases + XCTAssertEqual(source1.stringValue(padToEvery: -1), "11111") + + + // hex + + let source2 = 0xFFF.hex + + XCTAssertEqual(source2.stringValue(padToEvery: 0), "FFF") + XCTAssertEqual(source2.stringValue(padToEvery: 1), "FFF") + XCTAssertEqual(source2.stringValue(padToEvery: 2), "0FFF") + XCTAssertEqual(source2.stringValue(padToEvery: 6), "000FFF") + + XCTAssertEqual(source2.stringValue(padToEvery: 2, prefix: true), "0x0FFF") + + // edge cases + XCTAssertEqual(source2.stringValue(padToEvery: -1), "FFF") + + + // octal + + let source3 = 0o123456.octal + + XCTAssertEqual(source3.stringValue(padToEvery: 0), "123456") + XCTAssertEqual(source3.stringValue(padToEvery: 1), "123456") + XCTAssertEqual(source3.stringValue(padToEvery: 2), "123456") + XCTAssertEqual(source3.stringValue(padToEvery: 4), "00123456") + + XCTAssertEqual(source3.stringValue(padToEvery: 2, prefix: true), "0o123456") + + // edge cases + XCTAssertEqual(source3.stringValue(padToEvery: -1), "123456") + + } + + func testRadix_stringValue_PadToEvery_SplitEvery() { + + // binary + + let source1 = 0b0001_1111.binary + + XCTAssertEqual(source1.stringValue(padToEvery: 0, splitEvery: 0), "11111") + XCTAssertEqual(source1.stringValue(padToEvery: 1, splitEvery: 1), "1 1 1 1 1") + XCTAssertEqual(source1.stringValue(padToEvery: 1, splitEvery: 2), "1 11 11") + XCTAssertEqual(source1.stringValue(padToEvery: 2, splitEvery: 2), "01 11 11") + XCTAssertEqual(source1.stringValue(padToEvery: 4, splitEvery: 3), "00 011 111") + + // prefix treats it as a Swift Integer Literal, inserting underscores for splits + XCTAssertEqual(source1.stringValue(padToEvery: 2, splitEvery: 2, prefix: true), "0b01_11_11") + + // edge cases + XCTAssertEqual(source1.stringValue(padToEvery: 2, splitEvery: -1), "011111") + + + // hex + + let source2 = 0xFFF.hex + + XCTAssertEqual(source2.stringValue(padToEvery: 0, splitEvery: 0), "FFF") + XCTAssertEqual(source2.stringValue(padToEvery: 1, splitEvery: 1), "F F F") + XCTAssertEqual(source2.stringValue(padToEvery: 2, splitEvery: 2), "0F FF") + XCTAssertEqual(source2.stringValue(padToEvery: 8, splitEvery: 3), "00 000 FFF") + + // prefix treats it as a Swift Integer Literal, inserting underscores for splits + XCTAssertEqual(source2.stringValue(padToEvery: 2, splitEvery: 2, prefix: true), "0x0F_FF") + + // edge cases + XCTAssertEqual(source2.stringValue(padToEvery: 2, splitEvery: -1), "0FFF") + + + // octal + + let source3 = 0o123456.octal + + XCTAssertEqual(source3.stringValue(padToEvery: 0, splitEvery: 0), "123456") + XCTAssertEqual(source3.stringValue(padToEvery: 1, splitEvery: 1), "1 2 3 4 5 6") + XCTAssertEqual(source3.stringValue(padToEvery: 2, splitEvery: 2), "12 34 56") + XCTAssertEqual(source3.stringValue(padToEvery: 4, splitEvery: 3), "00 123 456") + + // prefix treats it as a Swift Integer Literal, inserting underscores for splits + XCTAssertEqual(source3.stringValue(padToEvery: 2, splitEvery: 2, prefix: true), "0o12_34_56") + + // edge cases + XCTAssertEqual(source3.stringValue(padToEvery: 2, splitEvery: -1), "123456") + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Tests.swift index a348a83..e32d9b9 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Tests.swift @@ -1,516 +1,512 @@ // // Radix Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-14. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_Init_Integer() { - - // basic init test, to check if it accepts a variety of integer types and radices - - // Int - - let radix1 = Radix(0xFF, base: 16) - - XCTAssertEqual(radix1.value.bitWidth, Int.bitWidth) - XCTAssertEqual(radix1.value, 0xFF) - XCTAssertEqual(radix1.base, 16) - - // UInt8 - - let radix2 = Radix(UInt8(0xFF), base: 8) - - XCTAssertEqual(radix2.value.bitWidth, UInt8.bitWidth) - XCTAssertEqual(radix2.value, 0xFF) - XCTAssertEqual(radix2.base, 8) - - } - - func testRadix_Init_String() { - - // basic init test - - // Int - - let radix1 = Radix("0xFF", base: 16) - - XCTAssertNotNil(radix1) - - XCTAssertEqual(radix1?.value.bitWidth, Int.bitWidth) - XCTAssertEqual(radix1?.value, 0xFF) - XCTAssertEqual(radix1?.base, 16) - - // UInt8 - - let radix2 = Radix("0xFF", base: 16) - - XCTAssertNotNil(radix2) - - XCTAssertEqual(radix2?.value.bitWidth, UInt8.bitWidth) - XCTAssertEqual(radix2?.value, 0xFF) - XCTAssertEqual(radix2?.base, 16) - - // edge cases - - // empty string - - let radix3 = Radix("", base: 16) - - XCTAssertNil(radix3) - - } - - func testRadix_Init_Integer_InvalidRadix() { - - // invalid radix (outside 2...36) - - let radix1 = Radix(123, base: -1) - - XCTAssertEqual(radix1.value, 123) - XCTAssertEqual(radix1.base, 10) // defaults to 10 if invalid - XCTAssertEqual(radix1.stringValue, "123") - - let radix2 = Radix(123, base: 0) - - XCTAssertEqual(radix2.value, 123) - XCTAssertEqual(radix2.base, 10) // defaults to 10 if invalid - XCTAssertEqual(radix2.stringValue, "123") - - let radix3 = Radix(123, base: 1) - - XCTAssertEqual(radix3.value, 123) - XCTAssertEqual(radix3.base, 10) // defaults to 10 if invalid - XCTAssertEqual(radix3.stringValue, "123") - - let radix4 = Radix(123, base: 37) - - XCTAssertEqual(radix4.value, 123) - XCTAssertEqual(radix4.base, 10) // defaults to 10 if invalid - XCTAssertEqual(radix4.stringValue, "123") - - } - - func testRadix_Init_String_InvalidRadix() { - - // invalid radix (outside 2...36) - - XCTAssertNil(Radix("1", base: -1)) - XCTAssertNil(Radix("1", base: 0)) - XCTAssertNil(Radix("1", base: 1)) - XCTAssertNil(Radix("1", base: 37)) - - } - - func testRadix_Init_Integer_NonStandardRadix() { - - // base-2 is binary; skip testing it here - - let radix3 = Radix(123, base: 3) - - XCTAssertEqual(radix3.value, 123) - XCTAssertEqual(radix3.base, 3) - XCTAssertEqual(radix3.stringValue, "11120") - - let radix4 = Radix(123, base: 4) - - XCTAssertEqual(radix4.value, 123) - XCTAssertEqual(radix4.base, 4) - XCTAssertEqual(radix4.stringValue, "1323") - - let radix5 = Radix(123, base: 5) - - XCTAssertEqual(radix5.value, 123) - XCTAssertEqual(radix5.base, 5) - XCTAssertEqual(radix5.stringValue, "443") - - let radix6 = Radix(123, base: 6) - - XCTAssertEqual(radix6.value, 123) - XCTAssertEqual(radix6.base, 6) - XCTAssertEqual(radix6.stringValue, "323") - - let radix7 = Radix(123, base: 7) - - XCTAssertEqual(radix7.value, 123) - XCTAssertEqual(radix7.base, 7) - XCTAssertEqual(radix7.stringValue, "234") - - // base-8 is octal; skip testing it here - - let radix9 = Radix(123, base: 9) - - XCTAssertEqual(radix9.value, 123) - XCTAssertEqual(radix9.base, 9) - XCTAssertEqual(radix9.stringValue, "146") - - let radix10 = Radix(123, base: 10) - - XCTAssertEqual(radix10.value, 123) - XCTAssertEqual(radix10.base, 10) - XCTAssertEqual(radix10.stringValue, "123") - - let radix11 = Radix(123, base: 11) - - XCTAssertEqual(radix11.value, 123) - XCTAssertEqual(radix11.base, 11) - XCTAssertEqual(radix11.stringValue, "102") - - let radix12 = Radix(123, base: 12) - - XCTAssertEqual(radix12.value, 123) - XCTAssertEqual(radix12.base, 12) - XCTAssertEqual(radix12.stringValue, "A3") - - let radix13 = Radix(123, base: 13) - - XCTAssertEqual(radix13.value, 123) - XCTAssertEqual(radix13.base, 13) - XCTAssertEqual(radix13.stringValue, "96") - - let radix14 = Radix(123, base: 14) - - XCTAssertEqual(radix14.value, 123) - XCTAssertEqual(radix14.base, 14) - XCTAssertEqual(radix14.stringValue, "8B") - - let radix15 = Radix(123, base: 15) - - XCTAssertEqual(radix15.value, 123) - XCTAssertEqual(radix15.base, 15) - XCTAssertEqual(radix15.stringValue, "83") - - // base-16 is hex; skip testing it here - - let radix17 = Radix(123, base: 17) - - XCTAssertEqual(radix17.value, 123) - XCTAssertEqual(radix17.base, 17) - XCTAssertEqual(radix17.stringValue, "74") - - let radix18 = Radix(123, base: 18) - - XCTAssertEqual(radix18.value, 123) - XCTAssertEqual(radix18.base, 18) - XCTAssertEqual(radix18.stringValue, "6F") - - let radix19 = Radix(123, base: 19) - - XCTAssertEqual(radix19.value, 123) - XCTAssertEqual(radix19.base, 19) - XCTAssertEqual(radix19.stringValue, "69") - - let radix20 = Radix(123, base: 20) - - XCTAssertEqual(radix20.value, 123) - XCTAssertEqual(radix20.base, 20) - XCTAssertEqual(radix20.stringValue, "63") - - let radix21 = Radix(123, base: 21) - - XCTAssertEqual(radix21.value, 123) - XCTAssertEqual(radix21.base, 21) - XCTAssertEqual(radix21.stringValue, "5I") - - let radix22 = Radix(123, base: 22) - - XCTAssertEqual(radix22.value, 123) - XCTAssertEqual(radix22.base, 22) - XCTAssertEqual(radix22.stringValue, "5D") - - let radix23 = Radix(123, base: 23) - - XCTAssertEqual(radix23.value, 123) - XCTAssertEqual(radix23.base, 23) - XCTAssertEqual(radix23.stringValue, "58") - - let radix24 = Radix(123, base: 24) - - XCTAssertEqual(radix24.value, 123) - XCTAssertEqual(radix24.base, 24) - XCTAssertEqual(radix24.stringValue, "53") - - let radix25 = Radix(123, base: 25) - - XCTAssertEqual(radix25.value, 123) - XCTAssertEqual(radix25.base, 25) - XCTAssertEqual(radix25.stringValue, "4N") - - let radix26 = Radix(123, base: 26) - - XCTAssertEqual(radix26.value, 123) - XCTAssertEqual(radix26.base, 26) - XCTAssertEqual(radix26.stringValue, "4J") - - let radix27 = Radix(123, base: 27) - - XCTAssertEqual(radix27.value, 123) - XCTAssertEqual(radix27.base, 27) - XCTAssertEqual(radix27.stringValue, "4F") - - let radix28 = Radix(123, base: 28) - - XCTAssertEqual(radix28.value, 123) - XCTAssertEqual(radix28.base, 28) - XCTAssertEqual(radix28.stringValue, "4B") - - let radix29 = Radix(123, base: 29) - - XCTAssertEqual(radix29.value, 123) - XCTAssertEqual(radix29.base, 29) - XCTAssertEqual(radix29.stringValue, "47") - - let radix30 = Radix(123, base: 30) - - XCTAssertEqual(radix30.value, 123) - XCTAssertEqual(radix30.base, 30) - XCTAssertEqual(radix30.stringValue, "43") - - let radix31 = Radix(123, base: 31) - - XCTAssertEqual(radix31.value, 123) - XCTAssertEqual(radix31.base, 31) - XCTAssertEqual(radix31.stringValue, "3U") - - let radix32 = Radix(123, base: 32) - - XCTAssertEqual(radix32.value, 123) - XCTAssertEqual(radix32.base, 32) - XCTAssertEqual(radix32.stringValue, "3R") - - let radix33 = Radix(123, base: 33) - - XCTAssertEqual(radix33.value, 123) - XCTAssertEqual(radix33.base, 33) - XCTAssertEqual(radix33.stringValue, "3O") - - let radix34 = Radix(123, base: 34) - - XCTAssertEqual(radix34.value, 123) - XCTAssertEqual(radix34.base, 34) - XCTAssertEqual(radix34.stringValue, "3L") - - let radix35 = Radix(123, base: 35) - - XCTAssertEqual(radix35.value, 123) - XCTAssertEqual(radix35.base, 35) - XCTAssertEqual(radix35.stringValue, "3I") - - let radix36 = Radix(123, base: 36) - - XCTAssertEqual(radix36.value, 123) - XCTAssertEqual(radix36.base, 36) - XCTAssertEqual(radix36.stringValue, "3F") - - } - - func testRadix_Init_String_NonStandardRadix() { - - // base-2 is binary; skip testing it here - - let radix3 = Radix("11120", base: 3) - - XCTAssertNotNil(radix3) - XCTAssertEqual(radix3?.value, 123) - XCTAssertEqual(radix3?.base, 3) - - let radix4 = Radix("1323", base: 4) - - XCTAssertNotNil(radix4) - XCTAssertEqual(radix4?.value, 123) - XCTAssertEqual(radix4?.base, 4) - - let radix5 = Radix("443", base: 5) - - XCTAssertNotNil(radix5) - XCTAssertEqual(radix5?.value, 123) - XCTAssertEqual(radix5?.base, 5) - - let radix6 = Radix("323", base: 6) - - XCTAssertNotNil(radix6) - XCTAssertEqual(radix6?.value, 123) - XCTAssertEqual(radix6?.base, 6) - - let radix7 = Radix("234", base: 7) - - XCTAssertNotNil(radix7) - XCTAssertEqual(radix7?.value, 123) - XCTAssertEqual(radix7?.base, 7) - - // base-8 is octal; skip testing it here - - let radix9 = Radix("146", base: 9) - - XCTAssertNotNil(radix9) - XCTAssertEqual(radix9?.value, 123) - XCTAssertEqual(radix9?.base, 9) - - let radix10 = Radix("123", base: 10) - - XCTAssertNotNil(radix10) - XCTAssertEqual(radix10?.value, 123) - XCTAssertEqual(radix10?.base, 10) - - let radix11 = Radix("102", base: 11) - - XCTAssertNotNil(radix11) - XCTAssertEqual(radix11?.value, 123) - XCTAssertEqual(radix11?.base, 11) - - let radix12 = Radix("A3", base: 12) - - XCTAssertNotNil(radix12) - XCTAssertEqual(radix12?.value, 123) - XCTAssertEqual(radix12?.base, 12) - - let radix13 = Radix("96", base: 13) - - XCTAssertNotNil(radix13) - XCTAssertEqual(radix13?.value, 123) - XCTAssertEqual(radix13?.base, 13) - - let radix14 = Radix("8B", base: 14) - - XCTAssertNotNil(radix14) - XCTAssertEqual(radix14?.value, 123) - XCTAssertEqual(radix14?.base, 14) - - let radix15 = Radix("83", base: 15) - - // base-16 is hex; skip testing it here - - XCTAssertNotNil(radix15) - XCTAssertEqual(radix15?.value, 123) - XCTAssertEqual(radix15?.base, 15) - - let radix17 = Radix("74", base: 17) - - XCTAssertNotNil(radix17) - XCTAssertEqual(radix17?.value, 123) - XCTAssertEqual(radix17?.base, 17) - - let radix18 = Radix("6F", base: 18) - - XCTAssertNotNil(radix18) - XCTAssertEqual(radix18?.value, 123) - XCTAssertEqual(radix18?.base, 18) - - let radix19 = Radix("69", base: 19) - - XCTAssertNotNil(radix19) - XCTAssertEqual(radix19?.value, 123) - XCTAssertEqual(radix19?.base, 19) - - let radix20 = Radix("63", base: 20) - - XCTAssertNotNil(radix20) - XCTAssertEqual(radix20?.value, 123) - XCTAssertEqual(radix20?.base, 20) - - let radix21 = Radix("5I", base: 21) - - XCTAssertNotNil(radix21) - XCTAssertEqual(radix21?.value, 123) - XCTAssertEqual(radix21?.base, 21) - - let radix22 = Radix("5D", base: 22) - - XCTAssertNotNil(radix22) - XCTAssertEqual(radix22?.value, 123) - XCTAssertEqual(radix22?.base, 22) - - let radix23 = Radix("58", base: 23) - - XCTAssertNotNil(radix23) - XCTAssertEqual(radix23?.value, 123) - XCTAssertEqual(radix23?.base, 23) - - let radix24 = Radix("53", base: 24) - - XCTAssertNotNil(radix24) - XCTAssertEqual(radix24?.value, 123) - XCTAssertEqual(radix24?.base, 24) - - let radix25 = Radix("4N", base: 25) - - XCTAssertNotNil(radix25) - XCTAssertEqual(radix25?.value, 123) - XCTAssertEqual(radix25?.base, 25) - - let radix26 = Radix("4J", base: 26) - - XCTAssertNotNil(radix26) - XCTAssertEqual(radix26?.value, 123) - XCTAssertEqual(radix26?.base, 26) - - let radix27 = Radix("4F", base: 27) - - XCTAssertNotNil(radix27) - XCTAssertEqual(radix27?.value, 123) - XCTAssertEqual(radix27?.base, 27) - - let radix28 = Radix("4B", base: 28) - - XCTAssertNotNil(radix28) - XCTAssertEqual(radix28?.value, 123) - XCTAssertEqual(radix28?.base, 28) - - let radix29 = Radix("47", base: 29) - - XCTAssertNotNil(radix29) - XCTAssertEqual(radix29?.value, 123) - XCTAssertEqual(radix29?.base, 29) - - let radix30 = Radix("43", base: 30) - - XCTAssertNotNil(radix30) - XCTAssertEqual(radix30?.value, 123) - XCTAssertEqual(radix30?.base, 30) - - let radix31 = Radix("3U", base: 31) - - XCTAssertNotNil(radix31) - XCTAssertEqual(radix31?.value, 123) - XCTAssertEqual(radix31?.base, 31) - - let radix32 = Radix("3R", base: 32) - - XCTAssertNotNil(radix32) - XCTAssertEqual(radix32?.value, 123) - XCTAssertEqual(radix32?.base, 32) - - let radix33 = Radix("3O", base: 33) - - XCTAssertNotNil(radix33) - XCTAssertEqual(radix33?.value, 123) - XCTAssertEqual(radix33?.base, 33) - - let radix34 = Radix("3L", base: 34) - - XCTAssertNotNil(radix34) - XCTAssertEqual(radix34?.value, 123) - XCTAssertEqual(radix34?.base, 34) - - let radix35 = Radix("3I", base: 35) - - XCTAssertNotNil(radix35) - XCTAssertEqual(radix35?.value, 123) - XCTAssertEqual(radix35?.base, 35) - - let radix36 = Radix("3F", base: 36) - - XCTAssertNotNil(radix36) - XCTAssertEqual(radix36?.value, 123) - XCTAssertEqual(radix36?.base, 36) - - } - + + func testRadix_Init_Integer() { + + // basic init test, to check if it accepts a variety of integer types and radices + + // Int + + let radix1 = Radix(0xFF, base: 16) + + XCTAssertEqual(radix1.value.bitWidth, Int.bitWidth) + XCTAssertEqual(radix1.value, 0xFF) + XCTAssertEqual(radix1.base, 16) + + // UInt8 + + let radix2 = Radix(UInt8(0xFF), base: 8) + + XCTAssertEqual(radix2.value.bitWidth, UInt8.bitWidth) + XCTAssertEqual(radix2.value, 0xFF) + XCTAssertEqual(radix2.base, 8) + + } + + func testRadix_Init_String() { + + // basic init test + + // Int + + let radix1 = Radix("0xFF", base: 16) + + XCTAssertNotNil(radix1) + + XCTAssertEqual(radix1?.value.bitWidth, Int.bitWidth) + XCTAssertEqual(radix1?.value, 0xFF) + XCTAssertEqual(radix1?.base, 16) + + // UInt8 + + let radix2 = Radix("0xFF", base: 16) + + XCTAssertNotNil(radix2) + + XCTAssertEqual(radix2?.value.bitWidth, UInt8.bitWidth) + XCTAssertEqual(radix2?.value, 0xFF) + XCTAssertEqual(radix2?.base, 16) + + // edge cases + + // empty string + + let radix3 = Radix("", base: 16) + + XCTAssertNil(radix3) + + } + + func testRadix_Init_Integer_InvalidRadix() { + + // invalid radix (outside 2...36) + + let radix1 = Radix(123, base: -1) + + XCTAssertEqual(radix1.value, 123) + XCTAssertEqual(radix1.base, 10) // defaults to 10 if invalid + XCTAssertEqual(radix1.stringValue, "123") + + let radix2 = Radix(123, base: 0) + + XCTAssertEqual(radix2.value, 123) + XCTAssertEqual(radix2.base, 10) // defaults to 10 if invalid + XCTAssertEqual(radix2.stringValue, "123") + + let radix3 = Radix(123, base: 1) + + XCTAssertEqual(radix3.value, 123) + XCTAssertEqual(radix3.base, 10) // defaults to 10 if invalid + XCTAssertEqual(radix3.stringValue, "123") + + let radix4 = Radix(123, base: 37) + + XCTAssertEqual(radix4.value, 123) + XCTAssertEqual(radix4.base, 10) // defaults to 10 if invalid + XCTAssertEqual(radix4.stringValue, "123") + + } + + func testRadix_Init_String_InvalidRadix() { + + // invalid radix (outside 2...36) + + XCTAssertNil(Radix("1", base: -1)) + XCTAssertNil(Radix("1", base: 0)) + XCTAssertNil(Radix("1", base: 1)) + XCTAssertNil(Radix("1", base: 37)) + + } + + func testRadix_Init_Integer_NonStandardRadix() { + + // base-2 is binary; skip testing it here + + let radix3 = Radix(123, base: 3) + + XCTAssertEqual(radix3.value, 123) + XCTAssertEqual(radix3.base, 3) + XCTAssertEqual(radix3.stringValue, "11120") + + let radix4 = Radix(123, base: 4) + + XCTAssertEqual(radix4.value, 123) + XCTAssertEqual(radix4.base, 4) + XCTAssertEqual(radix4.stringValue, "1323") + + let radix5 = Radix(123, base: 5) + + XCTAssertEqual(radix5.value, 123) + XCTAssertEqual(radix5.base, 5) + XCTAssertEqual(radix5.stringValue, "443") + + let radix6 = Radix(123, base: 6) + + XCTAssertEqual(radix6.value, 123) + XCTAssertEqual(radix6.base, 6) + XCTAssertEqual(radix6.stringValue, "323") + + let radix7 = Radix(123, base: 7) + + XCTAssertEqual(radix7.value, 123) + XCTAssertEqual(radix7.base, 7) + XCTAssertEqual(radix7.stringValue, "234") + + // base-8 is octal; skip testing it here + + let radix9 = Radix(123, base: 9) + + XCTAssertEqual(radix9.value, 123) + XCTAssertEqual(radix9.base, 9) + XCTAssertEqual(radix9.stringValue, "146") + + let radix10 = Radix(123, base: 10) + + XCTAssertEqual(radix10.value, 123) + XCTAssertEqual(radix10.base, 10) + XCTAssertEqual(radix10.stringValue, "123") + + let radix11 = Radix(123, base: 11) + + XCTAssertEqual(radix11.value, 123) + XCTAssertEqual(radix11.base, 11) + XCTAssertEqual(radix11.stringValue, "102") + + let radix12 = Radix(123, base: 12) + + XCTAssertEqual(radix12.value, 123) + XCTAssertEqual(radix12.base, 12) + XCTAssertEqual(radix12.stringValue, "A3") + + let radix13 = Radix(123, base: 13) + + XCTAssertEqual(radix13.value, 123) + XCTAssertEqual(radix13.base, 13) + XCTAssertEqual(radix13.stringValue, "96") + + let radix14 = Radix(123, base: 14) + + XCTAssertEqual(radix14.value, 123) + XCTAssertEqual(radix14.base, 14) + XCTAssertEqual(radix14.stringValue, "8B") + + let radix15 = Radix(123, base: 15) + + XCTAssertEqual(radix15.value, 123) + XCTAssertEqual(radix15.base, 15) + XCTAssertEqual(radix15.stringValue, "83") + + // base-16 is hex; skip testing it here + + let radix17 = Radix(123, base: 17) + + XCTAssertEqual(radix17.value, 123) + XCTAssertEqual(radix17.base, 17) + XCTAssertEqual(radix17.stringValue, "74") + + let radix18 = Radix(123, base: 18) + + XCTAssertEqual(radix18.value, 123) + XCTAssertEqual(radix18.base, 18) + XCTAssertEqual(radix18.stringValue, "6F") + + let radix19 = Radix(123, base: 19) + + XCTAssertEqual(radix19.value, 123) + XCTAssertEqual(radix19.base, 19) + XCTAssertEqual(radix19.stringValue, "69") + + let radix20 = Radix(123, base: 20) + + XCTAssertEqual(radix20.value, 123) + XCTAssertEqual(radix20.base, 20) + XCTAssertEqual(radix20.stringValue, "63") + + let radix21 = Radix(123, base: 21) + + XCTAssertEqual(radix21.value, 123) + XCTAssertEqual(radix21.base, 21) + XCTAssertEqual(radix21.stringValue, "5I") + + let radix22 = Radix(123, base: 22) + + XCTAssertEqual(radix22.value, 123) + XCTAssertEqual(radix22.base, 22) + XCTAssertEqual(radix22.stringValue, "5D") + + let radix23 = Radix(123, base: 23) + + XCTAssertEqual(radix23.value, 123) + XCTAssertEqual(radix23.base, 23) + XCTAssertEqual(radix23.stringValue, "58") + + let radix24 = Radix(123, base: 24) + + XCTAssertEqual(radix24.value, 123) + XCTAssertEqual(radix24.base, 24) + XCTAssertEqual(radix24.stringValue, "53") + + let radix25 = Radix(123, base: 25) + + XCTAssertEqual(radix25.value, 123) + XCTAssertEqual(radix25.base, 25) + XCTAssertEqual(radix25.stringValue, "4N") + + let radix26 = Radix(123, base: 26) + + XCTAssertEqual(radix26.value, 123) + XCTAssertEqual(radix26.base, 26) + XCTAssertEqual(radix26.stringValue, "4J") + + let radix27 = Radix(123, base: 27) + + XCTAssertEqual(radix27.value, 123) + XCTAssertEqual(radix27.base, 27) + XCTAssertEqual(radix27.stringValue, "4F") + + let radix28 = Radix(123, base: 28) + + XCTAssertEqual(radix28.value, 123) + XCTAssertEqual(radix28.base, 28) + XCTAssertEqual(radix28.stringValue, "4B") + + let radix29 = Radix(123, base: 29) + + XCTAssertEqual(radix29.value, 123) + XCTAssertEqual(radix29.base, 29) + XCTAssertEqual(radix29.stringValue, "47") + + let radix30 = Radix(123, base: 30) + + XCTAssertEqual(radix30.value, 123) + XCTAssertEqual(radix30.base, 30) + XCTAssertEqual(radix30.stringValue, "43") + + let radix31 = Radix(123, base: 31) + + XCTAssertEqual(radix31.value, 123) + XCTAssertEqual(radix31.base, 31) + XCTAssertEqual(radix31.stringValue, "3U") + + let radix32 = Radix(123, base: 32) + + XCTAssertEqual(radix32.value, 123) + XCTAssertEqual(radix32.base, 32) + XCTAssertEqual(radix32.stringValue, "3R") + + let radix33 = Radix(123, base: 33) + + XCTAssertEqual(radix33.value, 123) + XCTAssertEqual(radix33.base, 33) + XCTAssertEqual(radix33.stringValue, "3O") + + let radix34 = Radix(123, base: 34) + + XCTAssertEqual(radix34.value, 123) + XCTAssertEqual(radix34.base, 34) + XCTAssertEqual(radix34.stringValue, "3L") + + let radix35 = Radix(123, base: 35) + + XCTAssertEqual(radix35.value, 123) + XCTAssertEqual(radix35.base, 35) + XCTAssertEqual(radix35.stringValue, "3I") + + let radix36 = Radix(123, base: 36) + + XCTAssertEqual(radix36.value, 123) + XCTAssertEqual(radix36.base, 36) + XCTAssertEqual(radix36.stringValue, "3F") + + } + + func testRadix_Init_String_NonStandardRadix() { + + // base-2 is binary; skip testing it here + + let radix3 = Radix("11120", base: 3) + + XCTAssertNotNil(radix3) + XCTAssertEqual(radix3?.value, 123) + XCTAssertEqual(radix3?.base, 3) + + let radix4 = Radix("1323", base: 4) + + XCTAssertNotNil(radix4) + XCTAssertEqual(radix4?.value, 123) + XCTAssertEqual(radix4?.base, 4) + + let radix5 = Radix("443", base: 5) + + XCTAssertNotNil(radix5) + XCTAssertEqual(radix5?.value, 123) + XCTAssertEqual(radix5?.base, 5) + + let radix6 = Radix("323", base: 6) + + XCTAssertNotNil(radix6) + XCTAssertEqual(radix6?.value, 123) + XCTAssertEqual(radix6?.base, 6) + + let radix7 = Radix("234", base: 7) + + XCTAssertNotNil(radix7) + XCTAssertEqual(radix7?.value, 123) + XCTAssertEqual(radix7?.base, 7) + + // base-8 is octal; skip testing it here + + let radix9 = Radix("146", base: 9) + + XCTAssertNotNil(radix9) + XCTAssertEqual(radix9?.value, 123) + XCTAssertEqual(radix9?.base, 9) + + let radix10 = Radix("123", base: 10) + + XCTAssertNotNil(radix10) + XCTAssertEqual(radix10?.value, 123) + XCTAssertEqual(radix10?.base, 10) + + let radix11 = Radix("102", base: 11) + + XCTAssertNotNil(radix11) + XCTAssertEqual(radix11?.value, 123) + XCTAssertEqual(radix11?.base, 11) + + let radix12 = Radix("A3", base: 12) + + XCTAssertNotNil(radix12) + XCTAssertEqual(radix12?.value, 123) + XCTAssertEqual(radix12?.base, 12) + + let radix13 = Radix("96", base: 13) + + XCTAssertNotNil(radix13) + XCTAssertEqual(radix13?.value, 123) + XCTAssertEqual(radix13?.base, 13) + + let radix14 = Radix("8B", base: 14) + + XCTAssertNotNil(radix14) + XCTAssertEqual(radix14?.value, 123) + XCTAssertEqual(radix14?.base, 14) + + let radix15 = Radix("83", base: 15) + + // base-16 is hex; skip testing it here + + XCTAssertNotNil(radix15) + XCTAssertEqual(radix15?.value, 123) + XCTAssertEqual(radix15?.base, 15) + + let radix17 = Radix("74", base: 17) + + XCTAssertNotNil(radix17) + XCTAssertEqual(radix17?.value, 123) + XCTAssertEqual(radix17?.base, 17) + + let radix18 = Radix("6F", base: 18) + + XCTAssertNotNil(radix18) + XCTAssertEqual(radix18?.value, 123) + XCTAssertEqual(radix18?.base, 18) + + let radix19 = Radix("69", base: 19) + + XCTAssertNotNil(radix19) + XCTAssertEqual(radix19?.value, 123) + XCTAssertEqual(radix19?.base, 19) + + let radix20 = Radix("63", base: 20) + + XCTAssertNotNil(radix20) + XCTAssertEqual(radix20?.value, 123) + XCTAssertEqual(radix20?.base, 20) + + let radix21 = Radix("5I", base: 21) + + XCTAssertNotNil(radix21) + XCTAssertEqual(radix21?.value, 123) + XCTAssertEqual(radix21?.base, 21) + + let radix22 = Radix("5D", base: 22) + + XCTAssertNotNil(radix22) + XCTAssertEqual(radix22?.value, 123) + XCTAssertEqual(radix22?.base, 22) + + let radix23 = Radix("58", base: 23) + + XCTAssertNotNil(radix23) + XCTAssertEqual(radix23?.value, 123) + XCTAssertEqual(radix23?.base, 23) + + let radix24 = Radix("53", base: 24) + + XCTAssertNotNil(radix24) + XCTAssertEqual(radix24?.value, 123) + XCTAssertEqual(radix24?.base, 24) + + let radix25 = Radix("4N", base: 25) + + XCTAssertNotNil(radix25) + XCTAssertEqual(radix25?.value, 123) + XCTAssertEqual(radix25?.base, 25) + + let radix26 = Radix("4J", base: 26) + + XCTAssertNotNil(radix26) + XCTAssertEqual(radix26?.value, 123) + XCTAssertEqual(radix26?.base, 26) + + let radix27 = Radix("4F", base: 27) + + XCTAssertNotNil(radix27) + XCTAssertEqual(radix27?.value, 123) + XCTAssertEqual(radix27?.base, 27) + + let radix28 = Radix("4B", base: 28) + + XCTAssertNotNil(radix28) + XCTAssertEqual(radix28?.value, 123) + XCTAssertEqual(radix28?.base, 28) + + let radix29 = Radix("47", base: 29) + + XCTAssertNotNil(radix29) + XCTAssertEqual(radix29?.value, 123) + XCTAssertEqual(radix29?.base, 29) + + let radix30 = Radix("43", base: 30) + + XCTAssertNotNil(radix30) + XCTAssertEqual(radix30?.value, 123) + XCTAssertEqual(radix30?.base, 30) + + let radix31 = Radix("3U", base: 31) + + XCTAssertNotNil(radix31) + XCTAssertEqual(radix31?.value, 123) + XCTAssertEqual(radix31?.base, 31) + + let radix32 = Radix("3R", base: 32) + + XCTAssertNotNil(radix32) + XCTAssertEqual(radix32?.value, 123) + XCTAssertEqual(radix32?.base, 32) + + let radix33 = Radix("3O", base: 33) + + XCTAssertNotNil(radix33) + XCTAssertEqual(radix33?.value, 123) + XCTAssertEqual(radix33?.base, 33) + + let radix34 = Radix("3L", base: 34) + + XCTAssertNotNil(radix34) + XCTAssertEqual(radix34?.value, 123) + XCTAssertEqual(radix34?.base, 34) + + let radix35 = Radix("3I", base: 35) + + XCTAssertNotNil(radix35) + XCTAssertEqual(radix35?.value, 123) + XCTAssertEqual(radix35?.base, 35) + + let radix36 = Radix("3F", base: 36) + + XCTAssertNotNil(radix36) + XCTAssertEqual(radix36?.value, 123) + XCTAssertEqual(radix36?.base, 36) + + } + } diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Type Extensions Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Type Extensions Tests.swift index 63dd123..d8b9e42 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Type Extensions Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Type Extensions Tests.swift @@ -1,103 +1,99 @@ // // Radix Type Extensions Tests.swift -// SwiftRadix -// -// Created by Steffan Andrews on 2020-09-14. -// MIT License -// https://github.com/orchetect/SwiftRadix +// SwiftRadix • https://github.com/orchetect/SwiftRadix // import XCTest @testable import SwiftRadix extension SwiftRadixTests { - - func testRadix_TypeExtensions_BinaryInteger() { - - let validString = "1100" - - XCTAssertEqual( 0b1100 .radix(base: 2).stringValue, validString) - XCTAssertEqual(UInt (0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual( Int8 (0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual(UInt8 (0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual( Int16(0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual(UInt16(0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual( Int32(0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual(UInt32(0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual( Int64(0b1100).radix(base: 2).stringValue, validString) - XCTAssertEqual(UInt64(0b1100).radix(base: 2).stringValue, validString) - - } - - func testRadix_TypeExtensions_String() { - - let validValue = 0b1100 - - - let radix1 = "0b1100".radix(base: 2) // Int default - - XCTAssertNotNil(radix1) - - XCTAssertEqual(radix1?.value, Int(validValue)) - - let radix2 = "1100".radix(base: 2) // Int default - - XCTAssertNotNil(radix2) - - XCTAssertEqual(radix2?.value, Int(validValue)) - - let radix3 = "1100".radix(base: 2, as: Int32.self) - - XCTAssertNotNil(radix3) - - XCTAssertEqual(radix3?.value, Int32(validValue)) - - } - - func testRadix_TypeExtensions_StringArray() { - - let source = ["0000", "0b1111"] - - let radixArray1 = source.radix(base: 2) // Int default - - XCTAssertEqual(radixArray1.count, 2) - - XCTAssertEqual(radixArray1[0]?.value, 0b0000) - XCTAssertEqual(radixArray1[1]?.value, 0b1111) - - let radixArray2 = source.radix(base: 2, as: Int32.self) - - XCTAssertEqual(radixArray2.count, 2) - - XCTAssertEqual(radixArray2[0]?.value, Int32(0b0000)) - XCTAssertEqual(radixArray2[1]?.value, Int32(0b1111)) - - } - - func testRadix_TypeExtensions_BinIntCollection() { - - let source = [0b0000, 0b1111] - - let radixArray = source.radix(base: 2) - - XCTAssertEqual(radixArray.count, 2) - - XCTAssertEqual(radixArray[0].value, 0b0000) - XCTAssertEqual(radixArray[1].value, 0b1111) - - } - - func testRadix_TypeExtensions_Data() { - - let source = Data([0b0000, 0b1111]) - - let radixArray = source.radix(base: 2) - - XCTAssertEqual(radixArray.count, 2) - - XCTAssertEqual(radixArray[0].value, 0b0000) - XCTAssertEqual(radixArray[1].value, 0b1111) - - } - + + func testRadix_TypeExtensions_BinaryInteger() { + + let validString = "1100" + + XCTAssertEqual( 0b1100 .radix(base: 2).stringValue, validString) + XCTAssertEqual(UInt (0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual( Int8 (0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual(UInt8 (0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual( Int16(0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual(UInt16(0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual( Int32(0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual(UInt32(0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual( Int64(0b1100).radix(base: 2).stringValue, validString) + XCTAssertEqual(UInt64(0b1100).radix(base: 2).stringValue, validString) + + } + + func testRadix_TypeExtensions_String() { + + let validValue = 0b1100 + + + let radix1 = "0b1100".radix(base: 2) // Int default + + XCTAssertNotNil(radix1) + + XCTAssertEqual(radix1?.value, Int(validValue)) + + let radix2 = "1100".radix(base: 2) // Int default + + XCTAssertNotNil(radix2) + + XCTAssertEqual(radix2?.value, Int(validValue)) + + let radix3 = "1100".radix(base: 2, as: Int32.self) + + XCTAssertNotNil(radix3) + + XCTAssertEqual(radix3?.value, Int32(validValue)) + + } + + func testRadix_TypeExtensions_StringArray() { + + let source = ["0000", "0b1111"] + + let radixArray1 = source.radix(base: 2) // Int default + + XCTAssertEqual(radixArray1.count, 2) + + XCTAssertEqual(radixArray1[0]?.value, 0b0000) + XCTAssertEqual(radixArray1[1]?.value, 0b1111) + + let radixArray2 = source.radix(base: 2, as: Int32.self) + + XCTAssertEqual(radixArray2.count, 2) + + XCTAssertEqual(radixArray2[0]?.value, Int32(0b0000)) + XCTAssertEqual(radixArray2[1]?.value, Int32(0b1111)) + + } + + func testRadix_TypeExtensions_BinIntCollection() { + + let source = [0b0000, 0b1111] + + let radixArray = source.radix(base: 2) + + XCTAssertEqual(radixArray.count, 2) + + XCTAssertEqual(radixArray[0].value, 0b0000) + XCTAssertEqual(radixArray[1].value, 0b1111) + + } + + func testRadix_TypeExtensions_Data() { + + let source = Data([0b0000, 0b1111]) + + let radixArray = source.radix(base: 2) + + XCTAssertEqual(radixArray.count, 2) + + XCTAssertEqual(radixArray[0].value, 0b0000) + XCTAssertEqual(radixArray[1].value, 0b1111) + + } + }