github.com/tetratelabs/tinymem@v0.1.0/exports_test.go (about) 1 package tinymem 2 3 import ( 4 "testing" 5 "unsafe" 6 ) 7 8 func Test_mallocRoundTrip(t *testing.T) { 9 t.Run("free when not yet allocated", func(t *testing.T) { 10 free(123) // doesn't panic 11 }) 12 13 size := uint32(3) 14 15 ptr := malloc(size) 16 t.Run("malloc", func(t *testing.T) { 17 buf := unsafe.Slice((*byte)(unsafe.Pointer(ptr)), size) 18 buf[0] = 'f' 19 buf[1] = 'o' 20 buf[2] = 'o' 21 22 expected := "foo" 23 if have := PtrToString(ptr, size); expected != have { 24 t.Errorf("Unexpected string, have %s, expected %s", have, expected) 25 } 26 }) 27 28 t.Run("free", func(t *testing.T) { 29 free(ptr) 30 31 if _, ok := alivePointers[ptr]; ok { 32 t.Errorf("Unexpected to still have a string") 33 } 34 }) 35 }