xstr_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package xstr
  2. import (
  3. "testing"
  4. )
  5. func TestJoinInts(t *testing.T) {
  6. // test empty slice
  7. is := []int64{}
  8. s := JoinInts(is)
  9. if s != "" {
  10. t.Errorf("input:%v,output:%s,result is incorrect", is, s)
  11. } else {
  12. t.Logf("input:%v,output:%s", is, s)
  13. }
  14. // test len(slice)==1
  15. is = []int64{1}
  16. s = JoinInts(is)
  17. if s != "1" {
  18. t.Errorf("input:%v,output:%s,result is incorrect", is, s)
  19. } else {
  20. t.Logf("input:%v,output:%s", is, s)
  21. }
  22. // test len(slice)>1
  23. is = []int64{1, 2, 3}
  24. s = JoinInts(is)
  25. if s != "1,2,3" {
  26. t.Errorf("input:%v,output:%s,result is incorrect", is, s)
  27. } else {
  28. t.Logf("input:%v,output:%s", is, s)
  29. }
  30. }
  31. func TestSplitInts(t *testing.T) {
  32. // test empty slice
  33. s := ""
  34. is, err := SplitInts(s)
  35. if err != nil || len(is) != 0 {
  36. t.Error(err)
  37. }
  38. // test split int64
  39. s = "1,2,3"
  40. is, err = SplitInts(s)
  41. if err != nil || len(is) != 3 {
  42. t.Error(err)
  43. }
  44. }
  45. func BenchmarkJoinInts(b *testing.B) {
  46. is := make([]int64, 10000, 10000)
  47. for i := int64(0); i < 10000; i++ {
  48. is[i] = i
  49. }
  50. b.ResetTimer()
  51. b.RunParallel(func(pb *testing.PB) {
  52. for pb.Next() {
  53. JoinInts(is)
  54. }
  55. })
  56. }