github.com/tunabay/go-bitarray@v1.3.1/bitarray_format_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  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/tunabay/go-bitarray"
    12  )
    13  
    14  func TestBitArray_Format_sprintf(t *testing.T) {
    15  	ba := bitarray.MustParse("0000-1111 0101-0000 1010-1111 1001-0")
    16  	tcs := [][]string{
    17  		{"%b", "00001111010100001010111110010"},
    18  		{"%s", "00001111010100001010111110010"},
    19  		{"%v", "00001111010100001010111110010"},
    20  
    21  		{"% b", "00001111 01010000 10101111 10010"},
    22  		{"%#b", "0000-1111 0101-0000 1010-1111 1001-0"},
    23  		{"%# b", "0000-1111 0101-0000 1010-1111 1001-0"},
    24  
    25  		{"%q", `"00001111010100001010111110010"`},
    26  		{"% q", `"00001111 01010000 10101111 10010"`},
    27  		{"%#q", `"0000-1111 0101-0000 1010-1111 1001-0"`},
    28  
    29  		{"%o", "0365025744"},
    30  		{"% o", "03650257 44"},
    31  		{"%+o", "0365025744(pad=1)"},
    32  		{"%+ o", "03650257 44 (pad=1)"},
    33  		{"% +o", "03650257 44 (pad=1)"},
    34  		{"%#o", "0365-0257 44"},
    35  		{"%#+o", "0365-0257 44 (pad=1)"},
    36  
    37  		{"%x", "0f50af90"},
    38  		{"%X", "0F50AF90"},
    39  		{"% x", "0f50af90"},
    40  		{"%#x", "0f50 af90"},
    41  		{"%+x", "0f50af90(pad=3)"},
    42  		{"%#+x", "0f50 af90 (pad=3)"},
    43  
    44  		{"[%50b]", "[                     00001111010100001010111110010]"},
    45  		{"[%-50b]", "[00001111010100001010111110010                     ]"},
    46  		{"[% 50b]", "[                  00001111 01010000 10101111 10010]"},
    47  		{"[% -50b]", "[00001111 01010000 10101111 10010                  ]"},
    48  		{"[%#50b]", "[              0000-1111 0101-0000 1010-1111 1001-0]"},
    49  		{"[%#-50b]", "[0000-1111 0101-0000 1010-1111 1001-0              ]"},
    50  
    51  		{"[%50q]", `[                   "00001111010100001010111110010"]`},
    52  		{"[%-50q]", `["00001111010100001010111110010"                   ]`},
    53  		{"[% 50q]", `[                "00001111 01010000 10101111 10010"]`},
    54  		{"[% -50q]", `["00001111 01010000 10101111 10010"                ]`},
    55  		{"[%#50q]", `[            "0000-1111 0101-0000 1010-1111 1001-0"]`},
    56  		{"[%#-50q]", `["0000-1111 0101-0000 1010-1111 1001-0"            ]`},
    57  
    58  		{"[%50x]", "[                                          0f50af90]"},
    59  		{"[%-50x]", "[0f50af90                                          ]"},
    60  		{"[%#50x]", "[                                         0f50 af90]"},
    61  		{"[%#-50x]", "[0f50 af90                                         ]"},
    62  		{"[%#+50x]", "[                                 0f50 af90 (pad=3)]"},
    63  		{"[%#+-50x]", "[0f50 af90 (pad=3)                                 ]"},
    64  
    65  		{"[%50o]", "[                                        0365025744]"},
    66  		{"[%-50o]", "[0365025744                                        ]"},
    67  		{"[% 50o]", "[                                       03650257 44]"},
    68  		{"[% -50o]", "[03650257 44                                       ]"},
    69  		{"[%#50o]", "[                                      0365-0257 44]"},
    70  		{"[%#-50o]", "[0365-0257 44                                      ]"},
    71  		{"[%+50o]", "[                                 0365025744(pad=1)]"},
    72  		{"[%+-50o]", "[0365025744(pad=1)                                 ]"},
    73  		{"[% +50o]", "[                               03650257 44 (pad=1)]"},
    74  		{"[% +-50o]", "[03650257 44 (pad=1)                               ]"},
    75  		{"[%#+50o]", "[                              0365-0257 44 (pad=1)]"},
    76  		{"[%#+-50o]", "[0365-0257 44 (pad=1)                              ]"},
    77  
    78  		// errors
    79  		{"%z", `%!z(BitArray="0000-1111 0101-0000 1010-1111 1001-0")`}, // unknown verb z
    80  		{"%m", `%!m(BitArray="0000-1111 0101-0000 1010-1111 1001-0")`}, // unknown verb m
    81  		{"%010b", `%!b(ERROR: unsupported flag 0)`},                    // 0 flag for b
    82  		{"%010s", `%!s(ERROR: unsupported flag 0)`},                    // 0 flag for s
    83  		{"%010q", `%!q(ERROR: unsupported flag 0)`},                    // 0 flag for q
    84  		{"%010o", `%!o(ERROR: unsupported flag 0)`},                    // 0 flag for o
    85  		{"%010x", `%!x(ERROR: unsupported flag 0)`},                    // 0 flag for x
    86  		{"%010X", `%!X(ERROR: unsupported flag 0)`},                    // 0 flag for X
    87  		{"%#v", `%!v(ERROR: unsupported flag #)`},                      // # flag for v
    88  	}
    89  	for _, tc := range tcs {
    90  		s := fmt.Sprintf(tc[0], ba)
    91  		if s != tc[1] {
    92  			t.Errorf("unexpected output for %q", tc[0])
    93  			t.Logf(" got: %q", s)
    94  			t.Logf("want: %q", tc[1])
    95  		}
    96  	}
    97  }