github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/std/kvpair_test.go (about)

     1  package std
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestKVPairs(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	kvs := KVPairs{
    13  		{Key: []byte("k2"), Value: []byte("")},
    14  		{Key: []byte("k1"), Value: []byte("2")},
    15  		{Key: []byte("k1"), Value: []byte("1")},
    16  		{Key: []byte("k1"), Value: []byte("2")},
    17  	}
    18  
    19  	// Sort() essentially tests Less() and Swap() as well
    20  	assert.Equal(t, 4, kvs.Len())
    21  	kvs.Sort()
    22  	assert.Equal(t, 4, kvs.Len())
    23  
    24  	kvs2 := KVPairs{
    25  		{Key: []byte("k1"), Value: []byte("1")},
    26  		{Key: []byte("k1"), Value: []byte("2")},
    27  		{Key: []byte("k1"), Value: []byte("2")},
    28  		{Key: []byte("k2"), Value: []byte("")},
    29  	}
    30  	for i := 0; i < kvs.Len(); i++ {
    31  		assert.Equal(t, kvs[i].Key, kvs2[i].Key)
    32  		assert.Equal(t, kvs[i].Value, kvs2[i].Value)
    33  	}
    34  }
    35  
    36  func TestKI64Pairs(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	kvs := KI64Pairs{
    40  		{Key: []byte("k2"), Value: 0},
    41  		{Key: []byte("k1"), Value: 2},
    42  		{Key: []byte("k1"), Value: 1},
    43  		{Key: []byte("k1"), Value: 2},
    44  	}
    45  
    46  	// Sort() essentially tests Less() and Swap() as well
    47  	assert.Equal(t, 4, kvs.Len())
    48  	kvs.Sort()
    49  	assert.Equal(t, 4, kvs.Len())
    50  
    51  	kvs2 := KI64Pairs{
    52  		{Key: []byte("k1"), Value: 1},
    53  		{Key: []byte("k1"), Value: 2},
    54  		{Key: []byte("k1"), Value: 2},
    55  		{Key: []byte("k2"), Value: 0},
    56  	}
    57  	for i := 0; i < kvs.Len(); i++ {
    58  		assert.Equal(t, kvs[i].Key, kvs2[i].Key)
    59  		assert.Equal(t, kvs[i].Value, kvs2[i].Value)
    60  	}
    61  }