Skip to content

Commit

Permalink
feat: floor, ceil, neg, abs, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martonlederer committed Apr 11, 2024
1 parent 430108a commit a840831
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 37 deletions.
72 changes: 64 additions & 8 deletions src/Quantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
107 changes: 80 additions & 27 deletions src/quantity.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)];
Expand Down Expand Up @@ -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);
});
});
20 changes: 18 additions & 2 deletions src/token.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
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({
host: "arweave.net",
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();
});
});

0 comments on commit a840831

Please sign in to comment.