Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | #endregion |
| 33 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | 86a249f | 2015-02-12 10:36:48 -0800 | [diff] [blame] | 35 | using System.Runtime.InteropServices; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 36 | using Grpc.Core.Internal; |
| 37 | using NUnit.Framework; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 38 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | namespace Grpc.Core.Internal.Tests |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 40 | { |
| 41 | public class TimespecTest |
| 42 | { |
| 43 | [Test] |
| 44 | public void Now() |
| 45 | { |
| 46 | var timespec = Timespec.Now; |
| 47 | } |
| 48 | |
| 49 | [Test] |
Jan Tattermusch | 86a249f | 2015-02-12 10:36:48 -0800 | [diff] [blame] | 50 | public void InfFuture() |
| 51 | { |
| 52 | var timespec = Timespec.InfFuture; |
| 53 | } |
| 54 | |
| 55 | [Test] |
| 56 | public void TimespecSizeIsNativeSize() |
| 57 | { |
| 58 | Assert.AreEqual(Timespec.NativeSize, Marshal.SizeOf(typeof(Timespec))); |
| 59 | } |
| 60 | |
| 61 | [Test] |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 62 | public void Add() |
| 63 | { |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 64 | var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 }; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 65 | var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10)); |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 66 | Assert.AreEqual(result.tv_sec, new IntPtr(12355)); |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 67 | Assert.AreEqual(result.tv_nsec, 123456789); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | [Test] |
| 71 | public void Add_Nanos() |
| 72 | { |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 73 | var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 }; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 74 | var result = t.Add(TimeSpan.FromTicks(10)); |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 75 | Assert.AreEqual(result.tv_sec, new IntPtr(12345)); |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 76 | Assert.AreEqual(result.tv_nsec, 123456789 + 1000); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | [Test] |
| 80 | public void Add_NanosOverflow() |
| 81 | { |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 82 | var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 999999999 }; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 83 | var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10 + 10)); |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 84 | Assert.AreEqual(result.tv_sec, new IntPtr(12356)); |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 85 | Assert.AreEqual(result.tv_nsec, 999); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |