gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/binutil/bitvector_test.go (about)

     1  package binutil
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  var setTests = []struct {
    10  	toSet    []uint
    11  	expected uint
    12  	size     uint
    13  }{
    14  	{[]uint{0}, 1, 8},
    15  	{[]uint{1}, 2, 8},
    16  	{[]uint{0, 1}, 3, 8},
    17  	{[]uint{2}, 4, 8},
    18  	{[]uint{0, 2}, 5, 8},
    19  	{[]uint{1, 2}, 6, 8},
    20  	{[]uint{0, 1, 2}, 7, 8},
    21  	{[]uint{0, 8}, 257, 16},
    22  }
    23  
    24  func validateSet(t *testing.T, setBits []uint, actual, expected uint) {
    25  	if actual != expected {
    26  		t.Errorf("BitVector.Set(%v) = %d Expected %d", setBits, actual, expected)
    27  	}
    28  }
    29  
    30  func TestSet(t *testing.T) {
    31  	for _, st := range setTests {
    32  		bv := new(BitVector)
    33  		for _, idx := range st.toSet {
    34  			bv.Set(idx)
    35  		}
    36  		switch st.size {
    37  		case 8:
    38  			e := uint8(st.expected)
    39  			a := uint8(bv.Bytes()[0])
    40  			validateSet(t, st.toSet, uint(a), uint(e))
    41  		case 16:
    42  			e := uint16(st.expected)
    43  			a := binary.BigEndian.Uint16(bv.Bytes())
    44  			validateSet(t, st.toSet, uint(a), uint(e))
    45  		}
    46  	}
    47  }
    48  
    49  func renderBinary(u, size uint) {
    50  	for i := size - 1; i >= 0 && i < size; i-- {
    51  		v := u & (1 << i)
    52  		if v > 0 {
    53  			fmt.Print(1)
    54  		} else {
    55  			fmt.Print(0)
    56  		}
    57  		if i%4 == 0 {
    58  			fmt.Print(" ")
    59  		}
    60  	}
    61  }
    62  
    63  func TestUnSet(t *testing.T) {
    64  	// sanityCheck(0x1234)
    65  	t.SkipNow()
    66  	var bv *BitVector
    67  	bv = new(BitVector)
    68  	// 0-3 are unused
    69  	// command
    70  	// for i := 0; i < 0xF; i++ {
    71  	// 	bv.Set(uint(i))
    72  	// 	bv.Print()
    73  	// }
    74  	bv.SizeForPosition(16)
    75  	bv.SetN(0x05>>1, 8, 0)
    76  	bv.SetN(0x05<<7, 8, 8)
    77  
    78  	// 9 - 12 are reserved
    79  	// command 2
    80  	bv.SetN(0x3F, 4, 8)
    81  	// // source id
    82  	bv.SetN(0x1234>>8, 8, 2*8)
    83  	bv.SetN(0x1234, 8, 3*8)
    84  	bv.SetN(0x08, 8, 4*8)
    85  	t.Error("TestUnSet not implemented.")
    86  }
    87  
    88  func TestValue(t *testing.T) {
    89  	t.SkipNow()
    90  	t.Error("TestValue not implemented.")
    91  }
    92  
    93  func TestValueN(t *testing.T) {
    94  	bv := new(BitVector)
    95  	var expected, size, index uint = 0x0102, 16, 3
    96  	bv.SetN(expected, size, index)
    97  	var actual = bv.ValueN(size, index)
    98  	if expected != actual {
    99  		t.Errorf("BitVector.ValueN(%d, %d) = %d, Expected %d", size, index,
   100  			actual, expected)
   101  	}
   102  }
   103  
   104  func TestSizeForPosition(t *testing.T) {
   105  	t.SkipNow()
   106  	t.Error("TestSizeForPosition not implemented.")
   107  }