github.com/cloudwego/frugal@v0.1.15/internal/binary/encoder/encoder_test.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package encoder
    18  
    19  import (
    20      `bytes`
    21      `encoding/base64`
    22      `testing`
    23  
    24      `github.com/davecgh/go-spew/spew`
    25      `github.com/stretchr/testify/require`
    26  )
    27  
    28  func TestEncoder_Encode(t *testing.T) {
    29      v := TranslatorTestStruct {
    30          A: true,
    31          B: 0x12,
    32          C: 12.34,
    33          D: 0x3456,
    34          E: 0x12345678,
    35          F: 0x66778899aabbccdd,
    36          G: "hello, world",
    37          H: []byte("testbytebuffer"),
    38          I: []int32{0x11223344, 0x55667788, 3, 4, 5},
    39          J: map[string]string{"asdf": "qwer", "zxcv": "hjkl"},
    40          K: map[string]*TranslatorTestStruct{
    41              "foo": {
    42                  B: -1,
    43              },
    44          },
    45          L: &(&struct{ x bool }{true}).x,
    46          M: &(&struct{ x int8 }{0x1}).x,
    47          N: &(&struct{ x float64 }{0x12345678}).x,
    48          O: &(&struct{ x int16 }{0x1234}).x,
    49          P: &(&struct{ x int32 }{0x123456}).x,
    50          Q: &(&struct{ x int64 }{0x12345678}).x,
    51      }
    52      nb := EncodedSize(v)
    53      println("Estimated Size:", nb)
    54      buf := make([]byte, nb)
    55      ret, err := EncodeObject(buf, nil, v)
    56      if err != nil {
    57          println("Buffer Shortage:", ret - nb)
    58          require.NoError(t, err)
    59      }
    60      buf = buf[:ret]
    61      spew.Dump(buf)
    62      mm := bytes.NewBufferString("\x80\x01\x00\x01\x00\x00\x00\x01a\x00\x00\x00\x00")
    63      mm.Write(buf)
    64      println("Base64 Encoded Message:", base64.StdEncoding.EncodeToString(mm.Bytes()))
    65  }
    66  
    67  type StructSeekTest struct {
    68      H StructSeekTestSubStruct `frugal:"0,default,StructSeekTestSubStruct"`
    69      O []int8                  `frugal:"1,default,list<i8>"`
    70  }
    71  
    72  type StructSeekTestSubStruct struct {
    73      X int  `frugal:"0,default,i64"`
    74      Y *int `frugal:"1,optional,i64"`
    75  }
    76  
    77  func TestEncoder_StructSeek(t *testing.T) {
    78      c := StructSeekTest{O: []int8{-61}}
    79      buf := make([]byte, EncodedSize(c))
    80      _, _ = EncodeObject(buf, nil, c)
    81  }
    82  
    83  type ListI8UniqueOuter struct {
    84      Field0 *ListI8UniqueInner `frugal:"0,default,ListI8UniqueInner"`
    85  }
    86  
    87  type ListI8UniqueInner struct {
    88      Field12336 []int8 `frugal:"12336,default,set<i8>"`
    89  }
    90  
    91  func TestEncoder_ListI8Unique(t *testing.T) {
    92      want := &ListI8UniqueOuter{&ListI8UniqueInner{Field12336: []int8{-61, 67}}}
    93      buf := make([]byte, EncodedSize(want))
    94      _, err := EncodeObject(buf, nil, want)
    95      if err != nil {
    96          t.Fatal(err)
    97      }
    98  }
    99  
   100  type Foo struct {
   101      Message string `frugal:"1,required"`
   102  }
   103  
   104  type ContainerPointerElement struct {
   105      Data map[int64]*Foo `frugal:"1,required"`
   106  }
   107  
   108  func TestEncoder_ContainerPointerElement(t *testing.T) {
   109      want := &ContainerPointerElement{map[int64]*Foo{0x1234: nil}}
   110      nb := EncodedSize(want)
   111      println("encoded size =", nb)
   112      buf := make([]byte, nb)
   113      nx, err := EncodeObject(buf, nil, want)
   114      if err != nil {
   115          t.Fatal(err)
   116      }
   117      spew.Dump(buf[:nx])
   118      require.Equal(t, []byte{
   119          0x0d, 0x00, 0x01, 0x0a, 0x0c, 0x00, 0x00, 0x00, 0x01,   // field 1: map<i64, struct>, len = 1
   120          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34,         //     key   = (i64) 0x1234
   121          0x00,                                                   //     value = (struct) {}
   122          0x00,                                                   // end
   123      }, buf[:nx])
   124  }