Parse standard units of time.
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.

duration_test.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package duration
  2. import "testing"
  3. var durationTests = []struct {
  4. str string
  5. d Duration
  6. }{
  7. {"0s", 0},
  8. {"1ns", 1 * Nanosecond},
  9. {"1.1µs", 1100 * Nanosecond},
  10. {"2.2ms", 2200 * Microsecond},
  11. {"3.3s", 3300 * Millisecond},
  12. {"4m5s", 4*Minute + 5*Second},
  13. {"4m5.001s", 4*Minute + 5001*Millisecond},
  14. {"5h6m7.001s", 5*Hour + 6*Minute + 7001*Millisecond},
  15. {"2d3h4m", 2*Day + 3*Hour + 4*Minute},
  16. {"6w3d0h", 6*Week + 3*Day},
  17. {"8m0.000000001s", 8*Minute + 1*Nanosecond},
  18. {"15250w1d23h", 1<<63 - 1},
  19. {"-15250w1d23h", -1 << 63},
  20. }
  21. func TestDurationString(t *testing.T) {
  22. for _, tt := range durationTests {
  23. if str := tt.d.String(); str != tt.str {
  24. t.Errorf("Duration(%d).String() = %s, want %s", int64(tt.d), str, tt.str)
  25. } else {
  26. t.Logf("Duration(%d).String() = %s", int64(tt.d), str)
  27. }
  28. if tt.d > 0 {
  29. if str := (-tt.d).String(); str != "-"+tt.str {
  30. t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str)
  31. } else {
  32. t.Logf("Duration(%d).String() = %s", int64(-tt.d), str)
  33. }
  34. }
  35. }
  36. }
  37. var parseDurationTests = []struct {
  38. in string
  39. ok bool
  40. want Duration
  41. }{
  42. // simple
  43. {"0", true, 0},
  44. {"5s", true, 5 * Second},
  45. {"30s", true, 30 * Second},
  46. {"1478s", true, 1478 * Second},
  47. // sign
  48. {"-5s", true, -5 * Second},
  49. {"+5s", true, 5 * Second},
  50. {"-0", true, 0},
  51. {"+0", true, 0},
  52. // decimal
  53. {"5.0s", true, 5 * Second},
  54. {"5.6s", true, 5*Second + 600*Millisecond},
  55. {"5.s", true, 5 * Second},
  56. {".5s", true, 500 * Millisecond},
  57. {"1.0s", true, 1 * Second},
  58. {"1.00s", true, 1 * Second},
  59. {"1.004s", true, 1*Second + 4*Millisecond},
  60. {"1.0040s", true, 1*Second + 4*Millisecond},
  61. {"100.00100s", true, 100*Second + 1*Millisecond},
  62. // different units
  63. {"10ns", true, 10 * Nanosecond},
  64. {"11us", true, 11 * Microsecond},
  65. {"12µs", true, 12 * Microsecond}, // U+00B5
  66. {"12μs", true, 12 * Microsecond}, // U+03BC
  67. {"13ms", true, 13 * Millisecond},
  68. {"14s", true, 14 * Second},
  69. {"15m", true, 15 * Minute},
  70. {"16h", true, 16 * Hour},
  71. {"12d", true, 12 * Day},
  72. {"3w", true, 3 * Week},
  73. // composite durations
  74. {"3h30m", true, 3*Hour + 30*Minute},
  75. {"10.5s4m", true, 4*Minute + 10*Second + 500*Millisecond},
  76. {"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)},
  77. {"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
  78. {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
  79. {"2w3d12h", true, 2*Week + 3*Day + 12*Hour},
  80. // large value
  81. {"52763797000ns", true, 52763797000 * Nanosecond},
  82. // more than 9 digits after decimal point, see https://golang.org/issue/6617
  83. {"0.3333333333333333333h", true, 20 * Minute},
  84. // 9007199254740993 = 1<<53+1 cannot be stored precisely in a float64
  85. {"9007199254740993ns", true, (1<<53 + 1) * Nanosecond},
  86. // largest duration that can be represented by int64 in nanoseconds
  87. {"9223372036854775807ns", true, (1<<63 - 1) * Nanosecond},
  88. {"9223372036854775.807us", true, (1<<63 - 1) * Nanosecond},
  89. {"9223372036s854ms775us807ns", true, (1<<63 - 1) * Nanosecond},
  90. // large negative value
  91. {"-9223372036854775807ns", true, -1<<63 + 1*Nanosecond},
  92. // errors
  93. {"", false, 0},
  94. {"3", false, 0},
  95. {"-", false, 0},
  96. {"s", false, 0},
  97. {".", false, 0},
  98. {"-.", false, 0},
  99. {".s", false, 0},
  100. {"+.s", false, 0},
  101. {"3000000h", false, 0}, // overflow
  102. {"9223372036854775808ns", false, 0}, // overflow
  103. {"9223372036854775.808us", false, 0}, // overflow
  104. {"9223372036854ms775us808ns", false, 0}, // overflow
  105. // largest negative value of type int64 in nanoseconds should fail
  106. // see https://go-review.googlesource.com/#/c/2461/
  107. {"-9223372036854775808ns", false, 0},
  108. }
  109. func TestParseDuration(t *testing.T) {
  110. for _, tc := range parseDurationTests {
  111. d, err := ParseDuration(tc.in)
  112. if tc.ok && (err != nil || d != tc.want) {
  113. t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want)
  114. } else if !tc.ok && err == nil {
  115. t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in)
  116. } else {
  117. t.Logf("ParseDuration(%q) = %v", tc.in, d)
  118. }
  119. }
  120. }