Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Extra PseudoFloat tests #545

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions contracts/test/pseudoFloat-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,45 @@ describe("PseudoFloat", function () {
expect(stream).to.eq("0x");
});

it("1.23*10^n encode and decode, for all valid values of n", async () => {
// This test demonstrates that although only 10^30 can be put into the
// encoded exponent, this does not limit the range of values that can be
// represented. This is because the encoded mantissa can be arbitrarily
// large, and will include any additional factors of 10 that cannot be
// absorbed by the exponent.

// 1.23 * 10^2 = 123. Any smaller n produces a fractional value.
const firstValidN = 2;

// 1.23 * 10^76 = approx 2^253. Any larger n produces value > 2^256-1.
const lastValidN = 76;

for (let n = firstValidN; n <= lastValidN; n++) {
const value = BigNumber.from(123).mul(
BigNumber.from(10).pow(n - 2), // -2 to correct for 123 = 1.23 * 10^2
);

const encoded = encodePseudoFloat(value);
const [decodedValue, stream] = await pseudoFloat.decodePublic(encoded);
expect(decodedValue).to.eq(value);
expect(stream).to.eq("0x");
}
});

it("10^77 encode and decode", async () => {
const encoded = encodePseudoFloat(BigNumber.from(10).pow(77));
const [value, stream] = await pseudoFloat.decodePublic(encoded);
expect(value).to.eq(BigNumber.from(10).pow(77));
expect(stream).to.eq("0x");
});

it("MaxUint256 encode and decode", async () => {
const encoded = encodePseudoFloat(ethers.constants.MaxUint256);
const [value, stream] = await pseudoFloat.decodePublic(encoded);
expect(value).to.eq(ethers.constants.MaxUint256);
expect(stream).to.eq("0x");
});

it("Test tx.values from 20 blocks", async () => {
// The non-zero tx.value amounts of transactions in mainnet blocks 16472849
// to 16472868, excluding zeros.
Expand Down