|
| 1 | +// |
| 2 | +// HugeDecimalTests.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Evan Anderson on 7/9/23. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +import HugeNumbers |
| 10 | + |
| 11 | +struct HugeDecimalTests { |
| 12 | + func validate() async { |
| 13 | + await test_decimal() |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +extension HugeDecimalTests { |
| 18 | + private func test_decimal() async { |
| 19 | + var remainder:HugeRemainder = HugeRemainder(dividend: "1", divisor: "2") |
| 20 | + var result:HugeDecimal = remainder.to_decimal() |
| 21 | + var expected_result:HugeDecimal = HugeDecimal("5") |
| 22 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 23 | + |
| 24 | + remainder = HugeRemainder(dividend: "1", divisor: "4") |
| 25 | + result = remainder.to_decimal() |
| 26 | + expected_result = HugeDecimal("25") |
| 27 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 28 | + |
| 29 | + remainder = HugeRemainder(dividend: "1", divisor: "10") |
| 30 | + result = remainder.to_decimal() |
| 31 | + expected_result = HugeDecimal("1") |
| 32 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 33 | + |
| 34 | + remainder = HugeRemainder(dividend: "1", divisor: "1005") |
| 35 | + result = remainder.to_decimal() |
| 36 | + expected_result = HugeDecimal(value: HugeInt.zero, repeating_numbers: [8, 9, 3, 0, 2, 9, 5, 1, 8, 6, 3, 6, 2, 7, 4, 5, 0, 9, 8, 1, 2, 6, 5, 7, 8, 4, 2, 0, 5, 9, 9, 0, 0]) |
| 37 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 38 | + XCTAssert(result.description.elementsEqual(expected_result.description), "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 39 | + |
| 40 | + remainder = HugeRemainder(dividend: "1", divisor: "1010") |
| 41 | + result = remainder.to_decimal() |
| 42 | + expected_result = HugeDecimal(value: HugeInt.zero, repeating_numbers: [9, 9, 0, 0]) |
| 43 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 44 | + XCTAssert(result.description.elementsEqual(expected_result.description), "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 45 | + |
| 46 | + remainder = HugeDecimal("124").to_remainder |
| 47 | + var expected_remainder:HugeRemainder = HugeRemainder(dividend: "124", divisor: "1000") |
| 48 | + XCTAssert(remainder == expected_remainder, "test_decimal;remainder=\(result);expected_remainder=\(expected_remainder)") |
| 49 | + |
| 50 | + result = HugeDecimal("1234").distance_to_next_quotient |
| 51 | + expected_result = HugeDecimal("8766") |
| 52 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 53 | + |
| 54 | + result = HugeDecimal("100852").distance_to_next_quotient |
| 55 | + expected_result = HugeDecimal("899148") |
| 56 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 57 | + |
| 58 | + result = HugeDecimal("9999").distance_to_next_quotient |
| 59 | + expected_result = HugeDecimal("0001", remove_leading_zeros: false) |
| 60 | + XCTAssert(result == expected_result, "test_decimal;result=\(result);expected_result=\(expected_result)") |
| 61 | + |
| 62 | + test_decimal_addition() |
| 63 | + test_decimal_subtraction() |
| 64 | + test_decimal_multiplication() |
| 65 | + } |
| 66 | + private func test_decimal_addition() { |
| 67 | + var decimal:HugeDecimal = HugeDecimal("999") |
| 68 | + var (result, quotient):(HugeDecimal, HugeInt?) = decimal + HugeDecimal("001", remove_leading_zeros: false) |
| 69 | + var (expected_result, expected_quotient):(HugeDecimal, HugeInt?) = (HugeDecimal("000", remove_leading_zeros: false), HugeInt.one) |
| 70 | + XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_addition;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))") |
| 71 | + |
| 72 | + (result, quotient) = HugeDecimal("998") + HugeDecimal("001", remove_leading_zeros: false) |
| 73 | + (expected_result, expected_quotient) = (decimal, nil) |
| 74 | + XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_addition;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))") |
| 75 | + } |
| 76 | + private func test_decimal_subtraction() { |
| 77 | + var (result, quotient):(HugeDecimal, HugeInt?) = HugeDecimal("999") - HugeDecimal("001", remove_leading_zeros: false) |
| 78 | + var (expected_result, expected_quotient):(HugeDecimal, HugeInt?) = (HugeDecimal("998"), nil) |
| 79 | + XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_subtraction;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))") |
| 80 | + } |
| 81 | + private func test_decimal_multiplication() { |
| 82 | + var (quotient, result):(HugeInt?, HugeDecimal) = HugeDecimal("999") * HugeDecimal("2") |
| 83 | + var (expected_quotient, expected_result):(HugeInt?, HugeDecimal) = (nil, HugeDecimal("1998")) |
| 84 | + XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_multiplication;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))") |
| 85 | + |
| 86 | + (quotient, result) = HugeDecimal("999") * HugeInt("2") |
| 87 | + (expected_quotient, expected_result) = (HugeInt.one, HugeDecimal("998")) |
| 88 | + XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_multiplication;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))") |
| 89 | + } |
| 90 | +} |
0 commit comments