duration_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package ptypes
  32. import (
  33. "math"
  34. "testing"
  35. "time"
  36. "github.com/golang/protobuf/proto"
  37. durpb "github.com/golang/protobuf/ptypes/duration"
  38. )
  39. const (
  40. minGoSeconds = math.MinInt64 / int64(1e9)
  41. maxGoSeconds = math.MaxInt64 / int64(1e9)
  42. )
  43. var durationTests = []struct {
  44. proto *durpb.Duration
  45. isValid bool
  46. inRange bool
  47. dur time.Duration
  48. }{
  49. // The zero duration.
  50. {&durpb.Duration{Seconds: 0, Nanos: 0}, true, true, 0},
  51. // Some ordinary non-zero durations.
  52. {&durpb.Duration{Seconds: 100, Nanos: 0}, true, true, 100 * time.Second},
  53. {&durpb.Duration{Seconds: -100, Nanos: 0}, true, true, -100 * time.Second},
  54. {&durpb.Duration{Seconds: 100, Nanos: 987}, true, true, 100*time.Second + 987},
  55. {&durpb.Duration{Seconds: -100, Nanos: -987}, true, true, -(100*time.Second + 987)},
  56. // The largest duration representable in Go.
  57. {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64},
  58. // The smallest duration representable in Go.
  59. {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64},
  60. {nil, false, false, 0},
  61. {&durpb.Duration{Seconds: -100, Nanos: 987}, false, false, 0},
  62. {&durpb.Duration{Seconds: 100, Nanos: -987}, false, false, 0},
  63. {&durpb.Duration{Seconds: math.MinInt64, Nanos: 0}, false, false, 0},
  64. {&durpb.Duration{Seconds: math.MaxInt64, Nanos: 0}, false, false, 0},
  65. // The largest valid duration.
  66. {&durpb.Duration{Seconds: maxSeconds, Nanos: 1e9 - 1}, true, false, 0},
  67. // The smallest valid duration.
  68. {&durpb.Duration{Seconds: minSeconds, Nanos: -(1e9 - 1)}, true, false, 0},
  69. // The smallest invalid duration above the valid range.
  70. {&durpb.Duration{Seconds: maxSeconds + 1, Nanos: 0}, false, false, 0},
  71. // The largest invalid duration below the valid range.
  72. {&durpb.Duration{Seconds: minSeconds - 1, Nanos: -(1e9 - 1)}, false, false, 0},
  73. // One nanosecond past the largest duration representable in Go.
  74. {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0},
  75. // One nanosecond past the smallest duration representable in Go.
  76. {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0},
  77. // One second past the largest duration representable in Go.
  78. {&durpb.Duration{Seconds: maxGoSeconds + 1, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0},
  79. // One second past the smallest duration representable in Go.
  80. {&durpb.Duration{Seconds: minGoSeconds - 1, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0},
  81. }
  82. func TestValidateDuration(t *testing.T) {
  83. for _, test := range durationTests {
  84. err := validateDuration(test.proto)
  85. gotValid := (err == nil)
  86. if gotValid != test.isValid {
  87. t.Errorf("validateDuration(%v) = %t, want %t", test.proto, gotValid, test.isValid)
  88. }
  89. }
  90. }
  91. func TestDuration(t *testing.T) {
  92. for _, test := range durationTests {
  93. got, err := Duration(test.proto)
  94. gotOK := (err == nil)
  95. wantOK := test.isValid && test.inRange
  96. if gotOK != wantOK {
  97. t.Errorf("Duration(%v) ok = %t, want %t", test.proto, gotOK, wantOK)
  98. }
  99. if err == nil && got != test.dur {
  100. t.Errorf("Duration(%v) = %v, want %v", test.proto, got, test.dur)
  101. }
  102. }
  103. }
  104. func TestDurationProto(t *testing.T) {
  105. for _, test := range durationTests {
  106. if test.isValid && test.inRange {
  107. got := DurationProto(test.dur)
  108. if !proto.Equal(got, test.proto) {
  109. t.Errorf("DurationProto(%v) = %v, want %v", test.dur, got, test.proto)
  110. }
  111. }
  112. }
  113. }