github.com/afumu/libc@v0.0.6/memgrind_test.go (about)

     1  // Copyright 2021 The Libc Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !libc.membrk && libc.memgrind
     6  // +build !libc.membrk,libc.memgrind
     7  
     8  package libc // import "github.com/afumu/libc"
     9  
    10  import (
    11  	"math/rand"
    12  	"testing"
    13  )
    14  
    15  func TestTLSAllocator(t *testing.T) {
    16  	const rq = int(stackSegmentSize*3) / 4
    17  	MemAuditStart()
    18  	tls := NewTLS()
    19  	for i := 0; i < 10; i++ {
    20  		tls.Alloc(rq)
    21  	}
    22  	for i := 0; i < 10; i++ {
    23  		tls.Free(rq)
    24  	}
    25  	tls.Close()
    26  	if err := MemAuditReport(); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  }
    30  
    31  func TestTLSAllocator2(t *testing.T) {
    32  	MemAuditStart()
    33  	tls := NewTLS()
    34  	for rq := 1; rq < 1000; rq++ {
    35  		tls.Alloc(rq)
    36  	}
    37  	for rq := 999; rq > 0; rq-- {
    38  		tls.Free(rq)
    39  	}
    40  	tls.Close()
    41  	if err := MemAuditReport(); err != nil {
    42  		t.Fatal(err)
    43  	}
    44  }
    45  
    46  func TestTLSAllocator3(t *testing.T) {
    47  	a := make([]int, 1000)
    48  	r := rand.New(rand.NewSource(42))
    49  	for i := range a {
    50  		a[i] = int(r.Int31n(2 * int32(stackSegmentSize)))
    51  	}
    52  	MemAuditStart()
    53  	tls := NewTLS()
    54  	for _, rq := range a {
    55  		tls.Alloc(rq)
    56  	}
    57  	for i := len(a) - 1; i >= 0; i-- {
    58  		tls.Free(a[i])
    59  	}
    60  	tls.Close()
    61  	if err := MemAuditReport(); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  }