Skip to content

Commit

Permalink
test: refactor kvpair, add tests (#1057)
Browse files Browse the repository at this point in the history
- simplify Less() and remove the unnecessary panic check
- add comments
- add tests
  • Loading branch information
wolkykim authored Sep 6, 2023
1 parent df3a5bf commit 4586060
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
38 changes: 31 additions & 7 deletions tm2/pkg/std/kvpair.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ import (
//----------------------------------------
// KVPair

// KVPair is a key-value struct for []byte value.
type KVPair struct {
Key []byte
Value []byte
}

// KVPairs is a slice of KVPair.
type KVPairs []KVPair

// Sorting
func (kvs KVPairs) Len() int { return len(kvs) }
// Len returns the length of kvs.
func (kvs KVPairs) Len() int {
return len(kvs)
}

// Less reports whether kvs[i] should be ordered before kvs[j].
func (kvs KVPairs) Less(i, j int) bool {
switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
case -1:
Expand All @@ -30,22 +35,33 @@ func (kvs KVPairs) Less(i, j int) bool {
panic("invalid comparison result")
}
}
func (kvs KVPairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
func (kvs KVPairs) Sort() { sort.Sort(kvs) }

// Swap swaps the elements with indexes, i and j.
func (kvs KVPairs) Swap(i, j int) {
kvs[i], kvs[j] = kvs[j], kvs[i]
}

// Sort sorts a kvs in ascending order.
func (kvs KVPairs) Sort() {
sort.Sort(kvs)
}

//----------------------------------------
// KI64Pair

// KI64Pair is a key-value struct for int64 value.
type KI64Pair struct {
Key []byte
Value int64
}

// KI64Pairs is a slice of KI64Pair.
type KI64Pairs []KI64Pair

// Sorting
// Len returns the length of kvs.
func (kvs KI64Pairs) Len() int { return len(kvs) }

// Less reports whether kvs[i] should be ordered before kvs[j].
func (kvs KI64Pairs) Less(i, j int) bool {
switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
case -1:
Expand All @@ -58,5 +74,13 @@ func (kvs KI64Pairs) Less(i, j int) bool {
panic("invalid comparison result")
}
}
func (kvs KI64Pairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
func (kvs KI64Pairs) Sort() { sort.Sort(kvs) }

// Swap swaps the elements with indexes, i and j.
func (kvs KI64Pairs) Swap(i, j int) {
kvs[i], kvs[j] = kvs[j], kvs[i]
}

// Sort sorts a kvs in ascending order.
func (kvs KI64Pairs) Sort() {
sort.Sort(kvs)
}
57 changes: 57 additions & 0 deletions tm2/pkg/std/kvpair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package std

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestKVPairs(t *testing.T) {
kvs := KVPairs{
{Key: []byte("k2"), Value: []byte("")},
{Key: []byte("k1"), Value: []byte("2")},
{Key: []byte("k1"), Value: []byte("1")},
{Key: []byte("k1"), Value: []byte("2")},
}

// Sort() essentially tests Less() and Swap() as well
assert.Equal(t, 4, kvs.Len())
kvs.Sort()
assert.Equal(t, 4, kvs.Len())

kvs2 := KVPairs{
{Key: []byte("k1"), Value: []byte("1")},
{Key: []byte("k1"), Value: []byte("2")},
{Key: []byte("k1"), Value: []byte("2")},
{Key: []byte("k2"), Value: []byte("")},
}
for i := 0; i < kvs.Len(); i++ {
assert.Equal(t, kvs[i].Key, kvs2[i].Key)
assert.Equal(t, kvs[i].Value, kvs2[i].Value)
}
}

func TestKI64Pairs(t *testing.T) {
kvs := KI64Pairs{
{Key: []byte("k2"), Value: 0},
{Key: []byte("k1"), Value: 2},
{Key: []byte("k1"), Value: 1},
{Key: []byte("k1"), Value: 2},
}

// Sort() essentially tests Less() and Swap() as well
assert.Equal(t, 4, kvs.Len())
kvs.Sort()
assert.Equal(t, 4, kvs.Len())

kvs2 := KI64Pairs{
{Key: []byte("k1"), Value: 1},
{Key: []byte("k1"), Value: 2},
{Key: []byte("k1"), Value: 2},
{Key: []byte("k2"), Value: 0},
}
for i := 0; i < kvs.Len(); i++ {
assert.Equal(t, kvs[i].Key, kvs2[i].Key)
assert.Equal(t, kvs[i].Value, kvs2[i].Value)
}
}

0 comments on commit 4586060

Please sign in to comment.