github.com/sandwich-go/boost@v1.3.29/xhash/hash14v/converter_test.go (about)

     1  package hash14v
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/sandwich-go/boost/xcrypto/algorithm/vigenere"
     6  	"math"
     7  	"testing"
     8  )
     9  
    10  func Test_HashID(t *testing.T) {
    11  	test(t, nil)
    12  }
    13  
    14  func Test_HashID_WithHashKey(t *testing.T) {
    15  	test(t, []byte("test"))
    16  }
    17  
    18  func test(t *testing.T, hk []byte) {
    19  	oo := New(WithHashKey(hk), WithHashOffset([]byte("FAAAAAA")), WithUsingReservedBuff(true))
    20  	conv := oo.(*converter)
    21  	hashOffsetUsing := conv.hashOffset
    22  	hashOffsetUsing = vigenere.Encrypt(hashOffsetUsing, conv.hashKey)
    23  	uid := oo.ToId(hashOffsetUsing)
    24  	if uid != 0 {
    25  		t.Fatalf("uid with offset got:%d , except:%d", uid, 0)
    26  	}
    27  	hashID := oo.ToV(uid)
    28  	if string(hashID) != string(hashOffsetUsing) {
    29  		t.Fatalf("hashID got:%s , except:%s", string(hashID), string(hashOffsetUsing))
    30  	}
    31  
    32  	{
    33  		uid = uint64(0)
    34  		hashID = oo.ToV(uid)
    35  		fmt.Println(fmt.Sprintf("with hash offset:%s uid:%d got:%s ", hashOffsetUsing, uid, hashID))
    36  		if oo.ToId(hashID) != uid {
    37  			t.Fatalf("got:%d , except:%d", oo.ToId(hashID), uid)
    38  		}
    39  	}
    40  	{
    41  		uid = math.MaxUint64 - oo.Offset()
    42  		hashID = oo.ToV(uid)
    43  		fmt.Println(fmt.Sprintf("with hash offset:%s uid:%d got:%s ", hashOffsetUsing, uid, hashID))
    44  		if oo.ToId(hashID) != uid {
    45  			t.Fatalf("got:%d , except:%d", oo.ToId(hashID), uid)
    46  		}
    47  
    48  		if oo.ToV(uint64(math.MaxUint64)) != nil {
    49  			t.Fatal("should overflow")
    50  		}
    51  	}
    52  	{
    53  		var hashEqualOffsetLengthMax []byte
    54  		var hashEqualOffsetLengthMaxSamePrefix []byte
    55  		for range hashOffsetUsing {
    56  			hashEqualOffsetLengthMax = append(hashEqualOffsetLengthMax, 'Z')
    57  			hashEqualOffsetLengthMaxSamePrefix = append(hashEqualOffsetLengthMaxSamePrefix, 'Z')
    58  		}
    59  		hashEqualOffsetLengthMaxSamePrefix[0] = conv.hashOffset[0]
    60  		vigenere.EncryptAndInplace(hashEqualOffsetLengthMax, conv.hashKey)
    61  		vigenere.EncryptAndInplace(hashEqualOffsetLengthMaxSamePrefix, conv.hashKey)
    62  		uid1 := oo.ToId(hashEqualOffsetLengthMax)
    63  		uid2 := oo.ToId(hashEqualOffsetLengthMaxSamePrefix)
    64  		fmt.Println(fmt.Sprintf("with hash offset:%s, %s got :%d  %s got :%d", hashOffsetUsing, string(hashEqualOffsetLengthMax), uid1, hashEqualOffsetLengthMaxSamePrefix, uid2))
    65  	}
    66  }
    67  
    68  func BenchmarkFromUInt64(b *testing.B) {
    69  	b.ResetTimer()
    70  	b.ReportAllocs()
    71  	for i := 0; i < b.N; i++ {
    72  		ToV(123456789)
    73  	}
    74  }
    75  
    76  func BenchmarkToUInt64(b *testing.B) {
    77  	b.ResetTimer()
    78  	b.ReportAllocs()
    79  	for i := 0; i < b.N; i++ {
    80  		ToId([]byte("YESTTES"))
    81  	}
    82  }