github.com/code-to-go/safepool.lib@v0.0.0-20221205180519-ee25e63c226e/algo/hashsplit_test.go (about)

     1  package algo
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"math/rand"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Test_Hashsplit(t *testing.T) {
    14  	s := "This is a simple string test"
    15  	blocks, err := HashSplit(bytes.NewBufferString(s), 13, nil)
    16  	assert.NoErrorf(t, err, "Cannot split hash: %v", err)
    17  	assert.Equal(t, len(blocks), 1, "unexpected hashes number")
    18  
    19  	hash_str := hex.EncodeToString(blocks[0].Hash[:])
    20  	assert.Equal(t, "5468697320697320612073696d706c6520737472696e6720746573740e5751c0", hash_str,
    21  		"unexpected hash value")
    22  	assert.Equal(t, uint32(0), blocks[0].Length)
    23  
    24  	rn := make([]byte, 40000)
    25  	rand.Seed(1975)
    26  	rand.Read(rn)
    27  
    28  	blocks, err = HashSplit(bytes.NewBuffer(rn), 13, nil)
    29  	assert.NoErrorf(t, err, "Cannot split hash: %v", err)
    30  	//	assert.Equal(t, len(blocks), 17, "unexpected hashes number")
    31  	for idx, block := range blocks {
    32  		fmt.Printf("Block [%d] %d\n", idx, block.Length)
    33  	}
    34  
    35  	blocks2, err := HashSplit(bytes.NewBuffer(rn), 8, nil)
    36  	assert.NoErrorf(t, err, "Cannot split hash: %v", err)
    37  	for idx, block := range blocks2 {
    38  		fmt.Printf("Block [%d] %d\n", idx, block.Length)
    39  	}
    40  	assert.Equal(t, 18, len(blocks2), "unexpected hashes number")
    41  
    42  }
    43  
    44  func Test_HashDiff(t *testing.T) {
    45  	a := HashBlock{
    46  		Hash:   []byte{0},
    47  		Length: 4,
    48  	}
    49  	b := HashBlock{
    50  		Hash:   []byte{1},
    51  		Length: 8,
    52  	}
    53  	c := HashBlock{
    54  		Hash:   []byte{2},
    55  		Length: 8,
    56  	}
    57  
    58  	//	diffs := HashDiff([]HashBlock{a}, []HashBlock{b})
    59  	// assert.Len(t, diffs, 1)
    60  	// assert.Equal(t, Diff{0, 0, 4, 0, 8}, diffs[0])
    61  
    62  	diffs := HashDiff([]HashBlock{a, b, c}, []HashBlock{a, c})
    63  	//assert.Len(t, diffs, 1)
    64  	print(diffs)
    65  }