From 88170919318a9d6de5d6bb6d329719a590e0eaf2 Mon Sep 17 00:00:00 2001 From: yosshi4486 Date: Sat, 26 Dec 2020 10:00:27 +0900 Subject: [PATCH 1/5] test: Add tests for arithmetic. --- .../Rational/RationalArithmeticTests.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Tests/SternBroctTreeSwiftTests/Rational/RationalArithmeticTests.swift b/Tests/SternBroctTreeSwiftTests/Rational/RationalArithmeticTests.swift index 20e890f..ce10ead 100644 --- a/Tests/SternBroctTreeSwiftTests/Rational/RationalArithmeticTests.swift +++ b/Tests/SternBroctTreeSwiftTests/Rational/RationalArithmeticTests.swift @@ -130,4 +130,30 @@ class RationalArithmeticTests: XCTestCase { XCTAssertTrue(result.overflow) } + // This is benefit of adopting SignedNumeric. The protocol use `public init(integerLiteral value: IntegerLiteralType)` to adding integer. + func testAddingToIntegerNumber() { + let r = Rational8("2/3") + let result = r + 1 + XCTAssertEqual(result.description, "5/3") + } + + func testIntegerNumberAddingToRational() { + let r = Rational8("2/3") + let result = 1 + r + XCTAssertEqual(result.description, "5/3") + } + + func testMultiplyByIntegerNumber() { + let r = Rational8("2/3") + let result = r * 3 + XCTAssertEqual(result.description, "6/3") + } + + func testIntegerNumberMultiplyByRational() { + let r = Rational8("2/3") + let result = 3 * r + XCTAssertEqual(result.description, "6/3") + } + + } From a70adfcec03078883b2d689c9ce23bdff73d9709 Mon Sep 17 00:00:00 2001 From: yosshi4486 Date: Sat, 26 Dec 2020 10:27:55 +0900 Subject: [PATCH 2/5] refactor: Move elements to proper position. --- .../SternBroctTreeSwift/Core/Fraction.swift | 14 ++ .../SternBroctTreeSwift/Core/NSRational.swift | 2 +- .../SternBroctTreeSwift/Core/Rational.swift | 2 +- .../SternBroctTreeSwift/Core/Rational16.swift | 2 +- .../SternBroctTreeSwift/Core/Rational32.swift | 2 +- .../SternBroctTreeSwift/Core/Rational64.swift | 2 +- .../SternBroctTreeSwift/Core/Rational8.swift | 2 +- .../Core/SignedRational.swift | 226 ++++++++---------- .../Fraction/FractionTests.swift | 8 +- 9 files changed, 121 insertions(+), 139 deletions(-) diff --git a/Sources/SternBroctTreeSwift/Core/Fraction.swift b/Sources/SternBroctTreeSwift/Core/Fraction.swift index b8c8948..cac94e0 100644 --- a/Sources/SternBroctTreeSwift/Core/Fraction.swift +++ b/Sources/SternBroctTreeSwift/Core/Fraction.swift @@ -64,3 +64,17 @@ public protocol Fraction : SBTreeNode, SignedNumeric, Comparable, Hashable { func simplified() -> Self } + +extension Fraction where Number : BinaryInteger { + + /// Returns the interger value of mixed rational. + public var integerPartOfMixedFraction: Number { + return numerator / denominator + } + + /// Returns the numerator of mixed rational. + public var numeratorOfMixedFraction: Number { + return numerator % denominator + } + +} diff --git a/Sources/SternBroctTreeSwift/Core/NSRational.swift b/Sources/SternBroctTreeSwift/Core/NSRational.swift index b03502c..f74da18 100644 --- a/Sources/SternBroctTreeSwift/Core/NSRational.swift +++ b/Sources/SternBroctTreeSwift/Core/NSRational.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for reference semantics. The type stores and uses value rational internally. -public final class NSRational : NSObject, NSSecureCoding, SignedRational { +public final class NSRational : NSObject, NSSecureCoding, SignedRational, SBTreeNode { public typealias Number = Int diff --git a/Sources/SternBroctTreeSwift/Core/Rational.swift b/Sources/SternBroctTreeSwift/Core/Rational.swift index 5be5066..d75bfb4 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational.swift @@ -11,7 +11,7 @@ import Foundation /// /// Rational is specialized fraction which has interger denominator and numerator. /// On 32-bit platforms, Number is the same size as Int32, and on 64-bit platforms, Number is the same size as Int64. -public struct Rational : MutableSignedRational { +public struct Rational : MutableSignedRational, SBTreeNode { public typealias Number = Int diff --git a/Sources/SternBroctTreeSwift/Core/Rational16.swift b/Sources/SternBroctTreeSwift/Core/Rational16.swift index 0513226..a45d91f 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational16.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational16.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational16 : MutableSignedRational { +public struct Rational16 : MutableSignedRational, SBTreeNode { public typealias Number = Int16 diff --git a/Sources/SternBroctTreeSwift/Core/Rational32.swift b/Sources/SternBroctTreeSwift/Core/Rational32.swift index ea409ac..252d0c0 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational32.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational32.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational32 : MutableSignedRational { +public struct Rational32 : MutableSignedRational, SBTreeNode { public typealias Number = Int32 diff --git a/Sources/SternBroctTreeSwift/Core/Rational64.swift b/Sources/SternBroctTreeSwift/Core/Rational64.swift index fce4d92..b6fe1ed 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational64.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational64.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational64 : MutableSignedRational { +public struct Rational64 : MutableSignedRational, SBTreeNode { public typealias Number = Int64 diff --git a/Sources/SternBroctTreeSwift/Core/Rational8.swift b/Sources/SternBroctTreeSwift/Core/Rational8.swift index c4b2621..5220202 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational8.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational8.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational8 : MutableSignedRational { +public struct Rational8 : MutableSignedRational, SBTreeNode { public typealias Number = Int8 diff --git a/Sources/SternBroctTreeSwift/Core/SignedRational.swift b/Sources/SternBroctTreeSwift/Core/SignedRational.swift index 839b1c6..021472c 100644 --- a/Sources/SternBroctTreeSwift/Core/SignedRational.swift +++ b/Sources/SternBroctTreeSwift/Core/SignedRational.swift @@ -13,25 +13,6 @@ import Foundation /// like `addingReportingOverflow(:)` if you consider about overflow. public protocol SignedRational : Fraction, CustomFloatConvertible, CustomDoubleConvertible, CustomDecimalConvertible where Number : SignedInteger & FixedWidthInteger { - /// Returns a mediant from two fractions. - static func mediant(left: Self, right: Self) -> Self - - /// Retuns the result of mediant of the given left and right, along with a Boolean value indicating whether overflow occurred in the operation. - /// - /// - Parameters: - /// - left: The left value to mediant. - /// - right: The right value to mediant. - static func mediantReportingOverflow(left: Self, right: Self) -> (partialValue: Self, overflow: Bool) - - /// Returns a boolean value whether this and the other are adjacent. - /// - /// - Parameter other: The other concrete rational to determine adjacent. - /// - Returns: The two values are adjacent or not. - func isAdjacent(to other: Self) -> Bool - - /// Returns an array of R or L sequence which are backwardeded from this rational. - func backwardingMatrixSequence() -> [Matrix2x2] - /// Returns the sum of this value and the given value, along with a Boolean value indicating whether overflow occurred in the operation. /// /// - Parameter other: The value to add to this value. @@ -86,58 +67,6 @@ extension SignedRational { return commonFactor != 1 && commonFactor != -1 } - /// Returns a mediant from two fractions. - public static func mediant(left: Self, right: Self) -> Self { - let numerator = left.numerator + right.numerator - let denominator = left.denominator + right.denominator - return Self(numerator: numerator, denominator: denominator) - } - - /// Returns a mediant from two fractions. - public static func mediantReportingOverflow(left: Self, right: Self) -> (partialValue: Self, overflow: Bool) { - let (numeratorAddingResult, numeratorAddingOverflow) = left.numerator.addingReportingOverflow(right.numerator) - let (denominatorAddingResult, denominatorAddingOverflow) = left.denominator.addingReportingOverflow(right.denominator) - - if numeratorAddingOverflow || denominatorAddingOverflow { - return (Self(numerator: numeratorAddingResult, denominator: denominatorAddingResult), true) - } - - return (Self(numerator: numeratorAddingResult,denominator: denominatorAddingResult), false) - } - - public func backwardingMatrixSequence() -> [Matrix2x2] { - - // Start from R. - var mixPartSequence: [Number] = [] - var continueFraction = self - - while continueFraction.numerator > 1 || continueFraction.denominator > 1 { - let isEndIndeciesOfContinueFraction = continueFraction.numerator == 2 && continueFraction.denominator == 1 - if isEndIndeciesOfContinueFraction { - mixPartSequence.append(1) - break - } else { - mixPartSequence.append(continueFraction.mixedPart) - } - continueFraction = Self(numerator: continueFraction.denominator, denominator: continueFraction.mixedRemainder) - } - - let boxOfRLMatrixes: [[Matrix2x2]] = mixPartSequence.enumerated().compactMap({ index, value in - guard value > 0 else { - return nil - } - - let isEven = index % 2 == 0 - if isEven || index == 0 { - return Array(repeating: Matrix2x2.R, count: Int(value)) - } else { - return Array(repeating: Matrix2x2.L, count: Int(value)) - } - }) - - return boxOfRLMatrixes.flatMap({ $0 }) - } - public func comparedReportingOverflow(with other: Self) -> (partialValue: RationalComparisonResult, overflow: Bool) { let a = Number(numerator) let b = Number(denominator) @@ -170,60 +99,6 @@ extension SignedRational { } -/// Default implementation for SBTreeNode. -extension SignedRational { - - /// Returns a boolean value whether this and the other are adjacent. - /// - /// - Parameter other: The other concrete rational to determine adjacent. - /// - Returns: The two values are adjacent or not. - public func isAdjacent(to other: Self) -> Bool { - let (ad, adOverflow) = Number(numerator).multipliedReportingOverflow(by: Number(other.denominator)) - let (bc, bcOverflow) = Number(denominator).multipliedReportingOverflow(by: Number(other.numerator)) - - if !adOverflow && !bcOverflow { - return abs(ad - bc) == 1 - } else { - return false - } - } - - /// Returns simplicity of a rational. - /// - /// if **r=a/b** is in reduced form, **the simplicity of r** is defined to be **L(r)≡1/ab**. - public func simplicity() -> (partialValue: Self, overflow: Bool) { - let (ab, overflow) = Number(numerator).multipliedReportingOverflow(by: Number(denominator)) - return (Self("1/\(ab)"), overflow) - } - - /// Returns total of a rational. - /// - /// if **r=a/b** is in reduced form, define the total of r to be **t(r) ≡ a+b**. - public var total: Number { - return numerator + denominator - } - - /// Returns the interger value of mixed rational. - public var mixedPart: Number { - return numerator / denominator - } - - /// Returns the numerator of mixed rational. - public var mixedRemainder: Number { - return numerator % denominator - } - -} - -/// Default implementation for Equtable. -extension SignedRational { - - public static func == (lhs: Self, rhs: Self) -> Bool { - return lhs.numerator == rhs.numerator && lhs.denominator == rhs.denominator - } - -} - /// Default implementation for Comparable. extension SignedRational { @@ -251,10 +126,10 @@ extension SignedRational { public func hash(into hasher: inout Hasher) { - // In reduced form, SBTree node's fruction must be identified in the tree. - let x = simplified() - hasher.combine(x.numerator) - hasher.combine(x.denominator) + // Rational should be hashed in reduced form. + let reducedForm = simplified() + hasher.combine(reducedForm.numerator) + hasher.combine(reducedForm.denominator) } } @@ -330,6 +205,98 @@ extension SignedRational where Magnitude == Double, IntegerLiteralType == Number } +/// Default implementation for SBTreeNode. +extension SignedRational where Self : SBTreeNode { + + /// Returns a mediant from two fractions. + public static func mediant(left: Self, right: Self) -> Self { + let numerator = left.numerator + right.numerator + let denominator = left.denominator + right.denominator + return Self(numerator: numerator, denominator: denominator) + } + + /// Retuns the result of mediant of the given left and right, along with a Boolean value indicating whether overflow occurred in the operation. + /// + /// - Parameters: + /// - left: The left value to mediant. + /// - right: The right value to mediant. + public static func mediantReportingOverflow(left: Self, right: Self) -> (partialValue: Self, overflow: Bool) { + let (numeratorAddingResult, numeratorAddingOverflow) = left.numerator.addingReportingOverflow(right.numerator) + let (denominatorAddingResult, denominatorAddingOverflow) = left.denominator.addingReportingOverflow(right.denominator) + + if numeratorAddingOverflow || denominatorAddingOverflow { + return (Self(numerator: numeratorAddingResult, denominator: denominatorAddingResult), true) + } + + return (Self(numerator: numeratorAddingResult,denominator: denominatorAddingResult), false) + } + + /// Returns an array of R or L sequence which are backwardeded from this rational. + public func backwardingMatrixSequence() -> [Matrix2x2] { + + // Start from R. + var mixPartSequence: [Number] = [] + var continueFraction = self + + while continueFraction.numerator > 1 || continueFraction.denominator > 1 { + let isEndIndeciesOfContinueFraction = continueFraction.numerator == 2 && continueFraction.denominator == 1 + if isEndIndeciesOfContinueFraction { + mixPartSequence.append(1) + break + } else { + mixPartSequence.append(continueFraction.integerPartOfMixedFraction) + } + continueFraction = Self(numerator: continueFraction.denominator, denominator: continueFraction.numeratorOfMixedFraction) + } + + let boxOfRLMatrixes: [[Matrix2x2]] = mixPartSequence.enumerated().compactMap({ index, value in + guard value > 0 else { + return nil + } + + let isEven = index % 2 == 0 + if isEven || index == 0 { + return Array(repeating: Matrix2x2.R, count: Int(value)) + } else { + return Array(repeating: Matrix2x2.L, count: Int(value)) + } + }) + + return boxOfRLMatrixes.flatMap({ $0 }) + } + + /// Returns a boolean value whether this and the other are adjacent. + /// + /// - Parameter other: The other concrete rational to determine adjacent. + /// - Returns: The two values are adjacent or not. + public func isAdjacent(to other: Self) -> Bool { + let (ad, adOverflow) = Number(numerator).multipliedReportingOverflow(by: Number(other.denominator)) + let (bc, bcOverflow) = Number(denominator).multipliedReportingOverflow(by: Number(other.numerator)) + + if !adOverflow && !bcOverflow { + return abs(ad - bc) == 1 + } else { + return false + } + } + + /// Returns simplicity of a rational. + /// + /// if **r=a/b** is in reduced form, **the simplicity of r** is defined to be **L(r)≡1/ab**. + public func simplicity() -> (partialValue: Self, overflow: Bool) { + let (ab, overflow) = Number(numerator).multipliedReportingOverflow(by: Number(denominator)) + return (Self("1/\(ab)"), overflow) + } + + /// Returns total of a rational. + /// + /// if **r=a/b** is in reduced form, define the total of r to be **t(r) ≡ a+b**. + public var total: Number { + return numerator + denominator + } + +} + extension SignedRational { @@ -347,3 +314,4 @@ extension SignedRational { } + diff --git a/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift b/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift index a75c94b..6b199e7 100644 --- a/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift +++ b/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift @@ -167,22 +167,22 @@ class FractionTests: XCTestCase { func testMixedPart() { let f = Rational("11/3") - XCTAssertEqual(f.mixedPart, 3) + XCTAssertEqual(f.integerPart, 3) } func testMixedPartZero() { let f = Rational("3/11") - XCTAssertEqual(f.mixedPart, 0) + XCTAssertEqual(f.integerPart, 0) } func testMixedRemainder() { let f = Rational("11/3") - XCTAssertEqual(f.mixedRemainder, 2) + XCTAssertEqual(f.numeratorOfMixedFraction, 2) } func testMixedRemainderZer0() { let f = Rational("3/3") - XCTAssertEqual(f.mixedRemainder, 0) + XCTAssertEqual(f.numeratorOfMixedFraction, 0) } func testBackwardsMatrixSequnce1() { From e15a5728bb422c6c7cd71d96f5cfceb5442e6817 Mon Sep 17 00:00:00 2001 From: yosshi4486 Date: Sat, 26 Dec 2020 11:00:17 +0900 Subject: [PATCH 3/5] feat: Add fraction methods. --- .../SternBroctTreeSwift/Core/Fraction.swift | 23 +++++++ .../Fraction/FractionTests.swift | 63 ++++++++++++++++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Sources/SternBroctTreeSwift/Core/Fraction.swift b/Sources/SternBroctTreeSwift/Core/Fraction.swift index cac94e0..c5b03a1 100644 --- a/Sources/SternBroctTreeSwift/Core/Fraction.swift +++ b/Sources/SternBroctTreeSwift/Core/Fraction.swift @@ -77,4 +77,27 @@ extension Fraction where Number : BinaryInteger { return numerator % denominator } + /// Returns a Boolean value whether this fraction is unit fraction or not. + public var isUnit: Bool { + return numerator == 1 + } + + /// Returns a Boolean value whther this fraction is proper fraction or not. + public var isProper: Bool { + return numerator < denominator + } + + /// Returns a Boolean value whether this fraction is improper or not. + public var isImproper: Bool { + return denominator > numerator + } + + /// Returns a mixed fraction from this value. + /// + /// - Returns: The result touple cantains two values intergetPart and fraction. + /// The intergetPart is equal to `intergerPartOfMixedFraction` and the fraction numerator is equal to `numeratorOfMixedFraction` property. + public func mixed() -> (integerPart: Number, fraction: Self) { + return (integerPartOfMixedFraction, Self(numerator: numeratorOfMixedFraction, denominator: denominator)) + } + } diff --git a/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift b/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift index 6b199e7..183c3ad 100644 --- a/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift +++ b/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift @@ -167,12 +167,12 @@ class FractionTests: XCTestCase { func testMixedPart() { let f = Rational("11/3") - XCTAssertEqual(f.integerPart, 3) + XCTAssertEqual(f.integerPartOfMixedFraction, 3) } func testMixedPartZero() { let f = Rational("3/11") - XCTAssertEqual(f.integerPart, 0) + XCTAssertEqual(f.integerPartOfMixedFraction, 0) } func testMixedRemainder() { @@ -199,4 +199,63 @@ class FractionTests: XCTestCase { XCTAssertEqual(matrixSequence, [.R, .R, .L, .L, .R, .L]) } + func testUnitFraction() { + let rational = Rational("1/9") + XCTAssertTrue(rational.isUnit) + } + + func testNotUnitFraction() { + let rational = Rational("2/9") + XCTAssertFalse(rational.isUnit) + } + + func testNotUnitFractionWhenZero() { + let rational = Rational("0/9") + XCTAssertFalse(rational.isUnit) + } + + func testProperFraction() { + let rational = Rational("1/3") + XCTAssertTrue(rational.isProper) + } + + func testNotProperFraction() { + let rational = Rational("4/3") + XCTAssertFalse(rational.isProper) + } + + func testNotProperFractionWhenEqual() { + let rational = Rational("3/3") + XCTAssertFalse(rational.isProper) + } + + func testImproperFraction() { + let rational = Rational("3/4") + XCTAssertTrue(rational.isImproper) + } + + func testNotImproperFraction() { + let rational = Rational("3/2") + XCTAssertFalse(rational.isImproper) + } + + func testNotImproperFractionWhenEqual() { + let rational = Rational("3/3") + XCTAssertFalse(rational.isImproper) + } + + func testMixedForm() { + let rational = Rational("7/3") + let result = rational.mixed() + XCTAssertEqual(result.integerPart, 2) + XCTAssertEqual(result.fraction.description, "1/3") + } + + func testMixedFormIntegerZero() { + let rational = Rational("2/3") + let result = rational.mixed() + XCTAssertEqual(result.integerPart, 0) + XCTAssertEqual(result.fraction.description, "2/3") + } + } From 1e4590fb0f529311e38152c7f8a0bf5e20435567 Mon Sep 17 00:00:00 2001 From: yosshi4486 Date: Sat, 26 Dec 2020 11:08:45 +0900 Subject: [PATCH 4/5] refactor: Migrate to mixed() method. --- .../SternBroctTreeSwift/Core/Fraction.swift | 12 +---------- .../Core/SignedRational.swift | 6 ++++-- .../Fraction/FractionTests.swift | 20 ------------------- 3 files changed, 5 insertions(+), 33 deletions(-) diff --git a/Sources/SternBroctTreeSwift/Core/Fraction.swift b/Sources/SternBroctTreeSwift/Core/Fraction.swift index c5b03a1..52680f9 100644 --- a/Sources/SternBroctTreeSwift/Core/Fraction.swift +++ b/Sources/SternBroctTreeSwift/Core/Fraction.swift @@ -67,16 +67,6 @@ public protocol Fraction : SBTreeNode, SignedNumeric, Comparable, Hashable { extension Fraction where Number : BinaryInteger { - /// Returns the interger value of mixed rational. - public var integerPartOfMixedFraction: Number { - return numerator / denominator - } - - /// Returns the numerator of mixed rational. - public var numeratorOfMixedFraction: Number { - return numerator % denominator - } - /// Returns a Boolean value whether this fraction is unit fraction or not. public var isUnit: Bool { return numerator == 1 @@ -97,7 +87,7 @@ extension Fraction where Number : BinaryInteger { /// - Returns: The result touple cantains two values intergetPart and fraction. /// The intergetPart is equal to `intergerPartOfMixedFraction` and the fraction numerator is equal to `numeratorOfMixedFraction` property. public func mixed() -> (integerPart: Number, fraction: Self) { - return (integerPartOfMixedFraction, Self(numerator: numeratorOfMixedFraction, denominator: denominator)) + return (numerator / denominator, Self(numerator: numerator % denominator, denominator: denominator)) } } diff --git a/Sources/SternBroctTreeSwift/Core/SignedRational.swift b/Sources/SternBroctTreeSwift/Core/SignedRational.swift index 021472c..0208ae0 100644 --- a/Sources/SternBroctTreeSwift/Core/SignedRational.swift +++ b/Sources/SternBroctTreeSwift/Core/SignedRational.swift @@ -239,14 +239,16 @@ extension SignedRational where Self : SBTreeNode { var continueFraction = self while continueFraction.numerator > 1 || continueFraction.denominator > 1 { + let mixed = continueFraction.mixed() let isEndIndeciesOfContinueFraction = continueFraction.numerator == 2 && continueFraction.denominator == 1 if isEndIndeciesOfContinueFraction { mixPartSequence.append(1) break } else { - mixPartSequence.append(continueFraction.integerPartOfMixedFraction) + mixPartSequence.append(mixed.integerPart) } - continueFraction = Self(numerator: continueFraction.denominator, denominator: continueFraction.numeratorOfMixedFraction) + continueFraction = Self(numerator: mixed.fraction.denominator, + denominator: mixed.fraction.numerator) } let boxOfRLMatrixes: [[Matrix2x2]] = mixPartSequence.enumerated().compactMap({ index, value in diff --git a/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift b/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift index 183c3ad..4acdb4b 100644 --- a/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift +++ b/Tests/SternBroctTreeSwiftTests/Fraction/FractionTests.swift @@ -165,26 +165,6 @@ class FractionTests: XCTestCase { XCTAssertEqual(f.denominator, 3) } - func testMixedPart() { - let f = Rational("11/3") - XCTAssertEqual(f.integerPartOfMixedFraction, 3) - } - - func testMixedPartZero() { - let f = Rational("3/11") - XCTAssertEqual(f.integerPartOfMixedFraction, 0) - } - - func testMixedRemainder() { - let f = Rational("11/3") - XCTAssertEqual(f.numeratorOfMixedFraction, 2) - } - - func testMixedRemainderZer0() { - let f = Rational("3/3") - XCTAssertEqual(f.numeratorOfMixedFraction, 0) - } - func testBackwardsMatrixSequnce1() { let rational = Rational("3/11") let matrixSequence = rational.backwardingMatrixSequence() From c47a64108c3867552a44498822fb65ab81b1e16a Mon Sep 17 00:00:00 2001 From: yosshi4486 Date: Sat, 26 Dec 2020 11:11:53 +0900 Subject: [PATCH 5/5] refactor: Isolate SBTree inheritance from declear. --- Sources/SternBroctTreeSwift/Core/Rational.swift | 4 +++- Sources/SternBroctTreeSwift/Core/Rational16.swift | 3 ++- Sources/SternBroctTreeSwift/Core/Rational32.swift | 3 ++- Sources/SternBroctTreeSwift/Core/Rational64.swift | 3 ++- Sources/SternBroctTreeSwift/Core/Rational8.swift | 3 ++- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Sources/SternBroctTreeSwift/Core/Rational.swift b/Sources/SternBroctTreeSwift/Core/Rational.swift index d75bfb4..ec8cb68 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational.swift @@ -11,7 +11,7 @@ import Foundation /// /// Rational is specialized fraction which has interger denominator and numerator. /// On 32-bit platforms, Number is the same size as Int32, and on 64-bit platforms, Number is the same size as Int64. -public struct Rational : MutableSignedRational, SBTreeNode { +public struct Rational : MutableSignedRational { public typealias Number = Int @@ -44,3 +44,5 @@ public struct Rational : MutableSignedRational, SBTreeNode { } } + +extension Rational : SBTreeNode { } diff --git a/Sources/SternBroctTreeSwift/Core/Rational16.swift b/Sources/SternBroctTreeSwift/Core/Rational16.swift index a45d91f..1dfdd24 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational16.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational16.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational16 : MutableSignedRational, SBTreeNode { +public struct Rational16 : MutableSignedRational { public typealias Number = Int16 @@ -42,3 +42,4 @@ public struct Rational16 : MutableSignedRational, SBTreeNode { } +extension Rational16 : SBTreeNode { } diff --git a/Sources/SternBroctTreeSwift/Core/Rational32.swift b/Sources/SternBroctTreeSwift/Core/Rational32.swift index 252d0c0..d79d024 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational32.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational32.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational32 : MutableSignedRational, SBTreeNode { +public struct Rational32 : MutableSignedRational { public typealias Number = Int32 @@ -42,3 +42,4 @@ public struct Rational32 : MutableSignedRational, SBTreeNode { } +extension Rational32 : SBTreeNode { } diff --git a/Sources/SternBroctTreeSwift/Core/Rational64.swift b/Sources/SternBroctTreeSwift/Core/Rational64.swift index b6fe1ed..0f0a0b2 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational64.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational64.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational64 : MutableSignedRational, SBTreeNode { +public struct Rational64 : MutableSignedRational { public typealias Number = Int64 @@ -42,3 +42,4 @@ public struct Rational64 : MutableSignedRational, SBTreeNode { } +extension Rational64 : SBTreeNode { } diff --git a/Sources/SternBroctTreeSwift/Core/Rational8.swift b/Sources/SternBroctTreeSwift/Core/Rational8.swift index 5220202..c9bf8d0 100644 --- a/Sources/SternBroctTreeSwift/Core/Rational8.swift +++ b/Sources/SternBroctTreeSwift/Core/Rational8.swift @@ -8,7 +8,7 @@ import Foundation /// A rational type for value semantics. -public struct Rational8 : MutableSignedRational, SBTreeNode { +public struct Rational8 : MutableSignedRational { public typealias Number = Int8 @@ -42,3 +42,4 @@ public struct Rational8 : MutableSignedRational, SBTreeNode { } +extension Rational8 : SBTreeNode { }