@@ -14,28 +14,78 @@ describe('Memory', function () {
14
14
} ) ;
15
15
16
16
describe ( 'free pointer' , function ( ) {
17
- it ( 'sets memory pointer' , async function ( ) {
18
- const ptr = '0x00000000000000000000000000000000000000000000000000000000000000a0' ;
19
- expect ( await this . mock . $setFreePointer ( ptr ) ) . to . not . be . reverted ;
17
+ it ( 'sets free memory pointer' , async function ( ) {
18
+ const ptr = ethers . toBeHex ( 0xa0 , 32 ) ;
19
+ await expect ( this . mock . $setFreePointer ( ptr ) ) . to . not . be . reverted ;
20
20
} ) ;
21
21
22
- it ( 'gets memory pointer' , async function ( ) {
23
- expect ( await this . mock . $getFreePointer ( ) ) . to . equal (
24
- // Default pointer
25
- '0x0000000000000000000000000000000000000000000000000000000000000080' ,
22
+ it ( 'gets free memory pointer' , async function ( ) {
23
+ await expect ( this . mock . $getFreePointer ( ) ) . to . eventually . equal (
24
+ ethers . toBeHex ( 0x80 , 32 ) , // Default pointer
26
25
) ;
27
26
} ) ;
27
+ } ) ;
28
28
29
- it ( 'asBytes32' , async function ( ) {
30
- const ptr = ethers . toBeHex ( '0x1234' , 32 ) ;
31
- await this . mock . $setFreePointer ( ptr ) ;
32
- expect ( await this . mock . $asBytes32 ( ptr ) ) . to . equal ( ptr ) ;
29
+ it ( 'extractWord extracts a word' , async function ( ) {
30
+ const ptr = await this . mock . $getFreePointer ( ) ;
31
+ await expect ( this . mock . $extractWord ( ptr ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 32 ) ) ;
32
+ } ) ;
33
+
34
+ it ( 'extractByte extracts a byte' , async function ( ) {
35
+ const ptr = await this . mock . $getFreePointer ( ) ;
36
+ await expect ( this . mock . $extractByte ( ptr , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 1 ) ) ;
37
+ } ) ;
38
+
39
+ it ( 'contentPointer' , async function ( ) {
40
+ const data = ethers . toUtf8Bytes ( 'hello world' ) ;
41
+ const result = await this . mock . $contentPointer ( data ) ;
42
+ expect ( result ) . to . equal ( ethers . toBeHex ( 0xa0 , 32 ) ) ; // 0x80 is the default free pointer (length)
43
+ } ) ;
44
+
45
+ describe ( 'addOffset' , function ( ) {
46
+ it ( 'addOffset' , async function ( ) {
47
+ const basePtr = ethers . toBeHex ( 0x80 , 32 ) ;
48
+ const offset = 32 ;
49
+ const expectedPtr = ethers . toBeHex ( 0xa0 , 32 ) ;
50
+
51
+ await expect ( this . mock . $addOffset ( basePtr , offset ) ) . to . eventually . equal ( expectedPtr ) ;
33
52
} ) ;
34
53
35
- it ( 'asPointer' , async function ( ) {
54
+ it ( 'addOffsetwraps around' , async function ( ) {
55
+ const basePtr = ethers . toBeHex ( 0x80 , 32 ) ;
56
+ const offset = 256 ;
57
+ const expectedPtr = ethers . toBeHex ( 0x180 , 32 ) ;
58
+ await expect ( this . mock . $addOffset ( basePtr , offset ) ) . to . eventually . equal ( expectedPtr ) ;
59
+ } ) ;
60
+ } ) ;
61
+
62
+ describe ( 'pointer conversions' , function ( ) {
63
+ it ( 'asBytes32 / asPointer' , async function ( ) {
36
64
const ptr = ethers . toBeHex ( '0x1234' , 32 ) ;
37
- await this . mock . $setFreePointer ( ptr ) ;
38
- expect ( await this . mock . $asPointer ( ptr ) ) . to . equal ( ptr ) ;
65
+ await expect ( this . mock . $asBytes32 ( ptr ) ) . to . eventually . equal ( ptr ) ;
66
+ await expect ( this . mock . $asPointer ( ethers . Typed . bytes32 ( ptr ) ) ) . to . eventually . equal ( ptr ) ;
67
+ } ) ;
68
+
69
+ it ( 'asBytes / asPointer' , async function ( ) {
70
+ const ptr = await this . mock . $asPointer ( ethers . Typed . bytes ( ethers . toUtf8Bytes ( 'hello world' ) ) ) ;
71
+ expect ( ptr ) . to . equal ( ethers . toBeHex ( 0x80 , 32 ) ) ; // Default free pointer
72
+ await expect ( this . mock . $asBytes ( ptr ) ) . to . eventually . equal ( ethers . toBeHex ( 0x20 , 32 ) ) ;
73
+ } ) ;
74
+
75
+ it ( 'asUint256' , async function ( ) {
76
+ const value = 0x1234 ;
77
+ const ptr = ethers . toBeHex ( value , 32 ) ;
78
+ await expect ( this . mock . $asUint256 ( ptr ) ) . to . eventually . equal ( value ) ;
79
+ } ) ;
80
+ } ) ;
81
+
82
+ describe ( 'memory operations' , function ( ) {
83
+ it ( 'copy' , async function ( ) {
84
+ await expect ( this . mock . $copy ( ethers . toBeHex ( 0x80 , 32 ) , ethers . toBeHex ( 0xc0 , 32 ) , 32 ) ) . to . not . be . reverted ;
85
+ } ) ;
86
+
87
+ it ( 'copy with zero length' , async function ( ) {
88
+ await expect ( this . mock . $copy ( ethers . toBeHex ( 0x80 , 32 ) , ethers . toBeHex ( 0xc0 , 32 ) , 0 ) ) . to . not . be . reverted ;
39
89
} ) ;
40
90
} ) ;
41
91
} ) ;
0 commit comments