schedule_test.go 2.5 KB

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