UNPKG

@fractorysolutions/safe-units

Version:
87 lines 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var unitValueArithmetic_1 = require("../unitValueArithmetic"); describe("Unit value arithmetic", function () { function addSymbols(unit) { var result = {}; for (var dimension_1 in unit) { var exponent = unit[dimension_1]; result[dimension_1] = exponent === undefined ? undefined : [dimension_1, exponent]; } return result; } var x = (0, unitValueArithmetic_1.dimension)("x"); var y = (0, unitValueArithmetic_1.dimension)("y"); describe("bases", function () { it("should construct base units", function () { expect((0, unitValueArithmetic_1.dimension)("x")).toEqual(addSymbols({ x: "1" })); }); }); describe("multiplication", function () { it("should multiply two different base units correctly", function () { expect((0, unitValueArithmetic_1.multiplyUnits)(x, y)).toEqual(addSymbols({ x: "1", y: "1" })); }); it("should multiply two of the same base unit correctly", function () { expect((0, unitValueArithmetic_1.multiplyUnits)(x, x)).toEqual(addSymbols({ x: "2" })); }); it("should multiply complex units correctly", function () { var left = addSymbols({ x: "1", y: "-2" }); var right = addSymbols({ y: "1", z: "2" }); expect((0, unitValueArithmetic_1.multiplyUnits)(left, right)).toEqual(addSymbols({ x: "1", y: "-1", z: "2" })); }); it("should remove zero exponents from the result", function () { var left = addSymbols({ x: "1", y: "2", z: "3" }); var right = addSymbols({ x: "-1", y: "-2", z: "-3" }); expect((0, unitValueArithmetic_1.multiplyUnits)(left, right)).toEqual({}); }); it("should handle explicitly undefined and 0 exponents", function () { var left = addSymbols({ w: "0", x: "2", y: undefined }); var right = addSymbols({ x: undefined, y: "0", z: undefined }); expect((0, unitValueArithmetic_1.multiplyUnits)(left, right)).toEqual(addSymbols({ x: "2" })); }); }); describe("division", function () { it("should correctly divide units", function () { var left = addSymbols({ x: "2", y: "2" }); var right = addSymbols({ x: "2", y: "-1", z: "2" }); expect((0, unitValueArithmetic_1.divideUnits)(left, right)).toEqual(addSymbols({ y: "3", z: "-2" })); }); }); describe("exponentiation", function () { it("should square a simple unit", function () { expect((0, unitValueArithmetic_1.exponentiateUnit)(x, "2")).toEqual(addSymbols({ x: "2" })); }); it("should cube a simple unit", function () { expect((0, unitValueArithmetic_1.exponentiateUnit)(x, "3")).toEqual(addSymbols({ x: "3" })); }); it("should square a complex unit", function () { expect((0, unitValueArithmetic_1.exponentiateUnit)(addSymbols({ x: "1", y: "-2" }), "2")).toEqual(addSymbols({ x: "2", y: "-4" })); }); it("should invert a unit", function () { expect((0, unitValueArithmetic_1.exponentiateUnit)(addSymbols({ x: "-1", y: "2", z: "-3" }), "-1")).toEqual(addSymbols({ x: "1", y: "-2", z: "3" })); }); it("should return the same unit when raised to the one", function () { var input = addSymbols({ x: "-1", y: "2", z: "-3" }); expect((0, unitValueArithmetic_1.exponentiateUnit)(input, "1")).toEqual(input); }); it("should return a dimensionless unit when raised to the zero", function () { expect((0, unitValueArithmetic_1.exponentiateUnit)(addSymbols({ x: "-1", y: "2", z: "-3" }), "0")).toEqual({}); }); it("should handle explicitly undefined and 0 exponents", function () { expect((0, unitValueArithmetic_1.exponentiateUnit)(addSymbols({ x: "2", y: undefined, z: "0" }), "2")).toEqual(addSymbols({ x: "4" })); }); }); describe("roots", function () { it("should square root the unit", function () { expect((0, unitValueArithmetic_1.nthRootUnit)(addSymbols({ x: "4", y: "-2" }), "2")).toEqual(addSymbols({ x: "2", y: "-1" })); }); it("should cube root the unit", function () { expect((0, unitValueArithmetic_1.nthRootUnit)(addSymbols({ x: "3", y: "-3" }), "3")).toEqual(addSymbols({ x: "1", y: "-1" })); }); it("should handle explicitly undefined and 0 exponents", function () { expect((0, unitValueArithmetic_1.nthRootUnit)(addSymbols({ x: "2", y: undefined, z: "0" }), "2")).toEqual(addSymbols({ x: "1" })); }); }); }); //# sourceMappingURL=unitValueArithmeticTests.js.map