blob: 5831121add39f69e30eb5dd41da3e046beeee003 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * 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 Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// 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 Tattermuscha7608b02015-02-03 17:54:38 -080034using System;
Jan Tattermusch86a249f2015-02-12 10:36:48 -080035using System.Runtime.InteropServices;
Jan Tattermusch30868622015-02-19 09:22:33 -080036using Grpc.Core.Internal;
37using NUnit.Framework;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080038
Jan Tattermusch30868622015-02-19 09:22:33 -080039namespace Grpc.Core.Internal.Tests
Jan Tattermuscha7608b02015-02-03 17:54:38 -080040{
41 public class TimespecTest
42 {
43 [Test]
44 public void Now()
45 {
46 var timespec = Timespec.Now;
47 }
48
49 [Test]
Jan Tattermusch86a249f2015-02-12 10:36:48 -080050 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 Tattermuscha7608b02015-02-03 17:54:38 -080062 public void Add()
63 {
Craig Tiller2510ce42015-07-17 14:24:36 -070064 var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 };
Jan Tattermuscha7608b02015-02-03 17:54:38 -080065 var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10));
Jan Tattermusch61f93b22015-02-12 12:45:52 -080066 Assert.AreEqual(result.tv_sec, new IntPtr(12355));
Craig Tiller2510ce42015-07-17 14:24:36 -070067 Assert.AreEqual(result.tv_nsec, 123456789);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080068 }
69
70 [Test]
71 public void Add_Nanos()
72 {
Craig Tiller2510ce42015-07-17 14:24:36 -070073 var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 };
Jan Tattermuscha7608b02015-02-03 17:54:38 -080074 var result = t.Add(TimeSpan.FromTicks(10));
Jan Tattermusch61f93b22015-02-12 12:45:52 -080075 Assert.AreEqual(result.tv_sec, new IntPtr(12345));
Craig Tiller2510ce42015-07-17 14:24:36 -070076 Assert.AreEqual(result.tv_nsec, 123456789 + 1000);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080077 }
78
79 [Test]
80 public void Add_NanosOverflow()
81 {
Craig Tiller2510ce42015-07-17 14:24:36 -070082 var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 999999999 };
Jan Tattermuscha7608b02015-02-03 17:54:38 -080083 var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10 + 10));
Jan Tattermusch61f93b22015-02-12 12:45:52 -080084 Assert.AreEqual(result.tv_sec, new IntPtr(12356));
Craig Tiller2510ce42015-07-17 14:24:36 -070085 Assert.AreEqual(result.tv_nsec, 999);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080086 }
87 }
88}