github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/lockset/table_test.go (about)

     1  package lockset_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/egonelbre/exp/lockset"
     7  )
     8  
     9  func TestTable(t *testing.T) {
    10  	table := lockset.NewTable()
    11  
    12  	A := &lockset.Lock{}
    13  	B := &lockset.Lock{}
    14  	C := &lockset.Lock{}
    15  
    16  	// A -> B -> C
    17  	assertNil(t, table.Locked(A))
    18  	assertNil(t, table.Locked(B))
    19  	assertNil(t, table.Locked(C))
    20  	assertTrue(t, table.Unlocking(C))
    21  	assertTrue(t, table.Unlocking(B))
    22  	assertTrue(t, table.Unlocking(A))
    23  
    24  	// B -> C
    25  	assertNil(t, table.Locked(B))
    26  	assertNil(t, table.Locked(C))
    27  	assertTrue(t, table.Unlocking(C))
    28  	assertTrue(t, table.Unlocking(B))
    29  
    30  	// B -> C -> A!
    31  	assertNil(t, table.Locked(B))
    32  	assertNil(t, table.Locked(C))
    33  	assertNotNil(t, table.Locked(A))
    34  	assertTrue(t, table.Unlocking(A))
    35  	assertTrue(t, table.Unlocking(C))
    36  	assertTrue(t, table.Unlocking(B))
    37  }
    38  
    39  func assertNil(t *testing.T, inv *lockset.Inversion) {
    40  	t.Helper()
    41  	if inv != nil {
    42  		t.Fatal(inv.String())
    43  	}
    44  }
    45  
    46  func assertNotNil(t *testing.T, inv *lockset.Inversion) {
    47  	t.Helper()
    48  	if inv == nil {
    49  		t.Fatal("expected inversion")
    50  	}
    51  }
    52  
    53  func assertTrue(t *testing.T, v bool) {
    54  	t.Helper()
    55  	if v != true {
    56  		t.Fatal("expected true")
    57  	}
    58  }