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