ymd_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import "testing"
  3. func TestYMD(t *testing.T) {
  4. table := map[YMD][5]int{
  5. YMD(20220127): {2022, 1, 27, 20220101, 20220131},
  6. YMD(22000220): {2200, 2, 20, 22000201, 22000228}, // Not a leap year
  7. YMD(20200217): {2020, 2, 17, 20200201, 20200229}, // 2020 Leap year
  8. YMD(19991031): {1999, 10, 31, 19991001, 19991031},
  9. YMD(19900228): {1990, 2, 28, 19900201, 19900228},
  10. YMD(20381231): {2038, 12, 31, 20381201, 20381231},
  11. }
  12. for d, ts := range table {
  13. newDate := NewYMD(ts[0], ts[1], ts[2])
  14. if newDate != d {
  15. t.Errorf("NewDBDate %v, got %v expected %v\n",
  16. ts, newDate, d)
  17. }
  18. year, month, day := newDate.YMD()
  19. if (year != ts[0]) || (month != ts[1]) || (day != ts[2]) {
  20. t.Errorf("YMD %v, got %d,%d,%d expected %v\n",
  21. newDate, year, month, day, ts)
  22. }
  23. // convert DBDate to time.Time
  24. newTime := ToTime(d)
  25. // convert time.Time to DBDate
  26. dateFromTime := NewYMDFromTime(newTime)
  27. if newTime.Year() != ts[0] {
  28. t.Errorf("DBDateTime %v, Year got %d expected %d\n", d, newTime.Year(), ts[0])
  29. }
  30. if int(newTime.Month()) != ts[1] {
  31. t.Errorf("DBDateTime %v, Month got %d expected %d\n", d, newTime.Month(), ts[1])
  32. }
  33. if newTime.Day() != ts[2] {
  34. t.Errorf("DBDateTime %v, Day got %d expected %d\n", d, newTime.Day(), ts[2])
  35. }
  36. if newTime.Year() != newDate.Year() {
  37. t.Errorf("Year %v, got %d and %d\n",
  38. d, newTime.Year(), newDate.Year())
  39. }
  40. if int(newTime.Month()) != newDate.Month() {
  41. t.Errorf("Month %v, got %d and %d\n",
  42. d, newTime.Month(), newDate.Month())
  43. }
  44. if newTime.Day() != newDate.Day() {
  45. t.Errorf("Year %v, got %d and %d\n",
  46. d, newTime.Day(), newDate.Day())
  47. }
  48. if dateFromTime.Year() != newDate.Year() {
  49. t.Errorf("DFT Year %v, got %d and %d\n",
  50. d, dateFromTime.Year(), newDate.Year())
  51. }
  52. if dateFromTime.Month() != newDate.Month() {
  53. t.Errorf("DFT Month %v, got %d and %d\n",
  54. d, dateFromTime.Month(), newDate.Month())
  55. }
  56. if dateFromTime.Day() != newDate.Day() {
  57. t.Errorf("DFT Year %v, got %d and %d\n",
  58. d, dateFromTime.Day(), newDate.Day())
  59. }
  60. newDate.First()
  61. if int(newDate) != ts[3] {
  62. t.Errorf("First %v, got %d, expected %d\n",
  63. d, newDate, ts[3])
  64. }
  65. newDate.Last()
  66. if int(newDate) != ts[4] {
  67. t.Errorf("Last %v, got %d, expected %d\n",
  68. d, newDate, ts[4])
  69. }
  70. newYM := newDate.YM()
  71. if int(newYM) != int(newDate)/100 {
  72. t.Errorf("YM got %d, expected %d\n", newYM, int(newDate)/100)
  73. }
  74. backToDate := newYM.YMD()
  75. newDate.First()
  76. if newDate != backToDate {
  77. t.Errorf("YM to YMD got %d, expected %d\n", backToDate, newDate)
  78. }
  79. }
  80. }
  81. func TestYM(t *testing.T) {
  82. table := map[YM][2]int{
  83. YM(202201): {2022, 1},
  84. YM(220002): {2200, 2},
  85. YM(202002): {2020, 2},
  86. YM(199910): {1999, 10},
  87. YM(199002): {1990, 2},
  88. YM(203812): {2038, 12},
  89. }
  90. for d, ts := range table {
  91. if d.Year() != ts[0] {
  92. t.Errorf("Year got %d, expected %d\n", d.Year(), ts[0])
  93. }
  94. if d.Month() != ts[1] {
  95. t.Errorf("Month got %d, expected %d\n", d.Month(), ts[1])
  96. }
  97. newYM := NewYM(ts[0], ts[1])
  98. if newYM != d {
  99. t.Errorf("NewYM got %d, expected %d\n", newYM, d)
  100. }
  101. }
  102. }