github.com/hernad/nomad@v1.6.112/nomad/structs/bitmap_test.go (about)

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