test(stringx): update stringx test
This commit is contained in:
parent
8e6f18f887
commit
e6987546ae
|
@ -1,140 +1,82 @@
|
|||
package stringx
|
||||
|
||||
import (
|
||||
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||
"gitee.com/timedb/wheatCache/pkg/structure"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStringSingle_Set(t *testing.T) {
|
||||
s := NewStringSingle()
|
||||
resp, err := s.Set(&proto.SetRequest{
|
||||
Val: "189",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "189")
|
||||
i, err := s.Get(&proto.GetRequest{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, i.Result, "189")
|
||||
s.Set(&proto.SetRequest{
|
||||
Val: "1.25",
|
||||
})
|
||||
i, err = s.Get(&proto.GetRequest{})
|
||||
require.Equal(t, i.Result, "1.25")
|
||||
resp, length := s.Set("189")
|
||||
require.Equal(t, resp, "189")
|
||||
require.Equal(t, length, structure.UpdateLength(0))
|
||||
i := s.Get()
|
||||
require.Equal(t, i, "189")
|
||||
|
||||
s.Set(&proto.SetRequest{
|
||||
Val: "awdgiugaiuwdhoawd",
|
||||
})
|
||||
i, err = s.Get(&proto.GetRequest{})
|
||||
require.Equal(t, i.Result, "awdgiugaiuwdhoawd")
|
||||
resp, length = s.Set("189.12")
|
||||
require.Equal(t, resp, "189.12")
|
||||
require.Equal(t, length, structure.UpdateLength(0))
|
||||
i = s.Get()
|
||||
require.Equal(t, i, "189.12")
|
||||
|
||||
resp, length = s.Set("awdawd")
|
||||
require.Equal(t, resp, "awdawd")
|
||||
require.Equal(t, length, structure.UpdateLength(-2))
|
||||
i = s.Get()
|
||||
require.Equal(t, i, "awdawd")
|
||||
}
|
||||
|
||||
func TestStringSingle_Add(t *testing.T) {
|
||||
s := NewStringSingle()
|
||||
_, err := s.Set(&proto.SetRequest{
|
||||
Val: "135",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
s.Add(&proto.AddRequest{
|
||||
Renewal: 9,
|
||||
})
|
||||
resp, err := s.Get(&proto.GetRequest{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "144")
|
||||
s.Set("189")
|
||||
s.Add(1)
|
||||
i := s.Get()
|
||||
require.Equal(t, i, "190")
|
||||
|
||||
// float
|
||||
_, err = s.Set(&proto.SetRequest{
|
||||
Val: "135.33",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
s.Add(&proto.AddRequest{
|
||||
Renewal: 3,
|
||||
})
|
||||
resp, err = s.Get(&proto.GetRequest{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "138.33")
|
||||
s.Set("189.2")
|
||||
s.Add(1)
|
||||
i = s.Get()
|
||||
require.Equal(t, i, "190.20")
|
||||
|
||||
// string
|
||||
_, err = s.Set(&proto.SetRequest{
|
||||
Val: "awjdwad",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
_, err = s.Add(&proto.AddRequest{
|
||||
Renewal: 3,
|
||||
})
|
||||
s.Set("wad")
|
||||
_, err := s.Add(1)
|
||||
require.Error(t, err)
|
||||
resp, err = s.Get(&proto.GetRequest{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "awjdwad")
|
||||
}
|
||||
|
||||
func TestStringSingle_Reduce(t *testing.T) {
|
||||
s := NewStringSingle()
|
||||
_, err := s.Set(&proto.SetRequest{
|
||||
Val: "135",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
s.Reduce(&proto.ReduceRequest{
|
||||
Renewal: 5,
|
||||
})
|
||||
resp, err := s.Get(&proto.GetRequest{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "130")
|
||||
s.Set("189")
|
||||
s.Reduce(1)
|
||||
i := s.Get()
|
||||
require.Equal(t, i, "188")
|
||||
|
||||
// float
|
||||
_, err = s.Set(&proto.SetRequest{
|
||||
Val: "135.33",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
s.Reduce(&proto.ReduceRequest{
|
||||
Renewal: 3,
|
||||
})
|
||||
resp, err = s.Get(&proto.GetRequest{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "132.33")
|
||||
s.Set("189.2")
|
||||
s.Reduce(1)
|
||||
i = s.Get()
|
||||
require.Equal(t, i, "188.20")
|
||||
|
||||
// string
|
||||
_, err = s.Set(&proto.SetRequest{
|
||||
Val: "awjdwad",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
_, err = s.Reduce(&proto.ReduceRequest{
|
||||
Renewal: 3,
|
||||
})
|
||||
s.Set("wad")
|
||||
_, err := s.Reduce(1)
|
||||
require.Error(t, err)
|
||||
resp, err = s.Get(&proto.GetRequest{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "awjdwad")
|
||||
}
|
||||
|
||||
func TestStringSingle_Getbit(t *testing.T) {
|
||||
s := NewStringSingle()
|
||||
_, err := s.Setbit(&proto.SetbitRequest{
|
||||
Offer: 1009,
|
||||
Val: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
length := s.Setbit(1009, true)
|
||||
require.Equal(t, length, structure.UpdateLength(1002))
|
||||
|
||||
res, err := s.Getbit(&proto.GetbitRequest{
|
||||
Offer: 1009,
|
||||
})
|
||||
res, err := s.Getbit(1009)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, res.Val, true)
|
||||
res, err = s.Getbit(&proto.GetbitRequest{
|
||||
Offer: 1008,
|
||||
})
|
||||
require.Equal(t, res, true)
|
||||
|
||||
res, err = s.Getbit(1008)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, res.Val, false)
|
||||
require.Equal(t, res, false)
|
||||
|
||||
_, err = s.Setbit(&proto.SetbitRequest{
|
||||
Offer: 1009,
|
||||
Val: false,
|
||||
})
|
||||
|
||||
res, err = s.Getbit(&proto.GetbitRequest{
|
||||
Offer: 1009,
|
||||
})
|
||||
length = s.Setbit(1009, false)
|
||||
require.Equal(t, length, structure.UpdateLength(0))
|
||||
res, err = s.Getbit(1009)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, res.Val, false)
|
||||
|
||||
require.Equal(t, res, false)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue