go.temporal.io/server@v1.23.0/common/number/number_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package number
    26  
    27  import (
    28  	"math/rand"
    29  	"testing"
    30  
    31  	"github.com/stretchr/testify/require"
    32  	"github.com/stretchr/testify/suite"
    33  )
    34  
    35  type (
    36  	numberSuite struct {
    37  		suite.Suite
    38  		*require.Assertions
    39  	}
    40  )
    41  
    42  func TestNumberSuite(t *testing.T) {
    43  	s := new(numberSuite)
    44  	suite.Run(t, s)
    45  }
    46  
    47  func (s *numberSuite) SetupSuite() {}
    48  
    49  func (s *numberSuite) TearDownSuite() {}
    50  
    51  func (s *numberSuite) SetupTest() {
    52  	s.Assertions = require.New(s.T())
    53  }
    54  
    55  func (s *numberSuite) TearDownTest() {
    56  
    57  }
    58  
    59  func (s *numberSuite) TestInt() {
    60  	number := rand.Intn(128)
    61  	for _, n := range []interface{}{
    62  		int8(number),
    63  		int16(number),
    64  		int32(number),
    65  		int64(number),
    66  		int(number),
    67  	} {
    68  		s.Equal(float64(number), NewNumber(n).GetFloatOrDefault(rand.Float64()))
    69  		s.Equal(int(number), NewNumber(n).GetIntOrDefault(rand.Int()))
    70  		s.Equal(uint(number), NewNumber(n).GetUintOrDefault(uint(rand.Uint64())))
    71  	}
    72  }
    73  
    74  func (s *numberSuite) TestUint() {
    75  	number := rand.Intn(256)
    76  	for _, n := range []interface{}{
    77  		uint8(number),
    78  		uint16(number),
    79  		uint32(number),
    80  		uint64(number),
    81  		uint(number),
    82  	} {
    83  		s.Equal(float64(number), NewNumber(n).GetFloatOrDefault(rand.Float64()))
    84  		s.Equal(int(number), NewNumber(n).GetIntOrDefault(rand.Int()))
    85  		s.Equal(uint(number), NewNumber(n).GetUintOrDefault(uint(rand.Uint64())))
    86  	}
    87  }
    88  
    89  func (s *numberSuite) TestFloat() {
    90  	number := rand.Float32() * float32(rand.Int())
    91  	for _, n := range []interface{}{
    92  		float32(number),
    93  		float64(number),
    94  	} {
    95  		s.Equal(float64(number), NewNumber(n).GetFloatOrDefault(rand.Float64()))
    96  		s.Equal(int(number), NewNumber(n).GetIntOrDefault(rand.Int()))
    97  		s.Equal(uint(number), NewNumber(n).GetUintOrDefault(uint(rand.Uint64())))
    98  	}
    99  }