Skip to content

Commit

Permalink
move tests to new gas report
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jun 19, 2023
1 parent 0244cae commit 884afc0
Show file tree
Hide file tree
Showing 28 changed files with 1,300 additions and 380 deletions.
708 changes: 708 additions & 0 deletions packages/store/gas-report.json

Large diffs are not rendered by default.

35 changes: 23 additions & 12 deletions packages/store/test/Bytes.t.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { Bytes } from "../src/Bytes.sol";

contract BytesTest is Test {
contract BytesTest is Test, GasReporter {
function testToBytes32() public {
bytes memory input = new bytes(32);
input[0] = 0x01;
input[31] = 0x02;

// !gasreport create bytes32 from bytes memory with offset 0
startGasReport("create bytes32 from bytes memory with offset 0");
bytes32 output = Bytes.toBytes32(input, 0);
endGasReport();

assertEq(uint256(output), 0x0100000000000000000000000000000000000000000000000000000000000002);
}
Expand All @@ -21,8 +23,9 @@ contract BytesTest is Test {
input[0 + 16] = 0x01;
input[31 + 16] = 0x02;

// !gasreport create bytes32 from bytes memory with offset 16
startGasReport("create bytes32 from bytes memory with offset 16");
bytes32 output = Bytes.toBytes32(input, 16);
endGasReport();

assertEq(uint256(output), 0x0100000000000000000000000000000000000000000000000000000000000002);
}
Expand All @@ -31,8 +34,9 @@ contract BytesTest is Test {
bytes memory a = bytes("a");
bytes memory b = bytes("a");

// !gasreport compare equal bytes
startGasReport("compare equal bytes");
bool equals = Bytes.equals(a, b);
endGasReport();

assertTrue(equals);
}
Expand All @@ -41,8 +45,9 @@ contract BytesTest is Test {
bytes memory a = bytes("a");
bytes memory b = bytes("b");

// !gasreport compare unequal bytes
startGasReport("compare unequal bytes");
bool equals = Bytes.equals(a, b);
endGasReport();

assertFalse(equals);
}
Expand All @@ -62,8 +67,9 @@ contract BytesTest is Test {
a[3] = 0x04;
a[4] = 0x05;

// !gasreport slice bytes3 with offset 1
startGasReport("slice bytes3 with offset 1");
bytes3 b = Bytes.slice3(a, 1);
endGasReport();

assertEq(b.length, 3);
assertEq(uint256(uint8(b[0])), 0x02);
Expand All @@ -75,17 +81,19 @@ contract BytesTest is Test {
bytes32 original = keccak256("some data");
bytes memory input = abi.encodePacked(bytes10(keccak256("irrelevant data")), original);

// !gasreport slice bytes32 with offset 10
startGasReport("slice bytes32 with offset 10");
bytes32 output = Bytes.slice32(input, 10);
endGasReport();

assertEq(output, original);
}

function testSetBytes1() public {
bytes32 input = bytes32(0);

// !gasreport set bytes1 in bytes32
startGasReport("set bytes1 in bytes32");
Bytes.setBytes1(input, 8, 0xff);
endGasReport();

assertEq(Bytes.setBytes1(input, 0, 0x01), bytes32(bytes1(0x01)));
assertEq(Bytes.setBytes1(input, 31, 0x01), bytes32(uint256(0x01)));
Expand All @@ -94,8 +102,9 @@ contract BytesTest is Test {
function testSetBytes2() public {
bytes32 input = bytes32(0);

// !gasreport set bytes2 in bytes32
startGasReport("set bytes2 in bytes32");
Bytes.setBytes2(input, 8, 0xffff);
endGasReport();

assertEq(Bytes.setBytes2(input, 0, 0xffff), bytes32(bytes2(0xffff)));
assertEq(Bytes.setBytes2(input, 30, 0xffff), bytes32(uint256(0xffff)));
Expand All @@ -104,8 +113,9 @@ contract BytesTest is Test {
function testSetBytes4() public {
bytes32 input = bytes32(0);

// !gasreport set bytes4 in bytes32
startGasReport("set bytes4 in bytes32");
Bytes.setBytes4(input, 8, 0xffffffff);
endGasReport();

assertEq(Bytes.setBytes4(input, 0, 0xffffffff), bytes32(bytes4(0xffffffff)));
assertEq(Bytes.setBytes4(input, 30, 0xffffffff), bytes32(uint256(0xffff)));
Expand All @@ -127,8 +137,9 @@ contract BytesTest is Test {

bytes memory input = abi.encodePacked(first, remainder);

// !gasreport set bytes4 in bytes memory
startGasReport("set bytes4 in bytes memory");
bytes memory result = Bytes.setBytes4(input, 0, overwrite);
endGasReport();

// First 4 bytes should be overwritten
assertEq(bytes4(result), overwrite);
Expand Down
8 changes: 5 additions & 3 deletions packages/store/test/KeyEncoding.t.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { KeyEncoding, KeyEncodingTableId } from "../src/codegen/Tables.sol";
import { ExampleEnum } from "../src/codegen/Types.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol";
import { Schema } from "../src/Schema.sol";

contract KeyEncodingTest is Test, StoreReadWithStubs {
contract KeyEncodingTest is Test, GasReporter, StoreReadWithStubs {
function testRegisterAndGetSchema() public {
// !gasreport register KeyEncoding schema
startGasReport("register KeyEncoding schema");
KeyEncoding.registerSchema();
endGasReport();

Schema registeredSchema = StoreCore.getSchema(KeyEncodingTableId);
Schema declaredSchema = KeyEncoding.getSchema();
Expand Down
17 changes: 11 additions & 6 deletions packages/store/test/Mixed.t.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { Mixed, MixedData, MixedTableId } from "../src/codegen/Tables.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol";
import { Schema } from "../src/Schema.sol";

contract MixedTest is Test, StoreReadWithStubs {
contract MixedTest is Test, GasReporter, StoreReadWithStubs {
MixedData private testMixed;

function testRegisterAndGetSchema() public {
// !gasreport register Mixed schema
startGasReport("register Mixed schema");
Mixed.registerSchema();
endGasReport();

Schema registeredSchema = StoreCore.getSchema(MixedTableId);
Schema declaredSchema = Mixed.getSchema();
Expand All @@ -30,11 +32,13 @@ contract MixedTest is Test, StoreReadWithStubs {
a32[1] = 4;
string memory s = "some string";

// !gasreport set record in Mixed
startGasReport("set record in Mixed");
Mixed.set({ key: key, u32: 1, u128: 2, a32: a32, s: s });
endGasReport();

// !gasreport get record from Mixed
startGasReport("get record from Mixed");
MixedData memory mixed = Mixed.get(key);
endGasReport();

assertEq(mixed.u32, 1);
assertEq(mixed.u128, 2);
Expand All @@ -48,7 +52,8 @@ contract MixedTest is Test, StoreReadWithStubs {
mixed.a32[0] = 3;
mixed.a32[1] = 4;

// !gasreport store Mixed struct in storage (native solidity)
startGasReport("store Mixed struct in storage (native solidity)");
testMixed = mixed;
endGasReport();
}
}
17 changes: 11 additions & 6 deletions packages/store/test/PackedCounter.t.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { PackedCounter, PackedCounterLib } from "../src/PackedCounter.sol";

contract PackedCounterTest is Test {
contract PackedCounterTest is Test, GasReporter {
function testTotal() public {
uint40[] memory counters = new uint40[](4);
counters[0] = 1;
counters[1] = 2;
counters[2] = 3;
counters[3] = 4;

// !gasreport pack uint40 array into PackedCounter
startGasReport("pack uint40 array into PackedCounter");
PackedCounter packedCounter = PackedCounterLib.pack(counters);
endGasReport();

// !gasreport get total of PackedCounter
startGasReport("get total of PackedCounter");
packedCounter.total();
endGasReport();

assertEq(packedCounter.total(), 10);
}
Expand All @@ -30,8 +33,9 @@ contract PackedCounterTest is Test {

PackedCounter packedCounter = PackedCounterLib.pack(counters);

// !gasreport get value at index of PackedCounter
startGasReport("get value at index of PackedCounter");
packedCounter.atIndex(3);
endGasReport();

assertEq(packedCounter.atIndex(0), 1);
assertEq(packedCounter.atIndex(1), 2);
Expand All @@ -48,8 +52,9 @@ contract PackedCounterTest is Test {

PackedCounter packedCounter = PackedCounterLib.pack(counters);

// !gasreport set value at index of PackedCounter
startGasReport("set value at index of PackedCounter");
packedCounter = packedCounter.setAtIndex(2, 5);
endGasReport();

assertEq(packedCounter.atIndex(0), 1);
assertEq(packedCounter.atIndex(1), 2);
Expand Down
28 changes: 18 additions & 10 deletions packages/store/test/Schema.t.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import { Test, console } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { Schema, SchemaLib } from "../src/Schema.sol";

// TODO add tests for all schema types
contract SchemaTest is Test {
contract SchemaTest is Test, GasReporter {
function testEncodeDecodeSchema() public {
uint256 gas = gasleft();
Schema schema = SchemaLib.encode(
Expand All @@ -20,8 +21,9 @@ contract SchemaTest is Test {
gas = gas - gasleft();
console.log("GAS REPORT: encode schema with 6 entries [SchemaLib.encode]: %s", gas);

// !gasreport get schema type at index
startGasReport("get schema type at index");
SchemaType schemaType1 = schema.atIndex(0);
endGasReport();

assertEq(uint8(schemaType1), uint8(SchemaType.UINT8));
assertEq(uint8(schema.atIndex(1)), uint8(SchemaType.UINT16));
Expand Down Expand Up @@ -137,8 +139,9 @@ contract SchemaTest is Test {
SchemaType.UINT32_ARRAY // 0 bytes (because it's dynamic)
);

// !gasreport get static data length from schema
startGasReport("get static data length from schema");
uint256 length = schema.staticDataLength();
endGasReport();

assertEq(length, 55);
}
Expand All @@ -153,8 +156,9 @@ contract SchemaTest is Test {
SchemaType.UINT32_ARRAY // 0 bytes (because it's dynamic)
);

// !gasreport get number of static fields from schema
startGasReport("get number of static fields from schema");
uint256 num = schema.numStaticFields();
endGasReport();

assertEq(num, 5);
}
Expand All @@ -169,13 +173,14 @@ contract SchemaTest is Test {
SchemaType.UINT32_ARRAY // 0 bytes (because it's dynamic)
);

// !gasreport get number of dynamic fields from schema
startGasReport("get number of dynamic fields from schema");
uint256 num = schema.numDynamicFields();
endGasReport();

assertEq(num, 1);
}

function testValidate() public pure {
function testValidate() public {
SchemaType[] memory schema = new SchemaType[](28);
schema[0] = SchemaType.UINT256;
schema[1] = SchemaType.UINT256;
Expand Down Expand Up @@ -207,8 +212,9 @@ contract SchemaTest is Test {
schema[27] = SchemaType.UINT32_ARRAY;
Schema encodedSchema = SchemaLib.encode(schema);

// !gasreport validate schema
startGasReport("validate schema");
encodedSchema.validate({ allowEmpty: false });
endGasReport();
}

function testFailValidate() public pure {
Expand All @@ -219,17 +225,19 @@ contract SchemaTest is Test {
SchemaType[] memory schema = new SchemaType[](0);
Schema encodedSchema = SchemaLib.encode(schema);

// !gasreport check if schema is empty (empty schema)
startGasReport("check if schema is empty (empty schema)");
bool empty = encodedSchema.isEmpty();
endGasReport();

assertTrue(empty);
}

function testIsEmptyFalse() public {
Schema encodedSchema = SchemaLib.encode(SchemaType.UINT256);

// !gasreport check if schema is empty (non-empty schema)
startGasReport("check if schema is empty (non-empty schema)");
bool empty = encodedSchema.isEmpty();
endGasReport();

assertFalse(empty);
}
Expand Down
Loading

0 comments on commit 884afc0

Please sign in to comment.