github.com/mitghi/x@v0.0.0-20191206171256-71e86edf750d/bit/bitset_test.go (about)

     1  /* MIT License
     2  *
     3  * Copyright (c) 2018 Mike Taghavi <mitghi[at]gmail.com>
     4  *
     5  * Permission is hereby granted, free of charge, to any person obtaining a copy
     6  * of this software and associated documentation files (the "Software"), to deal
     7  * in the Software without restriction, including without limitation the rights
     8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  * copies of the Software, and to permit persons to whom the Software is
    10  * furnished to do so, subject to the following conditions:
    11  * The above copyright notice and this permission notice shall be included in all
    12  * copies or substantial portions of the Software.
    13  *
    14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20  * SOFTWARE.
    21   */
    22  
    23  package bit
    24  
    25  import (
    26  	"fmt"
    27  	"testing"
    28  )
    29  
    30  func TestBitArray(t *testing.T) {
    31  	var (
    32  		ba       *BitArray = NewBitArray(32)
    33  		expected []uint8   = []uint8{0x83, 0x0, 0x83, 0x80}
    34  		setter   []int     = []int{1, 2, 8, 17, 18, 24, 32}
    35  	)
    36  	if ba == nil {
    37  		t.Error("unable to initialize.")
    38  	}
    39  	if ba.size != 32 {
    40  		t.Errorf("expected ba.size==32, got %d.", ba.size)
    41  	}
    42  	// set bits
    43  	for _, s := range setter {
    44  		if err := ba.Set(s); err != nil {
    45  			t.Errorf("expected Set(%d)==true; got %v.", s, err)
    46  		}
    47  	}
    48  	// validity check
    49  	for i := 0; i < len(expected); i++ {
    50  		if expected[i] != ba.bits[i] {
    51  			t.Errorf("assertion failed; iteration(%d), expected %d, got %d.", i, expected[i], ba.bits[i])
    52  		}
    53  	}
    54  	if err := ba.Set(33); err == nil {
    55  		t.Errorf("expected Set(33)!=nil; got %v.", err)
    56  	}
    57  	if _, err := ba.Get(33); err == nil {
    58  		t.Errorf("expected Get(33)!=nil; got %v.", err)
    59  	}
    60  	if value, err := ba.Get(8); err != nil {
    61  		t.Errorf("expected Get(8)!=nil; value %d, got %v.", value, err)
    62  	}
    63  	if err := ba.Flip(15); err != nil {
    64  		t.Errorf("expected Flip(15)==nil; got %v.", err)
    65  	}
    66  	if ok, err := ba.IsSet(15); ok && err != nil {
    67  		t.Errorf("expected IsSet(15)==true, nil; got %t, %v.", ok, err)
    68  	}
    69  }
    70  
    71  func TestBitArray2(t *testing.T) {
    72  	var (
    73  		ba *BitArray = NewBitArray(16)
    74  	)
    75  	if ba == nil {
    76  		t.Error("unable to initialize.")
    77  	}
    78  	if ba.size != 16 {
    79  		t.Errorf("expected ba.size==32, got %d.", ba.size)
    80  	}
    81  	fmt.Println(ba.Set(1))
    82  	fmt.Println(ba.Set(2))
    83  	fmt.Println(ba.Set(16))
    84  	fmt.Println(ba)
    85  }