github.com/coyove/sdss@v0.0.0-20231129015646-c2ec58cca6a2/future/chrony/helpers_test.go (about)

     1  /*
     2  Copyright (c) Facebook, Inc. and its affiliates.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package chrony
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestFloat(t *testing.T) {
    26  	testCases := []struct {
    27  		in  chronyFloat
    28  		out float64
    29  	}{
    30  		{
    31  			in:  chronyFloat(0),
    32  			out: 0.0,
    33  		},
    34  		{
    35  			in:  chronyFloat(17091950),
    36  			out: -0.490620,
    37  		},
    38  		{
    39  			in:  chronyFloat(-90077357),
    40  			out: 0.039435696,
    41  		},
    42  	}
    43  
    44  	for _, testCase := range testCases {
    45  		// can't really compare big floats, thus measure delta
    46  		require.InDelta(
    47  			t,
    48  			testCase.out,
    49  			testCase.in.ToFloat(),
    50  			0.000001,
    51  		)
    52  	}
    53  }
    54  
    55  func TestRefidToString(t *testing.T) {
    56  	testCases := []struct {
    57  		in  uint32
    58  		out string
    59  	}{
    60  		{
    61  			in:  0,
    62  			out: "",
    63  		},
    64  		{
    65  			in:  1196446464,
    66  			out: "GPS",
    67  		},
    68  		{
    69  			in:  2139029761,
    70  			out: "",
    71  		},
    72  	}
    73  
    74  	for _, testCase := range testCases {
    75  		require.Equal(
    76  			t,
    77  			testCase.out,
    78  			RefidToString(testCase.in),
    79  		)
    80  	}
    81  }
    82  
    83  func TestNTPTestsFlagsString(t *testing.T) {
    84  	testCases := []struct {
    85  		in  uint16
    86  		out []string
    87  	}{
    88  		{
    89  			in:  255,
    90  			out: []string{"tst_delay_dev_ration", "tst_sync_loop"},
    91  		},
    92  		{
    93  			in:  65535,
    94  			out: []string{},
    95  		},
    96  	}
    97  
    98  	for _, testCase := range testCases {
    99  		require.ElementsMatch(
   100  			t,
   101  			testCase.out,
   102  			ReadNTPTestFlags(testCase.in),
   103  		)
   104  	}
   105  }