github.com/searKing/golang/go@v1.2.74/bytes/bytes_test.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bytes_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	bytes_ "github.com/searKing/golang/go/bytes"
    11  )
    12  
    13  func TestPadLeft(t *testing.T) {
    14  	table := []struct {
    15  		Q   string
    16  		pad string
    17  		n   int
    18  		R   string
    19  	}{
    20  		{
    21  			Q:   "a",
    22  			pad: "*",
    23  			n:   -1,
    24  			R:   "a",
    25  		},
    26  		{
    27  			Q:   "a",
    28  			pad: "*",
    29  			n:   10,
    30  			R:   "*********a",
    31  		},
    32  		{
    33  			Q:   "a",
    34  			pad: "*^",
    35  			n:   5,
    36  			R:   "*^*^a",
    37  		},
    38  		{
    39  			Q:   "a",
    40  			pad: "*^",
    41  			n:   6,
    42  			R:   "*^*^ a",
    43  		},
    44  	}
    45  
    46  	for i, test := range table {
    47  		qr := bytes_.PadLeft([]byte(test.Q), []byte(test.pad), test.n)
    48  		if string(qr) != test.R {
    49  			t.Errorf("#%d. got %q, want %q", i, qr, test.R)
    50  		}
    51  	}
    52  }
    53  
    54  func TestPadRight(t *testing.T) {
    55  	table := []struct {
    56  		Q   string
    57  		pad string
    58  		n   int
    59  		R   string
    60  	}{
    61  		{
    62  			Q:   "a",
    63  			pad: "*",
    64  			n:   -1,
    65  			R:   "a",
    66  		},
    67  		{
    68  			Q:   "a",
    69  			pad: "*",
    70  			n:   1,
    71  			R:   "a",
    72  		},
    73  		{
    74  			Q:   "a",
    75  			pad: "*",
    76  			n:   10,
    77  			R:   "a*********",
    78  		},
    79  		{
    80  			Q:   "a",
    81  			pad: "*^",
    82  			n:   5,
    83  			R:   "a*^*^",
    84  		},
    85  		{
    86  			Q:   "a",
    87  			pad: "*^",
    88  			n:   6,
    89  			R:   "a *^*^",
    90  		},
    91  	}
    92  
    93  	for i, test := range table {
    94  		qr := bytes_.PadRight([]byte(test.Q), []byte(test.pad), test.n)
    95  		if string(qr) != test.R {
    96  			t.Errorf("#%d. got %q, want %q", i, qr, test.R)
    97  		}
    98  	}
    99  }
   100  
   101  func TestReverse(t *testing.T) {
   102  	table := []struct {
   103  		Q string
   104  		R string
   105  	}{
   106  		{
   107  			Q: "abc123",
   108  			R: "321cba",
   109  		},
   110  		{
   111  			Q: "Hello, 世界",
   112  			R: "\x8c\x95疸\xe4 ,olleH",
   113  		},
   114  	}
   115  
   116  	for i, test := range table {
   117  		qr := bytes_.Reverse([]byte(test.Q))
   118  		if string(qr) != test.R {
   119  			t.Errorf("#%d. got %q, want %q", i, qr, test.R)
   120  		}
   121  	}
   122  }