-
Notifications
You must be signed in to change notification settings - Fork 0
/
packing_test.go
66 lines (62 loc) · 1.51 KB
/
packing_test.go
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
package main
import (
"testing"
"github.com/rensr/spack/solidity"
)
// The findOptimalPacking function doesn't use the fields in the
// slots, so we can leave them empty.
func TestFindOptimalPacking(t *testing.T) {
tests := []struct {
name string
options [][]solidity.StorageSlot
expected []solidity.StorageSlot
}{
{
name: "Single best answer",
options: [][]solidity.StorageSlot{
{
{Offset: 32}, {Offset: 16}, {Offset: 18}, {Offset: 27},
},
{
{Offset: 32}, {Offset: 16}, {Offset: 13}, {Offset: 32},
},
{
{Offset: 30}, {Offset: 15}, {Offset: 20}, {Offset: 30},
},
},
expected: []solidity.StorageSlot{
{Offset: 32}, {Offset: 16}, {Offset: 13}, {Offset: 32},
},
},
{
name: "Multiple best answers - pick the first",
options: [][]solidity.StorageSlot{
{
{Offset: 32}, {Offset: 32}, {Offset: 17}, {Offset: 17},
},
{
{Offset: 32}, {Offset: 32}, {Offset: 18}, {Offset: 16},
},
{
{Offset: 32}, {Offset: 32}, {Offset: 10}, {Offset: 24},
},
},
expected: []solidity.StorageSlot{
{Offset: 32}, {Offset: 32}, {Offset: 17}, {Offset: 17},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := findOptimalPacking(tt.options)
if len(actual) != len(tt.expected) {
t.Errorf("Expected %v, got %v", tt.expected, actual)
}
for i := range actual {
if actual[i].Offset != tt.expected[i].Offset {
t.Errorf("Expected %v, got %v", tt.expected, actual)
}
}
})
}
}