github.com/bruceshao/lockfree@v1.1.3-0.20230816090528-e89824c0a6e9/buffer_test.go (about) 1 /* 2 * Copyright (C) THL A29 Limited, a Tencent company. All rights reserved. 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 */ 7 8 package lockfree 9 10 import ( 11 "fmt" 12 "reflect" 13 "testing" 14 "time" 15 "unsafe" 16 ) 17 18 func TestA(t *testing.T) { 19 sli := make([]uint8, 8) 20 sh := *(*reflect.SliceHeader)(unsafe.Pointer(&sli)) 21 mem := unsafe.Pointer(sh.Data) 22 Sets(mem) 23 fmt.Printf("%v\n", sli[0]) 24 fmt.Printf("%v\n", sli[1]) 25 fmt.Printf("%v\n", sli[2]) 26 fmt.Printf("%v\n", sli[3]) 27 fmt.Printf("%v\n", sli[4]) 28 fmt.Printf("%v\n", sli[5]) 29 fmt.Printf("%v\n", sli[6]) 30 fmt.Printf("%v\n", sli[7]) 31 } 32 33 func Sets(mem unsafe.Pointer) { 34 for i := 0; i < 8; i++ { 35 *(*uint8)(unsafe.Pointer(uintptr(mem) + uintptr(i))) = 1 36 } 37 } 38 39 func TestX(t *testing.T) { 40 loop := 1000000 41 l := 1024 * 1024 42 bytes := make([]byte, l) 43 ts := time.Now() 44 for i := 0; i < loop; i++ { 45 bytes[i] = 0x01 46 } 47 tl := time.Since(ts) 48 fmt.Println(tl.Microseconds()) 49 } 50 51 func TestBuffer(t *testing.T) { 52 buf := newRingBuffer[uint64](1024) 53 buf.write(0, 1) 54 x := buf.element(0) 55 fmt.Println(x) 56 57 buf.write(1023, 2) 58 x = buf.element(1023) 59 fmt.Println(x) 60 61 buf.write(1024, 3) 62 x = buf.element(1024) 63 fmt.Println(x) 64 } 65 66 func TestBufferAlign32(t *testing.T) { 67 buf := newRingBuffer[uint32](1024) 68 buf.write(0, 1) 69 x := buf.element(0) 70 fmt.Println(x) 71 72 buf.write(1023, 2) 73 x = buf.element(1023) 74 fmt.Println(x) 75 76 buf.write(1024, 3) 77 x = buf.element(1024) 78 fmt.Println(x) 79 }