From a840831d2dfbf59b36a4ad1fa249c1e7cf6d0d27 Mon Sep 17 00:00:00 2001 From: Marton Lederer Date: Thu, 11 Apr 2024 11:32:15 +0200 Subject: [PATCH] feat: floor, ceil, neg, abs, tests --- src/Quantity.ts | 72 +++++++++++++++++++++++++---- src/quantity.test.ts | 107 ++++++++++++++++++++++++++++++++----------- src/token.test.ts | 20 +++++++- 3 files changed, 162 insertions(+), 37 deletions(-) diff --git a/src/Quantity.ts b/src/Quantity.ts index 674b78a..2b81d40 100644 --- a/src/Quantity.ts +++ b/src/Quantity.ts @@ -580,35 +580,91 @@ export default class Quantity { this.#qty = res.#qty; } - /*static __floor(x: Quantity) { - + /** + * Rounds down + * @param x Quantity to round down + * @returns Rounded down instance + */ + static __floor(x: Quantity) { + let res = Quantity.__trunc(x); + + if (x.#qty < 0n) { + res.#qty -= 10n ** x.#D; + } + + return res; } + /** + * Rounds down (in-place) + */ _floor() { - + const res = Quantity.__floor(this); + this.#qty = res.#qty; } + /** + * Rounds up + * @param x Quantity to round up + * @returns Rounded up instance + */ static __ceil(x: Quantity) { + let res = Quantity.__trunc(x); + + if (x.#qty > 0n) { + res.#qty += 10n ** x.#D; + } + return res; } + /** + * Rounds up (in-place) + */ _ceil() { - + const res = Quantity.__ceil(this); + this.#qty = res.#qty; } + /** + * Negate a quantity + * @param x Quantity to negate + * @returns Negated instance + */ static __neg(x: Quantity) { - + return new Quantity( + -x.#qty, + x.#D + ); } + /** + * Negate a quantity (in-place) + */ _neg() { - + this.#qty = -this.#qty; } + /** + * Get the absolute value of a quantity + * @param x Quantity to get the absolute value of + * @returns Absolute value + */ static __abs(x: Quantity) { + let res = x.clone(); + if (x.#qty < 0n) { + res.#qty = -res.#qty; + } + + return res; } + /** + * Get the absolute value of a quantity (in-place) + */ _abs() { - - }*/ + if (this.#qty >= 0n) return; + this.#qty = -this.#qty; + } } diff --git a/src/quantity.test.ts b/src/quantity.test.ts index f6e2dc6..257211b 100644 --- a/src/quantity.test.ts +++ b/src/quantity.test.ts @@ -1,21 +1,6 @@ -import { createDataItemSigner } from "@permaweb/aoconnect"; -import { JWKInterface } from "arweave/node/lib/wallet"; import Quantity from "./Quantity"; -import Arweave from "arweave"; -import Token from "./Token"; describe("Quantity testing", () => { - let wallet: JWKInterface; - - beforeAll(async () => { - const arweave = new Arweave({ - host: "arweave.net", - port: 443, - protocol: "https" - }); - wallet = await arweave.wallets.generate(); - }); - test("Init quantity", () => { const d = 2n; const int = 1n; @@ -308,18 +293,6 @@ describe("Quantity testing", () => { expect(inst1.toString()).toEqual("6.25"); }); - test("Is of token method", async () => { - const tkn = await Token( - "Sa0iBLPNyJQrwpTTG-tWLQU-1QeUAJA73DdxGGiKoJc", - createDataItemSigner(wallet) - ); - const validQty = new Quantity(324n, 3n); - const invalidQty = new Quantity(14529n, 5n); - - expect(Quantity.isQuantityOf(validQty, tkn)).toBeTruthy(); - expect(Quantity.isQuantityOf(invalidQty, tkn)).toBeFalsy(); - }); - test("Quantity min()", () => { const min = new Quantity(1n, 10n); const list = [new Quantity(456n, 2n), min, new Quantity(1n, 5n)]; @@ -353,4 +326,84 @@ describe("Quantity testing", () => { expect(inst.raw).toEqual(whole); }); + + test("Static floor", () => { + const whole = 5400n; + const inst1 = new Quantity(whole + 11n, 2n); + + expect(Quantity.__floor(inst1).raw.toString()).toEqual(whole.toString()); + + const inst2 = new Quantity(-whole + 11n, 2n); + + expect(Quantity.__floor(inst2).raw.toString()).toEqual((-whole).toString()); + }); + + test("In-place floor", () => { + const whole = -89340000n; + const inst = new Quantity(whole + 385n, 4n); + + inst._floor(); + + expect(inst.raw.toString()).toEqual(whole.toString()); + }); + + test("Static ceil", () => { + const whole = 9200n; + const inst1 = new Quantity(whole - 11n, 2n); + + expect(Quantity.__ceil(inst1).raw.toString()).toEqual(whole.toString()); + + const inst2 = new Quantity(-whole - 11n, 2n); + + expect(Quantity.__ceil(inst2).raw.toString()).toEqual((-whole).toString()); + }); + + test("In-place ceil", () => { + const whole = -21570000n; + const inst = new Quantity(whole - 385n, 4n); + + inst._ceil(); + + expect(inst.raw.toString()).toEqual(whole.toString()); + }); + + test("Static negation", () => { + const val = 4568n; + + expect(Quantity.__neg(new Quantity(val, 2n)).raw).toEqual(-val); + expect(Quantity.__neg(new Quantity(-val, 2n)).raw).toEqual(val); + }); + + test("In-place negation", () => { + const val = 83754n; + const inst = new Quantity(val, 2n); + + inst._neg(); + + expect(inst.raw).toEqual(-val); + + inst._neg(); + + expect(inst.raw).toEqual(val); + }); + + test("Static absolute", () => { + const val = 4556n; + + expect(Quantity.__abs(new Quantity(-val, 2n)).raw).toEqual(val); + expect(Quantity.__abs(new Quantity(val, 2n)).raw).toEqual(val); + }); + + test("In-place absolute", () => { + const val = 89415n; + const inst1 = new Quantity(-val, 3n); + inst1._abs(); + + expect(inst1.raw).toEqual(val); + + const inst2 = new Quantity(val, 5n); + inst2._abs(); + + expect(inst2.raw).toEqual(val); + }); }); diff --git a/src/token.test.ts b/src/token.test.ts index c0fc522..9b6c0de 100644 --- a/src/token.test.ts +++ b/src/token.test.ts @@ -1,9 +1,12 @@ import { createDataItemSigner } from "@permaweb/aoconnect"; +import { JWKInterface } from "arweave/node/lib/wallet"; import Token, { type TokenInstance } from "./Token"; +import Quantity from "./Quantity"; import Arweave from "arweave"; describe("Token testing", () => { let token: TokenInstance; + let wallet: JWKInterface; beforeAll(async () => { const arweave = new Arweave({ @@ -11,15 +14,28 @@ describe("Token testing", () => { port: 443, protocol: "https" }); + wallet = await arweave.wallets.generate(); token = await Token( "Sa0iBLPNyJQrwpTTG-tWLQU-1QeUAJA73DdxGGiKoJc", - createDataItemSigner(await arweave.wallets.generate()) + createDataItemSigner(wallet) ); - }); + }, 12000); test("Load token", () => { expect(token).not.toBeUndefined(); expect(token.info).not.toBeUndefined(); expect(typeof token.info?.Name).toBe("string"); }); + + test("Quantity is of token", async () => { + const tkn = await Token( + "Sa0iBLPNyJQrwpTTG-tWLQU-1QeUAJA73DdxGGiKoJc", + createDataItemSigner(wallet) + ); + const validQty = new Quantity(324n, 3n); + const invalidQty = new Quantity(14529n, 5n); + + expect(Quantity.isQuantityOf(validQty, tkn)).toBeTruthy(); + expect(Quantity.isQuantityOf(invalidQty, tkn)).toBeFalsy(); + }); });