github.com/ledgerwatch/erigon-lib@v1.0.0/sais/gsa/gsa_test.go (about)

     1  package gsa
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/ledgerwatch/erigon-lib/sais"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestExampleGSA(t *testing.T) {
    12  	R := [][]byte{
    13  		[]byte("hihi"),
    14  		[]byte("alexhihialex"),
    15  		[]byte("alex"),
    16  	}
    17  	str, n := ConcatAll(R)
    18  	sa := make([]uint, n)
    19  	lcp := make([]int, n)
    20  	da := make([]int32, n)
    21  	_ = GSA(str, sa, lcp, da)
    22  
    23  	fmt.Printf("sa: %d, lcp: %d\n", sa, lcp)
    24  	PrintArrays(str, sa, lcp, da)
    25  	PrintRepeats(str, sa, da)
    26  	gsa := SA2GSA(sa, da)
    27  	fmt.Printf("gsa: %d, da: %d\n", gsa, da)
    28  }
    29  
    30  func TestGSA(t *testing.T) {
    31  	R := [][]byte{{4, 5, 6, 4, 5, 6, 4, 5, 6}}
    32  	str, n := ConcatAll(R)
    33  	sa := make([]uint, n)
    34  	lcp := make([]int, n)
    35  	da := make([]int32, n)
    36  	_ = GSA(str, sa, lcp, da)
    37  	assert.Equal(t, []uint{10, 9, 6, 3, 0, 7, 4, 1, 8, 5, 2}, sa[:n])
    38  }
    39  
    40  const N = 100_000
    41  
    42  func BenchmarkName(b *testing.B) {
    43  	R := make([][]byte, 0, N)
    44  	for i := 0; i < N; i++ {
    45  		R = append(R, []byte("hihihi"))
    46  	}
    47  	superstring := make([]byte, 0, 1024)
    48  
    49  	for _, a := range R {
    50  		for _, b := range a {
    51  			superstring = append(superstring, 1, b)
    52  		}
    53  		superstring = append(superstring, 0, 0)
    54  	}
    55  
    56  	sa := make([]int32, len(superstring))
    57  	b.ResetTimer()
    58  	for i := 0; i < b.N; i++ {
    59  		err := sais.Sais(superstring, sa)
    60  		if err != nil {
    61  			panic(err)
    62  		}
    63  	}
    64  }
    65  func BenchmarkName2(b *testing.B) {
    66  	R := make([][]byte, 0, N)
    67  	for i := 0; i < N; i++ {
    68  		R = append(R, []byte("hihihi"))
    69  	}
    70  	str, n := ConcatAll(R)
    71  	sa := make([]uint, n)
    72  	lcp := make([]int, n)
    73  	da := make([]int32, n)
    74  	b.ResetTimer()
    75  	for i := 0; i < b.N; i++ {
    76  		_ = GSA(str, sa, lcp, da)
    77  	}
    78  }