mips_test_memory.js 3.26 KB
Newer Older
George Hotz's avatar
George Hotz committed
1
const { expect } = require("chai");
2
const { writeMemory } = require("../scripts/lib")
George Hotz's avatar
George Hotz committed
3

4 5 6 7
function randint(n) {
  return Math.floor(Math.random() * n)
}

George Hotz's avatar
George Hotz committed
8
describe("MIPSMemory contract", function () {
George Hotz's avatar
George Hotz committed
9
  beforeEach(async function () {
George Hotz's avatar
George Hotz committed
10
    const MIPSMemory = await ethers.getContractFactory("MIPSMemory")
George Hotz's avatar
George Hotz committed
11
    mm = await MIPSMemory.deploy()
George Hotz's avatar
George Hotz committed
12
    await mm.AddTrieNode(new Uint8Array([0x80]))
George Hotz's avatar
George Hotz committed
13 14 15
  })
  it("write from new should work", async function() {
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
George Hotz's avatar
George Hotz committed
16

17 18
    root = await writeMemory(mm, root, 0, 1)
    root = await writeMemory(mm, root, 4, 2)
19 20 21

    expect(await mm.ReadMemory(root, 0)).to.equal(1)
    expect(await mm.ReadMemory(root, 4)).to.equal(2)
George Hotz's avatar
George Hotz committed
22 23
  })
  it("write three should work", async function() {
24 25
    await mm.AddTrieNode(new Uint8Array([0x80]))
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
George Hotz's avatar
George Hotz committed
26

27 28 29
    root = await writeMemory(mm, root, 0, 1)
    root = await writeMemory(mm, root, 4, 2)
    root = await writeMemory(mm, root, 0x40, 3)
30 31 32 33

    expect(await mm.ReadMemory(root, 0)).to.equal(1)
    expect(await mm.ReadMemory(root, 4)).to.equal(2)
    expect(await mm.ReadMemory(root, 0x40)).to.equal(3)
George Hotz's avatar
George Hotz committed
34
  })
George Hotz's avatar
George Hotz committed
35
  it("write other three should work", async function() {
George Hotz's avatar
George Hotz committed
36 37
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"

38 39 40
    root = await writeMemory(mm, root, 0x7fffd00c, 1)
    root = await writeMemory(mm, root, 0x7fffd010, 2)
    root = await writeMemory(mm, root, 0x7fffcffc, 3)
George Hotz's avatar
George Hotz committed
41 42 43 44

    expect(await mm.ReadMemory(root, 0x7fffd00c)).to.equal(1)
    expect(await mm.ReadMemory(root, 0x7fffd010)).to.equal(2)
    expect(await mm.ReadMemory(root, 0x7fffcffc)).to.equal(3)
George Hotz's avatar
George Hotz committed
45
  })
George Hotz's avatar
George Hotz committed
46
  it("bug found fuzzing 1", async function() {
47
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
48 49 50
    root = await writeMemory(mm, root, 0, 0)
    root = await writeMemory(mm, root, 0, 1)
    root = await writeMemory(mm, root, 0, 2)
George Hotz's avatar
George Hotz committed
51
  })
52
  it("fuzzing should be okay", async function() {
George Hotz's avatar
George Hotz committed
53 54
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
    let kv = {}
55 56

    for (var i = 0; i < 100; i++) {
George Hotz's avatar
George Hotz committed
57
      const keys = Object.keys(kv)
58
      const choice = Math.random()
George Hotz's avatar
George Hotz committed
59
      if (choice < 0.3 || keys.length == 0) {
60
        // write new key
George Hotz's avatar
George Hotz committed
61
        const key = randint(0x100)*4
62
        const value = randint(0x100000000)
George Hotz's avatar
George Hotz committed
63
        console.log("writing", key, value)
64
        root = await writeMemory(mm, root, key, value)
George Hotz's avatar
George Hotz committed
65
        kv[key] = value
George Hotz's avatar
George Hotz committed
66 67 68 69 70
      } else if (choice < 0.5) {
        // write new high key
        const key = randint(0x100)*4 + 0x10000000
        const value = randint(0x100000000)
        console.log("writing", key, value)
71
        root = await writeMemory(mm, root, key, value)
George Hotz's avatar
George Hotz committed
72
        kv[key] = value
73 74 75 76
      } else if (choice > 0.7) {
        // read old key
        const idx = randint(keys.length)
        const key = keys[idx]
George Hotz's avatar
George Hotz committed
77 78
        console.log("reading", key)
        expect(await mm.ReadMemory(root, key)).to.equal(kv[key])
79 80 81 82 83
      } else {
        // rewrite old key
        const idx = randint(keys.length)
        const key = keys[idx]
        const value = randint(0x100000000)
George Hotz's avatar
George Hotz committed
84
        console.log("writing", key, value)
85
        root = await writeMemory(mm, root, key, value)
George Hotz's avatar
George Hotz committed
86
        kv[key] = value
87 88
      }
    }
89
  }).timeout(60000)
George Hotz's avatar
George Hotz committed
90
})