github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/unsafe/unsafe_test.go (about) 1 // Copyright 2018 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache-2.0 3 // license that can be found in the LICENSE file. 4 5 package unsafe_test 6 7 import ( 8 "fmt" 9 "testing" 10 11 "github.com/Schaudge/grailbase/unsafe" 12 ) 13 14 func TestBytesToString(t *testing.T) { 15 for _, src := range []string{"", "abc"} { 16 d := unsafe.BytesToString([]byte(src)) 17 if d != src { 18 t.Error(d) 19 } 20 } 21 } 22 23 func ExampleBytesToString() { 24 fmt.Println(unsafe.BytesToString([]byte{'A', 'b', 'C'})) 25 // Output: AbC 26 } 27 28 func TestStringToBytes(t *testing.T) { 29 for _, src := range []string{"", "abc"} { 30 d := unsafe.StringToBytes(src) 31 if string(d) != src { 32 t.Error(d) 33 } 34 } 35 } 36 37 func ExampleStringToBytes() { 38 fmt.Println(unsafe.StringToBytes("AbC")) 39 // Output: [65 98 67] 40 } 41 42 func TestExtendBytes(t *testing.T) { 43 for _, src := range []string{"aceg", "abcdefghi"} { 44 d := []byte(src) 45 dExt := d[:3] 46 unsafe.ExtendBytes(&dExt, len(src)) 47 if string(dExt) != src { 48 t.Error(dExt) 49 } 50 } 51 } 52 53 func ExampleExtendBytes() { 54 d := []byte{'A', 'b', 'C'} 55 d = d[:1] 56 unsafe.ExtendBytes(&d, 2) 57 fmt.Println(d) 58 // Output: [65 98] 59 } 60