Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor kvpair, add tests #1057

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading