github.com/coyove/common@v0.0.0-20240403014525-f70e643f9de8/shoco/shoco_test.go (about)

     1  package shoco
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func randomString() string {
    10  	ret := ""
    11  	_rand := rand.New(rand.NewSource(time.Now().UnixNano()))
    12  	ln := _rand.Intn(64) + 64
    13  
    14  	for i := 0; i < ln; i++ {
    15  		ret += string(32 + byte(_rand.Intn(90)))
    16  	}
    17  
    18  	return ret
    19  }
    20  
    21  func randomBytes() []byte {
    22  	_rand := rand.New(rand.NewSource(time.Now().UnixNano()))
    23  	ln := _rand.Intn(64) + 64
    24  	ret := make([]byte, ln)
    25  
    26  	for i := 0; i < ln; i++ {
    27  		ret[i] = byte(_rand.Intn(256))
    28  	}
    29  
    30  	return ret
    31  }
    32  
    33  func TestShoco(t *testing.T) {
    34  	t.Log("Testing shoco compressing and decompressing")
    35  
    36  	for i := 0; i < 65536; i++ {
    37  		v := randomString()
    38  		if v != Decompress(Compress(v)) {
    39  			t.Error("Shoco failed", v)
    40  		}
    41  	}
    42  }
    43  
    44  func TestShocoRandomDecompression(t *testing.T) {
    45  	t.Log("Testing shoco random decompressing")
    46  
    47  	for i := 0; i < 65536; i++ {
    48  		v := randomBytes()
    49  		Decompress(v)
    50  	}
    51  }