string_test.go 1.0 KB

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