|
| 1 | +//=----------------------------------------------------------------------------= |
| 2 | +// This source file is part of the Numberick open source project. |
| 3 | +// |
| 4 | +// Copyright (c) 2023 Oscar Byström Ericsson |
| 5 | +// Licensed under Apache License, Version 2.0 |
| 6 | +// |
| 7 | +// See http://www.apache.org/licenses/LICENSE-2.0 for license information. |
| 8 | +//=----------------------------------------------------------------------------= |
| 9 | + |
| 10 | +#if DEBUG |
| 11 | + |
| 12 | +import NBKCoreKit |
| 13 | +import NBKFlexibleWidthKit |
| 14 | +import NBKSignedKit |
| 15 | +import XCTest |
| 16 | + |
| 17 | +private typealias W = [UInt] |
| 18 | +private typealias X = [UInt64] |
| 19 | +private typealias Y = [UInt32] |
| 20 | + |
| 21 | +//*============================================================================* |
| 22 | +// MARK: * NBK x Signed x Comparisons |
| 23 | +//*============================================================================* |
| 24 | + |
| 25 | +final class NBKSignedTestsOnComparisonsAsSIntXL: XCTestCase { |
| 26 | + |
| 27 | + typealias T = NBKSigned<UIntXL> |
| 28 | + typealias M = NBKSigned<UIntXL>.Magnitude |
| 29 | + typealias D = NBKSigned<UIntXL>.Digit |
| 30 | + typealias U = NBKSigned<UIntXL>.Digit.Magnitude |
| 31 | + |
| 32 | + //=------------------------------------------------------------------------= |
| 33 | + // MARK: Tests |
| 34 | + //=------------------------------------------------------------------------= |
| 35 | + |
| 36 | + func testIsZero() { |
| 37 | + XCTAssertTrue (( T(0)).isZero) |
| 38 | + XCTAssertTrue ((-T(0)).isZero) |
| 39 | + XCTAssertFalse(( T(1)).isZero) |
| 40 | + XCTAssertFalse((-T(1)).isZero) |
| 41 | + } |
| 42 | + |
| 43 | + func testIsLessThanZero() { |
| 44 | + XCTAssertFalse(( T(0)).isLessThanZero) |
| 45 | + XCTAssertFalse((-T(0)).isLessThanZero) |
| 46 | + XCTAssertFalse(( T(1)).isLessThanZero) |
| 47 | + XCTAssertTrue ((-T(1)).isLessThanZero) |
| 48 | + } |
| 49 | + |
| 50 | + func testIsMoreThanZero() { |
| 51 | + XCTAssertFalse(( T(0)).isMoreThanZero) |
| 52 | + XCTAssertFalse((-T(0)).isMoreThanZero) |
| 53 | + XCTAssertTrue (( T(1)).isMoreThanZero) |
| 54 | + XCTAssertFalse((-T(1)).isMoreThanZero) |
| 55 | + } |
| 56 | + |
| 57 | + func testSignum() { |
| 58 | + NBKAssertSignum(( T(0)), Int( 0)) |
| 59 | + NBKAssertSignum((-T(0)), Int( 0)) |
| 60 | + NBKAssertSignum(( T(1)), Int( 1)) |
| 61 | + NBKAssertSignum((-T(1)), Int(-1)) |
| 62 | + } |
| 63 | + |
| 64 | + //=------------------------------------------------------------------------= |
| 65 | + // MARK: Tests |
| 66 | + //=------------------------------------------------------------------------= |
| 67 | + |
| 68 | + func testHashing() { |
| 69 | + var union = Set<T>() |
| 70 | + union.insert( T(0)) |
| 71 | + union.insert( T(0)) |
| 72 | + union.insert(-T(0)) |
| 73 | + union.insert(-T(0)) |
| 74 | + union.insert( T(1)) |
| 75 | + union.insert( T(1)) |
| 76 | + union.insert(-T(1)) |
| 77 | + union.insert(-T(1)) |
| 78 | + XCTAssertEqual(union.count, 3) |
| 79 | + } |
| 80 | + |
| 81 | + func testComparingLargeWithLarge() { |
| 82 | + NBKAssertComparisons(T(M(words:[0, 2, 3, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1)) |
| 83 | + NBKAssertComparisons(T(M(words:[1, 0, 3, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1)) |
| 84 | + NBKAssertComparisons(T(M(words:[1, 2, 0, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1)) |
| 85 | + NBKAssertComparisons(T(M(words:[1, 2, 3, 0] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1)) |
| 86 | + NBKAssertComparisons(T(M(words:[0, 2, 3, 4] as W)), T(M(words:[0, 2, 3, 4] as W)), Int(0)) |
| 87 | + NBKAssertComparisons(T(M(words:[1, 0, 3, 4] as W)), T(M(words:[1, 0, 3, 4] as W)), Int(0)) |
| 88 | + NBKAssertComparisons(T(M(words:[1, 2, 0, 4] as W)), T(M(words:[1, 2, 0, 4] as W)), Int(0)) |
| 89 | + NBKAssertComparisons(T(M(words:[1, 2, 3, 0] as W)), T(M(words:[1, 2, 3, 0] as W)), Int(0)) |
| 90 | + NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[0, 2, 3, 4] as W)), Int(1)) |
| 91 | + NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 0, 3, 4] as W)), Int(1)) |
| 92 | + NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 2, 0, 4] as W)), Int(1)) |
| 93 | + NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 2, 3, 0] as W)), Int(1)) |
| 94 | + } |
| 95 | + |
| 96 | + //=------------------------------------------------------------------------= |
| 97 | + // MARK: Tests x Digit (and Self) |
| 98 | + //=------------------------------------------------------------------------= |
| 99 | + |
| 100 | + func testComparingSmallWithSmall() { |
| 101 | + NBKAssertComparisonsByDigit( T(0), D(0), Int(0)) |
| 102 | + NBKAssertComparisonsByDigit( T(0), -D(0), Int(0)) |
| 103 | + NBKAssertComparisonsByDigit(-T(0), D(0), Int(0)) |
| 104 | + NBKAssertComparisonsByDigit(-T(0), -D(0), Int(0)) |
| 105 | + |
| 106 | + NBKAssertComparisonsByDigit( T(1), D(1), Int(0)) |
| 107 | + NBKAssertComparisonsByDigit( T(1), -D(1), Int(1)) |
| 108 | + NBKAssertComparisonsByDigit(-T(1), D(1), -Int(1)) |
| 109 | + NBKAssertComparisonsByDigit(-T(1), -D(1), Int(0)) |
| 110 | + |
| 111 | + NBKAssertComparisonsByDigit( T(2), D(3), -Int(1)) |
| 112 | + NBKAssertComparisonsByDigit( T(2), -D(3), Int(1)) |
| 113 | + NBKAssertComparisonsByDigit(-T(2), D(3), -Int(1)) |
| 114 | + NBKAssertComparisonsByDigit(-T(2), -D(3), Int(1)) |
| 115 | + |
| 116 | + NBKAssertComparisonsByDigit( T(3), D(2), Int(1)) |
| 117 | + NBKAssertComparisonsByDigit( T(3), -D(2), Int(1)) |
| 118 | + NBKAssertComparisonsByDigit(-T(3), D(2), -Int(1)) |
| 119 | + NBKAssertComparisonsByDigit(-T(3), -D(2), -Int(1)) |
| 120 | + } |
| 121 | + |
| 122 | + //=------------------------------------------------------------------------= |
| 123 | + // MARK: Tests x Miscellaneous |
| 124 | + //=------------------------------------------------------------------------= |
| 125 | + |
| 126 | + func testOverloadsAreUnambiguous() { |
| 127 | + func becauseThisCompilesSuccessfully(_ x: inout T) { |
| 128 | + XCTAssertNotNil(x.signum()) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + func testOverloadsAreUnambiguousWhenUsingIntegerLiterals() { |
| 133 | + func becauseThisCompilesSuccessfully(_ x: inout T) { |
| 134 | + XCTAssertNotNil(x.compared(to: 0)) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +//*============================================================================* |
| 140 | +// MARK: * NBK x Signed x Comparisons x Assertions |
| 141 | +//*============================================================================* |
| 142 | + |
| 143 | +private func NBKAssertSignum<M>( |
| 144 | +_ operand: NBKSigned<M>, _ signum: Int, |
| 145 | +file: StaticString = #file, line: UInt = #line) { |
| 146 | + XCTAssertEqual(Int(operand.signum() as Int), signum, file: file, line: line) |
| 147 | +} |
| 148 | + |
| 149 | +private func NBKAssertComparisons<M>( |
| 150 | +_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>, _ signum: Int, |
| 151 | +file: StaticString = #file, line: UInt = #line) { |
| 152 | + XCTAssertEqual(lhs == rhs, signum == 0, file: file, line: line) |
| 153 | + XCTAssertEqual(lhs != rhs, signum != 0, file: file, line: line) |
| 154 | + |
| 155 | + XCTAssertEqual(lhs < rhs, signum == -1, file: file, line: line) |
| 156 | + XCTAssertEqual(lhs <= rhs, signum != 1, file: file, line: line) |
| 157 | + |
| 158 | + XCTAssertEqual(lhs > rhs, signum == 1, file: file, line: line) |
| 159 | + XCTAssertEqual(lhs >= rhs, signum != -1, file: file, line: line) |
| 160 | + |
| 161 | + XCTAssertEqual(lhs.compared(to: rhs), signum, file: file, line: line) |
| 162 | +} |
| 163 | + |
| 164 | +private func NBKAssertComparisonsByDigit<M>( |
| 165 | +_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>.Digit, _ signum: Int, |
| 166 | +file: StaticString = #file, line: UInt = #line) { |
| 167 | + //=------------------------------------------= |
| 168 | + NBKAssertComparisons(lhs, NBKSigned<M>(digit: rhs), signum) |
| 169 | + //=------------------------------------------= |
| 170 | + XCTAssertEqual(lhs.compared(to: rhs), signum, file: file, line: line) |
| 171 | +} |
| 172 | + |
| 173 | +#endif |
0 commit comments