github.com/tunabay/go-bitarray@v1.3.1/bitarray_encoding_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  	"encoding/hex"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/tunabay/go-bitarray"
    14  )
    15  
    16  func TestBitArray_MarshalBinary(t *testing.T) {
    17  	tdt := []string{
    18  		"", "",
    19  		"0", "04",
    20  		"1", "84",
    21  		"00", "03",
    22  		"11", "c3",
    23  		"000", "02",
    24  		"111", "e2",
    25  		"0000", "01",
    26  		"1111", "f1",
    27  		"0000-0", "00",
    28  		"1111-1", "f8",
    29  		"0000-00", "0007",
    30  		"1111-11", "fc07",
    31  		"0000-000", "0006",
    32  		"1111-111", "fe06",
    33  		"0000-0000", "0005",
    34  		"1111-1111", "ff05",
    35  		"0000-0000 0", "0004",
    36  		"1111-1111 1", "ff84",
    37  
    38  		// 127 bits
    39  		"0x_0000_0000_0000_0000_0000_0000_0000_000 000",
    40  		"0000000000000000000000000000000006",
    41  		"0x_ffff_ffff_ffff_ffff_ffff_ffff_ffff_fff 111",
    42  		"fffffffffffffffffffffffffffffffe06",
    43  
    44  		// 128 bits
    45  		"0x_0000_0000_0000_0000_0000_0000_0000_0000",
    46  		"0000000000000000000000000000000005",
    47  		"0x_ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff",
    48  		"ffffffffffffffffffffffffffffffff05",
    49  
    50  		// 129 bits
    51  		"0x_0000_0000_0000_0000_0000_0000_0000_0000 0",
    52  		"0000000000000000000000000000000004",
    53  		"0x_ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff 1",
    54  		"ffffffffffffffffffffffffffffffff84",
    55  	}
    56  	for i := 0; i < len(tdt); i += 2 {
    57  		baO := bitarray.MustParse(tdt[i]).ZOptimize()
    58  		baE := baO.ZExpand()
    59  		want, _ := hex.DecodeString(tdt[i+1])
    60  		if got, _ := baO.MarshalBinary(); !bytes.Equal(got, want) {
    61  			t.Error("unexpected result:")
    62  			t.Logf(" src: %#b", baO)
    63  			t.Logf(" src: %s", baO.D())
    64  			t.Logf(" got: % x", got)
    65  			t.Logf("want: % x", want)
    66  		}
    67  		if got, _ := baE.MarshalBinary(); !bytes.Equal(got, want) {
    68  			t.Error("unexpected result:")
    69  			t.Logf(" src: %#b", baE)
    70  			t.Logf(" src: %s", baE.D())
    71  			t.Logf(" got: % x", got)
    72  			t.Logf("want: % x", want)
    73  		}
    74  	}
    75  	var nilba *bitarray.BitArray
    76  	if got, _ := nilba.MarshalBinary(); got == nil || len(got) != 0 {
    77  		t.Errorf("unexpected result: got %+v, want empty", got)
    78  	}
    79  }
    80  
    81  // also tests MarshalJSON, MarshalYAML
    82  func TestBitArray_MarshalText(t *testing.T) {
    83  	tdt := []string{
    84  		"", "",
    85  		"0", "0",
    86  		"1", "1",
    87  		"0000", "0000",
    88  		"0000-0000", "00000000",
    89  		"0000-0000 0", "000000000",
    90  		"1111-1111", "11111111",
    91  		"1111-111111", "1111111111",
    92  
    93  		// 127 bits
    94  		"0x_0000_0000_0000_0000_0000_0000_0000_000 000",
    95  		"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    96  		"0x_ffff_ffff_ffff_ffff_ffff_ffff_ffff_fff 111",
    97  		"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
    98  
    99  		// 128 bits
   100  		"0x_0000_0000_0000_0000_0000_0000_0000_0000",
   101  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   102  		"0x_ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff",
   103  		"11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
   104  
   105  		// 129 bits
   106  		"0x_0000_0000_0000_0000_0000_0000_0000_0000 0",
   107  		"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   108  		"0x_ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff 1",
   109  		"111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
   110  	}
   111  	chk := func(ba *bitarray.BitArray, want string) {
   112  		gotB, _ := ba.MarshalText()
   113  		if string(gotB) != want {
   114  			t.Error("MarshalText: unexpected result:")
   115  			t.Logf(" src: %#b", ba)
   116  			t.Logf(" src: %s", ba.D())
   117  			t.Logf(" got: %s", gotB)
   118  			t.Logf("want: %s", want)
   119  		}
   120  		gotB, _ = ba.MarshalJSON()
   121  		jwant := fmt.Sprintf(`"%s"`, want)
   122  		if string(gotB) != jwant {
   123  			t.Error("MarshalJSON: unexpected result:")
   124  			t.Logf(" src: %#b", ba)
   125  			t.Logf(" src: %s", ba.D())
   126  			t.Logf(" got: %s", gotB)
   127  			t.Logf("want: %s", jwant)
   128  		}
   129  		yif, _ := ba.MarshalYAML()
   130  		ystr, ok := yif.(string)
   131  		if !ok {
   132  			t.Fatalf("MarshalYAML: unexpected type: %T, %+v", yif, yif)
   133  		}
   134  		if ystr != want {
   135  			t.Error("MarshalYAML: unexpected result:")
   136  			t.Logf(" src: %#b", ba)
   137  			t.Logf(" src: %s", ba.D())
   138  			t.Logf(" got: %s", ystr)
   139  			t.Logf("want: %s", want)
   140  		}
   141  	}
   142  	for i := 0; i < len(tdt); i += 2 {
   143  		baO := bitarray.MustParse(tdt[i]).ZOptimize()
   144  		baE := baO.ZExpand()
   145  		chk(baO, tdt[i+1])
   146  		chk(baE, tdt[i+1])
   147  	}
   148  	var nilba *bitarray.BitArray
   149  	if got, _ := nilba.MarshalText(); got == nil || len(got) != 0 {
   150  		t.Errorf("unexpected result: got %+v, want empty", got)
   151  	}
   152  	if got, _ := nilba.MarshalJSON(); string(got) != `""` {
   153  		t.Errorf(`unexpected result: got %s, want ""`, got)
   154  	}
   155  	if got, _ := nilba.MarshalYAML(); got != nil {
   156  		t.Errorf("unexpected result: got (%T) %+v, want nil", got, got)
   157  	}
   158  }