github.com/cloudwego/frugal@v0.1.15/internal/utils/list_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 utils
    18  
    19  import (
    20      "testing"
    21      "sync"
    22      "unsafe"
    23  )
    24  
    25  func Test_ListNode_Prepend(t *testing.T) {
    26      n, parallel := 1000, 8
    27      head := ListNode{}
    28      wg := sync.WaitGroup{}
    29      wg.Add(parallel)
    30      filler := func(n int) {
    31          defer wg.Done()
    32          for i := 0; i < n; i++ {
    33              v := i
    34              head.Prepend((unsafe.Pointer)(&v))
    35          }
    36      }
    37      for i := 0; i < parallel; i++ {
    38          go filler(n)
    39      }
    40      wg.Wait()
    41      i := 0
    42      for p := head.next; p != nil; p = p.next {
    43          i += 1
    44      }
    45      if i != parallel * n {
    46          t.Errorf("got %v, expected %v", i, parallel * n)
    47      }
    48  }