github.com/tunabay/go-bitarray@v1.3.1/buffer_rw_test.go (about)

     1  // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be found in
     3  // the LICENSE file.
     4  
     5  package bitarray_test
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  
    11  	"github.com/tunabay/go-bitarray"
    12  )
    13  
    14  func TestBuffer_BitAt(t *testing.T) {
    15  	buf := bitarray.NewBuffer(18)
    16  	chk := func(off int, want byte) {
    17  		t.Helper()
    18  		bit := buf.BitAt(off)
    19  		if bit != want {
    20  			t.Errorf("unexpected: off=%d: got %d, want %d.", off, bit, want)
    21  		}
    22  	}
    23  
    24  	buf.PutBitArrayAt(0, bitarray.MustParse("1100-1111 0000-1010 11"))
    25  	chk(0, 1)
    26  	chk(1, 1)
    27  	chk(7, 1)
    28  	chk(8, 0)
    29  	chk(15, 0)
    30  	chk(16, 1)
    31  	chk(17, 1)
    32  	buf.PutBitArrayAt(0, bitarray.MustParse("0011-0000 1111-0101 00"))
    33  	chk(0, 0)
    34  	chk(1, 0)
    35  	chk(7, 0)
    36  	chk(8, 1)
    37  	chk(15, 1)
    38  	chk(16, 0)
    39  	chk(17, 0)
    40  
    41  	chkpanic := func(off int) {
    42  		var bit byte
    43  		defer func() {
    44  			if recover() == nil {
    45  				t.Errorf("panic expected: off=%d, bit=%d.", off, bit)
    46  			}
    47  		}()
    48  		bit = buf.BitAt(off)
    49  	}
    50  	chkpanic(-1)
    51  	chkpanic(18)
    52  }
    53  
    54  func TestBuffer_PutBitAt(t *testing.T) {
    55  	buf := bitarray.NewBuffer(18)
    56  	chk := func(wantS string) {
    57  		t.Helper()
    58  		buf.V()
    59  		got := buf.BitArray()
    60  		want := bitarray.MustParse(wantS)
    61  		if !got.Equal(want) {
    62  			t.Error("unexpected:")
    63  			t.Logf(" got: %#b", got)
    64  			t.Logf("want: %#b", want)
    65  			t.Logf(" buf: %s", buf.D())
    66  		}
    67  	}
    68  	chk("0000-0000 0000-0000 00")
    69  	buf.PutBitAt(0, 1)
    70  	buf.PutBitAt(1, 0)
    71  	buf.PutBitAt(7, 1)
    72  	buf.PutBitAt(8, 1)
    73  	buf.PutBitAt(12, 1)
    74  	buf.PutBitAt(15, 0)
    75  	buf.PutBitAt(16, 0)
    76  	buf.PutBitAt(17, 1)
    77  	chk("1000-0001 1000-1000 01")
    78  	buf.PutBitAt(0, 0)
    79  	buf.PutBitAt(7, 0)
    80  	buf.PutBitAt(8, 0)
    81  	buf.PutBitAt(17, 0)
    82  	chk("0000-0000 0000-1000 00")
    83  
    84  	chkpanic := func(off int, bit byte) {
    85  		defer func() {
    86  			if recover() == nil {
    87  				t.Errorf("panic expected: off=%d, bit=%d.", off, bit)
    88  			}
    89  		}()
    90  		buf.PutBitAt(off, bit)
    91  	}
    92  	chkpanic(-1, 1)
    93  	chkpanic(18, 0)
    94  }
    95  
    96  func TestBuffer_BitArrayAt(t *testing.T) {
    97  	buf := bitarray.NewBuffer(22)
    98  	chk := func(off, nBits int, wantS string) {
    99  		t.Helper()
   100  		want := bitarray.MustParse(wantS)
   101  		got := buf.BitArrayAt(off, nBits)
   102  		if !got.Equal(want) {
   103  			t.Errorf("unexpected: off=%d, nBits=%d", off, nBits)
   104  			t.Logf(" got: %#b", got)
   105  			t.Logf("want: %#b", want)
   106  		}
   107  	}
   108  
   109  	buf.PutBitArrayAt(0, bitarray.MustParse("1100-1111 0000-1010 1100-11"))
   110  	chk(0, 0, "")
   111  	chk(22, 0, "")
   112  	chk(0, 4, "1100")
   113  	chk(2, 6, "00-1111")
   114  	chk(4, 8, "1111-0000")
   115  	chk(7, 1, "1")
   116  	chk(7, 3, "100")
   117  	chk(8, 1, "0")
   118  	chk(4, 14, "1111 0000-1010 11")
   119  	chk(18, 4, "0011")
   120  	chk(21, 1, "1")
   121  
   122  	chkpanic := func(off, nBits int) {
   123  		var ba *bitarray.BitArray
   124  		defer func() {
   125  			if recover() == nil {
   126  				t.Errorf("panic expected: off=%d, nBits=%d, got=%#b.", off, nBits, ba)
   127  			}
   128  		}()
   129  		ba = buf.BitArrayAt(off, nBits)
   130  	}
   131  	chkpanic(-1, 4)
   132  	chkpanic(19, 4)
   133  	chkpanic(0, -1)
   134  	chkpanic(0, 99)
   135  	chkpanic(99, 1)
   136  }
   137  
   138  func TestBuffer_PutBitArrayAt(t *testing.T) {
   139  	buf := bitarray.NewBuffer(22)
   140  	chk := func(wantS string) {
   141  		t.Helper()
   142  		buf.V()
   143  		got := buf.BitArray()
   144  		want := bitarray.MustParse(wantS)
   145  		if !got.Equal(want) {
   146  			t.Error("unexpected:")
   147  			t.Logf(" got: %#b", got)
   148  			t.Logf("want: %#b", want)
   149  			t.Logf(" buf: %s", buf.D())
   150  		}
   151  	}
   152  	chk("0000-0000 0000-0000 0000-00")
   153  	buf.PutBitArrayAt(0, nil)
   154  	chk("0000-0000 0000-0000 0000-00")
   155  	buf.PutBitArrayAt(22, bitarray.NewZeroFilled(0))
   156  	chk("0000-0000 0000-0000 0000-00")
   157  	buf.PutBitArrayAt(2, bitarray.NewOneFilled(9))
   158  	chk("0011-1111 1110-0000 0000-00")
   159  	buf.PutBitArrayAt(4, bitarray.NewZeroFilled(3))
   160  	chk("0011-0001 1110-0000 0000-00")
   161  	buf.PutBitArrayAt(7, bitarray.MustParse("1010-1010 10"))
   162  	chk("0011-0001 0101-0101 0000-00")
   163  	buf.PutBitArrayAt(16, bitarray.NewOneFilled(6))
   164  	chk("0011-0001 0101-0101 1111-11")
   165  	buf.PutBitArrayAt(14, bitarray.NewZeroFilled(6))
   166  	chk("0011-0001 0101-0100 0000-11")
   167  	buf.PutBitArrayAt(12, bitarray.NewZeroFilled(10))
   168  	chk("0011-0001 0101-0000 0000-00")
   169  	buf.PutBitArrayAt(0, bitarray.NewOneFilled(22))
   170  	chk("1111-1111 1111-1111 1111-11")
   171  	buf.PutBitArrayAt(8, bitarray.MustParse("0011 1100"))
   172  	chk("1111-1111 0011-1100 1111-11")
   173  	buf.PutBitArrayAt(21, bitarray.NewZeroFilled(1))
   174  	chk("1111-1111 0011-1100 1111-10")
   175  
   176  	chkpanic := func(off int, ba bitarray.BitArrayer) {
   177  		defer func() {
   178  			if recover() == nil {
   179  				t.Errorf("panic expected: off=%d, ba=%v.", off, ba)
   180  			}
   181  		}()
   182  		buf.PutBitArrayAt(off, ba)
   183  	}
   184  	chkpanic(-1, nil)
   185  	chkpanic(-1, bitarray.NewZeroFilled(0))
   186  	chkpanic(-1, bitarray.NewZeroFilled(8))
   187  	chkpanic(0, bitarray.NewZeroFilled(23))
   188  	chkpanic(0, bitarray.NewOneFilled(23))
   189  	chkpanic(22, bitarray.NewZeroFilled(1))
   190  	chkpanic(23, bitarray.NewZeroFilled(0))
   191  	chkpanic(24, bitarray.NewZeroFilled(0))
   192  	chkpanic(16, bitarray.NewZeroFilled(7))
   193  }
   194  
   195  func TestBuffer_ByteAt(t *testing.T) {
   196  	buf := bitarray.NewBuffer(22)
   197  	chk := func(off int, want byte) {
   198  		t.Helper()
   199  		got := buf.ByteAt(off)
   200  		if got != want {
   201  			t.Errorf("unexpected: off=%d, got=%08b, want=%08b", off, got, want)
   202  		}
   203  	}
   204  
   205  	buf.PutBitArrayAt(0, bitarray.MustParse("1100-1111 0000-1010 1100-11"))
   206  	chk(0, 0b_1100_1111)
   207  	chk(1, 0b_1001_1110)
   208  	chk(2, 0b_0011_1100)
   209  	chk(3, 0b_0111_1000)
   210  	chk(4, 0b_1111_0000)
   211  	chk(5, 0b_1110_0001)
   212  	chk(6, 0b_1100_0010)
   213  	chk(7, 0b_1000_0101)
   214  	chk(8, 0b_0000_1010)
   215  	chk(9, 0b_0001_0101)
   216  	chk(10, 0b_0010_1011)
   217  	chk(10, 0b_0010_1011)
   218  	chk(14, 0b_1011_0011)
   219  
   220  	chkpanic := func(off int) {
   221  		var b byte
   222  		defer func() {
   223  			if recover() == nil {
   224  				t.Errorf("panic expected: off=%d, got=%08b.", off, b)
   225  			}
   226  		}()
   227  		b = buf.ByteAt(off)
   228  	}
   229  	chkpanic(-1)
   230  	chkpanic(15)
   231  	buf.Resize(0, bitarray.AlignLeft)
   232  	chkpanic(0)
   233  }
   234  
   235  func TestBuffer_PutByteAt(t *testing.T) {
   236  	buf := bitarray.NewBuffer(30)
   237  	chk := func(wantS string) {
   238  		t.Helper()
   239  		buf.V()
   240  		got := buf.BitArray()
   241  		want := bitarray.MustParse(wantS)
   242  		if !got.Equal(want) {
   243  			t.Error("unexpected:")
   244  			t.Logf(" got: %#b", got)
   245  			t.Logf("want: %#b", want)
   246  			t.Logf(" buf: %s", buf.D())
   247  		}
   248  	}
   249  	chk("0000-0000 0000-0000 0000-0000 0000-00")
   250  	buf.PutByteAt(0, 0b_0000_0000)
   251  	chk("0000-0000 0000-0000 0000-0000 0000-00")
   252  	buf.PutByteAt(0, 0b_1010_1100)
   253  	chk("1010_1100 0000-0000 0000-0000 0000-00")
   254  	buf.PutByteAt(1, 0b_0011_0111)
   255  	chk("1001_1011 1000-0000 0000-0000 0000-00")
   256  	buf.PutByteAt(2, 0b_1111_1111)
   257  	chk("1011_1111 1100-0000 0000-0000 0000-00")
   258  	buf.PutByteAt(3, 0b_0000_0001)
   259  	chk("1010_0000 0010-0000 0000-0000 0000-00")
   260  	buf.PutByteAt(7, 0b_1100_0011)
   261  	chk("1010_0001 1000-0110 0000-0000 0000-00")
   262  	buf.PutByteAt(15, 0b_1111_1111)
   263  	chk("1010_0001 1000-0111 1111-1110 0000-00")
   264  	buf.PutByteAt(16, 0b_0011_0101)
   265  	chk("1010_0001 1000-0111 0011-0101 0000-00")
   266  	buf.PutByteAt(22, 0b_1110_0111)
   267  	chk("1010_0001 1000-0111 0011-0111 1001-11")
   268  
   269  	chkpanic := func(off int, b byte) {
   270  		defer func() {
   271  			if recover() == nil {
   272  				t.Errorf("panic expected: off=%d, ba=%#b.", off, buf)
   273  			}
   274  		}()
   275  		buf.PutByteAt(off, b)
   276  	}
   277  	chkpanic(-1, 0)
   278  	chkpanic(-99, 0)
   279  	chkpanic(23, 0)
   280  	chkpanic(30, 0)
   281  	buf.Resize(0, bitarray.AlignLeft)
   282  	chkpanic(0, 0)
   283  }
   284  
   285  func TestBuffer_RawBytes(t *testing.T) {
   286  	chk := func(buf *bitarray.Buffer, want ...byte) {
   287  		t.Helper()
   288  		got := buf.RawBytes()
   289  		if !bytes.Equal(got, want) {
   290  			t.Error("unexpected RawBytes():")
   291  			t.Logf(" got: %08b", got)
   292  			t.Logf("want: %08b", want)
   293  			t.Logf(" buf: %s", buf.D())
   294  		}
   295  	}
   296  
   297  	ba := bitarray.MustParse("1100-1111 0000-1010 1100-1110 0011-01")
   298  	buf := bitarray.NewBufferFromBitArray(ba)
   299  	chk(
   300  		buf,
   301  		0b_1100_1111, 0b_0000_1010, 0b_1100_1110, 0b_0011_0100,
   302  	)
   303  	chk(
   304  		buf.Slice(0, 17),
   305  		0b_1100_1111, 0b_0000_1010, 0b_1100_1110,
   306  	)
   307  	chk(
   308  		buf.Slice(4, 28),
   309  		0b_1111_0000, 0b_1010_1100, 0b_1110_0011,
   310  	)
   311  	chk(
   312  		buf.Slice(4, 24),
   313  		0b_1111_0000, 0b_1010_1100, 0b_1110_0000,
   314  	)
   315  	chk(
   316  		buf.Slice(4, 24).SliceToEnd(4),
   317  		0b_0000_1010, 0b_1100_1110,
   318  	)
   319  	chk(
   320  		buf.Slice(4, 24).Slice(4, 16),
   321  		0b_0000_1010, 0b_1100_1110,
   322  	)
   323  	chk(
   324  		buf.Slice(4, 24).SliceToEnd(6),
   325  		0b_0010_1011, 0b_0011_1000,
   326  	)
   327  }
   328  
   329  func TestBuffer_Bytes(t *testing.T) {
   330  	chk := func(buf *bitarray.Buffer, want ...byte) {
   331  		t.Helper()
   332  		got := buf.Bytes()
   333  		if !bytes.Equal(got, want) {
   334  			t.Error("unexpected Bytes():")
   335  			t.Logf(" got: %08b", got)
   336  			t.Logf("want: %08b", want)
   337  			t.Logf(" buf: %s", buf.D())
   338  		}
   339  	}
   340  
   341  	ba := bitarray.MustParse("1100-1111 0000-1010 1100-1110 0011-01")
   342  	buf := bitarray.NewBufferFromBitArray(ba)
   343  	chk(
   344  		buf,
   345  		0b_1100_1111, 0b_0000_1010, 0b_1100_1110, 0b_0011_0100,
   346  	)
   347  	chk(
   348  		buf.Slice(0, 17),
   349  		0b_1100_1111, 0b_0000_1010, 0b_1000_0000,
   350  	)
   351  	chk(
   352  		buf.Slice(4, 28),
   353  		0b_1111_0000, 0b_1010_1100, 0b_1110_0011,
   354  	)
   355  	chk(
   356  		buf.Slice(4, 24),
   357  		0b_1111_0000, 0b_1010_1100, 0b_1110_0000,
   358  	)
   359  	chk(
   360  		buf.Slice(4, 24).SliceToEnd(4),
   361  		0b_0000_1010, 0b_1100_1110,
   362  	)
   363  	chk(
   364  		buf.Slice(4, 24).Slice(4, 16),
   365  		0b_0000_1010, 0b_1100_0000,
   366  	)
   367  	chk(
   368  		buf.Slice(4, 24).SliceToEnd(6),
   369  		0b_0010_1011, 0b_0011_1000,
   370  	)
   371  }
   372  
   373  func TestBuffer_BytesAt(t *testing.T) {
   374  	buf := bitarray.NewBuffer(30)
   375  	chk := func(off, nBytes int, want ...byte) {
   376  		t.Helper()
   377  		got := buf.BytesAt(off, nBytes)
   378  		if !bytes.Equal(got, want) {
   379  			t.Errorf("unexpected: off=%d, nBytes=%d:", off, nBytes)
   380  			t.Logf(" got: %08b", got)
   381  			t.Logf("want: %08b", want)
   382  			t.Logf(" buf: %s", buf.D())
   383  		}
   384  	}
   385  
   386  	buf.PutBitArrayAt(0, bitarray.MustParse("1100-1111 0000-1010 1100-1110 0011-01"))
   387  	chk(0, 0)
   388  	chk(30, 0)
   389  	chk(0, 1, 0b_1100_1111)
   390  	chk(0, 2, 0b_1100_1111, 0b_0000_1010)
   391  	chk(0, 3, 0b_1100_1111, 0b_0000_1010, 0b_1100_1110)
   392  	chk(1, 3, 0b_1001_1110, 0b_0001_0101, 0b_1001_1100)
   393  	chk(2, 3, 0b_0011_1100, 0b_0010_1011, 0b_0011_1000)
   394  	chk(3, 3, 0b_0111_1000, 0b_0101_0110, 0b_0111_0001)
   395  	chk(4, 3, 0b_1111_0000, 0b_1010_1100, 0b_1110_0011)
   396  	chk(5, 3, 0b_1110_0001, 0b_0101_1001, 0b_1100_0110)
   397  	chk(6, 3, 0b_1100_0010, 0b_1011_0011, 0b_1000_1101)
   398  	chk(7, 2, 0b_1000_0101, 0b_0110_0111)
   399  	chk(8, 2, 0b_0000_1010, 0b_1100_1110)
   400  	chk(9, 2, 0b_0001_0101, 0b_1001_1100)
   401  	chk(22, 1, 0b_1000_1101)
   402  
   403  	chkpanic := func(off, nBytes int) {
   404  		var b []byte
   405  		defer func() {
   406  			if recover() == nil {
   407  				t.Errorf("panic expected: off=%d, nBytes=%d, got=%08b.", off, nBytes, b)
   408  			}
   409  		}()
   410  		b = buf.BytesAt(off, nBytes)
   411  	}
   412  	chkpanic(-1, 1)
   413  	chkpanic(-1, 0)
   414  	chkpanic(0, 99)
   415  	chkpanic(0, 4)
   416  	chkpanic(0, -1)
   417  	chkpanic(31, 0)
   418  	chkpanic(23, 1)
   419  	chkpanic(15, 2)
   420  	chkpanic(7, 3)
   421  	buf.Resize(0, bitarray.AlignLeft)
   422  	chkpanic(0, 1)
   423  	chkpanic(1, 0)
   424  }
   425  
   426  func TestBuffer_PutBytesAt(t *testing.T) {
   427  	buf := bitarray.NewBuffer(30)
   428  	chk := func(wantS string) {
   429  		t.Helper()
   430  		buf.V()
   431  		got := buf.BitArray()
   432  		want := bitarray.MustParse(wantS)
   433  		if !got.Equal(want) {
   434  			t.Error("unexpected:")
   435  			t.Logf(" got: %#b", got)
   436  			t.Logf("want: %#b", want)
   437  			t.Logf(" buf: %s", buf.D())
   438  		}
   439  	}
   440  	chk("0000-0000 0000-0000 0000-0000 0000-00")
   441  	buf.PutBytesAt(0, []byte{})
   442  	chk("0000-0000 0000-0000 0000-0000 0000-00")
   443  	buf.PutBytesAt(0, []byte{0xAA})
   444  	chk("1010-1010 0000-0000 0000-0000 0000-00")
   445  	buf.PutBytesAt(4, []byte{0x55})
   446  	chk("1010-0101 0101-0000 0000-0000 0000-00")
   447  	buf.PutBytesAt(6, []byte{0xF3, 0xCC})
   448  	chk("1010-0111 1100-1111 0011-0000 0000-00")
   449  	buf.PutBytesAt(8, []byte{0x00, 0xFF})
   450  	chk("1010-0111 0000-0000 1111-1111 0000-00")
   451  	buf.PutBytesAt(6, []byte{0xAA, 0xAA, 0xAA})
   452  	chk("1010-0110 1010-1010 1010-1010 1010-10")
   453  	buf.PutBytesAt(6, []byte{0x00, 0x00, 0x00})
   454  	chk("1010-0100 0000-0000 0000-0000 0000-00")
   455  	buf.PutBytesAt(22, []byte{0x55})
   456  	chk("1010-0100 0000-0000 0000-0001 0101-01")
   457  	buf.PutBytesAt(7, []byte{0xFF, 0xAA})
   458  	chk("1010-0101 1111-1111 0101-0101 0101-01")
   459  	buf.PutBytesAt(30, []byte{})
   460  	chk("1010-0101 1111-1111 0101-0101 0101-01")
   461  	buf.PutBytesAt(29, []byte{})
   462  	chk("1010-0101 1111-1111 0101-0101 0101-01")
   463  	buf.PutBytesAt(22, []byte{0x00})
   464  	chk("1010-0101 1111-1111 0101-0100 0000-00")
   465  	buf.PutBytesAt(14, []byte{0xFF, 0xFF})
   466  	chk("1010-0101 1111-1111 1111-1111 1111-11")
   467  	buf.PutBytesAt(30, nil)
   468  	chk("1010-0101 1111-1111 1111-1111 1111-11")
   469  	buf.PutBytesAt(0, nil)
   470  	chk("1010-0101 1111-1111 1111-1111 1111-11")
   471  	buf.PutBytesAt(16, []byte{0x11})
   472  	chk("1010-0101 1111-1111 0001-0001 1111-11")
   473  
   474  	chkpanic := func(off int, b []byte) {
   475  		defer func() {
   476  			if recover() == nil {
   477  				t.Errorf("panic expected: off=%d, ba=%#b.", off, buf)
   478  			}
   479  		}()
   480  		buf.PutBytesAt(off, b)
   481  	}
   482  	chkpanic(-1, []byte{})
   483  	chkpanic(-99, []byte{})
   484  	chkpanic(31, []byte{})
   485  	chkpanic(23, []byte{0xff})
   486  	chkpanic(15, []byte{0xff, 0xff})
   487  	chkpanic(7, []byte{0xff, 0xff, 0xff})
   488  	chkpanic(0, []byte{0xff, 0xff, 0xff, 0xff})
   489  }