You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.6 KiB

  1. package cron
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestRange(t *testing.T) {
  8. ranges := []struct {
  9. expr string
  10. min, max uint
  11. expected uint64
  12. }{
  13. {"5", 0, 7, 1 << 5},
  14. {"0", 0, 7, 1 << 0},
  15. {"7", 0, 7, 1 << 7},
  16. {"5-5", 0, 7, 1 << 5},
  17. {"5-6", 0, 7, 1<<5 | 1<<6},
  18. {"5-7", 0, 7, 1<<5 | 1<<6 | 1<<7},
  19. {"5-6/2", 0, 7, 1 << 5},
  20. {"5-7/2", 0, 7, 1<<5 | 1<<7},
  21. {"5-7/1", 0, 7, 1<<5 | 1<<6 | 1<<7},
  22. {"*", 1, 3, 1<<1 | 1<<2 | 1<<3 | starBit},
  23. {"*/2", 1, 3, 1<<1 | 1<<3 | starBit},
  24. }
  25. for _, c := range ranges {
  26. actual := getRange(c.expr, bounds{c.min, c.max, nil})
  27. if actual != c.expected {
  28. t.Errorf("%s => (expected) %d != %d (actual)", c.expr, c.expected, actual)
  29. }
  30. }
  31. }
  32. func TestField(t *testing.T) {
  33. fields := []struct {
  34. expr string
  35. min, max uint
  36. expected uint64
  37. }{
  38. {"5", 1, 7, 1 << 5},
  39. {"5,6", 1, 7, 1<<5 | 1<<6},
  40. {"5,6,7", 1, 7, 1<<5 | 1<<6 | 1<<7},
  41. {"1,5-7/2,3", 1, 7, 1<<1 | 1<<5 | 1<<7 | 1<<3},
  42. }
  43. for _, c := range fields {
  44. actual := getField(c.expr, bounds{c.min, c.max, nil})
  45. if actual != c.expected {
  46. t.Errorf("%s => (expected) %d != %d (actual)", c.expr, c.expected, actual)
  47. }
  48. }
  49. }
  50. func TestBits(t *testing.T) {
  51. allBits := []struct {
  52. r bounds
  53. expected uint64
  54. }{
  55. {minutes, 0xfffffffffffffff}, // 0-59: 60 ones
  56. {hours, 0xffffff}, // 0-23: 24 ones
  57. {dom, 0xfffffffe}, // 1-31: 31 ones, 1 zero
  58. {months, 0x1ffe}, // 1-12: 12 ones, 1 zero
  59. {dow, 0x7f}, // 0-6: 7 ones
  60. }
  61. for _, c := range allBits {
  62. actual := all(c.r) // all() adds the starBit, so compensate for that..
  63. if c.expected|starBit != actual {
  64. t.Errorf("%d-%d/%d => (expected) %b != %b (actual)",
  65. c.r.min, c.r.max, 1, c.expected|starBit, actual)
  66. }
  67. }
  68. bits := []struct {
  69. min, max, step uint
  70. expected uint64
  71. }{
  72. {0, 0, 1, 0x1},
  73. {1, 1, 1, 0x2},
  74. {1, 5, 2, 0x2a}, // 101010
  75. {1, 4, 2, 0xa}, // 1010
  76. }
  77. for _, c := range bits {
  78. actual := getBits(c.min, c.max, c.step)
  79. if c.expected != actual {
  80. t.Errorf("%d-%d/%d => (expected) %b != %b (actual)",
  81. c.min, c.max, c.step, c.expected, actual)
  82. }
  83. }
  84. }
  85. func TestSpecSchedule(t *testing.T) {
  86. entries := []struct {
  87. expr string
  88. expected Schedule
  89. }{
  90. {"* 5 * * * *", &SpecSchedule{all(seconds), 1 << 5, all(hours), all(dom), all(months), all(dow)}},
  91. {"@every 5m", ConstantDelaySchedule{time.Duration(5) * time.Minute}},
  92. }
  93. for _, c := range entries {
  94. actual, err := Parse(c.expr)
  95. if err != nil {
  96. t.Error(err)
  97. }
  98. if !reflect.DeepEqual(actual, c.expected) {
  99. t.Errorf("%s => (expected) %b != %b (actual)", c.expr, c.expected, actual)
  100. }
  101. }
  102. }