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

     1  package leb128
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/wasilibs/wazerox/internal/testing/require"
     8  )
     9  
    10  // TestLeb128NoAlloc ensures no allocation required in the leb128 package.
    11  func TestLeb128NoAlloc(t *testing.T) {
    12  	t.Run("LoadUint32", func(t *testing.T) {
    13  		result := testing.Benchmark(BenchmarkLoadUint32)
    14  		require.Zero(t, result.AllocsPerOp())
    15  	})
    16  	t.Run("LoadUint64", func(t *testing.T) {
    17  		result := testing.Benchmark(BenchmarkLoadUint64)
    18  		require.Zero(t, result.AllocsPerOp())
    19  	})
    20  	t.Run("LoadInt32", func(t *testing.T) {
    21  		result := testing.Benchmark(BenchmarkLoadInt32)
    22  		require.Zero(t, result.AllocsPerOp())
    23  	})
    24  	t.Run("LoadInt64", func(t *testing.T) {
    25  		result := testing.Benchmark(BenchmarkLoadInt64)
    26  		require.Zero(t, result.AllocsPerOp())
    27  	})
    28  	t.Run("DecodeUint32", func(t *testing.T) {
    29  		result := testing.Benchmark(BenchmarkDecodeUint32)
    30  		require.Zero(t, result.AllocsPerOp())
    31  	})
    32  	t.Run("DecodeInt32", func(t *testing.T) {
    33  		result := testing.Benchmark(BenchmarkDecodeInt32)
    34  		require.Zero(t, result.AllocsPerOp())
    35  	})
    36  	t.Run("DecodeInt64", func(t *testing.T) {
    37  		result := testing.Benchmark(BenchmarkDecodeInt64)
    38  		require.Zero(t, result.AllocsPerOp())
    39  	})
    40  }
    41  
    42  func BenchmarkLoadUint32(b *testing.B) {
    43  	b.ReportAllocs()
    44  	for i := 0; i < b.N; i++ {
    45  		_, _, err := LoadUint32([]byte{0x80, 0x80, 0x80, 0x4f})
    46  		if err != nil {
    47  			b.Fatal(err)
    48  		}
    49  	}
    50  }
    51  
    52  func BenchmarkLoadUint64(b *testing.B) {
    53  	b.ReportAllocs()
    54  	for i := 0; i < b.N; i++ {
    55  		_, _, err := LoadUint64([]byte{0x80, 0x80, 0x80, 0x4f})
    56  		if err != nil {
    57  			b.Fatal(err)
    58  		}
    59  	}
    60  }
    61  
    62  func BenchmarkLoadInt32(b *testing.B) {
    63  	b.ReportAllocs()
    64  	for i := 0; i < b.N; i++ {
    65  		_, _, err := LoadInt32([]byte{0x80, 0x80, 0x80, 0x4f})
    66  		if err != nil {
    67  			b.Fatal(err)
    68  		}
    69  	}
    70  }
    71  
    72  func BenchmarkLoadInt64(b *testing.B) {
    73  	b.ReportAllocs()
    74  	for i := 0; i < b.N; i++ {
    75  		_, _, err := LoadInt64([]byte{0x80, 0x80, 0x80, 0x4f})
    76  		if err != nil {
    77  			b.Fatal(err)
    78  		}
    79  	}
    80  }
    81  
    82  func BenchmarkDecodeUint32(b *testing.B) {
    83  	b.ReportAllocs()
    84  	data := []byte{0x80, 0x80, 0x80, 0x4f}
    85  	r := bytes.NewReader(data)
    86  	for i := 0; i < b.N; i++ {
    87  		_, _, err := DecodeUint32(r)
    88  		if err != nil {
    89  			b.Fatal(err)
    90  		}
    91  		r.Reset(data)
    92  	}
    93  }
    94  
    95  func BenchmarkDecodeInt32(b *testing.B) {
    96  	b.ReportAllocs()
    97  	data := []byte{0x80, 0x80, 0x80, 0x4f}
    98  	r := bytes.NewReader(data)
    99  	for i := 0; i < b.N; i++ {
   100  		_, _, err := DecodeInt32(r)
   101  		if err != nil {
   102  			b.Fatal(err)
   103  		}
   104  		r.Reset(data)
   105  	}
   106  }
   107  
   108  func BenchmarkDecodeInt64(b *testing.B) {
   109  	b.ReportAllocs()
   110  	data := []byte{0x80, 0x80, 0x80, 0x4f}
   111  	r := bytes.NewReader(data)
   112  	for i := 0; i < b.N; i++ {
   113  		_, _, err := DecodeInt64(r)
   114  		if err != nil {
   115  			b.Fatal(err)
   116  		}
   117  		r.Reset(data)
   118  	}
   119  }