github.com/dubbogo/gost@v1.14.0/math/base_funcs_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxmath
    19  
    20  import (
    21  	"math"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestAbs(t *testing.T) {
    30  	mathTable := []struct {
    31  		in, want int64
    32  	}{
    33  		{-0, 0},
    34  		{1, 1},
    35  		{-1, 1},
    36  		{-3, 3},
    37  	}
    38  
    39  	for _, val := range mathTable {
    40  		assert.Equal(t, val.want, AbsInt64(val.in))
    41  		assert.Equal(t, int32(val.want), AbsInt32(int32(val.in)))
    42  		assert.Equal(t, int16(val.want), AbsInt16(int16(val.in)))
    43  		assert.Equal(t, int8(val.want), AbsInt8(int8(val.in)))
    44  	}
    45  
    46  	assert.Equal(t, int64(math.MaxInt64), AbsInt64(-math.MaxInt64))
    47  	assert.Equal(t, int32(math.MaxInt32), AbsInt32(-math.MaxInt32))
    48  	assert.Equal(t, int16(math.MaxInt16), AbsInt16(-math.MaxInt16))
    49  	assert.Equal(t, int8(math.MaxInt8), AbsInt8(-math.MaxInt8))
    50  }