github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/internal/luagc/safepool_test.go (about)

     1  package luagc
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestSafePool(t *testing.T) {
     9  	p := NewSafePool()
    10  	n1 := newIntPtr(1)
    11  	n2 := newIntPtr(2)
    12  	n3 := newIntPtr(3)
    13  	p.Mark(n1, Finalize)
    14  	p.Mark(n2, Finalize|Release)
    15  	p.Mark(n3, Release)
    16  	p.Mark(n1, Finalize)
    17  	w := p.Get(n1)
    18  	if w.Value() == nil {
    19  		t.Fatalf("Expected weakref to be non-nil")
    20  	}
    21  	if n := len(p.ExtractPendingFinalize()); n != 0 {
    22  		t.Fatalf("Expected no pending finalize, got %d", n)
    23  	}
    24  	if n := len(p.ExtractPendingRelease()); n != 0 {
    25  		t.Fatalf("Expected no pending release, got %d", n)
    26  	}
    27  	mf := p.ExtractAllMarkedFinalize()
    28  	if !reflect.DeepEqual(mf, []Value{n1, n2}) {
    29  		t.Fatalf("Incorrect marked finalize: %+v", mf)
    30  	}
    31  	if n := len(p.ExtractAllMarkedFinalize()); n != 0 {
    32  		t.Fatalf("Expected no marked finalize, got %d", n)
    33  	}
    34  	mr := p.ExtractAllMarkedRelease()
    35  	if !reflect.DeepEqual(mr, []Value{n3, n2}) {
    36  		t.Fatalf("Incorrect marked release: %+v", mf)
    37  	}
    38  	if n := len(p.ExtractAllMarkedRelease()); n != 0 {
    39  		t.Fatalf("Expected no marked release, got %d", n)
    40  	}
    41  }