github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/wasm/counts_test.go (about) 1 package wasm 2 3 import ( 4 "testing" 5 6 "github.com/tetratelabs/wazero/internal/testing/require" 7 ) 8 9 func TestModule_SectionElementCount(t *testing.T) { 10 i32, f32 := ValueTypeI32, ValueTypeF32 11 zero := uint32(0) 12 empty := ConstantExpression{Opcode: OpcodeI32Const, Data: const0} 13 14 tests := []struct { 15 name string 16 input *Module 17 expected map[string]uint32 18 }{ 19 { 20 name: "empty", 21 input: &Module{}, 22 expected: map[string]uint32{}, 23 }, 24 { 25 name: "NameSection", 26 input: &Module{NameSection: &NameSection{ModuleName: "simple"}}, 27 expected: map[string]uint32{"custom": 1}, 28 }, 29 { 30 name: "TypeSection", 31 input: &Module{ 32 TypeSection: []FunctionType{ 33 {}, 34 {Params: []ValueType{i32, i32}, Results: []ValueType{i32}}, 35 {Params: []ValueType{i32, i32, i32, i32}, Results: []ValueType{i32}}, 36 }, 37 }, 38 expected: map[string]uint32{"type": 3}, 39 }, 40 { 41 name: "TypeSection and ImportSection", 42 input: &Module{ 43 TypeSection: []FunctionType{ 44 {Params: []ValueType{i32, i32}, Results: []ValueType{i32}}, 45 {Params: []ValueType{f32, f32}, Results: []ValueType{f32}}, 46 }, 47 ImportSection: []Import{ 48 { 49 Module: "Math", Name: "Mul", 50 Type: ExternTypeFunc, 51 DescFunc: 1, 52 }, { 53 Module: "Math", Name: "Add", 54 Type: ExternTypeFunc, 55 DescFunc: 0, 56 }, 57 }, 58 }, 59 expected: map[string]uint32{"import": 2, "type": 2}, 60 }, 61 { 62 name: "TypeSection, FunctionSection, CodeSection, ExportSection and StartSection", 63 input: &Module{ 64 TypeSection: []FunctionType{{}}, 65 FunctionSection: []Index{0}, 66 CodeSection: []Code{ 67 {Body: []byte{OpcodeLocalGet, 0, OpcodeLocalGet, 1, OpcodeI32Add, OpcodeEnd}}, 68 }, 69 ExportSection: []Export{ 70 {Name: "AddInt", Type: ExternTypeFunc, Index: Index(0)}, 71 }, 72 StartSection: &zero, 73 }, 74 expected: map[string]uint32{"code": 1, "export": 1, "function": 1, "start": 1, "type": 1}, 75 }, 76 { 77 name: "MemorySection and DataSection", 78 input: &Module{ 79 MemorySection: &Memory{Min: 1}, 80 DataSection: []DataSegment{{OffsetExpression: empty}}, 81 }, 82 expected: map[string]uint32{"data": 1, "memory": 1}, 83 }, 84 { 85 name: "TableSection and ElementSection", 86 input: &Module{ 87 TableSection: []Table{{Min: 1}}, 88 ElementSection: []ElementSegment{{OffsetExpr: empty}}, 89 }, 90 expected: map[string]uint32{"element": 1, "table": 1}, 91 }, 92 { 93 name: "TableSection (multiple tables) and ElementSection", 94 input: &Module{ 95 TableSection: []Table{{Min: 1}, {Min: 2}}, 96 ElementSection: []ElementSegment{{OffsetExpr: empty}}, 97 }, 98 expected: map[string]uint32{"element": 1, "table": 2}, 99 }, 100 } 101 102 for _, tt := range tests { 103 tc := tt 104 105 t.Run(tc.name, func(t *testing.T) { 106 actual := map[string]uint32{} 107 for i := SectionID(0); i <= SectionIDData; i++ { 108 if size := tc.input.SectionElementCount(i); size > 0 { 109 actual[SectionIDName(i)] = size 110 } 111 } 112 require.Equal(t, tc.expected, actual) 113 }) 114 } 115 }