utils_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package util
  2. import (
  3. . "github.com/smartystreets/goconvey/convey"
  4. "testing"
  5. "time"
  6. )
  7. var periodTimeTest = [][]string{
  8. // time, period, now, expected
  9. {"11:00:00", "1h", "10:00:00", "0000-01-01 11:00:00"},
  10. {"11:00:00", "1m", "10:00:00", "0000-01-01 10:01:00"},
  11. {"11:00:00", "1s", "10:00:00", "0000-01-01 10:00:01"},
  12. {"11:00:00", "8h", "10:00:00", "0000-01-01 11:00:00"},
  13. {"11:00:00", "12h", "10:00:00", "0000-01-01 11:00:00"},
  14. {"11:00:00", "23h", "10:00:00", "0000-01-01 11:00:00"},
  15. {"09:00:10", "1h", "10:00:00", "0000-01-01 10:00:10"},
  16. {"09:00:10", "1m", "10:00:00", "0000-01-01 10:00:10"},
  17. {"09:00:10", "1s", "10:00:00", "0000-01-01 10:00:00"},
  18. {"09:00:10", "8h", "10:00:00", "0000-01-01 17:00:10"},
  19. {"09:00:10", "12h", "10:00:00", "0000-01-01 21:00:10"},
  20. {"09:00:10", "4h", "10:00:00", "0000-01-01 13:00:10"},
  21. {"09:00:10", "24h", "10:00:00", "0000-01-02 09:00:10"},
  22. {"09:00:10", "8h", "10:00:00", "0000-01-01 17:00:10"},
  23. }
  24. func TestGetNextPeriodTime(t *testing.T) {
  25. Convey("test get period", t, func() {
  26. for i, v := range periodTimeTest {
  27. //needTime, _ := time.Parse("15:04:05", v[0])
  28. period, _ := time.ParseDuration(v[1])
  29. now, _ := time.Parse("15:04:05", v[2])
  30. expected, _ := time.Parse("2006-01-02 15:04:05", v[3])
  31. actual, err := GetNextPeriodTime(v[0], period, now)
  32. So(err, ShouldEqual, nil)
  33. t.Logf("[%d]actual:+%v, expected:+%v", i, actual, expected)
  34. So(actual.Equal(expected), ShouldEqual, true)
  35. }
  36. })
  37. }
  38. func TestUnSetBit64(t *testing.T) {
  39. var (
  40. testCase = [][]int64{
  41. // attr, bit, result
  42. {1, 0, 0},
  43. {2, 0, 2},
  44. {2, 1, 0},
  45. {3, 1, 1},
  46. {3, 64, 3},
  47. }
  48. )
  49. Convey("test for unset bit 64", t, func() {
  50. for _, v := range testCase {
  51. So(UnSetBit64(v[0], uint(v[1])), ShouldEqual, v[2])
  52. }
  53. })
  54. }