github.com/m3db/m3@v1.5.0/src/x/time/unix_nano_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package time
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestUnixNano(t *testing.T) {
    31  	time := time.Unix(0, 1000)
    32  	unixNano := ToUnixNano(time)
    33  	require.Equal(t, UnixNano(1000), unixNano)
    34  	require.Equal(t, time, unixNano.ToTime())
    35  }
    36  
    37  func TestUnixNanoBefore(t *testing.T) {
    38  	t0 := UnixNano(0)
    39  	t1 := UnixNano(1)
    40  
    41  	require.Equal(t, true, t0.Before(t1))
    42  	require.Equal(t, false, t1.Before(t0))
    43  	require.Equal(t, false, t0.Before(t0))
    44  }
    45  
    46  func TestUnixNanoAfter(t *testing.T) {
    47  	t0 := UnixNano(0)
    48  	t1 := UnixNano(1)
    49  
    50  	require.Equal(t, false, t0.After(t1))
    51  	require.Equal(t, true, t1.After(t0))
    52  	require.Equal(t, false, t0.After(t0))
    53  }
    54  
    55  func TestUnixNanoEqual(t *testing.T) {
    56  	t0 := UnixNano(0)
    57  	t1 := UnixNano(1)
    58  
    59  	require.Equal(t, false, t0.Equal(t1))
    60  	require.Equal(t, false, t1.Equal(t0))
    61  	require.Equal(t, true, t0.Equal(t0))
    62  }
    63  
    64  func TestCompareTimes(t *testing.T) {
    65  	require.True(t, UnixNano(0).Before(UnixNano(1)))
    66  	require.True(t, UnixNano(1).After(UnixNano(0)))
    67  	require.False(t, UnixNano(1).Before(UnixNano(0)))
    68  	require.False(t, UnixNano(0).After(UnixNano(1)))
    69  
    70  	require.True(t, !UnixNano(0).Before(UnixNano(0)))
    71  	require.True(t, !UnixNano(0).After(UnixNano(0)))
    72  }
    73  
    74  func TestUnixNanoTruncate(t *testing.T) {
    75  	now := time.Unix(31415926, 42)
    76  	t0 := ToUnixNano(now)
    77  
    78  	require.Equal(t, ToUnixNano(now.Truncate(1*time.Second)), t0.Truncate(1*time.Second))
    79  	require.Equal(t, ToUnixNano(now.Truncate(0)), t0.Truncate(0))
    80  	require.Equal(t, ToUnixNano(now.Truncate(-10000)), t0.Truncate(-10000))
    81  }