github.com/gogo/protobuf@v1.3.2/types/duration_test.go (about) 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 32 package types 33 34 import ( 35 "math" 36 "testing" 37 "time" 38 39 "github.com/gogo/protobuf/proto" 40 ) 41 42 const ( 43 minGoSeconds = math.MinInt64 / int64(1e9) 44 maxGoSeconds = math.MaxInt64 / int64(1e9) 45 ) 46 47 var durationTests = []struct { 48 proto *Duration 49 isValid bool 50 inRange bool 51 dur time.Duration 52 }{ 53 // The zero duration. 54 {&Duration{Seconds: 0, Nanos: 0}, true, true, 0}, 55 // Some ordinary non-zero durations. 56 {&Duration{Seconds: 100, Nanos: 0}, true, true, 100 * time.Second}, 57 {&Duration{Seconds: -100, Nanos: 0}, true, true, -100 * time.Second}, 58 {&Duration{Seconds: 100, Nanos: 987}, true, true, 100*time.Second + 987}, 59 {&Duration{Seconds: -100, Nanos: -987}, true, true, -(100*time.Second + 987)}, 60 // The largest duration representable in Go. 61 {&Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64}, 62 // The smallest duration representable in Go. 63 {&Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64}, 64 {nil, false, false, 0}, 65 {&Duration{Seconds: -100, Nanos: 987}, false, false, 0}, 66 {&Duration{Seconds: 100, Nanos: -987}, false, false, 0}, 67 {&Duration{Seconds: math.MinInt64, Nanos: 0}, false, false, 0}, 68 {&Duration{Seconds: math.MaxInt64, Nanos: 0}, false, false, 0}, 69 // The largest valid duration. 70 {&Duration{Seconds: maxSeconds, Nanos: 1e9 - 1}, true, false, 0}, 71 // The smallest valid duration. 72 {&Duration{Seconds: minSeconds, Nanos: -(1e9 - 1)}, true, false, 0}, 73 // The smallest invalid duration above the valid range. 74 {&Duration{Seconds: maxSeconds + 1, Nanos: 0}, false, false, 0}, 75 // The largest invalid duration below the valid range. 76 {&Duration{Seconds: minSeconds - 1, Nanos: -(1e9 - 1)}, false, false, 0}, 77 // One nanosecond past the largest duration representable in Go. 78 {&Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0}, 79 // One nanosecond past the smallest duration representable in Go. 80 {&Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0}, 81 // One second past the largest duration representable in Go. 82 {&Duration{Seconds: maxGoSeconds + 1, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0}, 83 // One second past the smallest duration representable in Go. 84 {&Duration{Seconds: minGoSeconds - 1, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0}, 85 } 86 87 func TestValidateDuration(t *testing.T) { 88 for _, test := range durationTests { 89 err := validateDuration(test.proto) 90 gotValid := (err == nil) 91 if gotValid != test.isValid { 92 t.Errorf("validateDuration(%v) = %t, want %t", test.proto, gotValid, test.isValid) 93 } 94 } 95 } 96 97 func TestDurationFromProto(t *testing.T) { 98 for _, test := range durationTests { 99 got, err := DurationFromProto(test.proto) 100 gotOK := (err == nil) 101 wantOK := test.isValid && test.inRange 102 if gotOK != wantOK { 103 t.Errorf("DurationFromProto(%v) ok = %t, want %t", test.proto, gotOK, wantOK) 104 } 105 if err == nil && got != test.dur { 106 t.Errorf("DurationFromProto(%v) = %v, want %v", test.proto, got, test.dur) 107 } 108 } 109 } 110 111 func TestDurationProto(t *testing.T) { 112 for _, test := range durationTests { 113 if test.isValid && test.inRange { 114 got := DurationProto(test.dur) 115 if !proto.Equal(got, test.proto) { 116 t.Errorf("DurationProto(%v) = %v, want %v", test.dur, got, test.proto) 117 } 118 } 119 } 120 }