github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/bytepack_test.go (about)

     1  // Package cos provides common low-level types and utilities for all aistore projects
     2  /*
     3   * Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package cos_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/NVIDIA/aistore/cmn/cos"
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("BytePacker", func() {
    16  	It("Boolean", func() {
    17  		packer := cos.NewPacker(nil, 10)
    18  		arr := []bool{true, false, false, true}
    19  		for _, v := range arr {
    20  			packer.WriteBool(v)
    21  		}
    22  		bytes := packer.Bytes()
    23  		unpacker := cos.NewUnpacker(bytes)
    24  		for _, v := range arr {
    25  			read, err := unpacker.ReadBool()
    26  			Expect(err).ShouldNot(HaveOccurred())
    27  			Expect(read).To(Equal(v))
    28  		}
    29  	})
    30  	It("[U]int64", func() {
    31  		packer := cos.NewPacker(nil, 80)
    32  		for i := 100; i < 104; i++ {
    33  			packer.WriteInt64(int64(i))
    34  			packer.WriteUint64(uint64(i * 0xffffff))
    35  		}
    36  		bytes := packer.Bytes()
    37  		unpacker := cos.NewUnpacker(bytes)
    38  		for i := 100; i < 104; i++ {
    39  			i64, err := unpacker.ReadInt64()
    40  			Expect(err).ShouldNot(HaveOccurred())
    41  			Expect(i64).To(Equal(int64(i)))
    42  			u64, err := unpacker.ReadUint64()
    43  			Expect(err).ShouldNot(HaveOccurred())
    44  			Expect(u64).To(Equal(uint64(i * 0xffffff)))
    45  		}
    46  	})
    47  	It("[U]int32", func() {
    48  		packer := cos.NewPacker(nil, 80)
    49  		for i := 100; i < 104; i++ {
    50  			packer.WriteInt32(int32(i))
    51  			packer.WriteUint32(uint32(i * 0xffff))
    52  		}
    53  		bytes := packer.Bytes()
    54  		unpacker := cos.NewUnpacker(bytes)
    55  		for i := 100; i < 104; i++ {
    56  			i32, err := unpacker.ReadInt32()
    57  			Expect(err).ShouldNot(HaveOccurred())
    58  			Expect(i32).To(Equal(int32(i)))
    59  			u32, err := unpacker.ReadUint32()
    60  			Expect(err).ShouldNot(HaveOccurred())
    61  			Expect(u32).To(Equal(uint32(i * 0xffff)))
    62  		}
    63  	})
    64  	It("[U]int16", func() {
    65  		packer := cos.NewPacker(nil, 80)
    66  		for i := 100; i < 104; i++ {
    67  			packer.WriteInt16(int16(i))
    68  			packer.WriteUint16(uint16(i * 0xff))
    69  		}
    70  		bytes := packer.Bytes()
    71  		unpacker := cos.NewUnpacker(bytes)
    72  		for i := 100; i < 104; i++ {
    73  			i16, err := unpacker.ReadInt16()
    74  			Expect(err).ShouldNot(HaveOccurred())
    75  			Expect(i16).To(Equal(int16(i)))
    76  			u16, err := unpacker.ReadUint16()
    77  			Expect(err).ShouldNot(HaveOccurred())
    78  			Expect(u16).To(Equal(uint16(i * 0xff)))
    79  		}
    80  	})
    81  	It("String", func() {
    82  		packer := cos.NewPacker(nil, 120)
    83  		for i := 100; i < 104; i++ {
    84  			packer.WriteString(fmt.Sprintf("test-%04d", i))
    85  		}
    86  		bytes := packer.Bytes()
    87  		unpacker := cos.NewUnpacker(bytes)
    88  		for i := 100; i < 104; i++ {
    89  			s, err := unpacker.ReadString()
    90  			Expect(err).ShouldNot(HaveOccurred())
    91  			Expect(s).To(Equal(fmt.Sprintf("test-%04d", i)))
    92  		}
    93  	})
    94  	It("MapStrU16", func() {
    95  		packer := cos.NewPacker(nil, 400)
    96  		m := make(cos.MapStrUint16, 4)
    97  		for i := 100; i < 104; i++ {
    98  			key := fmt.Sprintf("test-%04d", i)
    99  			m[key] = uint16(i + 1)
   100  		}
   101  		for range 4 {
   102  			packer.WriteMapStrUint16(m)
   103  		}
   104  		bytes := packer.Bytes()
   105  		unpacker := cos.NewUnpacker(bytes)
   106  		for range 4 {
   107  			mp, err := unpacker.ReadMapStrUint16()
   108  			Expect(err).ShouldNot(HaveOccurred())
   109  			for k, v := range mp {
   110  				val, ok := m[k]
   111  				Expect(ok).To(BeTrue())
   112  				Expect(val).To(Equal(v))
   113  			}
   114  		}
   115  	})
   116  })