|
| 1 | +import { Decimal128 } from "../src/decimal128.mjs"; |
| 2 | +import { expectDecimal128 } from "./util.js"; |
| 3 | + |
| 4 | +describe("NaN", () => { |
| 5 | + test("works", () => { |
| 6 | + expect(new Decimal128("NaN").toExponential()).toStrictEqual("NaN"); |
| 7 | + }); |
| 8 | +}); |
| 9 | + |
| 10 | +describe("zero", () => { |
| 11 | + test("positive zero", () => { |
| 12 | + expect(new Decimal128("0").toExponential()).toStrictEqual("0e+0"); |
| 13 | + }); |
| 14 | + test("negative zero", () => { |
| 15 | + expect(new Decimal128("-0").toExponential()).toStrictEqual("-0e+0"); |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("infinity", () => { |
| 20 | + test("positive infinity", () => { |
| 21 | + expect(new Decimal128("Infinity").toExponential()).toStrictEqual( |
| 22 | + "Infinity" |
| 23 | + ); |
| 24 | + }); |
| 25 | + test("negative infinity", () => { |
| 26 | + expect(new Decimal128("-Infinity").toExponential()).toStrictEqual( |
| 27 | + "-Infinity" |
| 28 | + ); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("toExponential", function () { |
| 33 | + const d = "123.456"; |
| 34 | + const decimalD = new Decimal128(d); |
| 35 | + test("no argument", () => { |
| 36 | + expect(decimalD.toExponential()).toStrictEqual("1.23456e+2"); |
| 37 | + }); |
| 38 | + test("wrong argument type", () => { |
| 39 | + expect(() => decimalD.toExponential("foo")).toThrow(TypeError); |
| 40 | + }); |
| 41 | + test("empty options", () => { |
| 42 | + expect(decimalD.toExponential({})).toStrictEqual("1.23456e+2"); |
| 43 | + }); |
| 44 | + test("expected property missing", () => { |
| 45 | + expect(decimalD.toExponential({ foo: "bar" })).toStrictEqual( |
| 46 | + "1.23456e+2" |
| 47 | + ); |
| 48 | + }); |
| 49 | + test("more digits requested than integer digits available", () => { |
| 50 | + expectDecimal128(decimalD.toExponential({ digits: 7 }), "1.2345600e+2"); |
| 51 | + }); |
| 52 | + test("exact number of digits requested as digits available", () => { |
| 53 | + expectDecimal128(decimalD.toExponential({ digits: 6 }), "1.234560e+2"); |
| 54 | + }); |
| 55 | + test("possibly round non-integer part (1)", () => { |
| 56 | + expectDecimal128(decimalD.toExponential({ digits: 5 }), "1.23456e+2"); |
| 57 | + }); |
| 58 | + test("possibly round non-integer part (2)", () => { |
| 59 | + expectDecimal128(decimalD.toExponential({ digits: 4 }), "1.2345e+2"); |
| 60 | + }); |
| 61 | + test("same number of digits as available means no change", () => { |
| 62 | + expectDecimal128(decimalD.toExponential({ digits: 3 }), "1.234e+2"); |
| 63 | + }); |
| 64 | + test("cutoff if number has more digits than requested (1)", () => { |
| 65 | + expectDecimal128(decimalD.toExponential({ digits: 2 }), "1.23e+2"); |
| 66 | + }); |
| 67 | + test("cutoff if number has more digits than requested (2)", () => { |
| 68 | + expectDecimal128(decimalD.toExponential({ digits: 1 }), "1.2e+2"); |
| 69 | + }); |
| 70 | + test("zero decimal places throws", () => { |
| 71 | + expect(() => decimalD.toExponential({ digits: 0 })).toThrow(RangeError); |
| 72 | + }); |
| 73 | + test("negative number of decimal places", () => { |
| 74 | + expect(() => decimalD.toExponential({ digits: -1 })).toThrow( |
| 75 | + RangeError |
| 76 | + ); |
| 77 | + }); |
| 78 | + test("non-integer number throws", () => { |
| 79 | + expect(() => decimalD.toExponential({ digits: 1.5 })).toThrow( |
| 80 | + RangeError |
| 81 | + ); |
| 82 | + }); |
| 83 | + describe("negative", () => { |
| 84 | + let negD = decimalD.neg(); |
| 85 | + test("integer part", () => { |
| 86 | + expect(negD.toExponential({ digits: 3 }).toString()).toStrictEqual( |
| 87 | + "-1.234e+2" |
| 88 | + ); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe("to exponential string", () => { |
| 94 | + test("one", () => { |
| 95 | + expect( |
| 96 | + new Decimal128("1").toString({ format: "exponential" }) |
| 97 | + ).toStrictEqual("1e+0"); |
| 98 | + }); |
| 99 | + test("zero", () => { |
| 100 | + expect( |
| 101 | + new Decimal128("0").toString({ format: "exponential" }) |
| 102 | + ).toStrictEqual("0e+0"); |
| 103 | + }); |
| 104 | + test("minus zero", () => { |
| 105 | + expect( |
| 106 | + new Decimal128("-0").toString({ format: "exponential" }) |
| 107 | + ).toStrictEqual("-0e+0"); |
| 108 | + }); |
| 109 | + test("integer", () => { |
| 110 | + expect( |
| 111 | + new Decimal128("42").toString({ format: "exponential" }) |
| 112 | + ).toStrictEqual("4.2e+1"); |
| 113 | + }); |
| 114 | + |
| 115 | + test("round trip", () => { |
| 116 | + expect( |
| 117 | + new Decimal128("4.2E+0").toString({ format: "exponential" }) |
| 118 | + ).toStrictEqual("4.2e+0"); |
| 119 | + }); |
| 120 | + |
| 121 | + test("significant has one digit", () => { |
| 122 | + expect( |
| 123 | + new Decimal128("1").toString({ format: "exponential" }) |
| 124 | + ).toStrictEqual("1e+0"); |
| 125 | + }); |
| 126 | + test("negative exponent", () => { |
| 127 | + expect( |
| 128 | + new Decimal128("0.1").toString({ format: "exponential" }) |
| 129 | + ).toStrictEqual("1e-1"); |
| 130 | + }); |
| 131 | + test("negative exponent, multiple digits", () => { |
| 132 | + expect( |
| 133 | + new Decimal128("0.01042").toString({ format: "exponential" }) |
| 134 | + ).toStrictEqual("1.042e-2"); |
| 135 | + }); |
| 136 | +}); |
0 commit comments