(t *testing.T)
| 75 | } |
| 76 | |
| 77 | func 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 | } |
nothing calls this directly
no test coverage detected