duration_test.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ptypes
  5. import (
  6. "math"
  7. "testing"
  8. "time"
  9. "github.com/golang/protobuf/proto"
  10. durpb "github.com/golang/protobuf/ptypes/duration"
  11. )
  12. const (
  13. minGoSeconds = math.MinInt64 / int64(1e9)
  14. maxGoSeconds = math.MaxInt64 / int64(1e9)
  15. )
  16. var durationTests = []struct {
  17. proto *durpb.Duration
  18. isValid bool
  19. inRange bool
  20. dur time.Duration
  21. }{
  22. // The zero duration.
  23. {&durpb.Duration{Seconds: 0, Nanos: 0}, true, true, 0},
  24. // Some ordinary non-zero durations.
  25. {&durpb.Duration{Seconds: 100, Nanos: 0}, true, true, 100 * time.Second},
  26. {&durpb.Duration{Seconds: -100, Nanos: 0}, true, true, -100 * time.Second},
  27. {&durpb.Duration{Seconds: 100, Nanos: 987}, true, true, 100*time.Second + 987},
  28. {&durpb.Duration{Seconds: -100, Nanos: -987}, true, true, -(100*time.Second + 987)},
  29. // The largest duration representable in Go.
  30. {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64},
  31. // The smallest duration representable in Go.
  32. {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64},
  33. {nil, false, false, 0},
  34. {&durpb.Duration{Seconds: -100, Nanos: 987}, false, false, 0},
  35. {&durpb.Duration{Seconds: 100, Nanos: -987}, false, false, 0},
  36. {&durpb.Duration{Seconds: math.MinInt64, Nanos: 0}, false, false, 0},
  37. {&durpb.Duration{Seconds: math.MaxInt64, Nanos: 0}, false, false, 0},
  38. // The largest valid duration.
  39. {&durpb.Duration{Seconds: maxSeconds, Nanos: 1e9 - 1}, true, false, 0},
  40. // The smallest valid duration.
  41. {&durpb.Duration{Seconds: minSeconds, Nanos: -(1e9 - 1)}, true, false, 0},
  42. // The smallest invalid duration above the valid range.
  43. {&durpb.Duration{Seconds: maxSeconds + 1, Nanos: 0}, false, false, 0},
  44. // The largest invalid duration below the valid range.
  45. {&durpb.Duration{Seconds: minSeconds - 1, Nanos: -(1e9 - 1)}, false, false, 0},
  46. // One nanosecond past the largest duration representable in Go.
  47. {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0},
  48. // One nanosecond past the smallest duration representable in Go.
  49. {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0},
  50. // One second past the largest duration representable in Go.
  51. {&durpb.Duration{Seconds: maxGoSeconds + 1, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0},
  52. // One second past the smallest duration representable in Go.
  53. {&durpb.Duration{Seconds: minGoSeconds - 1, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0},
  54. }
  55. func TestValidateDuration(t *testing.T) {
  56. for _, test := range durationTests {
  57. err := validateDuration(test.proto)
  58. gotValid := (err == nil)
  59. if gotValid != test.isValid {
  60. t.Errorf("validateDuration(%v) = %t, want %t", test.proto, gotValid, test.isValid)
  61. }
  62. }
  63. }
  64. func TestDuration(t *testing.T) {
  65. for _, test := range durationTests {
  66. got, err := Duration(test.proto)
  67. gotOK := (err == nil)
  68. wantOK := test.isValid && test.inRange
  69. if gotOK != wantOK {
  70. t.Errorf("Duration(%v) ok = %t, want %t", test.proto, gotOK, wantOK)
  71. }
  72. if err == nil && got != test.dur {
  73. t.Errorf("Duration(%v) = %v, want %v", test.proto, got, test.dur)
  74. }
  75. }
  76. }
  77. func TestDurationProto(t *testing.T) {
  78. for _, test := range durationTests {
  79. if test.isValid && test.inRange {
  80. got := DurationProto(test.dur)
  81. if !proto.Equal(got, test.proto) {
  82. t.Errorf("DurationProto(%v) = %v, want %v", test.dur, got, test.proto)
  83. }
  84. }
  85. }
  86. }