github.com/gopacket/gopacket@v1.1.0/time_test.go (about)

     1  // Copyright 2019 The GoPacket Authors. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree.
     6  
     7  package gopacket
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestToDuration(t *testing.T) {
    15  	for i, test := range []struct {
    16  		r TimestampResolution
    17  		d time.Duration
    18  	}{
    19  		{
    20  			TimestampResolutionMillisecond,
    21  			time.Millisecond,
    22  		},
    23  		{
    24  			TimestampResolutionMicrosecond,
    25  			time.Microsecond,
    26  		},
    27  		{
    28  			TimestampResolutionNanosecond,
    29  			time.Nanosecond,
    30  		},
    31  		{
    32  			TimestampResolutionNTP,
    33  			0, // this is not representable since it's ~0.233 nanoseconds
    34  		},
    35  		{
    36  			TimestampResolution{2, -16},
    37  			15258,
    38  		},
    39  		{
    40  			TimestampResolution{2, 1},
    41  			2 * time.Second,
    42  		},
    43  		{
    44  			TimestampResolution{10, 1},
    45  			10 * time.Second,
    46  		},
    47  		{
    48  			TimestampResolution{10, 0},
    49  			time.Second,
    50  		},
    51  		{
    52  			TimestampResolution{2, 0},
    53  			time.Second,
    54  		},
    55  		{
    56  			TimestampResolution{0, 0},
    57  			0,
    58  		},
    59  		{
    60  			TimestampResolution{3, 2},
    61  			9 * time.Second,
    62  		},
    63  		{
    64  			TimestampResolution{3, -2},
    65  			111111111,
    66  		},
    67  	} {
    68  		d := test.r.ToDuration()
    69  		if d != test.d {
    70  			t.Errorf("%d: resolution: %s want: %d got: %d", i, test.r, test.d, d)
    71  		}
    72  	}
    73  }