github.com/uber/kraken@v0.1.4/utils/memsize/memsize_test.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package memsize
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestFormat(t *testing.T) {
    23  	tests := []struct {
    24  		bytes    uint64
    25  		expected string
    26  	}{
    27  		{0, "0B"},
    28  		{20 * B, "20.00B"},
    29  		{256 * KB, "256.00KB"},
    30  		{90 * MB, "90.00MB"},
    31  		{2 * GB, "2.00GB"},
    32  		{5 * TB, "5.00TB"},
    33  		{GB + 512*MB, "1.50GB"},
    34  	}
    35  	for _, test := range tests {
    36  		t.Run(test.expected, func(t *testing.T) {
    37  			require.Equal(t, test.expected, Format(test.bytes))
    38  		})
    39  	}
    40  }
    41  
    42  func TestBitFormat(t *testing.T) {
    43  	tests := []struct {
    44  		bits     uint64
    45  		expected string
    46  	}{
    47  		{0, "0bit"},
    48  		{20, "20.00bit"},
    49  		{256 * Kbit, "256.00Kbit"},
    50  		{90 * Mbit, "90.00Mbit"},
    51  		{2 * Gbit, "2.00Gbit"},
    52  		{5 * Tbit, "5.00Tbit"},
    53  		{Gbit + 512*Mbit, "1.50Gbit"},
    54  	}
    55  	for _, test := range tests {
    56  		t.Run(test.expected, func(t *testing.T) {
    57  			require.Equal(t, test.expected, BitFormat(test.bits))
    58  		})
    59  	}
    60  }