github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/ast/buffer_test.go (about)

     1  /**
     2   * Copyright 2023 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 ast
    18  
    19  import (
    20      `strconv`
    21      `testing`
    22  
    23      `github.com/stretchr/testify/require`
    24  )
    25  
    26  func makeNodes(l int) []Node {
    27      r := make([]Node, l)
    28      for i := 0; i < l; i++ {
    29          r[i] = NewBool(true)
    30      }
    31      return r
    32  }
    33  
    34  func makePairs(l int) []Pair {
    35      r := make([]Pair, l)
    36      for i := 0; i < l; i++ {
    37          r[i] = Pair{strconv.Itoa(i), NewBool(true)}
    38      }
    39      return r
    40  }
    41  
    42  func Test_linkedPairs_Push(t *testing.T) {
    43      type args struct {
    44          in  []Pair
    45          v Pair
    46          exp []Pair
    47      }
    48      tests := []struct {
    49          name   string
    50          args   args
    51      }{
    52          {
    53              name: "add empty",
    54              args: args{
    55                  in: []Pair{},
    56                  v: Pair{"a", NewBool(true)},
    57                  exp: []Pair{Pair{"a", NewBool(true)}},
    58              },
    59          },
    60          {
    61              name: "add one",
    62              args: args{
    63                  in: []Pair{{"a", NewBool(false)}},
    64                  v: Pair{"b", NewBool(true)},
    65                  exp: []Pair{{"a", NewBool(false)}, {"b", NewBool(true)}},
    66              },
    67          },
    68          {
    69              name: "add _DEFAULT_NODE_CAP",
    70              args: args{
    71                  in: makePairs(_DEFAULT_NODE_CAP),
    72                  v: Pair{strconv.Itoa(_DEFAULT_NODE_CAP), NewBool(true)},
    73                  exp: makePairs(_DEFAULT_NODE_CAP+1),
    74              },
    75          },
    76      }
    77      for _, tt := range tests {
    78          t.Run(tt.name, func(t *testing.T) {
    79              self := &linkedPairs{}
    80              self.FromSlice(tt.args.in)
    81              self.Push(tt.args.v)
    82              act := make([]Pair, self.Len())
    83              self.ToSlice(act)
    84              require.Equal(t, tt.args.exp, act)
    85          })
    86      }
    87  }
    88  
    89  
    90  func Test_linkedNodes_Push(t *testing.T) {
    91      type args struct {
    92          in  []Node
    93          v Node
    94          exp []Node
    95      }
    96      tests := []struct {
    97          name   string
    98          args   args
    99      }{
   100          {
   101              name: "add empty",
   102              args: args{
   103                  in: []Node{},
   104                  v: NewBool(true),
   105                  exp: []Node{NewBool(true)},
   106              },
   107          },
   108          {
   109              name: "add one",
   110              args: args{
   111                  in: []Node{NewBool(false)},
   112                  v: NewBool(true),
   113                  exp: []Node{NewBool(false), NewBool(true)},
   114              },
   115          },
   116          {
   117              name: "add _DEFAULT_NODE_CAP",
   118              args: args{
   119                  in: makeNodes(_DEFAULT_NODE_CAP),
   120                  v: NewBool(true),
   121                  exp: makeNodes(_DEFAULT_NODE_CAP+1),
   122              },
   123          },
   124      }
   125      for _, tt := range tests {
   126          t.Run(tt.name, func(t *testing.T) {
   127              self := &linkedNodes{}
   128              self.FromSlice(tt.args.in)
   129              self.Push(tt.args.v)
   130              act := make([]Node, self.Len())
   131              self.ToSlice(act)
   132              require.Equal(t, tt.args.exp, act)
   133          })
   134      }
   135  }
   136  
   137  func Test_linkedNodes_Pop(t *testing.T) {
   138      type args struct {
   139          in  []Node
   140          exp []Node
   141      }
   142      tests := []struct {
   143          name   string
   144          args   args
   145      }{
   146          {
   147              name: "remove empty",
   148              args: args{
   149                  in: []Node{},
   150                  exp: []Node{},
   151              },
   152          },
   153          {
   154              name: "remove one",
   155              args: args{
   156                  in: []Node{NewBool(false)},
   157                  exp: []Node{},
   158              },
   159          },
   160          {
   161              name: "add _DEFAULT_NODE_CAP",
   162              args: args{
   163                  in: makeNodes(_DEFAULT_NODE_CAP),
   164                  exp: makeNodes(_DEFAULT_NODE_CAP-1),
   165              },
   166          },
   167      }
   168      for _, tt := range tests {
   169          t.Run(tt.name, func(t *testing.T) {
   170              self := &linkedNodes{}
   171              self.FromSlice(tt.args.in)
   172              self.Pop()
   173              act := make([]Node, self.Len())
   174              self.ToSlice(act)
   175              require.Equal(t, tt.args.exp, act)
   176          })
   177      }
   178  }
   179  
   180  func Test_linkedNodes_MoveOne(t *testing.T) {
   181      type args struct {
   182          in  []Node
   183          source int
   184          target int
   185          exp []Node
   186      }
   187      tests := []struct {
   188          name   string
   189          args   args
   190      }{
   191          {
   192              name: "over index",
   193              args: args{
   194                  in: []Node{NewBool(true)},
   195                  source: 1,
   196                  target: 0,
   197                  exp: []Node{NewBool(true)},
   198              },
   199          },
   200          {
   201              name: "equal index",
   202              args: args{
   203                  in: []Node{NewBool(true)},
   204                  source: 0,
   205                  target: 0,
   206                  exp: []Node{NewBool(true)},
   207              },
   208          },
   209          {
   210              name: "forward index",
   211              args: args{
   212                  in: []Node{NewString("a"), NewString("b"), NewString("c")},
   213                  source: 0,
   214                  target: 2,
   215                  exp: []Node{NewString("b"), NewString("c"), NewString("a")},
   216              },
   217          },
   218          {
   219              name: "backward index",
   220              args: args{
   221                  in: []Node{NewString("a"), NewString("b"), NewString("c")},
   222                  source: 2,
   223                  target: 1,
   224                  exp: []Node{NewString("a"), NewString("c"), NewString("b")},
   225              },
   226          },
   227      }
   228      for _, tt := range tests {
   229          t.Run(tt.name, func(t *testing.T) {
   230              self := &linkedNodes{}
   231              self.FromSlice(tt.args.in)
   232              self.MoveOne(tt.args.source, tt.args.target)
   233              act := make([]Node, self.Len())
   234              self.ToSlice(act)
   235              require.Equal(t, tt.args.exp, act)
   236          })
   237      }
   238  }
   239  
   240