github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/math/math_helper_test.go (about)

     1  // Copyright 2021 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package math
    16  
    17  import (
    18  	"math"
    19  	"sync"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func Test_Max(t *testing.T) {
    26  	assert.Equal(t, int(20), Max(int(10), int(20)))
    27  	assert.Equal(t, int8(20), Max(int8(10), int8(20)))
    28  	assert.Equal(t, int16(20), Max(int16(10), int16(20)))
    29  	assert.Equal(t, int32(20), Max(int32(10), int32(20)))
    30  	assert.Equal(t, int64(20), Max(int64(10), int64(20)))
    31  
    32  	assert.Equal(t, uint(20), Max(uint(10), uint(20)))
    33  	assert.Equal(t, uint8(20), Max(uint8(10), uint8(20)))
    34  	assert.Equal(t, uint16(20), Max(uint16(10), uint16(20)))
    35  	assert.Equal(t, uint32(20), Max(uint32(10), uint32(20)))
    36  	assert.Equal(t, uint64(20), Max(uint64(10), uint64(20)))
    37  
    38  	assert.Equal(t, float32(20.1), Max(float32(10.1), float32(20.1)))
    39  	assert.Equal(t, float64(20.1), Max(float64(10.1), float64(20.1)))
    40  }
    41  
    42  func TestAtomicAddFloat64(t *testing.T) {
    43  	var num float64
    44  	var wg sync.WaitGroup
    45  	wg.Add(100)
    46  
    47  	for i := 0; i < 100; i++ {
    48  		go func() {
    49  			AtomicAddFloat64(&num, 1.0)
    50  			wg.Done()
    51  		}()
    52  	}
    53  
    54  	wg.Wait()
    55  
    56  	if num != 100.0 {
    57  		t.Errorf("Expected num to be 100.0, got %f", num)
    58  	}
    59  }
    60  
    61  func TestAtomicLoadFloat64(t *testing.T) {
    62  	var num = 42.0
    63  	result := AtomicLoadFloat64(&num)
    64  
    65  	if result != 42.0 {
    66  		t.Errorf("Expected result to be 42.0, got %f", result)
    67  	}
    68  }
    69  
    70  func TestAtomicStoreFloat64(t *testing.T) {
    71  	var num float64
    72  	AtomicStoreFloat64(&num, 10.5)
    73  
    74  	if num != 10.5 {
    75  		t.Errorf("Expected num to be 10.5, got %f", num)
    76  	}
    77  }
    78  
    79  func TestAtomicFloatFunctions(t *testing.T) {
    80  	var num float64
    81  	AtomicStoreFloat64(&num, 5.5)
    82  	if num != 5.5 {
    83  		t.Errorf("Expected num to be 5.5, got %f", num)
    84  	}
    85  
    86  	AtomicAddFloat64(&num, 2.5)
    87  	if num != 8.0 {
    88  		t.Errorf("Expected num to be 8.0, got %f", num)
    89  	}
    90  
    91  	result := AtomicLoadFloat64(&num)
    92  	if result != 8.0 {
    93  		t.Errorf("Expected result to be 8.0, got %f", result)
    94  	}
    95  
    96  	AtomicStoreFloat64(&num, math.NaN())
    97  	result = AtomicLoadFloat64(&num)
    98  	if !math.IsNaN(result) {
    99  		t.Errorf("Expected result to be NaN, got %f", result)
   100  	}
   101  }