Skip to content

Commit

Permalink
Add: Shift<T>.one (#57) (#66).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Aug 24, 2024
1 parent f73dfc4 commit 3daf36d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/CoreKit/BinaryInteger+Exponentiation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension BinaryInteger {
power = power.times(multiplier.value)
}

exponent = exponent.down(Shift(unchecked: Count(unchecked: 1)))
exponent = exponent.down(Shift.one)

if exponent.isZero {
return (power).veto(multiplier.error)
Expand Down
8 changes: 8 additions & 0 deletions Sources/CoreKit/Models/Guarantees/Shift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
Self(unchecked: Count.zero)
}

/// A bit index of `1`.
///
/// - Note: Shifting by `1` is common and the minimum binary integer size is `8`.
///
@inlinable public static var one: Self {
Self(unchecked: Count(unchecked: 1))
}

/// The maximum bit index of the target binary integer type.
///
/// ┌──────┬───────────────────────┐
Expand Down
25 changes: 20 additions & 5 deletions Tests/UltimathnumTests/Guarantees/Shift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ final class ShiftTests: XCTestCase {
// MARK: Tests
//=------------------------------------------------------------------------=

func testStaticInstances() {
func whereTheValueIs<Value>(_ type: Value.Type) where Value: UnsignedInteger {
typealias T = Shift<Value>

Test().same(T.min, T(Count(raw: 0 as IX)))
Test().same(T.one, T(Count(raw: 1 as IX)))
Test().same(T.max, T(Count(raw: IX(raw: Value.size) - 1)))
}

for type in coreSystemsIntegersWhereIsUnsigned {
whereTheValueIs(type)
}

whereTheValueIs(DoubleInt<U8>.self)
whereTheValueIs(DoubleInt<UX>.self)

whereTheValueIs(InfiniInt<U8>.self)
whereTheValueIs(InfiniInt<UX>.self)
}

func testInitExactly() {
func whereTheValueIs<Value>(_ type: Value.Type) where Value: UnsignedInteger {
typealias T = Shift<Value>
Expand Down Expand Up @@ -159,7 +179,6 @@ final class ShiftTests: XCTestCase {
//=----------------------------------=
let size = IX(raw: Value.size)
//=----------------------------------=
Test().none(T.min.inverse())
Test().none(T(Count(0 as IX)).inverse())
Test().same(T(Count(1 as IX)).inverse(), Shift(Count(raw: size - 1)))
Test().same(T(Count(2 as IX)).inverse(), Shift(Count(raw: size - 2)))
Expand All @@ -171,7 +190,6 @@ final class ShiftTests: XCTestCase {

Test().nay (T.predicate(Value.size))
Test().none(T(exactly: Value.size))
Test().same(T.max.inverse(), Shift(Count(1)))
Test().same(T(Count(raw: size - 1)).inverse(), Shift(Count(1 as IX)))
Test().same(T(Count(raw: size - 2)).inverse(), Shift(Count(2 as IX)))
Test().same(T(Count(raw: size - 3)).inverse(), Shift(Count(3 as IX)))
Expand Down Expand Up @@ -205,7 +223,6 @@ final class ShiftTests: XCTestCase {
//=----------------------------------=
let size = IX(raw: Value.size)
//=----------------------------------=
Test().same(T.min.natural(), Fallible(0 as IX))
Test().same(T(Count(0 as IX)).natural(), Fallible(0 as IX))
Test().same(T(Count(1 as IX)).natural(), Fallible(1 as IX))
Test().same(T(Count(2 as IX)).natural(), Fallible(2 as IX))
Expand All @@ -217,7 +234,6 @@ final class ShiftTests: XCTestCase {

Test().nay (T.predicate(Value.size))
Test().none(T(exactly: Value.size))
Test().same(T.max.natural(), Fallible(size - 1, error: Value.size.isInfinite))
Test().same(T(Count(raw: size - 1)).natural(), Fallible(size - 1, error: Value.size.isInfinite))
Test().same(T(Count(raw: size - 2)).natural(), Fallible(size - 2, error: Value.size.isInfinite))
Test().same(T(Count(raw: size - 3)).natural(), Fallible(size - 3, error: Value.size.isInfinite))
Expand Down Expand Up @@ -256,7 +272,6 @@ final class ShiftTests: XCTestCase {
}

if Value.size.isInfinite {
Test().same(T.max.promoted(), Value.max)
Test().same(T(Count(raw: ~1 as IX)).promoted(), Value.max)
Test().same(T(Count(raw: ~2 as IX)).promoted(), Value.max - 1)
Test().same(T(Count(raw: ~3 as IX)).promoted(), Value.max - 2)
Expand Down

0 comments on commit 3daf36d

Please sign in to comment.