-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmemory.test.gr
More file actions
84 lines (79 loc) 路 2.48 KB
/
Copy pathmemory.test.gr
File metadata and controls
84 lines (79 loc) 路 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module MemoryTest
from "runtime/unsafe/wasmi32" include WasmI32
from "runtime/unsafe/memory" include Memory
from "runtime/malloc" include Malloc
from "runtime/debugPrint" include DebugPrint
// Memory.copy
@unsafe
let test = () => {
use WasmI32.{ (+), (-), (==), ltU as (<), gtU as (>) }
let length = 10n
let section1 = Malloc.malloc(length)
let section2 = Malloc.malloc(length)
// Clear both sections byte by byte
for (let mut i = 0n; i < length; i += 1n) {
WasmI32.store8(section1, 0n, i)
WasmI32.store8(section2, 0n, i)
}
// Set section1 to 1,2,...length
for (let mut i = 0n; i < length; i += 1n) {
WasmI32.store8(section1, i, i)
}
// Copy section1 to section2
Memory.copy(section2, section1, length)
// Verify the copy was successful
for (let mut i = 0n; i < length; i += 1n) {
assert WasmI32.load8U(section1, i) == WasmI32.load8U(section2, i)
}
// Verify that overlapping regions are handled correctly by copying section1 to itself with an offset
let shift = 2n
Memory.copy(section1, section1 + shift, length - shift)
for (let mut i = 0n; i < length - shift; i += 1n) {
assert WasmI32.load8U(section1, i) == i + shift
}
}
test()
// Memory.fill
@unsafe
let test = () => {
use WasmI32.{ (+), (==), ltU as (<) }
let length = 10n
let section = Malloc.malloc(10n)
// Clear the section byte by byte
for (let mut i = 0n; i < length; i += 1n) {
WasmI32.store8(section, 0n, i)
}
// Fill the section with `255`
Memory.fill(section, 255n, length)
// Verify the fill was successful
for (let mut i = 0n; i < length; i += 1n) {
assert WasmI32.load8U(section, i) == 255n
}
}
test()
// Memory.compare
@unsafe
let test = () => {
use WasmI32.{ (-), (==), (<), (>) }
let length = 10n
let section1 = Malloc.malloc(length)
let section2 = Malloc.malloc(length)
// Equal regions
Memory.fill(section1, 0n, length)
Memory.fill(section2, 0n, length)
assert Memory.compare(section1, section2, 0n) == 0n
// First region less than second
Memory.fill(section1, 0n, length)
Memory.fill(section2, 1n, length)
assert Memory.compare(section1, section2, length) < 0n
// First region greater than second
Memory.fill(section1, 1n, length)
Memory.fill(section2, 0n, length)
assert Memory.compare(section1, section2, length) > 0n
// Regions differ at the last byte
Memory.fill(section1, 0n, length)
WasmI32.store8(section1, 255n, length - 1n)
Memory.fill(section2, 0n, length)
assert Memory.compare(section1, section2, length) > 0n
}
test()