aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/GrpcCoreTests/TimespecTest.cs
blob: cab59fa5b39678d6ab1fdbdc6a65b5ecc5393740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using NUnit.Framework;
using System.Runtime.InteropServices;
using Google.GRPC.Core.Internal;

namespace Google.GRPC.Core.Internal.Tests
{
    public class TimespecTest
    {
        [Test]
        public void Now()
        {
            var timespec = Timespec.Now;
        }

        [Test]
        public void InfFuture()
        {
            var timespec = Timespec.InfFuture;
        }

        [Test]
        public void TimespecSizeIsNativeSize()
        {
            Assert.AreEqual(Timespec.NativeSize, Marshal.SizeOf(typeof(Timespec)));
        }

        [Test]
        public void Add()
        {
            var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(123456789) };
            var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10));
            Assert.AreEqual(result.tv_sec, new IntPtr(12355));
            Assert.AreEqual(result.tv_nsec, new IntPtr(123456789));
        }

        [Test]
        public void Add_Nanos()
        {
            var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(123456789) };
            var result = t.Add(TimeSpan.FromTicks(10));
            Assert.AreEqual(result.tv_sec, new IntPtr(12345));
            Assert.AreEqual(result.tv_nsec, new IntPtr(123456789 + 1000));
        }

        [Test]
        public void Add_NanosOverflow()
        {
            var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(999999999) };
            var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10 + 10));
            Assert.AreEqual(result.tv_sec, new IntPtr(12356));
            Assert.AreEqual(result.tv_nsec, new IntPtr(999));
        }
    }
}