github.com/tunabay/go-bitarray@v1.3.1/bitarray_parse_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 "math/rand" 9 "testing" 10 11 "github.com/tunabay/go-bitarray" 12 ) 13 14 func TestParse(t *testing.T) { 15 tdt := []string{ 16 " ", "", 17 "0", "0", 18 "1", "1", 19 "1+1", "11", 20 "0x_f0:f0:37:37", "11110000111100000011011100110111", 21 "0110110", "0110110", 22 " 1010-1111 \t 0011-1100 \r\n", "1010111100111100", 23 " 0xf \n +01", "111101", 24 " 000 + 0o0753 + 111 ", "000000111101011111", 25 26 "10101010\n0b_1111_1111 \n \n 0101010 + 111111111 \t\n+01010\t\n", 27 "1010101011111111010101011111111101010", 28 29 // 84 bits 30 "0o_7777_7777_7777_7777_7777_7777_7777", 31 "111111111111111111111111111111111111111111111111111111111111111111111111111111111111", 32 33 // 128 bits 34 "0x_0000_0000_0000_0000_0000_0000_0000_0000", 35 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 36 "0x_ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff", 37 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", 38 39 // 96 bits 40 "0o_0000_0000_0000_0000_0000_0000_0000_0000", 41 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 42 43 // 132 bits 44 "0x_0000_0000_0000_0000_0000_0000_0000_0000_0", 45 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 46 } 47 for i := 0; i < len(tdt); i += 2 { 48 ba, err := bitarray.Parse(tdt[i]) 49 if err != nil { 50 t.Errorf("%q: unexpected error: %s", tdt[i], err) 51 continue 52 } 53 ba.V() 54 if ba.String() != tdt[i+1] { 55 t.Errorf("%q: unexpected result.", tdt[i]) 56 t.Logf(" got: %#b", ba) 57 t.Logf(" got: %s", ba.D()) 58 t.Logf(" want: %q", tdt[i+1]) 59 } 60 } 61 } 62 63 func TestParse_rand(t *testing.T) { 64 tokens := []struct { 65 s, d string 66 e bool 67 }{ 68 {"0", "-", false}, 69 {"10", "-", false}, 70 {"01", "-", false}, 71 {"101", "-", false}, 72 {"1010", "-", false}, 73 {"10101", "-", false}, 74 {"01010", "-", false}, 75 {"101010", "-", false}, 76 {"0101010", "-", false}, 77 {"1010101", "-", false}, 78 {"1111111", "-", false}, 79 {"10101010", "-", false}, 80 {"01010101", "-", false}, 81 {"000000000", "-", false}, 82 {"111111111", "-", false}, 83 {"0000-0000 0000-0000 0000-0000", "000000000000000000000000", false}, 84 {"0000-0000 0000-0000 0000-0000 0", "0000000000000000000000000", false}, 85 {"1111-1111 1111-1111 1111-1111", "111111111111111111111111", false}, 86 {"1111-1111 1111-1111 1111-1111 1", "1111111111111111111111111", false}, 87 {"10_10", "1010", false}, 88 {"0b_1111_1111", "11111111", false}, 89 {"0b_1111_0101_01", "1111010101", false}, 90 {"0x5", "0101", false}, 91 {"0xF77", "111101110111", false}, 92 {"0o_0755", "000111101101", false}, 93 {"0o00", "000000", false}, 94 {"0o1234", "001010011100", false}, 95 {"0o567", "101110111", false}, 96 {"0xbeef", "1011111011101111", false}, 97 {"0xDEAD", "1101111010101101", false}, 98 {"0x333333333", "001100110011001100110011001100110011", false}, 99 {"0x:ff:33:88", "111111110011001110001000", false}, 100 {"0x000", "000000000000", false}, 101 {"0x_123", "000100100011", false}, 102 {"0x_45678", "01000101011001111000", false}, 103 {"0x_9a-bc:de-f", "1001101010111100110111101111", false}, 104 {"0x98", "10011000", false}, 105 {"0xa8 (pad=1)", "1010100", false}, 106 {"0xb8 (pad=2)", "101110", false}, 107 {"0xc8 (pad=3)", "11001", false}, 108 {"0o54", "101100", false}, 109 {"0o64 (pad=1)", "11010", false}, 110 {"0o74 (pad=2)", "1111", false}, 111 } 112 seps := []string{ 113 "+", " ", "\n", "\r\n", " + ", "\t", "\t+\t", " \t\n+", 114 "+\n", "\n\n ", "\n+\n", " \n \n ", "\n\n\n", 115 } 116 sps := []string{ 117 "", " ", "\r\n", " \n\t\n \n", "\t", "\r\n\r\n", 118 " \n", "\t\n", "\t\n", 119 } 120 for i := 0; i < 50000; i++ { 121 var src, want string 122 iserr := false 123 ntokens := rand.Intn(20) 124 for i := 0; i < ntokens; i++ { 125 if i != 0 { 126 src += seps[rand.Intn(len(seps))] 127 } 128 tok := tokens[rand.Intn(len(tokens))] 129 src += tok.s 130 exp := tok.d 131 if exp == "-" { 132 exp = tok.s 133 } 134 want += exp 135 iserr = iserr || tok.e 136 } 137 src = sps[rand.Intn(len(sps))] + src + sps[rand.Intn(len(sps))] 138 if ba, err := bitarray.Parse(src); err != nil { 139 if !iserr { 140 t.Errorf("%q: unexpected error.", src) 141 t.Logf(" err: %v", err) 142 } else { 143 // t.Logf(" pass: %v", err) 144 } 145 } else { 146 if iserr { 147 t.Errorf("%q: error expected but no error.", src) 148 t.Logf(" got: %q", ba) 149 } else { 150 s := ba.String() 151 if s != want { 152 t.Errorf("%q: unexpected result.", src) 153 t.Logf(" got: %q", s) 154 t.Logf(" want: %q", want) 155 } else { 156 // t.Logf(" pass: %q", s) 157 } 158 } 159 } 160 } 161 } 162 163 func TestParse_error(t *testing.T) { 164 tcs := []string{ 165 "invalid data", 166 "0bx00", 167 "0x0101x", 168 "0x_", 169 "0x", 170 "0q0", // invalid base specifier 171 "0b2", // invalid bin 172 "0o8", // invalid oct 173 "0xX", // invalid hex 174 "0b1111(pad=1)", // invalid pad 175 "0xffff(pad=4)", // invalid pad 176 "0xfffff(pad=-1)", // invalid pad 177 "0o777(pad=3)", // invalid pad 178 "0o7777(pad=-1)", // invalid pad 179 "0b__100", // invalid separator 180 "0000--1111", // invalid separator 181 "-00001111", // invalid separator 182 "00001111-", // invalid separator 183 "0x::0", // invalid separator 184 "0000 ++ 0000", 185 "+ 0000", 186 "0000 + ", 187 } 188 for _, tc := range tcs { 189 ba, err := bitarray.Parse(tc) 190 if err == nil { 191 t.Error("error expected:") 192 t.Logf("data: %q", tc) 193 t.Logf(" got: %#b", ba) 194 t.Logf(" got: %s", ba.D()) 195 // continue 196 } 197 // t.Logf("pass: %s", err) 198 } 199 } 200 201 func TestMustParse_panic(t *testing.T) { 202 func() { 203 var ba *bitarray.BitArray 204 defer func() { 205 if recover() == nil { 206 t.Errorf("panic expected: got %#b", ba) 207 } 208 }() 209 ba = bitarray.MustParse("invalid input") 210 }() 211 }