github.com/MetalBlockchain/metalgo@v1.11.9/database/helpers_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package database
     5  
     6  import (
     7  	"math/rand"
     8  	"slices"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/MetalBlockchain/metalgo/utils"
    15  )
    16  
    17  func TestSortednessUint64(t *testing.T) {
    18  	seed := time.Now().UnixNano()
    19  	t.Log("Seed: ", seed)
    20  	rand := rand.New(rand.NewSource(seed)) //#nosec G404
    21  
    22  	ints := make([]uint64, 1024)
    23  	for i := range ints {
    24  		ints[i] = rand.Uint64()
    25  	}
    26  	slices.Sort(ints)
    27  
    28  	intBytes := make([][]byte, 1024)
    29  	for i, val := range ints {
    30  		intBytes[i] = PackUInt64(val)
    31  	}
    32  	require.True(t, utils.IsSortedBytes(intBytes))
    33  }
    34  
    35  func TestSortednessUint32(t *testing.T) {
    36  	seed := time.Now().UnixNano()
    37  	t.Log("Seed: ", seed)
    38  	rand := rand.New(rand.NewSource(seed)) //#nosec G404
    39  
    40  	ints := make([]uint32, 1024)
    41  	for i := range ints {
    42  		ints[i] = rand.Uint32()
    43  	}
    44  	slices.Sort(ints)
    45  
    46  	intBytes := make([][]byte, 1024)
    47  	for i, val := range ints {
    48  		intBytes[i] = PackUInt32(val)
    49  	}
    50  	require.True(t, utils.IsSortedBytes(intBytes))
    51  }