github.com/quantosnetwork/Quantos@v0.0.0-20220306172517-e20b28c5a29a/hashtable/htable_test.go (about)

     1  package hashtable
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"github.com/davecgh/go-spew/spew"
     6  	"log"
     7  	"math/rand"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  
    13  
    14  func TestHashTable_Put(t *testing.T) {
    15  	htable := &HashTable{}
    16  	contentKey, contentVal := generateHashtableContent(10)
    17  	for i, c := range contentKey {
    18  		htable.Put(c, contentVal[i])
    19  	}
    20  
    21  
    22  	//htable.PrintHashTable()
    23  
    24  	/*if htable.Size() < 10 {
    25  		log.Fatalf("hashtable should have 10 records it only has: %v", htable.Size())
    26  	}*/
    27  
    28  }
    29  
    30  func createMockHashtable(size int) *HashTable {
    31  	htable := &HashTable{}
    32  	contentKey, contentVal := generateHashtableContent(size)
    33  	for i, c := range contentKey {
    34  		htable.Put(c, contentVal[i])
    35  	}
    36  	return htable
    37  }
    38  
    39  func TestHashTable_ToBytes(t *testing.T) {
    40  
    41  	ht := createMockHashtable(10124)
    42  	b, err := ht.ToBytes()
    43  	if err != nil {
    44  		log.Fatal(err)
    45  	}
    46  	spew.Dump(b)
    47  
    48  }
    49  
    50  func randomBytes() []byte {
    51  	buf := make([]byte, 32)
    52  	rand.Seed(time.Now().UnixNano())
    53  	rand.Read(buf)
    54  	return buf
    55  }
    56  
    57  func generateHashtableContent(size int) ([]Key, []Value) {
    58  
    59  	k := make([]Key, size)
    60  	v := make([]Value, size)
    61  
    62  	for i := 0; i < size; i++ {
    63  		k[i] = Key(randomBytes())
    64  		v[i] = Value(generateRandomHash(randomBytes()))
    65  	}
    66  	return k, v
    67  
    68  }
    69  
    70  func generateRandomHash(src []byte) []byte {
    71  	hasher := sha256.New()
    72  	hasher.Write(src)
    73  	return hasher.Sum(nil)
    74  }