github.com/searKing/golang/go@v1.2.117/math/float_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math_test
     6  
     7  import (
     8  	math_ "math"
     9  	"testing"
    10  
    11  	"github.com/searKing/golang/go/math"
    12  )
    13  
    14  type TruncPrecisionCaseTest struct {
    15  	input  float64
    16  	n      int
    17  	output float64
    18  }
    19  
    20  var (
    21  	truncPrecisionCaseTests = []TruncPrecisionCaseTest{
    22  		{
    23  			input:  -100.00001,
    24  			n:      2,
    25  			output: -100,
    26  		}, {
    27  			input:  1254.567890,
    28  			n:      -2,
    29  			output: 1300,
    30  		}, {
    31  			input:  1234.567890,
    32  			n:      -1,
    33  			output: 1230,
    34  		}, {
    35  			input:  1234.567890,
    36  			n:      0,
    37  			output: 1235,
    38  		}, {
    39  			input:  1234.567890,
    40  			n:      1,
    41  			output: 1234.6,
    42  		}, {
    43  			input:  1234.567890,
    44  			n:      6,
    45  			output: 1234.56789,
    46  		}, {
    47  			input:  1234.567890,
    48  			n:      10,
    49  			output: 1234.56789,
    50  		}, {
    51  			input:  math_.Inf(-1),
    52  			n:      1,
    53  			output: math_.Inf(-1),
    54  		}, {
    55  			input:  math_.Copysign(0, -1),
    56  			n:      1,
    57  			output: math_.Copysign(0, -1),
    58  		}, {
    59  			input:  0,
    60  			n:      1,
    61  			output: 0,
    62  		}, {
    63  			input:  math_.Inf(1),
    64  			n:      1,
    65  			output: math_.Inf(1),
    66  		}, {
    67  			input:  math_.NaN(),
    68  			n:      1,
    69  			output: math_.NaN(),
    70  		},
    71  	}
    72  )
    73  
    74  func TestTruncPrecision(t *testing.T) {
    75  	for n, test := range truncPrecisionCaseTests {
    76  		if got := math.TruncPrecision(test.input, test.n); !math.Alike(got, test.output) {
    77  			t.Errorf("#%d: TruncPrecision(%g,%d) = %g, want %g", n, test.input, test.n, got, test.output)
    78  		}
    79  	}
    80  }