diff --git a/pkg/structure/value_test.go b/pkg/structure/value_test.go new file mode 100644 index 0000000..4e660e6 --- /dev/null +++ b/pkg/structure/value_test.go @@ -0,0 +1,86 @@ +package structure + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestValue_SetString(t *testing.T) { + value := Value{ + val: make([]byte, 32, 64), + } + testStr := "awjkbk沙袋jdawd" + value.SetString(testStr) + + require.Equal(t, testStr, value.ToString()) +} + +func TestValue_GetString(t *testing.T) { + value := NewValue() + str := "" + for i := 0; i < 100; i++ { + str += strconv.Itoa(i) + } + value.SetString(str) + require.Equal(t, value.ToString(), str) + + value.SetString("aw1") + require.Equal(t, value.ToString(), "aw1") +} + +func Test_IntToBytes(t *testing.T) { + value := NewValue() + value.SetInt(2<<62 - 1) + require.Equal(t, value.GetLength(), 8) +} + +func TestValue_ToInt(t *testing.T) { + value := NewValue() + value.SetInt(90) + rel, err := value.ToInt() + require.NoError(t, err) + require.Equal(t, rel, int64(90)) + value.SetString("abc") + require.Equal(t, value.ToString(), "abc") + _, err = value.ToInt() + require.Error(t, err) +} + +func TestValue_SetFloat64(t *testing.T) { + value := NewValue() + value.SetFloat64(2.35) + f, err := value.ToFloat64() + require.NoError(t, err) + require.Equal(t, f, 2.35) + + value.SetInt(-10) + rel, err := value.ToInt() + require.NoError(t, err) + require.Equal(t, rel, int64(-10)) + + value.SetFloat64(-2.35) + f, err = value.ToFloat64() + require.NoError(t, err) + require.Equal(t, f, -2.35) +} + +func TestValue_InferValue(t *testing.T) { + value := NewValue() + value.InferValue("hello world") + require.Equal(t, value.GetDynamicType(), DynamicString) + require.Equal(t, value.ToString(), "hello world") + + value.InferValue("1") + require.Equal(t, value.GetDynamicType(), DynamicInt) + i, err := value.ToInt() + require.NoError(t, err) + require.Equal(t, i, int64(1)) + + value.InferValue("-1.2") + require.Equal(t, value.GetDynamicType(), DynamicFloat) + f, err := value.ToFloat64() + require.NoError(t, err) + require.Equal(t, f, -1.2) +}