string_test.go 1021 B

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