forked from ethereum/solidity-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.sol
252 lines (212 loc) · 9.88 KB
/
memory.sol
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
pragma solidity ^0.4.16;
pragma experimental "v0.5.0";
pragma experimental "ABIEncoderV2";
import {STLTest} from "../STLTest.sol";
import {Memory} from "../../src/unsafe/Memory.sol";
/* solhint-disable max-line-length */
/* solhint-disable no-unused-vars */
/*********************** Base test contract ************************/
/* solhint-disable no-empty-blocks */
contract MemoryTest is STLTest {}
/* solhint-disable no-empty-blocks */
/*********************** Equality **************************/
contract TestMemoryEqualsZeroLength is MemoryTest {
function testImpl() internal {
assert(Memory.equals(0, 0, 0));
}
}
contract TestMemoryEqualsItselfDifferentAddresses is MemoryTest {
function testImpl() internal {
/* solhint-disable no-unused-vars */
bytes memory bts = hex"0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f";
for (uint i = 0; i < 10; i++) {
assert(Memory.equals(0x15*i, 0x15*i, 50));
}
}
}
contract TestMemoryEqualsItselfDifferentLengths is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f";
for (uint i = 0; i < 10; i++) {
assert(Memory.equals(0x40, 0x40, 30*i));
}
}
}
contract TestMemoryNotEqual is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"0102030405060708090a0b";
uint btsAddr;
assembly {
btsAddr := bts
}
assert(!Memory.equals(btsAddr, btsAddr + 0x20, 10));
}
}
contract TestMemoryNotEqualCommutative is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"0102030405060708090a0b";
uint btsAddr;
assembly {
btsAddr := bts
}
assert(!Memory.equals(btsAddr, btsAddr + 0x20, 10) && !Memory.equals(btsAddr + 0x20, btsAddr, 10));
}
}
contract TestMemoryEqualsBytesZeroLength is MemoryTest {
function testImpl() internal {
assert(Memory.equals(0, 0, new bytes(0)));
}
}
contract TestMemoryEqualsThrowsBytesTooShort is MemoryTest {
function testImpl() internal {
bytes memory bts = new bytes(2);
Memory.equals(0, 3, bts);
}
}
/********************* Allocation and creating references **************************/
contract TestMemoryAllocate is MemoryTest {
function testImpl() internal {
uint cur;
assembly {
cur := mload(0x40)
}
var mem = Memory.allocate(45);
assert(mem == cur);
uint newCur;
assembly {
newCur := mload(0x40)
}
assert(newCur == cur + 45);
}
}
contract TestMemoryAllocateZeroLength is MemoryTest {
function testImpl() internal {
uint cur;
assembly {
cur := mload(0x40)
}
var mem = Memory.allocate(0);
assert(mem == cur);
uint newCur;
assembly {
newCur := mload(0x40)
}
assert(newCur == cur);
}
}
contract TestMemoryPtrBytes is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"0102030405060708090a0b";
var addr = Memory.ptr(bts);
uint btsAddr;
assembly {
btsAddr := bts
}
assert(addr == btsAddr);
}
}
contract TestMemoryDataPtrBytes is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"0102030405060708090a0b";
var addr = Memory.dataPtr(bts);
uint btsDataAddr;
assembly {
btsDataAddr := add(bts, 0x20)
}
assert(addr == btsDataAddr);
}
}
contract TestMemoryFromBytes is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"0102030405060708090a0b";
var (addr, len) = Memory.fromBytes(bts);
assert(len == bts.length);
uint btsDataAddr;
assembly {
btsDataAddr := add(bts, 0x20)
}
assert(addr == btsDataAddr);
}
}
/********************* Copying **************************/
contract TestMemoryCopy is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"ffaaffaaffaaffaaffaaffaaffaaffffaaffaaffaaffaaffaaffaaffaaffffaaffaaffaaffaaffaaffaaffaaff";
var (src, len) = Memory.fromBytes(bts);
var dest = Memory.allocate(len);
Memory.copy(src, dest, len);
assert(Memory.equals(dest, len, bts));
}
}
contract TestMemoryCopyLengthZero is MemoryTest {
function testImpl() internal {
bytes memory bts = new bytes(0);
bytes memory bts2 = hex"ffaa";
var (src, ) = Memory.fromBytes(bts);
var (dest, ) = Memory.fromBytes(bts2);
Memory.copy(src, dest, 0);
// Check that bts2 is still intact.
assert(bts2[0] == hex"ff");
assert(bts2[1] == hex"aa");
}
}
contract TestMemoryCopyDoesNotChangeSrc is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"ffaaffaaffaaffaaffaaffaaffaaffffaaffaaffaaffaaffaaffaaffaaffffaaffaaffaaffaaffaaffaaffaaff";
var (src, len) = Memory.fromBytes(bts);
var dest = Memory.allocate(len);
Memory.copy(src, dest, len);
assert(Memory.equals(src, len, bts));
}
}
/********************* To types **************************/
contract TestMemoryToBytes is MemoryTest {
function testImpl() internal {
bytes memory bts = hex"ffaaffaaffaaffaaffaaffaaffaaff";
var (addr, len) = Memory.fromBytes(bts);
var bts2 = Memory.toBytes(addr, len);
assert(bts2.length == bts.length);
for (uint i = 0; i < bts.length; i++) {
assert(bts[i] == bts2[i]);
}
}
}
contract TestMemoryToUint is MemoryTest {
function testImpl() internal {
uint n = 12345;
var addr = Memory.allocate(32);
assembly {
mstore(addr, n)
}
var n2 = Memory.toUint(addr);
assert(n == n2);
}
}
contract TestMemoryToBytes32 is MemoryTest {
function testImpl() internal {
bytes32 b32 = 0x112233;
var addr = Memory.allocate(32);
assembly {
mstore(addr, b32)
}
var res = Memory.toBytes32(addr);
assert(res == b32);
}
}
/*
contract TestMemoryToByte is MemoryTest {
function testImpl() internal {
bytes32 b32 = "abcdefghijklmnopabcdefghijklmnop";
var addr = Memory.allocate(32);
assembly {
mstore(addr, b32)
}
for (uint8 i = 0; i < 32; i++) {
var res = Memory.toByte(addr, i);
assert(res == b32[i]);
}
}
}
*/
/* solhint-enable max-line-length */
/* solhint-enable no-unused-vars */