github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/datasize/bitsize_test.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package datasize
    19  
    20  import (
    21  	"math"
    22  	"strconv"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestBits(t *testing.T) {
    29  	table := []struct {
    30  		value         BitSize
    31  		valueExpected uint64
    32  	}{
    33  		{B, 8},
    34  		{10 * B, 80},
    35  		{0.5 * B, 4},
    36  		{0.2 * B, 1},
    37  		{0.1 * B, 0},
    38  	}
    39  
    40  	for _, tt := range table {
    41  		assert.Equal(t, tt.valueExpected, tt.value.Bits())
    42  	}
    43  }
    44  
    45  func TestBytes(t *testing.T) {
    46  	table := []struct {
    47  		value         BitSize
    48  		valueExpected uint64
    49  	}{
    50  		{KiB, 1024},
    51  		{10 * KiB, 10240},
    52  		{0.5 * KiB, 512},
    53  		{0.001 * KiB, 1},
    54  	}
    55  
    56  	for _, tt := range table {
    57  		assert.Equal(t, tt.valueExpected, tt.value.Bytes())
    58  	}
    59  }
    60  
    61  func Test_String(t *testing.T) {
    62  	tests := []struct {
    63  		input BitSize
    64  		want  string
    65  	}{
    66  		{0, "0b"},
    67  		{Bit, "1b"},
    68  		{B, "1B"},
    69  		{KiB, "1.0KiB"},
    70  		{MiB, "1.0MiB"},
    71  		{GiB, "1.0GiB"},
    72  		{TiB, "1.0TiB"},
    73  		{PiB, "1.0PiB"},
    74  		{EiB, "1.0EiB"},
    75  		{400 * TiB, "400.0TiB"},
    76  		{2048 * MiB, "2.0GiB"},
    77  		{B + KiB, "1.0KiB"},
    78  		{MiB + 20*KiB, "1.0MiB"},
    79  		{100*MiB + KiB, "100.0MiB"},
    80  		{50 * B, "50B"},
    81  		{1024 * B, "1.0KiB"},
    82  		{1500 * B, "1.5KiB"},
    83  		{1024 * 1024 * B, "1.0MiB"},
    84  		{1024 * 1024 * 1024 * B, "1.0GiB"},
    85  		{BitSize(math.MaxUint64 + 1), "2.0EiB"},
    86  	}
    87  	for idx, tt := range tests {
    88  		t.Run(strconv.Itoa(idx), func(t *testing.T) {
    89  			result := tt.input.String()
    90  			assert.Equal(t, tt.want, result)
    91  		})
    92  	}
    93  }