github.com/iotexproject/iotex-core@v1.14.1-rc1/crypto/cryptosort_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package crypto
     7  
     8  import (
     9  	"bytes"
    10  	"testing"
    11  
    12  	"github.com/iotexproject/go-pkgs/hash"
    13  	"github.com/iotexproject/iotex-core/pkg/enc"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestCryptoSort(t *testing.T) {
    18  	var hashes [][]byte
    19  	var hashescp [][]byte
    20  	for i := 100000; i < 100100; i++ {
    21  		ii := make([]byte, 8)
    22  		enc.MachineEndian.PutUint64(ii, uint64(i))
    23  		h := hash.Hash256b(ii)
    24  		hashes = append(hashes, h[:])
    25  		hashescp = append(hashescp, h[:])
    26  	}
    27  
    28  	Sort(hashes, 481)
    29  
    30  	same := true
    31  	for i, s := range hashes {
    32  		if !bytes.Equal(s, hashescp[i]) {
    33  			same = false
    34  			break
    35  		}
    36  	}
    37  	assert.False(t, same)
    38  }
    39  
    40  func TestCryptoSortCandidates(t *testing.T) {
    41  	var candidates []string
    42  	var candidatesCp []string
    43  	for i := 100000; i < 100100; i++ {
    44  		ii := make([]byte, 8)
    45  		enc.MachineEndian.PutUint64(ii, uint64(i))
    46  		h := hash.Hash256b(ii)
    47  		candidates = append(candidates, string(h[:]))
    48  		candidatesCp = append(candidatesCp, string(h[:]))
    49  	}
    50  
    51  	SortCandidates(candidates, 481, CryptoSeed)
    52  
    53  	same := true
    54  	for i, s := range candidates {
    55  		if s != candidatesCp[i] {
    56  			same = false
    57  			break
    58  		}
    59  	}
    60  	assert.False(t, same)
    61  }