Skip to content

Commit

Permalink
cleanup + tests rework
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Mar 30, 2023
1 parent f186e38 commit dee4a6c
Show file tree
Hide file tree
Showing 23 changed files with 321 additions and 432 deletions.
20 changes: 17 additions & 3 deletions Sources/ANKFoundation/ANKBitPatternConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,21 @@ public protocol ANKBitPatternConvertible<BitPattern> {
// MARK: + Details where Bit Pattern is Self
//=----------------------------------------------------------------------------=

extension ANKBitPatternConvertible where BitPattern == Self {
@_transparent public var bitPattern: BitPattern { self }
@_transparent public init(bitPattern source: some ANKBitPatternConvertible<BitPattern>) { self = source.bitPattern }
extension ANKBitPatternConvertible where BitPattern == Self {

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

@_transparent public init(bitPattern source: some ANKBitPatternConvertible<BitPattern>) {
self = source.bitPattern
}

//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=

@_transparent public var bitPattern: BitPattern {
self
}
}
46 changes: 23 additions & 23 deletions Sources/ANKFoundation/Models/ANKSigned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,11 @@
}

//*============================================================================*
// MARK: * ANK x Signed x Comparison
// MARK: * ANK x Signed x Comparisons
//*============================================================================*

extension ANKSigned {

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

@inlinable public static func ==(lhs: Self, rhs: Self) -> Bool {
//=--------------------------------------=
if lhs.sign != rhs.sign {
return lhs.isZero && rhs.isZero
}
//=--------------------------------------=
return lhs.magnitude == rhs.magnitude
}

@inlinable public static func <(lhs: Self, rhs: Self) -> Bool {
//=--------------------------------------=
if lhs.sign != rhs.sign {
return (lhs.sign != ANKSign.plus) && !(lhs.isZero && rhs.isZero)
}
//=--------------------------------------=
return (lhs.sign == ANKSign.plus) == (lhs.magnitude < rhs.magnitude)
}

//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -173,6 +151,28 @@ extension ANKSigned {
hasher.combine(self.magnitude)
hasher.combine(self.normalizedSign)
}

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

@inlinable public static func ==(lhs: Self, rhs: Self) -> Bool {
//=--------------------------------------=
if lhs.sign != rhs.sign {
return lhs.isZero && rhs.isZero
}
//=--------------------------------------=
return lhs.magnitude == rhs.magnitude
}

@inlinable public static func <(lhs: Self, rhs: Self) -> Bool {
//=--------------------------------------=
if lhs.sign != rhs.sign {
return (lhs.sign != ANKSign.plus) && !(lhs.isZero && rhs.isZero)
}
//=--------------------------------------=
return (lhs.sign == ANKSign.plus) == (lhs.magnitude < rhs.magnitude)
}
}

//*============================================================================*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import ANKFoundation

//*============================================================================*
// MARK: * ANK x Full Width x Comparison
// MARK: * ANK x Full Width x Comparisons
//*============================================================================*

extension ANKFullWidth {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@ import ANKFoundation
import XCTest

//*============================================================================*
// MARK: * Types x ANKFixedWidthInteger
// MARK: * ANK x Core Integer x Bits
//*============================================================================*

final class TypesTestsOnANKFixedWidthInteger: XCTestCase {
final class ANKCoreIntegerTestsOnBits: XCTestCase {

typealias T = any ANKFixedWidthInteger.Type
typealias S = any ANKSignedFixedWidthInteger.Type
typealias T = any (ANKCoreInteger).Type
typealias S = any (ANKCoreInteger & ANKSignedFixedWidthInteger).Type

//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=

let types: [T] = Types.ANKFixedWidthInteger

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testTypesCount() {
XCTAssertEqual(types.count, 10)
}
let types: [T] = typesOfANKCoreInteger

//=------------------------------------------------------------------------=
// MARK: Tests
Expand All @@ -54,17 +46,6 @@ final class TypesTestsOnANKFixedWidthInteger: XCTestCase {
XCTAssertEqual(type.max .leastSignificantBit, true )
}
}

//=------------------------------------------------------------------------=
// MARK: Tests x Signed
//=------------------------------------------------------------------------=

func testNegatedReportingOverflow() {
for case let type as S in types {
XCTAssertEqual(type.min.negatedReportingOverflow().overflow, true )
XCTAssertEqual(type.max.negatedReportingOverflow().overflow, false)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@ import ANKFoundation
import XCTest

//*============================================================================*
// MARK: * Types x ANKBinaryInteger
// MARK: * ANK x Core Integer x Comparisons
//*============================================================================*

final class TypesTestsOnANKBinaryInteger: XCTestCase {
final class ANKCoreIntegerTestsOnComparisons: XCTestCase {

typealias T = any ANKBinaryInteger.Type
typealias S = any ANKSignedInteger.Type
typealias T = any (ANKCoreInteger).Type
typealias S = any (ANKCoreInteger & ANKSignedFixedWidthInteger).Type

//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=

let types: [T] = Types.ANKBinaryInteger

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testTypesCount() {
XCTAssertEqual(types.count, 10)
}
let types: [T] = typesOfANKCoreInteger

//=------------------------------------------------------------------------=
// MARK: Tests
Expand All @@ -43,9 +35,7 @@ final class TypesTestsOnANKBinaryInteger: XCTestCase {
for type: T in types {
XCTAssertFalse(type.init( 1).isZero)
XCTAssertTrue (type.init( 0).isZero)

guard type is S else { continue }

guard type.isSigned else { continue }
XCTAssertFalse(type.init(-1).isZero)
}
}
Expand All @@ -54,9 +44,7 @@ final class TypesTestsOnANKBinaryInteger: XCTestCase {
for type: T in types {
XCTAssertFalse(type.init( 1).isLessThanZero)
XCTAssertFalse(type.init( 0).isLessThanZero)

guard type is S else { continue }

guard type.isSigned else { continue }
XCTAssertTrue (type.init(-1).isLessThanZero)
}
}
Expand All @@ -65,9 +53,7 @@ final class TypesTestsOnANKBinaryInteger: XCTestCase {
for type: T in types {
XCTAssertTrue (type.init( 1).isMoreThanZero)
XCTAssertFalse(type.init( 0).isMoreThanZero)

guard type is S else { continue }

guard type.isSigned else { continue }
XCTAssertFalse(type.init(-1).isMoreThanZero)
}
}
Expand Down
73 changes: 73 additions & 0 deletions Tests/ANKFoundationTests/ANKCoreInteger+Complements.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//=----------------------------------------------------------------------------=
// This source file is part of the AwesomeNumbersKit open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

#if DEBUG

import ANKFoundation
import XCTest

//*============================================================================*
// MARK: * ANK x Core Integer x Complements
//*============================================================================*

final class ANKCoreIntegerTestsOnComplements: XCTestCase {

typealias T = any (ANKCoreInteger).Type
typealias S = any (ANKCoreInteger & ANKSignedFixedWidthInteger).Type

//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=

let types: [T] = typesOfANKCoreInteger

//=------------------------------------------------------------------------=
// MARK: Tests x Bit Pattern
//=------------------------------------------------------------------------=

func testInitBitPattern() {
func whereIsSigned<T>(_ type: T.Type) where T: ANKCoreInteger {
XCTAssertEqual(T(bitPattern: T.Magnitude.min), T( 0))
XCTAssertEqual(T(bitPattern: T.Magnitude.max), T(-1))

XCTAssertEqual(T(bitPattern: (T.Magnitude(1) << (T.bitWidth - 1))), T.min)
XCTAssertEqual(T(bitPattern: ~(T.Magnitude(1) << (T.bitWidth - 1))), T.max)
}

func whereIsUnsigned<T>(_ type: T.Type) where T: ANKCoreInteger {
XCTAssertEqual(T(bitPattern: T.Magnitude.min), T.min)
XCTAssertEqual(T(bitPattern: T.Magnitude.max), T.max)
}

for type: T in types {
type.isSigned ? whereIsSigned(type) : whereIsUnsigned(type)
}
}

func testValueAsBitPattern() {
func whereIsSigned<T>(_ type: T.Type) where T: ANKCoreInteger {
XCTAssertEqual(T( 0).bitPattern, T.Magnitude.min)
XCTAssertEqual(T(-1).bitPattern, T.Magnitude.max)

XCTAssertEqual(T.min.bitPattern, (T.Magnitude(1) << (T.bitWidth - 1)))
XCTAssertEqual(T.max.bitPattern, ~(T.Magnitude(1) << (T.bitWidth - 1)))
}

func whereIsUnsigned<T>(_ type: T.Type) where T: ANKCoreInteger {
XCTAssertEqual(T.min.bitPattern, T.Magnitude.min)
XCTAssertEqual(T.max.bitPattern, T.Magnitude.max)
}

for type: T in types {
type.isSigned ? whereIsSigned(type) : whereIsUnsigned(type)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ import ANKFoundation
import XCTest

//*============================================================================*
// MARK: * Types x ANKLargeBinaryInteger
// MARK: * ANK x Core Integer x Negation
//*============================================================================*

final class TypesTestsOnANKLargeBinaryInteger: XCTestCase {
final class ANKCoreIntegerTestsOnNegation: XCTestCase {

typealias T = any ANKLargeBinaryInteger.Type
typealias S = any ANKSignedLargeBinaryInteger.Type
typealias T = any (ANKCoreInteger).Type
typealias S = any (ANKCoreInteger & ANKSignedFixedWidthInteger).Type

//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=

let types: [T] = Types.ANKLargeBinaryInteger
let types: [T] = typesOfANKCoreInteger

//=------------------------------------------------------------------------=
// MARK: Tests
// MARK: Tests x Negation
//=------------------------------------------------------------------------=

func testTypesCount() {
XCTAssertEqual(types.count, 2)
func testNegatedReportingOverflow() {
for case let type as S in types {
XCTAssertEqual(type.min.negatedReportingOverflow().overflow, true )
XCTAssertEqual(type.max.negatedReportingOverflow().overflow, false)
}
}
}

Expand Down
Loading

0 comments on commit dee4a6c

Please sign in to comment.