github.com/dotlike13/wemix30_go@v1.8.23/swarm/network/bitvector/bitvector_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package bitvector 18 19 import "testing" 20 21 func TestBitvectorNew(t *testing.T) { 22 _, err := New(0) 23 if err != errInvalidLength { 24 t.Errorf("expected err %v, got %v", errInvalidLength, err) 25 } 26 27 _, err = NewFromBytes(nil, 0) 28 if err != errInvalidLength { 29 t.Errorf("expected err %v, got %v", errInvalidLength, err) 30 } 31 32 _, err = NewFromBytes([]byte{0}, 9) 33 if err != errInvalidLength { 34 t.Errorf("expected err %v, got %v", errInvalidLength, err) 35 } 36 37 _, err = NewFromBytes(make([]byte, 8), 8) 38 if err != nil { 39 t.Error(err) 40 } 41 } 42 43 func TestBitvectorGetSet(t *testing.T) { 44 for _, length := range []int{ 45 1, 46 2, 47 4, 48 8, 49 9, 50 15, 51 16, 52 } { 53 bv, err := New(length) 54 if err != nil { 55 t.Errorf("error for length %v: %v", length, err) 56 } 57 58 for i := 0; i < length; i++ { 59 if bv.Get(i) { 60 t.Errorf("expected false for element on index %v", i) 61 } 62 } 63 64 func() { 65 defer func() { 66 if err := recover(); err == nil { 67 t.Errorf("expecting panic") 68 } 69 }() 70 bv.Get(length + 8) 71 }() 72 73 for i := 0; i < length; i++ { 74 bv.Set(i, true) 75 for j := 0; j < length; j++ { 76 if j == i { 77 if !bv.Get(j) { 78 t.Errorf("element on index %v is not set to true", i) 79 } 80 } else { 81 if bv.Get(j) { 82 t.Errorf("element on index %v is not false", i) 83 } 84 } 85 } 86 87 bv.Set(i, false) 88 89 if bv.Get(i) { 90 t.Errorf("element on index %v is not set to false", i) 91 } 92 } 93 } 94 } 95 96 func TestBitvectorNewFromBytesGet(t *testing.T) { 97 bv, err := NewFromBytes([]byte{8}, 8) 98 if err != nil { 99 t.Error(err) 100 } 101 if !bv.Get(3) { 102 t.Fatalf("element 3 is not set to true: state %08b", bv.b[0]) 103 } 104 }