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

fix: add more ut #2

Merged
merged 1 commit into from
Aug 14, 2023
Merged
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
59 changes: 57 additions & 2 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package diskhash

import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"

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

func destroyTable(t *Table) {
Expand Down Expand Up @@ -59,9 +60,63 @@ func testTablePut(t *testing.T, valueLen uint32, count int) {
defer destroyTable(table)

for i := 0; i < count; i++ {
err = table.Put(GetTestKey(i), []byte(strings.Repeat("D", int(valueLen))), func(slot Slot) (bool, error) {
key := GetTestKey(i)
value := []byte(strings.Repeat("D", int(valueLen)))

err = table.Put(key, value, func(slot Slot) (bool, error) {
return false, nil
})

assert.Nil(t, err)
}
}

func TestTableCrud(t *testing.T) {
dir, err := os.MkdirTemp("", "diskhash-test-crud")
assert.Nil(t, err)

options := DefaultOptions
options.DirPath = dir
options.SlotValueLength = 32
table, err := Open(options)
assert.Nil(t, err)
defer destroyTable(table)

for i := 0; i < 100; i++ {
var cur []byte

getFunc := func(slot Slot) (bool, error) {
cur = slot.Value
return false, nil
}
updateFunc := func(slot Slot) (bool, error) {
return false, nil
}

key := GetTestKey(i)
value := []byte(strings.Repeat("D", 32))

// put
err = table.Put(key, value, updateFunc)
assert.Nil(t, err)

// get
err = table.Get(key, getFunc)
assert.Nil(t, err)
assert.Equal(t, value, cur)

// put different value
value = []byte(strings.Repeat("A", 32))
err = table.Put(key, value, updateFunc)
assert.Nil(t, err)

// get after put different value
err = table.Get(key, getFunc)
assert.Nil(t, err)
assert.Equal(t, value, cur)

// delete
err = table.Delete(key, updateFunc)
assert.Nil(t, err)
}
}
Loading