1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2015 Google Inc.  All rights reserved.
4 // https://developers.google.com/protocol-buffers/
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 #endregion
32 
33 using NUnit.Framework;
34 using System;
35 
36 namespace Google.Protobuf.WellKnownTypes
37 {
38     public class TimestampTest
39     {
40         [Test]
FromAndToDateTime()41         public void FromAndToDateTime()
42         {
43             DateTime utcMin = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
44             DateTime utcMax = DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc);
45             AssertRoundtrip(new Timestamp { Seconds = -62135596800 }, utcMin);
46             AssertRoundtrip(new Timestamp { Seconds = 253402300799, Nanos = 999999900 }, utcMax);
47             AssertRoundtrip(new Timestamp(), new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
48             AssertRoundtrip(new Timestamp { Nanos = 1000000}, new DateTime(1970, 1, 1, 0, 0, 0, 1, DateTimeKind.Utc));
49             AssertRoundtrip(new Timestamp { Seconds = -1, Nanos = 999000000 }, new DateTime(1969, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc));
50             AssertRoundtrip(new Timestamp { Seconds = 3600 }, new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Utc));
51             AssertRoundtrip(new Timestamp { Seconds = -3600 }, new DateTime(1969, 12, 31, 23, 0, 0, DateTimeKind.Utc));
52         }
53 
54         [Test]
ToDateTimeTruncation()55         public void ToDateTimeTruncation()
56         {
57             var t1 = new Timestamp { Seconds = 1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 };
58             Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 1, DateTimeKind.Utc).AddMilliseconds(1), t1.ToDateTime());
59 
60             var t2 = new Timestamp { Seconds = -1, Nanos = 1000000 + Duration.NanosecondsPerTick - 1 };
61             Assert.AreEqual(new DateTime(1969, 12, 31, 23, 59, 59).AddMilliseconds(1), t2.ToDateTime());
62         }
63 
64         [Test]
65         [TestCase(Timestamp.UnixSecondsAtBclMinValue - 1, Timestamp.MaxNanos)]
66         [TestCase(Timestamp.UnixSecondsAtBclMaxValue + 1, 0)]
67         [TestCase(0, -1)]
68         [TestCase(0, Timestamp.MaxNanos + 1)]
ToDateTime_OutOfRange(long seconds, int nanoseconds)69         public void ToDateTime_OutOfRange(long seconds, int nanoseconds)
70         {
71             var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
72             Assert.Throws<InvalidOperationException>(() => value.ToDateTime());
73         }
74 
75         // 1ns larger or smaller than the above values
76         [Test]
77         [TestCase(Timestamp.UnixSecondsAtBclMinValue, 0)]
78         [TestCase(Timestamp.UnixSecondsAtBclMaxValue, Timestamp.MaxNanos)]
79         [TestCase(0, 0)]
80         [TestCase(0, Timestamp.MaxNanos)]
ToDateTime_ValidBoundaries(long seconds, int nanoseconds)81         public void ToDateTime_ValidBoundaries(long seconds, int nanoseconds)
82         {
83             var value = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
84             value.ToDateTime();
85         }
86 
AssertRoundtrip(Timestamp timestamp, DateTime dateTime)87         private static void AssertRoundtrip(Timestamp timestamp, DateTime dateTime)
88         {
89             Assert.AreEqual(timestamp, Timestamp.FromDateTime(dateTime));
90             Assert.AreEqual(dateTime, timestamp.ToDateTime());
91             Assert.AreEqual(DateTimeKind.Utc, timestamp.ToDateTime().Kind);
92         }
93 
94         [Test]
Arithmetic()95         public void Arithmetic()
96         {
97             Timestamp t1 = new Timestamp { Seconds = 10000, Nanos = 5000 };
98             Timestamp t2 = new Timestamp { Seconds = 8000, Nanos = 10000 };
99             Duration difference = new Duration { Seconds = 1999, Nanos = Duration.NanosecondsPerSecond - 5000 };
100             Assert.AreEqual(difference, t1 - t2);
101             Assert.AreEqual(-difference, t2 - t1);
102 
103             Assert.AreEqual(t1, t2 + difference);
104             Assert.AreEqual(t2, t1 - difference);
105         }
106 
107         [Test]
ToString_NonNormalized()108         public void ToString_NonNormalized()
109         {
110             // Just a single example should be sufficient...
111             var duration = new Timestamp { Seconds = 1, Nanos = -1 };
112             Assert.AreEqual("{ \"@warning\": \"Invalid Timestamp\", \"seconds\": \"1\", \"nanos\": -1 }", duration.ToString());
113         }
114 
115         [Test]
Comparability()116         public void Comparability()
117         {
118             Timestamp
119                 a = null,
120                 b = new Timestamp { Seconds = 1, Nanos = 1 },
121                 c = new Timestamp { Seconds = 1, Nanos = 10 },
122                 d = new Timestamp { Seconds = 10, Nanos = 1 },
123                 e = new Timestamp { Seconds = 10, Nanos = 10 };
124 
125             Assert.IsTrue(b.CompareTo(a) > 0); // null is always first (according to default behavior of Array.Sort)
126             Assert.IsTrue(b.CompareTo(b) == 0);
127             Assert.IsTrue(b.CompareTo(b.Clone()) == 0);
128             Assert.IsTrue(b.CompareTo(c) < 0);
129             Assert.IsTrue(b.CompareTo(d) < 0);
130             Assert.IsTrue(b.CompareTo(e) < 0);
131 
132             Assert.IsTrue(c.CompareTo(a) > 0);
133             Assert.IsTrue(c.CompareTo(b) > 0);
134             Assert.IsTrue(c.CompareTo(c) == 0);
135             Assert.IsTrue(c.CompareTo(c.Clone()) == 0);
136             Assert.IsTrue(c.CompareTo(d) < 0);
137             Assert.IsTrue(c.CompareTo(e) < 0);
138 
139             Assert.IsTrue(d.CompareTo(a) > 0);
140             Assert.IsTrue(d.CompareTo(b) > 0);
141             Assert.IsTrue(d.CompareTo(c) > 0);
142             Assert.IsTrue(d.CompareTo(d) == 0);
143             Assert.IsTrue(d.CompareTo(d.Clone()) == 0);
144             Assert.IsTrue(d.CompareTo(e) < 0);
145 
146             Assert.IsTrue(e.CompareTo(a) > 0);
147             Assert.IsTrue(e.CompareTo(b) > 0);
148             Assert.IsTrue(e.CompareTo(c) > 0);
149             Assert.IsTrue(e.CompareTo(d) > 0);
150             Assert.IsTrue(e.CompareTo(e) == 0);
151             Assert.IsTrue(e.CompareTo(e.Clone()) == 0);
152         }
153 
154 
155         [Test]
ComparabilityOperators()156         public void ComparabilityOperators()
157         {
158             Timestamp
159                 a = null,
160                 b = new Timestamp { Seconds = 1, Nanos = 1 },
161                 c = new Timestamp { Seconds = 1, Nanos = 10 },
162                 d = new Timestamp { Seconds = 10, Nanos = 1 },
163                 e = new Timestamp { Seconds = 10, Nanos = 10 };
164 
165 #pragma warning disable CS1718 // Comparison made to same variable
166             Assert.IsTrue(b > a);
167             Assert.IsTrue(b == b);
168             Assert.IsTrue(b == b.Clone());
169             Assert.IsTrue(b < c);
170             Assert.IsTrue(b < d);
171             Assert.IsTrue(b < e);
172 
173             Assert.IsTrue(c > a);
174             Assert.IsTrue(c > b);
175             Assert.IsTrue(c == c);
176             Assert.IsTrue(c == c.Clone());
177             Assert.IsTrue(c < d);
178             Assert.IsTrue(c < e);
179 
180             Assert.IsTrue(d > a);
181             Assert.IsTrue(d > b);
182             Assert.IsTrue(d > c);
183             Assert.IsTrue(d == d);
184             Assert.IsTrue(d == d.Clone());
185             Assert.IsTrue(d < e);
186 
187             Assert.IsTrue(e > a);
188             Assert.IsTrue(e > b);
189             Assert.IsTrue(e > c);
190             Assert.IsTrue(e > d);
191             Assert.IsTrue(e == e);
192             Assert.IsTrue(e == e.Clone());
193 
194             Assert.IsTrue(b >= a);
195             Assert.IsTrue(b <= c);
196             Assert.IsTrue(b <= d);
197             Assert.IsTrue(b <= e);
198 
199             Assert.IsTrue(c >= a);
200             Assert.IsTrue(c >= b);
201             Assert.IsTrue(c <= d);
202             Assert.IsTrue(c <= e);
203 
204             Assert.IsTrue(d >= a);
205             Assert.IsTrue(d >= b);
206             Assert.IsTrue(d >= c);
207             Assert.IsTrue(d <= e);
208 
209             Assert.IsTrue(e >= a);
210             Assert.IsTrue(e >= b);
211             Assert.IsTrue(e >= c);
212             Assert.IsTrue(e >= d);
213 #pragma warning restore CS1718 // Comparison made to same variable
214         }
215     }
216 }
217