github.com/tunabay/go-bitarray@v1.3.1/buffer_integer_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  	"testing"
     9  
    10  	"github.com/tunabay/go-bitarray"
    11  )
    12  
    13  // also tests PutUint8
    14  func TestBuffer_Uint8(t *testing.T) {
    15  	tcs := []struct {
    16  		s string
    17  		v uint8
    18  	}{
    19  		{"", 0},
    20  		{"0", 0},
    21  		{"1", 1},
    22  		{"00", 0b_00},
    23  		{"01", 0b_01},
    24  		{"11", 0b_11},
    25  		{"000000", 0b_000000},
    26  		{"000001", 0b_000001},
    27  		{"100000", 0b_100000},
    28  		{"111111", 0b_111111},
    29  		{"011111", 0b_011111},
    30  		{"111110", 0b_111110},
    31  		{"0000000", 0b_0000000},
    32  		{"0000001", 0b_0000001},
    33  		{"1000000", 0b_1000000},
    34  		{"1000111", 0b_1000111},
    35  		{"1100000", 0b_1100000},
    36  		{"1100111", 0b_1100111},
    37  		{"1111111", 0b_1111111},
    38  		{"00000000", 0b_00000000},
    39  		{"00000001", 0b_00000001},
    40  		{"00001111", 0b_00001111},
    41  		{"10000000", 0b_10000000},
    42  		{"11110000", 0b_11110000},
    43  		{"11111111", 0b_11111111},
    44  		{"000000001", 0b_00000000},
    45  		{"000000011", 0b_00000001},
    46  		{"100000001", 0b_10000000},
    47  		{"111111110", 0b_11111111},
    48  		{"0000000011111111", 0b_00000000},
    49  		{"1000000111111111", 0b_10000001},
    50  		{"1000000100000000", 0b_10000001},
    51  	}
    52  	for _, tc := range tcs {
    53  		core := bitarray.MustParse(tc.s)
    54  		for padbit := byte(0); padbit < 2; padbit++ {
    55  			pad := bitarray.New(padbit)
    56  			for prefix := 0; prefix < 18; prefix++ {
    57  				pre := pad.Repeat(prefix)
    58  				for suffix := 0; suffix < 10; suffix++ {
    59  					suf := pad.Repeat(suffix)
    60  					ba := bitarray.Join([]*bitarray.BitArray{pre, core, suf}, nil)
    61  					buf := bitarray.NewBufferFromBitArray(ba)
    62  					buf = buf.Slice(prefix, prefix+core.Len())
    63  					if got := buf.Uint8(); got != tc.v {
    64  						t.Errorf("unexpected result: got %d, want %d", got, tc.v)
    65  						t.Logf(" buf: %#b", buf)
    66  						t.Logf("core: %#b", core)
    67  					}
    68  				}
    69  			}
    70  		}
    71  	}
    72  	for _, tc := range tcs {
    73  		core := bitarray.MustParse(tc.s)
    74  		clen := core.Len()
    75  		if 8 < clen {
    76  			continue // skip
    77  		}
    78  		for padbit := byte(0); padbit < 2; padbit++ {
    79  			pad := bitarray.New(padbit)
    80  			for prefix := 0; prefix < 18; prefix++ {
    81  				for suffix := 0; suffix < 10; suffix++ {
    82  					ba := pad.Repeat(prefix + clen + suffix)
    83  					buf := bitarray.NewBufferFromBitArray(ba)
    84  					buf.Slice(prefix, prefix+clen).PutUint8(tc.v)
    85  					got := buf.BitArray().Slice(prefix, prefix+clen)
    86  					if !got.Equal(core) {
    87  						t.Errorf(
    88  							"unexpected result: v=%d, pre=%d, suf=%d",
    89  							tc.v, prefix, suffix,
    90  						)
    91  						t.Logf(" got: %#b", got)
    92  						t.Logf("want: %#b", core)
    93  						t.Logf(" buf: %#b", buf)
    94  					}
    95  				}
    96  			}
    97  		}
    98  	}
    99  }
   100  
   101  // also tests PutUint16
   102  func TestBuffer_Uint16(t *testing.T) {
   103  	tcs := []struct {
   104  		s string
   105  		v uint16
   106  	}{
   107  		{"", 0},
   108  		{"0", 0},
   109  		{"1", 1},
   110  		{"00", 0b_00},
   111  		{"01", 0b_01},
   112  		{"11", 0b_11},
   113  		{"11110001", 0b_11110001},
   114  		{"111100011", 0b_111100011},
   115  		{"00000000000000", 0b_00000000000000},
   116  		{"00000000000001", 0b_00000000000001},
   117  		{"10000000000000", 0b_10000000000000},
   118  		{"11111111111111", 0b_11111111111111},
   119  		{"01111111111111", 0b_01111111111111},
   120  		{"11111111111110", 0b_11111111111110},
   121  		{"000000000000000", 0b_000000000000000},
   122  		{"000000000000001", 0b_000000000000001},
   123  		{"100000000000000", 0b_100000000000000},
   124  		{"100000000000111", 0b_100000000000111},
   125  		{"110000000000000", 0b_110000000000000},
   126  		{"110000000000111", 0b_110000000000111},
   127  		{"111111111111111", 0b_111111111111111},
   128  		{"0000000000000000", 0b_0000000000000000},
   129  		{"0000000000000001", 0b_0000000000000001},
   130  		{"0000000000001111", 0b_0000000000001111},
   131  		{"1000000000000000", 0b_1000000000000000},
   132  		{"1111000000000000", 0b_1111000000000000},
   133  		{"1111000011111100", 0b_1111000011111100},
   134  		{"1111111111111111", 0b_1111111111111111},
   135  		{"00000000000000001", 0b_0000000000000000},
   136  		{"00000000000000011", 0b_0000000000000001},
   137  		{"10000000000000001", 0b_1000000000000000},
   138  		{"11111111111111110", 0b_1111111111111111},
   139  		{"00000000000000001111111111111111", 0b_0000000000000000},
   140  		{"10000000000000011111111111111111", 0b_1000000000000001},
   141  		{"10000000000000010000000000000000", 0b_1000000000000001},
   142  	}
   143  	for _, tc := range tcs {
   144  		core := bitarray.MustParse(tc.s)
   145  		for padbit := byte(0); padbit < 2; padbit++ {
   146  			pad := bitarray.New(padbit)
   147  			for prefix := 0; prefix < 18; prefix++ {
   148  				pre := pad.Repeat(prefix)
   149  				for suffix := 0; suffix < 10; suffix++ {
   150  					suf := pad.Repeat(suffix)
   151  					ba := bitarray.Join([]*bitarray.BitArray{pre, core, suf}, nil)
   152  					buf := bitarray.NewBufferFromBitArray(ba)
   153  					buf = buf.Slice(prefix, prefix+core.Len())
   154  					if got := buf.Uint16(); got != tc.v {
   155  						t.Errorf("unexpected result: got %d, want %d", got, tc.v)
   156  						t.Logf(" buf: %#b", buf)
   157  						t.Logf("core: %#b", core)
   158  					}
   159  				}
   160  			}
   161  		}
   162  	}
   163  	for _, tc := range tcs {
   164  		core := bitarray.MustParse(tc.s)
   165  		clen := core.Len()
   166  		if 16 < clen {
   167  			continue // skip
   168  		}
   169  		for padbit := byte(0); padbit < 2; padbit++ {
   170  			pad := bitarray.New(padbit)
   171  			for prefix := 0; prefix < 18; prefix++ {
   172  				for suffix := 0; suffix < 10; suffix++ {
   173  					ba := pad.Repeat(prefix + clen + suffix)
   174  					buf := bitarray.NewBufferFromBitArray(ba)
   175  					buf.Slice(prefix, prefix+clen).PutUint16(tc.v)
   176  					got := buf.BitArray().Slice(prefix, prefix+clen)
   177  					if !got.Equal(core) {
   178  						t.Errorf(
   179  							"unexpected result: v=%d, pre=%d, suf=%d",
   180  							tc.v, prefix, suffix,
   181  						)
   182  						t.Logf(" got: %#b", got)
   183  						t.Logf("want: %#b", core)
   184  						t.Logf(" buf: %#b", buf)
   185  					}
   186  				}
   187  			}
   188  		}
   189  	}
   190  }
   191  
   192  // also tests PutUint32
   193  func TestBuffer_Uint32(t *testing.T) {
   194  	tcs := []struct {
   195  		s string
   196  		v uint32
   197  	}{
   198  		{"", 0},
   199  		{"0", 0},
   200  		{"1", 1},
   201  		{"00", 0b_00},
   202  		{"01", 0b_01},
   203  		{"11", 0b_11},
   204  		{"11110001", 0b_11110001},
   205  		{"111100011", 0b_111100011},
   206  		{"000000000000000000000000000000", 0b_000000000000000000000000000000},
   207  		{"000000000000000000000000000001", 0b_000000000000000000000000000001},
   208  		{"100000000000000000000000000000", 0b_100000000000000000000000000000},
   209  		{"111111111111111111111111111111", 0b_111111111111111111111111111111},
   210  		{"011111111111111111111111111111", 0b_011111111111111111111111111111},
   211  		{"111111111111111111111111111110", 0b_111111111111111111111111111110},
   212  		{"0000000000000000000000000000000", 0b_0000000000000000000000000000000},
   213  		{"0000000000000000000000000000001", 0b_0000000000000000000000000000001},
   214  		{"1000000000000000000000000000000", 0b_1000000000000000000000000000000},
   215  		{"1000000000000000000000000000111", 0b_1000000000000000000000000000111},
   216  		{"1100000000000000000000000000000", 0b_1100000000000000000000000000000},
   217  		{"1100000000000000000000000000111", 0b_1100000000000000000000000000111},
   218  		{"1111111111111111111111111111111", 0b_1111111111111111111111111111111},
   219  		{"00000000000000000000000000000000", 0b_00000000000000000000000000000000},
   220  		{"00000000000000000000000000000001", 0b_00000000000000000000000000000001},
   221  		{"00000000000000000000000000001111", 0b_00000000000000000000000000001111},
   222  		{"10000000000000000000000000000000", 0b_10000000000000000000000000000000},
   223  		{"11110000000000000000000000000000", 0b_11110000000000000000000000000000},
   224  		{"11110000101000001100000011111100", 0b_11110000101000001100000011111100},
   225  		{"11111111111111111111111111111111", 0b_11111111111111111111111111111111},
   226  		{"000000000000000000000000000000001", 0b_00000000000000000000000000000000},
   227  		{"000000000000000000000000000000011", 0b_00000000000000000000000000000001},
   228  		{"100000000000000000000000000000001", 0b_10000000000000000000000000000000},
   229  		{"111111111111111111111111111111110", 0b_11111111111111111111111111111111},
   230  		{"000000000000000000000000000000001111111111111111", 0b_00000000000000000000000000000000},
   231  		{"100000000000000000000000000000011111111111111111", 0b_10000000000000000000000000000001},
   232  		{"100000000000000000000000000000010000000000000000", 0b_10000000000000000000000000000001},
   233  	}
   234  	for _, tc := range tcs {
   235  		core := bitarray.MustParse(tc.s)
   236  		for padbit := byte(0); padbit < 2; padbit++ {
   237  			pad := bitarray.New(padbit)
   238  			for prefix := 0; prefix < 18; prefix++ {
   239  				pre := pad.Repeat(prefix)
   240  				for suffix := 0; suffix < 10; suffix++ {
   241  					suf := pad.Repeat(suffix)
   242  					ba := bitarray.Join([]*bitarray.BitArray{pre, core, suf}, nil)
   243  					buf := bitarray.NewBufferFromBitArray(ba)
   244  					buf = buf.Slice(prefix, prefix+core.Len())
   245  					if got := buf.Uint32(); got != tc.v {
   246  						t.Errorf("unexpected result: got %d, want %d", got, tc.v)
   247  						t.Logf(" buf: %#b", buf)
   248  						t.Logf("core: %#b", core)
   249  					}
   250  				}
   251  			}
   252  		}
   253  	}
   254  	for _, tc := range tcs {
   255  		core := bitarray.MustParse(tc.s)
   256  		clen := core.Len()
   257  		if 32 < clen {
   258  			continue // skip
   259  		}
   260  		for padbit := byte(0); padbit < 2; padbit++ {
   261  			pad := bitarray.New(padbit)
   262  			for prefix := 0; prefix < 18; prefix++ {
   263  				for suffix := 0; suffix < 10; suffix++ {
   264  					ba := pad.Repeat(prefix + clen + suffix)
   265  					buf := bitarray.NewBufferFromBitArray(ba)
   266  					buf.Slice(prefix, prefix+clen).PutUint32(tc.v)
   267  					got := buf.BitArray().Slice(prefix, prefix+clen)
   268  					if !got.Equal(core) {
   269  						t.Errorf(
   270  							"unexpected result: v=%d, pre=%d, suf=%d",
   271  							tc.v, prefix, suffix,
   272  						)
   273  						t.Logf(" got: %#b", got)
   274  						t.Logf("want: %#b", core)
   275  						t.Logf(" buf: %#b", buf)
   276  					}
   277  				}
   278  			}
   279  		}
   280  	}
   281  }
   282  
   283  // also tests PutUint64
   284  func TestBuffer_Uint64(t *testing.T) {
   285  	tcs := []struct {
   286  		s string
   287  		v uint64
   288  	}{
   289  		{"", 0},
   290  		{"0", 0},
   291  		{"1", 1},
   292  		{"00", 0b_00},
   293  		{"01", 0b_01},
   294  		{"11", 0b_11},
   295  		{"11110001", 0b_11110001},
   296  		{"111100011", 0b_111100011},
   297  		{
   298  			"00000000000000000000000000000000000000000000000000000000000000",
   299  			0b_00000000000000000000000000000000000000000000000000000000000000,
   300  		},
   301  		{
   302  			"00000000000000000000000000000000000000000000000000000000000001",
   303  			0b_00000000000000000000000000000000000000000000000000000000000001,
   304  		},
   305  		{
   306  			"10000000000000000000000000000000000000000000000000000000000000",
   307  			0b_10000000000000000000000000000000000000000000000000000000000000,
   308  		},
   309  		{
   310  			"11111111111111111111111111111111111111111111111111111111111111",
   311  			0b_11111111111111111111111111111111111111111111111111111111111111,
   312  		},
   313  		{
   314  			"01111111111111111111111111111111111111111111111111111111111111",
   315  			0b_01111111111111111111111111111111111111111111111111111111111111,
   316  		},
   317  		{
   318  			"11111111111111111111111111111111111111111111111111111111111110",
   319  			0b_11111111111111111111111111111111111111111111111111111111111110,
   320  		},
   321  		{
   322  			"000000000000000000000000000000000000000000000000000000000000000",
   323  			0b_000000000000000000000000000000000000000000000000000000000000000,
   324  		},
   325  		{
   326  			"000000000000000000000000000000000000000000000000000000000000001",
   327  			0b_000000000000000000000000000000000000000000000000000000000000001,
   328  		},
   329  		{
   330  			"100000000000000000000000000000000000000000000000000000000000000",
   331  			0b_100000000000000000000000000000000000000000000000000000000000000,
   332  		},
   333  		{
   334  			"100000000000000000000000000000000000000000000000000000000000111",
   335  			0b_100000000000000000000000000000000000000000000000000000000000111,
   336  		},
   337  		{
   338  			"110000000000000000000000000000000000000000000000000000000000000",
   339  			0b_110000000000000000000000000000000000000000000000000000000000000,
   340  		},
   341  		{
   342  			"110000000000000000000000000000000000000000000000000000000000111",
   343  			0b_110000000000000000000000000000000000000000000000000000000000111,
   344  		},
   345  		{
   346  			"111111111111111111111111111111111111111111111111111111111111111",
   347  			0b_111111111111111111111111111111111111111111111111111111111111111,
   348  		},
   349  		{
   350  			"0000000000000000000000000000000000000000000000000000000000000000",
   351  			0b_0000000000000000000000000000000000000000000000000000000000000000,
   352  		},
   353  		{
   354  			"0000000000000000000000000000000000000000000000000000000000000001",
   355  			0b_0000000000000000000000000000000000000000000000000000000000000001,
   356  		},
   357  		{
   358  			"0000000000000000000000000000000000000000000000000000000000001111",
   359  			0b_0000000000000000000000000000000000000000000000000000000000001111,
   360  		},
   361  		{
   362  			"1000000000000000000000000000000000000000000000000000000000000000",
   363  			0b_1000000000000000000000000000000000000000000000000000000000000000,
   364  		},
   365  		{
   366  			"1111000000000000000000000000000000000000000000000000000000000000",
   367  			0b_1111000000000000000000000000000000000000000000000000000000000000,
   368  		},
   369  		{
   370  			"1111000010100000110000001111110011100000110011001101000010111100",
   371  			0b_1111000010100000110000001111110011100000110011001101000010111100,
   372  		},
   373  		{
   374  			"1111111111111111111111111111111111111111111111111111111111111111",
   375  			0b_1111111111111111111111111111111111111111111111111111111111111111,
   376  		},
   377  		{
   378  			"00000000000000000000000000000000000000000000000000000000000000001",
   379  			0b_0000000000000000000000000000000000000000000000000000000000000000,
   380  		},
   381  		{
   382  			"00000000000000000000000000000000000000000000000000000000000000011",
   383  			0b_0000000000000000000000000000000000000000000000000000000000000001,
   384  		},
   385  		{
   386  			"10000000000000000000000000000000000000000000000000000000000000001",
   387  			0b_1000000000000000000000000000000000000000000000000000000000000000,
   388  		},
   389  		{
   390  			"11111111111111111111111111111111111111111111111111111111111111110",
   391  			0b_1111111111111111111111111111111111111111111111111111111111111111,
   392  		},
   393  		{
   394  			"00000000000000000000000000000000000000000000000000000000000000001111111111111111",
   395  			0b_0000000000000000000000000000000000000000000000000000000000000000,
   396  		},
   397  		{
   398  			"10000000000000000000000000000000000000000000000000000000000000011111111111111111",
   399  			0b_1000000000000000000000000000000000000000000000000000000000000001,
   400  		},
   401  		{
   402  			"10000000000000000000000000000000000000000000000000000000000000010000000000000000",
   403  			0b_1000000000000000000000000000000000000000000000000000000000000001,
   404  		},
   405  	}
   406  	for _, tc := range tcs {
   407  		core := bitarray.MustParse(tc.s)
   408  		for padbit := byte(0); padbit < 2; padbit++ {
   409  			pad := bitarray.New(padbit)
   410  			for prefix := 0; prefix < 18; prefix++ {
   411  				pre := pad.Repeat(prefix)
   412  				for suffix := 0; suffix < 10; suffix++ {
   413  					suf := pad.Repeat(suffix)
   414  					ba := bitarray.Join([]*bitarray.BitArray{pre, core, suf}, nil)
   415  					buf := bitarray.NewBufferFromBitArray(ba)
   416  					buf = buf.Slice(prefix, prefix+core.Len())
   417  					if got := buf.Uint64(); got != tc.v {
   418  						t.Errorf("unexpected result: got %d, want %d", got, tc.v)
   419  						t.Logf(" buf: %#b", buf)
   420  						t.Logf("core: %#b", core)
   421  					}
   422  				}
   423  			}
   424  		}
   425  	}
   426  	for _, tc := range tcs {
   427  		core := bitarray.MustParse(tc.s)
   428  		clen := core.Len()
   429  		if 64 < clen {
   430  			continue // skip
   431  		}
   432  		for padbit := byte(0); padbit < 2; padbit++ {
   433  			pad := bitarray.New(padbit)
   434  			for prefix := 0; prefix < 18; prefix++ {
   435  				for suffix := 0; suffix < 10; suffix++ {
   436  					ba := pad.Repeat(prefix + clen + suffix)
   437  					buf := bitarray.NewBufferFromBitArray(ba)
   438  					buf.Slice(prefix, prefix+clen).PutUint64(tc.v)
   439  					got := buf.BitArray().Slice(prefix, prefix+clen)
   440  					if !got.Equal(core) {
   441  						t.Errorf(
   442  							"unexpected result: v=%d, pre=%d, suf=%d",
   443  							tc.v, prefix, suffix,
   444  						)
   445  						t.Logf(" got: %#b", got)
   446  						t.Logf("want: %#b", core)
   447  						t.Logf(" buf: %#b", buf)
   448  					}
   449  				}
   450  			}
   451  		}
   452  	}
   453  }