github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/asm/assembler_test.go (about)

     1  package asm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/wasilibs/wazerox/internal/testing/require"
     7  )
     8  
     9  func TestNewStaticConstPool(t *testing.T) {
    10  	p := NewStaticConstPool()
    11  	require.NotNil(t, p.addedConsts)
    12  }
    13  
    14  func TestStaticConst_AddOffsetFinalizedCallback(t *testing.T) {
    15  	p := NewStaticConstPool()
    16  	const firstUseOffset uint64 = 100
    17  
    18  	// Add first const.
    19  	c := NewStaticConst([]byte{1})
    20  	p.AddConst(c, firstUseOffset)
    21  	require.Equal(t, firstUseOffset, p.FirstUseOffsetInBinary)
    22  	require.Equal(t, 1, len(p.Consts))
    23  	require.Equal(t, 1, len(p.addedConsts))
    24  
    25  	// Adding the same *StaticConst doesn't affect the state.
    26  	p.AddConst(c, firstUseOffset+10000)
    27  	require.Equal(t, firstUseOffset, p.FirstUseOffsetInBinary)
    28  	require.Equal(t, 1, len(p.Consts))
    29  	require.Equal(t, 1, len(p.addedConsts))
    30  
    31  	// Add another const.
    32  	c2 := NewStaticConst([]byte{1, 2})
    33  	p.AddConst(c2, firstUseOffset+100)
    34  	require.Equal(t, firstUseOffset, p.FirstUseOffsetInBinary) // first use doesn't change!
    35  	require.Equal(t, 2, len(p.Consts))
    36  	require.Equal(t, 2, len(p.addedConsts))
    37  }
    38  
    39  func TestStaticConst_SetOffsetInBinary(t *testing.T) {
    40  	sc := NewStaticConst([]byte{1})
    41  	const offset uint64 = 100
    42  	sc.AddOffsetFinalizedCallback(func(offsetOfConstInBinary uint64) {
    43  		require.Equal(t, offset, offsetOfConstInBinary)
    44  	})
    45  	sc.SetOffsetInBinary(offset)
    46  }