Skip to content

Commit

Permalink
test: add tests for memory expansion on access
Browse files Browse the repository at this point in the history
Removed a couple of tests for write beyond capacity.
This is because memory is now auto expanded during write.
  • Loading branch information
nvnx7 committed May 22, 2022
1 parent 8bb5c44 commit f596ecd
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions packages/vm/tests/api/evm/memory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ tape('Memory', (t) => {
const m = new Memory()
t.test('should have 0 capacity initially', (st) => {
st.equal(m._store.length, 0)
st.throws(() => m.write(0, 3, Buffer.from([1, 2, 3])), /capacity/)
st.end()
})

Expand All @@ -25,11 +24,6 @@ tape('Memory', (t) => {
st.end()
})

t.test('should not write value beyond capacity', (st) => {
st.throws(() => m.write(30, 3, Buffer.from([1, 2, 3])), /capacity/)
st.end()
})

t.test('should write value', (st) => {
m.write(29, 3, Buffer.from([1, 2, 3]))
st.ok(m.read(29, 5).equals(Buffer.from([1, 2, 3, 0, 0])))
Expand All @@ -40,4 +34,44 @@ tape('Memory', (t) => {
st.throws(() => m.write(0, 5, Buffer.from([8, 8, 8])), /size/)
st.end()
})

t.test(
'should expand by word (32 bytes) properly when writing to previously untouched location',
(st) => {
const memory = new Memory()
memory.write(0, 1, Buffer.from([1]))
st.equal(memory._store.length, 32)

memory.write(1, 3, Buffer.from([2, 2, 2]))
st.equal(memory._store.length, 32)

memory.write(4, 32, Buffer.allocUnsafe(32).fill(3))
st.equal(memory._store.length, 64)

memory.write(36, 32, Buffer.allocUnsafe(32).fill(4))
st.equal(memory._store.length, 96)

st.end()
}
)

t.test('should expand by word (32 bytes) when reading a previously untouched location', (st) => {
const memory = new Memory()
memory.read(0, 8)
st.equal(memory._store.length, 32)

memory.read(1, 16)
st.equal(memory._store.length, 32)

memory.read(1, 32)
st.equal(memory._store.length, 64)

memory.read(32, 32)
st.equal(memory._store.length, 64)

memory.read(33, 32)
st.equal(memory._store.length, 96)

st.end()
})
})

0 comments on commit f596ecd

Please sign in to comment.