github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/serial/string_test.go (about)

     1  package serial_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	. "github.com/xmplusdev/xmcore/common/serial"
     9  )
    10  
    11  func TestToString(t *testing.T) {
    12  	s := "a"
    13  	data := []struct {
    14  		Value  interface{}
    15  		String string
    16  	}{
    17  		{Value: s, String: s},
    18  		{Value: &s, String: s},
    19  		{Value: errors.New("t"), String: "t"},
    20  		{Value: []byte{'b', 'c'}, String: "[98 99]"},
    21  	}
    22  
    23  	for _, c := range data {
    24  		if r := cmp.Diff(ToString(c.Value), c.String); r != "" {
    25  			t.Error(r)
    26  		}
    27  	}
    28  }
    29  
    30  func TestConcat(t *testing.T) {
    31  	testCases := []struct {
    32  		Input  []interface{}
    33  		Output string
    34  	}{
    35  		{
    36  			Input: []interface{}{
    37  				"a", "b",
    38  			},
    39  			Output: "ab",
    40  		},
    41  	}
    42  
    43  	for _, testCase := range testCases {
    44  		actual := Concat(testCase.Input...)
    45  		if actual != testCase.Output {
    46  			t.Error("Unexpected output: ", actual, " but want: ", testCase.Output)
    47  		}
    48  	}
    49  }
    50  
    51  func BenchmarkConcat(b *testing.B) {
    52  	input := []interface{}{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}
    53  
    54  	b.ReportAllocs()
    55  	for i := 0; i < b.N; i++ {
    56  		_ = Concat(input...)
    57  	}
    58  }