github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/tests/math_test.go (about)

     1  //nolint:dupl // copy-paste benign and can wait
     2  // Package test provides tests for common low-level types and utilities for all aistore projects
     3  /*
     4   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     5   */
     6  package tests_test
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/NVIDIA/aistore/cmn/cos"
    13  	"github.com/NVIDIA/aistore/tools/tassert"
    14  )
    15  
    16  func TestFastLog2(t *testing.T) {
    17  	log22 := cos.FastLog2(2)
    18  	log42 := cos.FastLog2(4)
    19  	log82 := cos.FastLog2(8)
    20  	log10242 := cos.FastLog2(1024)
    21  	tassert.Fatalf(t, log22 == 1, "wrong power of 2 log2 result; got %d, expected %d", log22, 1)
    22  	tassert.Fatalf(t, log42 == 2, "wrong power of 2 log2 result; got %d, expected %d", log42, 2)
    23  	tassert.Fatalf(t, log82 == 3, "wrong power of 2 log2 result; got %d, expected %d", log82, 3)
    24  	tassert.Fatalf(t, log10242 == 10, "wrong power of 2 log2 result; got %d, expected %d", log10242, 10)
    25  
    26  	log32 := cos.FastLog2(3)
    27  	log52 := cos.FastLog2(5)
    28  	log152 := cos.FastLog2(15)
    29  	log10232 := cos.FastLog2(1023)
    30  	log10252 := cos.FastLog2(1025)
    31  	tassert.Fatalf(t, log32 == 1, "wrong log2 result; got %d, expected %d", log32, 1)
    32  	tassert.Fatalf(t, log52 == 2, "wrong log2 result; got %d, expected %d", log52, 2)
    33  	tassert.Fatalf(t, log152 == 3, "wrong log2 result; got %d, expected %d", log152, 3)
    34  	tassert.Fatalf(t, log10232 == 9, "wrong log2 result; got %d, expected %d", log10232, 9)
    35  	tassert.Fatalf(t, log10252 == 10, "wrong log2 result; got %d, expected %d", log10252, 10)
    36  }
    37  
    38  func TestFastLog2Ceil(t *testing.T) {
    39  	log22 := cos.FastLog2(2)
    40  	log42 := cos.FastLog2(4)
    41  	log82 := cos.FastLog2(8)
    42  	log10242 := cos.FastLog2(1024)
    43  	tassert.Fatalf(t, log22 == 1, "wrong power of 2 ceil(log2) result; got %d, expected %d", log22, 1)
    44  	tassert.Fatalf(t, log42 == 2, "wrong power of 2 ceil(log2) result; got %d, expected %d", log42, 2)
    45  	tassert.Fatalf(t, log82 == 3, "wrong power of 2 ceil(log2) result; got %d, expected %d", log82, 3)
    46  	tassert.Fatalf(t, log10242 == 10, "wrong power of 2 ceil(log2) result; got %d, expected %d", log10242, 10)
    47  
    48  	log32 := cos.FastLog2Ceil(3)
    49  	log52 := cos.FastLog2Ceil(5)
    50  	log152 := cos.FastLog2Ceil(15)
    51  	log10232 := cos.FastLog2Ceil(1023)
    52  	log10252 := cos.FastLog2Ceil(1025)
    53  	tassert.Fatalf(t, log32 == 2, "wrong ceil(log2) result; got %d, expected %d", log32, 2)
    54  	tassert.Fatalf(t, log52 == 3, "wrong ceil(log2) result; got %d, expected %d", log52, 3)
    55  	tassert.Fatalf(t, log152 == 4, "wrong ceil(log2) result; got %d, expected %d", log152, 4)
    56  	tassert.Fatalf(t, log10232 == 10, "wrong ceil(log2) result; got %d, expected %d", log10232, 10)
    57  	tassert.Fatalf(t, log10252 == 11, "wrong ceil(log2) result; got %d, expected %d", log10252, 11)
    58  }
    59  
    60  func TestMinDuration(t *testing.T) {
    61  	baseTime := time.Minute
    62  
    63  	tassert.Fatalf(t, min(baseTime, baseTime+time.Second) == baseTime, "expected %s to be smaller than %s", baseTime, baseTime+time.Second)
    64  	tassert.Fatalf(t, min(baseTime, baseTime-time.Second) == baseTime-time.Second, "expected %s to be smaller than %s", baseTime-time.Second, baseTime)
    65  	tassert.Fatalf(t, min(baseTime, baseTime) == baseTime, "expected %s to be the same as %s", baseTime, baseTime)
    66  }
    67  
    68  func TestCeilAlign(t *testing.T) {
    69  	tassert.Fatalf(t, cos.CeilAlign(12, 3) == 12, "got %d, expected 12", cos.CeilAlign(12, 3))
    70  	tassert.Fatalf(t, cos.CeilAlign(10, 3) == 12, "got %d, expected 12", cos.CeilAlign(10, 3))
    71  	tassert.Fatalf(t, cos.CeilAlign(10, 1) == 10, "got %d, expected 10", cos.CeilAlign(10, 1))
    72  }
    73  
    74  func TestMin(t *testing.T) {
    75  	tassert.Errorf(t, min(0, 1, 2, 3, 4, 5, 1) == 0, "expected 0 to be the smallest, got %d", min(0, 1, 2, 3, 4, 5, 1))
    76  	tassert.Errorf(t, min(10, 100, -2) == -2, "expected -2 to be the smallest, got %d", min(10, 100, -2))
    77  	tassert.Errorf(t, min(1, 0) == 0, "expected 0 to be the smallest, got %d", min(1, 0))
    78  }