Commit 2199f863 authored by George Hotz's avatar George Hotz

add simple memory fuzzing test

parent ee199fe7
......@@ -8,10 +8,13 @@ async function write(mm, root, addr, data) {
root = l.data
}
}
console.log("new hash", root)
return root
}
function randint(n) {
return Math.floor(Math.random() * n)
}
describe("MIPSMemory contract", function () {
beforeEach(async function () {
const MIPSMemory = await ethers.getContractFactory("MIPSMemory")
......@@ -22,7 +25,9 @@ describe("MIPSMemory contract", function () {
let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
root = await write(mm, root, 0, 1)
console.log("new hash", root)
root = await write(mm, root, 4, 2)
console.log("new hash", root)
expect(await mm.ReadMemory(root, 0)).to.equal(1)
expect(await mm.ReadMemory(root, 4)).to.equal(2)
......@@ -32,8 +37,11 @@ describe("MIPSMemory contract", function () {
let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
root = await write(mm, root, 0, 1)
console.log("new hash", root)
root = await write(mm, root, 4, 2)
console.log("new hash", root)
root = await write(mm, root, 0x40, 3)
console.log("new hash", root)
expect(await mm.ReadMemory(root, 0)).to.equal(1)
expect(await mm.ReadMemory(root, 4)).to.equal(2)
......@@ -44,11 +52,45 @@ describe("MIPSMemory contract", function () {
let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
root = await write(mm, root, 0x7fffd00c, 1)
console.log("new hash", root)
root = await write(mm, root, 0x7fffd010, 2)
console.log("new hash", root)
root = await write(mm, root, 0x7fffcffc, 3)
console.log("new hash", root)
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)
})
it("fuzzing should be okay", async function() {
await mm.AddTrieNode(new Uint8Array([0x80]))
let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
let keys = []
let values = []
for (var i = 0; i < 100; i++) {
const choice = Math.random()
if (choice < 0.5 || keys.length == 0) {
// write new key
const key = randint(0x4000)*4
const value = randint(0x100000000)
root = await write(mm, root, key, value)
keys.push(key)
values.push(value)
} else if (choice > 0.7) {
// read old key
const idx = randint(keys.length)
const key = keys[idx]
const value = values[idx]
expect(await mm.ReadMemory(root, key)).to.equal(value)
} else {
// rewrite old key
const idx = randint(keys.length)
const key = keys[idx]
const value = randint(0x100000000)
root = await write(mm, root, key, value)
values[idx] = value
}
}
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment