github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/memstore/common/data_value_test.go (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"bytes"
    19  	"github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	"github.com/uber/aresdb/utils"
    22  	"unsafe"
    23  )
    24  
    25  var _ = ginkgo.Describe("data value", func() {
    26  
    27  	ginkgo.It("value comparison int8", func() {
    28  		var v1 int8 = -20
    29  		var v2 int8 = -20
    30  		Ω(CompareInt8(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) == 0).Should(BeTrue())
    31  		v2 = -30
    32  		Ω(CompareInt8(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) > 0).Should(BeTrue())
    33  		v2 = 1
    34  		Ω(CompareInt8(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) < 0).Should(BeTrue())
    35  	})
    36  
    37  	ginkgo.It("value comparison uint8", func() {
    38  		var v1 uint8 = 20
    39  		var v2 uint8 = 20
    40  		Ω(CompareUint8(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) == 0).Should(BeTrue())
    41  		v2 = 0
    42  		Ω(CompareUint8(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) > 0).Should(BeTrue())
    43  		v2 = 30
    44  		Ω(CompareUint8(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) < 0).Should(BeTrue())
    45  	})
    46  
    47  	ginkgo.It("value comparison int16", func() {
    48  		var v1 int16 = -20
    49  		var v2 int16 = -20
    50  		Ω(CompareInt16(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) == 0).Should(BeTrue())
    51  		v2 = -30
    52  		Ω(CompareInt16(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) > 0).Should(BeTrue())
    53  		v2 = 1
    54  		Ω(CompareInt16(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) < 0).Should(BeTrue())
    55  	})
    56  
    57  	ginkgo.It("value comparison uint16", func() {
    58  		var v1 uint16 = 20
    59  		var v2 uint16 = 20
    60  		Ω(CompareUint16(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) == 0).Should(BeTrue())
    61  		v2 = 0
    62  		Ω(CompareUint16(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) > 0).Should(BeTrue())
    63  		v2 = 30
    64  		Ω(CompareUint16(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) < 0).Should(BeTrue())
    65  	})
    66  
    67  	ginkgo.It("value comparison int32", func() {
    68  		var v1 int32 = -20
    69  		var v2 int32 = -20
    70  		Ω(CompareInt32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) == 0).Should(BeTrue())
    71  		v2 = -30
    72  		Ω(CompareInt32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) > 0).Should(BeTrue())
    73  		v2 = 1
    74  		Ω(CompareInt32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) < 0).Should(BeTrue())
    75  	})
    76  
    77  	ginkgo.It("value comparison uint32", func() {
    78  		var v1 uint32 = 20
    79  		var v2 uint32 = 20
    80  		Ω(CompareUint32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) == 0).Should(BeTrue())
    81  		v2 = 0
    82  		Ω(CompareUint32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) > 0).Should(BeTrue())
    83  		v2 = 30
    84  		Ω(CompareUint32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) < 0).Should(BeTrue())
    85  	})
    86  
    87  	ginkgo.It("value comparison float32", func() {
    88  		var v1 float32 = -0.35
    89  		var v2 float32 = -0.35
    90  		Ω(CompareFloat32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) == 0).Should(BeTrue())
    91  		v2 = -1.3
    92  		Ω(CompareFloat32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) > 0).Should(BeTrue())
    93  		v2 = 0.1
    94  		Ω(CompareFloat32(unsafe.Pointer(&v1), unsafe.Pointer(&v2)) < 0).Should(BeTrue())
    95  	})
    96  
    97  	ginkgo.It("value comparison bool", func() {
    98  		Ω(CompareBool(false, false)).Should(Equal(0))
    99  		Ω(CompareBool(true, true)).Should(Equal(0))
   100  		Ω(CompareBool(false, true)).Should(Equal(-1))
   101  		Ω(CompareBool(true, false)).Should(Equal(1))
   102  	})
   103  
   104  	ginkgo.It("test value from string", func() {
   105  		val, err := ValueFromString("null", Bool)
   106  		Ω(err).Should(BeNil())
   107  		Ω(val.Valid).Should(BeFalse())
   108  
   109  		// test bool val
   110  		val, err = ValueFromString("invalid bool", Bool)
   111  		Ω(err).ShouldNot(BeNil())
   112  		val, err = ValueFromString("true", Bool)
   113  		Ω(val.Valid).Should(BeTrue())
   114  		Ω(val.BoolVal).Should(BeTrue())
   115  
   116  		// int8 out of range
   117  		val, err = ValueFromString("128", Int8)
   118  		Ω(err).ShouldNot(BeNil())
   119  		val, err = ValueFromString("127", Int8)
   120  		Ω(val.Valid).Should(BeTrue())
   121  		Ω(*(*int8)(val.OtherVal)).Should(BeEquivalentTo(127))
   122  
   123  		// uint8
   124  		val, err = ValueFromString("256", Uint8)
   125  		Ω(err).ShouldNot(BeNil())
   126  		val, err = ValueFromString("255", Uint8)
   127  		Ω(val.Valid).Should(BeTrue())
   128  		Ω(*(*uint8)(val.OtherVal)).Should(BeEquivalentTo(255))
   129  
   130  		// small enum
   131  		val, err = ValueFromString("256", SmallEnum)
   132  		Ω(err).ShouldNot(BeNil())
   133  		val, err = ValueFromString("255", SmallEnum)
   134  		Ω(val.Valid).Should(BeTrue())
   135  		Ω(*(*uint8)(val.OtherVal)).Should(BeEquivalentTo(255))
   136  
   137  		// int16
   138  		val, err = ValueFromString("32768", Int16)
   139  		Ω(err).ShouldNot(BeNil())
   140  		val, err = ValueFromString("32767", Int16)
   141  		Ω(val.Valid).Should(BeTrue())
   142  		Ω(*(*int16)(val.OtherVal)).Should(BeEquivalentTo(32767))
   143  
   144  		// uint16
   145  		val, err = ValueFromString("65536", Uint16)
   146  		Ω(err).ShouldNot(BeNil())
   147  		val, err = ValueFromString("65535", Uint16)
   148  		Ω(val.Valid).Should(BeTrue())
   149  		Ω(*(*uint16)(val.OtherVal)).Should(BeEquivalentTo(65535))
   150  
   151  		// big enum
   152  		val, err = ValueFromString("65536", BigEnum)
   153  		Ω(err).ShouldNot(BeNil())
   154  		val, err = ValueFromString("65535", BigEnum)
   155  		Ω(val.Valid).Should(BeTrue())
   156  		Ω(*(*uint16)(val.OtherVal)).Should(BeEquivalentTo(65535))
   157  
   158  		// int32
   159  		val, err = ValueFromString("2147483648", Int32)
   160  		Ω(err).ShouldNot(BeNil())
   161  		val, err = ValueFromString("2147483647", Int32)
   162  		Ω(val.Valid).Should(BeTrue())
   163  		Ω(*(*int32)(val.OtherVal)).Should(BeEquivalentTo(2147483647))
   164  
   165  		// uint32
   166  		val, err = ValueFromString("4294967296", Uint32)
   167  		Ω(err).ShouldNot(BeNil())
   168  		val, err = ValueFromString("4294967295", Uint32)
   169  		Ω(val.Valid).Should(BeTrue())
   170  		Ω(*(*uint32)(val.OtherVal)).Should(BeEquivalentTo(4294967295))
   171  
   172  		// float32
   173  		val, err = ValueFromString("0.10.1", Float32)
   174  		Ω(err).ShouldNot(BeNil())
   175  		val, err = ValueFromString("0.1", Float32)
   176  		Ω(val.Valid).Should(BeTrue())
   177  		Ω(*(*float32)(val.OtherVal)).Should(BeEquivalentTo(float32(0.1)))
   178  
   179  		// uuid
   180  		val, err = ValueFromString("01000000000000000100000000000000", UUID)
   181  		Ω(err).Should(BeNil())
   182  		Ω(val.Valid).Should(BeTrue())
   183  		Ω(*(*[2]uint64)(val.OtherVal)).Should(Equal([2]uint64{1, 1}))
   184  
   185  		val, err = ValueFromString("01000000000000000100000000000", UUID)
   186  		Ω(err).ShouldNot(BeNil())
   187  
   188  		val, err = ValueFromString("01000000-00000000-01000000-00000000", UUID)
   189  		Ω(err).Should(BeNil())
   190  		Ω(*(*[2]uint64)(val.OtherVal)).Should(Equal([2]uint64{1, 1}))
   191  	})
   192  
   193  	ginkgo.It("GetBytes of GeoShapeGo should work", func() {
   194  		shape1 := &GeoShapeGo{
   195  			Polygons: [][]GeoPointGo{
   196  				{
   197  					{
   198  						180.0,
   199  						90.0,
   200  					},
   201  				},
   202  				{
   203  					{
   204  						180.0,
   205  						90.0,
   206  					},
   207  					{
   208  						180.0,
   209  						90.0,
   210  					},
   211  				},
   212  			},
   213  		}
   214  		Ω(shape1.GetBytes()).Should(Equal(24))
   215  	})
   216  
   217  	ginkgo.It("Read and Write GeoShapeGo should work", func() {
   218  		buffer := &bytes.Buffer{}
   219  		dataWriter := utils.NewStreamDataWriter(buffer)
   220  
   221  		shape1 := &GeoShapeGo{
   222  			Polygons: [][]GeoPointGo{
   223  				{
   224  					{
   225  						180.0,
   226  						90.0,
   227  					},
   228  				},
   229  				{
   230  					{
   231  						180.0,
   232  						90.0,
   233  					},
   234  					{
   235  						180.0,
   236  						90.0,
   237  					},
   238  				},
   239  			},
   240  		}
   241  
   242  		shape1.Write(&dataWriter)
   243  
   244  		shape2 := &GeoShapeGo{}
   245  		dataReader := utils.NewStreamDataReader(buffer)
   246  		shape2.Read(&dataReader)
   247  
   248  		Ω(shape2).Should(Equal(shape1))
   249  	})
   250  
   251  	ginkgo.It("ConvertToHumanReadable GeoShape should work", func() {
   252  		shape := &GeoShapeGo{
   253  			Polygons: [][]GeoPointGo{
   254  				{
   255  					{
   256  						90.0,
   257  						180.0,
   258  					},
   259  				},
   260  				{
   261  					{
   262  						90.0,
   263  						180.0,
   264  					},
   265  					{
   266  						90.0,
   267  						180.0,
   268  					},
   269  				},
   270  			},
   271  		}
   272  		dv := DataValue{
   273  			Valid: true,
   274  			GoVal: shape,
   275  		}
   276  		Ω(dv.ConvertToHumanReadable(GeoShape)).Should(Equal("Polygon((180.0000+90.0000),(180.0000+90.0000,180.0000+90.0000))"))
   277  	})
   278  })