Skip to content

Commit

Permalink
add delete and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roseduan committed Aug 14, 2023
1 parent f292746 commit fb94bc9
Showing 1 changed file with 77 additions and 16 deletions.
93 changes: 77 additions & 16 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,66 @@ func TestOpen(t *testing.T) {

func TestTable_Put(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTablePutOrGet(t, 16, 1000000, false)
testTableBaisc(t, 16, 1000000, false, false, false)
})
t.Run("20B-100000", func(t *testing.T) {
testTablePutOrGet(t, 16, 1000000, false)
testTableBaisc(t, 16, 1000000, false, false, false)
})
t.Run("1K-50000", func(t *testing.T) {
testTablePutOrGet(t, 1024, 50000, false)
testTableBaisc(t, 1024, 50000, false, false, false)
})
t.Run("4K-50000", func(t *testing.T) {
testTablePutOrGet(t, 4*1024, 50000, false)
testTableBaisc(t, 4*1024, 50000, false, false, false)
})
}

func TestTable_Get(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTablePutOrGet(t, 16, 1000000, true)
testTableBaisc(t, 16, 1000000, true, false, false)
})
t.Run("20B-1000000", func(t *testing.T) {
testTablePutOrGet(t, 16, 1000000, true)
testTableBaisc(t, 16, 1000000, true, false, false)
})
t.Run("1K-50000", func(t *testing.T) {
testTablePutOrGet(t, 1024, 50000, true)
testTableBaisc(t, 1024, 50000, true, false, false)
})
t.Run("4K-50000", func(t *testing.T) {
testTablePutOrGet(t, 4*1024, 50000, true)
testTableBaisc(t, 4*1024, 50000, true, false, false)
})
}

func testTablePutOrGet(t *testing.T, valueLen uint32, count int, needGet bool) {
name := "put"
if needGet {
name = "get"
}
dir, err := os.MkdirTemp("", "diskhash-test-"+name)
func TestTable_Delete(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, true, false)
})
t.Run("20B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, true, false)
})
t.Run("1K-50000", func(t *testing.T) {
testTableBaisc(t, 1024, 50000, false, true, false)
})
t.Run("4K-50000", func(t *testing.T) {
testTableBaisc(t, 4*1024, 50000, false, true, false)
})
}

func TestTable_Update(t *testing.T) {
t.Run("16B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, false, true)
})
t.Run("20B-1000000", func(t *testing.T) {
testTableBaisc(t, 16, 1000000, false, false, true)
})
t.Run("1K-50000", func(t *testing.T) {
testTableBaisc(t, 1024, 50000, false, false, true)
})
t.Run("4K-50000", func(t *testing.T) {
testTableBaisc(t, 4*1024, 50000, false, false, true)
})
}

func testTableBaisc(t *testing.T, valueLen uint32, count int, needGet, needDelete, needUpdate bool) {
dir, err := os.MkdirTemp("", "diskhash-test")
assert.Nil(t, err)

options := DefaultOptions
Expand All @@ -87,7 +113,7 @@ func testTablePutOrGet(t *testing.T, valueLen uint32, count int, needGet bool) {
assert.Nil(t, err)
}

if needGet {
getValue := func(target []byte) {
for i := 0; i < count; i++ {
key := GetTestKey(i)
var res []byte
Expand All @@ -100,9 +126,44 @@ func testTablePutOrGet(t *testing.T, valueLen uint32, count int, needGet bool) {
return false, nil
}
err := table.Get(key, matchKey)
assert.Equal(t, value, res)
assert.Equal(t, target, res)
assert.Nil(t, err)
}
}

if needGet {
getValue(value)
}

if needDelete {
for i := 0; i < count; i++ {
key := GetTestKey(i)
matchKey := func(slot Slot) (bool, error) {
if getKeyHash(key) == slot.Hash {
return true, nil
}
return false, nil
}
err := table.Delete(key, matchKey)
assert.Nil(t, err)
}
getValue(nil)
}

if needUpdate {
newValue := []byte(strings.Repeat("H", int(valueLen)))
for i := 0; i < count; i++ {
key := GetTestKey(i)
matchKey := func(slot Slot) (bool, error) {
if getKeyHash(key) == slot.Hash {
return true, nil
}
return false, nil
}
err := table.Put(key, newValue, matchKey)
assert.Nil(t, err)
}
getValue(newValue)
}
}

Expand Down

0 comments on commit fb94bc9

Please sign in to comment.