github.com/uchennaokeke444/nomad@v0.11.8/nomad/structs/bitmap_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestBitmap(t *testing.T) {
     9  	// Check invalid sizes
    10  	_, err := NewBitmap(0)
    11  	if err == nil {
    12  		t.Fatalf("bad")
    13  	}
    14  	_, err = NewBitmap(7)
    15  	if err == nil {
    16  		t.Fatalf("bad")
    17  	}
    18  
    19  	// Create a normal bitmap
    20  	var s uint = 256
    21  	b, err := NewBitmap(s)
    22  	if err != nil {
    23  		t.Fatalf("err: %v", err)
    24  	}
    25  
    26  	if b.Size() != s {
    27  		t.Fatalf("bad size")
    28  	}
    29  
    30  	// Set a few bits
    31  	b.Set(0)
    32  	b.Set(255)
    33  
    34  	// Verify the bytes
    35  	if b[0] == 0 {
    36  		t.Fatalf("bad")
    37  	}
    38  	if !b.Check(0) {
    39  		t.Fatalf("bad")
    40  	}
    41  
    42  	// Verify the bytes
    43  	if b[len(b)-1] == 0 {
    44  		t.Fatalf("bad")
    45  	}
    46  	if !b.Check(255) {
    47  		t.Fatalf("bad")
    48  	}
    49  
    50  	// All other bits should be unset
    51  	for i := 1; i < 255; i++ {
    52  		if b.Check(uint(i)) {
    53  			t.Fatalf("bad")
    54  		}
    55  	}
    56  
    57  	// Check the indexes
    58  	idxs := b.IndexesInRange(true, 0, 500)
    59  	expected := []int{0, 255}
    60  	if !reflect.DeepEqual(idxs, expected) {
    61  		t.Fatalf("bad: got %#v; want %#v", idxs, expected)
    62  	}
    63  
    64  	idxs = b.IndexesInRange(true, 1, 255)
    65  	expected = []int{255}
    66  	if !reflect.DeepEqual(idxs, expected) {
    67  		t.Fatalf("bad: got %#v; want %#v", idxs, expected)
    68  	}
    69  
    70  	idxs = b.IndexesInRange(false, 0, 256)
    71  	if len(idxs) != 254 {
    72  		t.Fatalf("bad")
    73  	}
    74  
    75  	idxs = b.IndexesInRange(false, 100, 200)
    76  	if len(idxs) != 101 {
    77  		t.Fatalf("bad")
    78  	}
    79  
    80  	// Check the copy is correct
    81  	b2, err := b.Copy()
    82  	if err != nil {
    83  		t.Fatalf("bad: %v", err)
    84  	}
    85  
    86  	if !reflect.DeepEqual(b, b2) {
    87  		t.Fatalf("bad")
    88  	}
    89  
    90  	// Clear
    91  	b.Clear()
    92  
    93  	// All bits should be unset
    94  	for i := 0; i < 256; i++ {
    95  		if b.Check(uint(i)) {
    96  			t.Fatalf("bad")
    97  		}
    98  	}
    99  
   100  	// Set a few bits
   101  	b.Set(0)
   102  	b.Set(255)
   103  	b.Unset(0)
   104  	b.Unset(255)
   105  
   106  	// Clear the bits
   107  	if b[0] != 0 {
   108  		t.Fatalf("bad")
   109  	}
   110  	if b.Check(0) {
   111  		t.Fatalf("bad")
   112  	}
   113  
   114  	// Verify the bytes
   115  	if b[len(b)-1] != 0 {
   116  		t.Fatalf("bad")
   117  	}
   118  	if b.Check(255) {
   119  		t.Fatalf("bad")
   120  	}
   121  }