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

     1  // Copyright (c) 2016 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/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestUnitValue(t *testing.T) {
    32  	inputs := []struct {
    33  		u        Unit
    34  		expected time.Duration
    35  	}{
    36  		{Second, time.Second},
    37  		{Millisecond, time.Millisecond},
    38  		{Microsecond, time.Microsecond},
    39  		{Nanosecond, time.Nanosecond},
    40  		{Minute, time.Minute},
    41  		{Hour, time.Hour},
    42  		{Day, 24 * time.Hour},
    43  		{Year, 24 * time.Hour * 365},
    44  	}
    45  	for _, input := range inputs {
    46  		v, err := input.u.Value()
    47  		require.NoError(t, err)
    48  		require.Equal(t, input.expected, v)
    49  	}
    50  
    51  	invalidUnit := Unit(100)
    52  	_, err := invalidUnit.Value()
    53  	require.Equal(t, errUnrecognizedTimeUnit, err)
    54  }
    55  
    56  func TestUnitCount(t *testing.T) {
    57  	inputs := []struct {
    58  		u        Unit
    59  		d        time.Duration
    60  		expected int
    61  	}{
    62  		{Second, time.Second, 1},
    63  		{Millisecond, 10 * time.Millisecond, 10},
    64  		{Microsecond, time.Nanosecond, 0},
    65  		{Nanosecond, time.Microsecond, 1000},
    66  		{Minute, 2 * time.Minute, 2},
    67  		{Hour, 3 * time.Hour, 3},
    68  		{Day, 49 * time.Hour, 2},
    69  		{Year, 366 * 24 * time.Hour, 1},
    70  	}
    71  	for _, input := range inputs {
    72  		c, err := input.u.Count(input.d)
    73  		require.NoError(t, err)
    74  		require.Equal(t, input.expected, c)
    75  	}
    76  
    77  	invalidUnit := Unit(100)
    78  	_, err := invalidUnit.Count(time.Second)
    79  	require.Error(t, err)
    80  
    81  	var (
    82  		u               = Second
    83  		invalidDuration = -1 * time.Second
    84  	)
    85  	_, err = u.Count(invalidDuration)
    86  	require.Error(t, err)
    87  }
    88  
    89  func TestUnitMustCount(t *testing.T) {
    90  	inputs := []struct {
    91  		u        Unit
    92  		d        time.Duration
    93  		expected int
    94  	}{
    95  		{Second, time.Second, 1},
    96  		{Millisecond, 10 * time.Millisecond, 10},
    97  		{Microsecond, time.Nanosecond, 0},
    98  		{Nanosecond, time.Microsecond, 1000},
    99  		{Minute, 2 * time.Minute, 2},
   100  		{Hour, 3 * time.Hour, 3},
   101  		{Day, 49 * time.Hour, 2},
   102  		{Year, 24 * 366 * time.Hour, 1},
   103  	}
   104  	for _, input := range inputs {
   105  		c := input.u.MustCount(input.d)
   106  		require.Equal(t, input.expected, c)
   107  	}
   108  
   109  	invalidUnit := Unit(100)
   110  	require.Panics(t, func() {
   111  		invalidUnit.MustCount(time.Second)
   112  	})
   113  
   114  	var (
   115  		u               = Second
   116  		invalidDuration = -1 * time.Second
   117  	)
   118  	require.Panics(t, func() {
   119  		u.MustCount(invalidDuration)
   120  	})
   121  }
   122  
   123  func TestUnitIsValid(t *testing.T) {
   124  	inputs := []struct {
   125  		u        Unit
   126  		expected bool
   127  	}{
   128  		{Second, true},
   129  		{Millisecond, true},
   130  		{Microsecond, true},
   131  		{Nanosecond, true},
   132  		{Minute, true},
   133  		{Hour, true},
   134  		{Day, true},
   135  		{Year, true},
   136  		{Unit(100), false},
   137  	}
   138  	for _, input := range inputs {
   139  		require.Equal(t, input.expected, input.u.IsValid())
   140  	}
   141  }
   142  
   143  func TestUnitFromDuration(t *testing.T) {
   144  	inputs := []struct {
   145  		d        time.Duration
   146  		expected Unit
   147  	}{
   148  		{time.Second, Second},
   149  		{time.Millisecond, Millisecond},
   150  		{time.Microsecond, Microsecond},
   151  		{time.Nanosecond, Nanosecond},
   152  		{time.Minute, Minute},
   153  		{time.Hour, Hour},
   154  		{24 * time.Hour, Day},
   155  		{365 * 24 * time.Hour, Year},
   156  	}
   157  	for _, input := range inputs {
   158  		u, err := UnitFromDuration(input.d)
   159  		require.NoError(t, err)
   160  		require.Equal(t, input.expected, u)
   161  	}
   162  }
   163  
   164  func TestUnitFromDurationError(t *testing.T) {
   165  	_, err := UnitFromDuration(time.Hour * 30)
   166  	require.Equal(t, errConvertDurationToUnit, err)
   167  }
   168  
   169  func TestMaxUnitForDuration(t *testing.T) {
   170  	inputs := []struct {
   171  		d                time.Duration
   172  		expectedMultiple int64
   173  		expectedUnit     Unit
   174  	}{
   175  		{0, 0, Nanosecond},
   176  		{30 * time.Nanosecond, 30, Nanosecond},
   177  		{60 * time.Microsecond, 60, Microsecond},
   178  		{999 * time.Millisecond, 999, Millisecond},
   179  		{20 * time.Second, 20, Second},
   180  		{70 * time.Second, 70, Second},
   181  		{120 * time.Second, 2, Minute},
   182  		{10 * time.Minute, 10, Minute},
   183  		{20 * time.Hour, 20, Hour},
   184  		{24 * time.Hour, 1, Day},
   185  		{25 * time.Hour, 25, Hour},
   186  		{24 * 8 * time.Hour, 8, Day},
   187  		{24 * 31 * time.Hour, 31, Day},
   188  		{24 * 365 * time.Hour, 1, Year},
   189  		{24 * 366 * time.Hour, 366, Day},
   190  		{24 * 365 * 2 * time.Hour, 2, Year},
   191  		{-30 * time.Nanosecond, -30, Nanosecond},
   192  		{-60 * time.Microsecond, -60, Microsecond},
   193  		{-999 * time.Millisecond, -999, Millisecond},
   194  		{-20 * time.Second, -20, Second},
   195  		{-70 * time.Second, -70, Second},
   196  		{-120 * time.Second, -2, Minute},
   197  		{-10 * time.Minute, -10, Minute},
   198  		{-20 * time.Hour, -20, Hour},
   199  		{-24 * time.Hour, -1, Day},
   200  		{-25 * time.Hour, -25, Hour},
   201  		{-24 * 8 * time.Hour, -8, Day},
   202  		{-24 * 31 * time.Hour, -31, Day},
   203  	}
   204  	for _, input := range inputs {
   205  		m, u := MaxUnitForDuration(input.d)
   206  		require.Equal(t, input.expectedMultiple, m)
   207  		require.Equal(t, input.expectedUnit, u)
   208  	}
   209  }
   210  
   211  func TestDurationFromUnit(t *testing.T) {
   212  	inputs := []struct {
   213  		u        Unit
   214  		expected time.Duration
   215  	}{
   216  		{Second, time.Second},
   217  		{Millisecond, time.Millisecond},
   218  		{Microsecond, time.Microsecond},
   219  		{Nanosecond, time.Nanosecond},
   220  		{Minute, time.Minute},
   221  		{Hour, time.Hour},
   222  		{Day, 24 * time.Hour},
   223  		{Year, 24 * time.Hour * 365},
   224  	}
   225  	for _, input := range inputs {
   226  		d, err := DurationFromUnit(input.u)
   227  		require.NoError(t, err)
   228  		require.Equal(t, input.expected, d)
   229  	}
   230  }
   231  
   232  func TestDurationFromUnitError(t *testing.T) {
   233  	_, err := DurationFromUnit(None)
   234  	require.Equal(t, errConvertUnitToDuration, err)
   235  }
   236  
   237  func TestUnitString(t *testing.T) {
   238  	tests := []struct {
   239  		u Unit
   240  		s string
   241  	}{
   242  		{Second, "s"},
   243  		{Millisecond, "ms"},
   244  		{Microsecond, "us"},
   245  		{Nanosecond, "ns"},
   246  		{Minute, "m"},
   247  		{Hour, "h"},
   248  		{Day, "d"},
   249  		{Year, "y"},
   250  		{None, "unknown"},
   251  	}
   252  
   253  	for _, test := range tests {
   254  		assert.Equal(t, test.s, test.u.String(), "invalid String() for %v", test.u)
   255  	}
   256  }