github.com/decred/dcrlnd@v0.7.6/chainscan/scanner_test.go (about)

     1  package chainscan
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // TestTargetSlice tests whether the targetSlice struct behaves correctly.
     8  func TestTargetSlice(t *testing.T) {
     9  
    10  	t1 := &target{version: 1}
    11  	t2 := &target{version: 2}
    12  	t3 := &target{version: 3}
    13  
    14  	ts := &targetSlice{}
    15  
    16  	assertContains := func(want []*target) {
    17  		t.Helper()
    18  		got := *ts
    19  		if len(want) != len(got) {
    20  			t.Fatalf("different slice lengths; want=%v got=%v", want, got)
    21  		}
    22  
    23  		for i := range want {
    24  			found := false
    25  			for j := range got {
    26  				if want[i] == got[j] {
    27  					found = true
    28  					break
    29  				}
    30  			}
    31  			if !found {
    32  				t.Fatalf("could not find wanted element %d. "+
    33  					"want=%v got=%v", i, want, got)
    34  			}
    35  
    36  		}
    37  	}
    38  
    39  	ts.add(t1)
    40  	ts.add(t2)
    41  	ts.add(t3)
    42  	assertContains([]*target{t1, t2, t3})
    43  
    44  	ts.del(t2)
    45  	assertContains([]*target{t1, t3})
    46  
    47  	ts.del(t1)
    48  	assertContains([]*target{t3})
    49  
    50  	ts.del(t3)
    51  	assertContains([]*target{})
    52  }