From 060c1d2bfb8bc8280178fa2500d198506732289e Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Sun, 26 Sep 2021 16:48:13 +0800 Subject: [PATCH] test(structure): test stringx type --- pkg/structure/stringx/string_test.go | 137 +++++++++++++++++++++++++-- pkg/structure/value_test.go | 11 +++ 2 files changed, 140 insertions(+), 8 deletions(-) diff --git a/pkg/structure/stringx/string_test.go b/pkg/structure/stringx/string_test.go index 4150247..2ae2d85 100644 --- a/pkg/structure/stringx/string_test.go +++ b/pkg/structure/stringx/string_test.go @@ -1,19 +1,140 @@ package stringx import ( - "fmt" "gitee.com/timedb/wheatCache/pkg/proto" "github.com/stretchr/testify/require" "testing" ) func TestStringSingle_Set(t *testing.T) { - s := new(StringSingle) - p := s.SizeByte() - require.Equal(t, p, int64(16)) - s.Set(&proto.SetRequest{ - Val: "", + s := NewStringSingle() + resp, err := s.Set(&proto.SetRequest{ + Val: "189", }) - p = s.SizeByte() - fmt.Println(p) + 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") + + s.Set(&proto.SetRequest{ + Val: "awdgiugaiuwdhoawd", + }) + i, err = s.Get(&proto.GetRequest{}) + require.Equal(t, i.Result, "awdgiugaiuwdhoawd") +} + +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") + + // 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") + + // string + _, err = s.Set(&proto.SetRequest{ + Val: "awjdwad", + }) + require.NoError(t, err) + _, err = s.Add(&proto.AddRequest{ + Renewal: 3, + }) + 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") + + // 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") + + // string + _, err = s.Set(&proto.SetRequest{ + Val: "awjdwad", + }) + require.NoError(t, err) + _, err = s.Reduce(&proto.ReduceRequest{ + Renewal: 3, + }) + 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) + + res, err := s.Getbit(&proto.GetbitRequest{ + Offer: 1009, + }) + require.NoError(t, err) + require.Equal(t, res.Val, true) + res, err = s.Getbit(&proto.GetbitRequest{ + Offer: 1008, + }) + require.NoError(t, err) + require.Equal(t, res.Val, false) + + _, err = s.Setbit(&proto.SetbitRequest{ + Offer: 1009, + Val: false, + }) + + res, err = s.Getbit(&proto.GetbitRequest{ + Offer: 1009, + }) + require.NoError(t, err) + require.Equal(t, res.Val, false) + } diff --git a/pkg/structure/value_test.go b/pkg/structure/value_test.go index 71558b1..7c93b50 100644 --- a/pkg/structure/value_test.go +++ b/pkg/structure/value_test.go @@ -116,3 +116,14 @@ func TestValue_ChangeValue(t *testing.T) { }) require.Equal(t, lengs, int64(39)) } + +func TestValue_SetByte(t *testing.T) { + value := NewValue() + value.SetByte(10001, true) + v, err := value.GetByte(123) + require.NoError(t, err) + require.Equal(t, v, false) + v, err = value.GetByte(10001) + require.NoError(t, err) + require.Equal(t, v, true) +}