MCPcopy
hub / github.com/tinygo-org/tinygo / TestMallocFree

Function TestMallocFree

tests/runtime_wasi/malloc_test.go:77–126  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

75}
76
77func TestMallocFree(t *testing.T) {
78 tests := []struct {
79 name string
80 getBuffer func() uintptr
81 content string
82 }{
83 {
84 name: "malloc",
85 getBuffer: getFilledBuffer_malloc,
86 content: "panda",
87 },
88 {
89 name: "calloc",
90 getBuffer: getFilledBuffer_calloc,
91 content: "pandabears",
92 },
93 {
94 name: "realloc",
95 getBuffer: getFilledBuffer_realloc,
96 content: "pandabear",
97 },
98 {
99 name: "realloc nil",
100 getBuffer: getFilledBuffer_reallocNil,
101 content: "panda",
102 },
103 }
104
105 for _, tc := range tests {
106 tt := tc
107 t.Run(tt.name, func(t *testing.T) {
108 bufPtr := tt.getBuffer()
109 // Don't use defer to free the buffer as it seems to cause the GC to track it.
110
111 // Churn GC, the pointer should still be valid until free is called.
112 for i := 0; i < 1000; i++ {
113 a := "hello" + strconv.Itoa(i)
114 // Some conditional logic to ensure optimization doesn't remove the loop completely.
115 if len(a) < 0 {
116 break
117 }
118 runtime.GC()
119 }
120
121 checkFilledBuffer(t, bufPtr, tt.content)
122
123 libc_free(unsafe.Pointer(bufPtr))
124 })
125 }
126}

Callers

nothing calls this directly

Calls 5

checkFilledBufferFunction · 0.85
GCMethod · 0.80
PointerMethod · 0.80
libc_freeFunction · 0.70
RunMethod · 0.45

Tested by

no test coverage detected