github.com/vmware/govmomi@v0.37.2/units/size_test.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package units
    18  
    19  import (
    20  	"math"
    21  	"testing"
    22  )
    23  
    24  func TestMB(t *testing.T) {
    25  	b := ByteSize(1024 * 1024)
    26  	expected := "1.0MB"
    27  	if b.String() != expected {
    28  		t.Errorf("Expected '%v' but got '%v'", expected, b)
    29  	}
    30  }
    31  
    32  func TestTenMB(t *testing.T) {
    33  	actual := ByteSize(10 * 1024 * 1024).String()
    34  	expected := "10.0MB"
    35  	if actual != expected {
    36  		t.Errorf("Expected '%v' but got '%v'", expected, actual)
    37  	}
    38  }
    39  
    40  func assertEquals(t *testing.T, expected string, actual ByteSize) {
    41  	if expected != actual.String() {
    42  		t.Errorf("Expected '%v' but got '%v'", expected, actual.String())
    43  	}
    44  }
    45  
    46  func TestByteSize(t *testing.T) {
    47  	assertEquals(t, "1B", ByteSize(1))
    48  	assertEquals(t, "10B", ByteSize(10))
    49  	assertEquals(t, "100B", ByteSize(100))
    50  	assertEquals(t, "1000B", ByteSize(1000))
    51  	assertEquals(t, "1.0KB", ByteSize(1024))
    52  	assertEquals(t, "1.0MB", ByteSize(1024*1024))
    53  	assertEquals(t, "1.0MB", ByteSize(1048576))
    54  	assertEquals(t, "10.0MB", ByteSize(10*math.Pow(1024, 2)))
    55  	assertEquals(t, "1.0GB", ByteSize(1024*1024*1024))
    56  	assertEquals(t, "1.0TB", ByteSize(1024*1024*1024*1024))
    57  	assertEquals(t, "1.0TB", ByteSize(1048576*1048576))
    58  	assertEquals(t, "1.0PB", ByteSize(1024*1024*1024*1024*1024))
    59  	assertEquals(t, "1.0EB", ByteSize(1024*1024*1024*1024*1024*1024))
    60  	assertEquals(t, "1.0EB", ByteSize(1048576*1048576*1048576))
    61  }
    62  
    63  func TestByteSizeSet(t *testing.T) {
    64  	var tests = []struct {
    65  		In     string
    66  		OutStr string
    67  		Out    ByteSize
    68  	}{
    69  		{
    70  			In:     "345",
    71  			OutStr: "345B",
    72  			Out:    345.0,
    73  		},
    74  		{
    75  			In:     "345b",
    76  			OutStr: "345B",
    77  			Out:    345.0,
    78  		},
    79  		{
    80  			In:     "345K",
    81  			OutStr: "345.0KB",
    82  			Out:    345 * KB,
    83  		},
    84  		{
    85  			In:     "345kb",
    86  			OutStr: "345.0KB",
    87  			Out:    345 * KB,
    88  		},
    89  		{
    90  			In:     "345kib",
    91  			OutStr: "345.0KB",
    92  			Out:    345 * KB,
    93  		},
    94  		{
    95  			In:     "345KiB",
    96  			OutStr: "345.0KB",
    97  			Out:    345 * KB,
    98  		},
    99  		{
   100  			In:     "345M",
   101  			OutStr: "345.0MB",
   102  			Out:    345 * MB,
   103  		},
   104  		{
   105  			In:     "345G",
   106  			OutStr: "345.0GB",
   107  			Out:    345 * GB,
   108  		},
   109  		{
   110  			In:     "345T",
   111  			OutStr: "345.0TB",
   112  			Out:    345 * TB,
   113  		},
   114  		{
   115  			In:     "345P",
   116  			OutStr: "345.0PB",
   117  			Out:    345 * PB,
   118  		},
   119  		{
   120  			In:     "3E",
   121  			OutStr: "3.0EB",
   122  			Out:    3 * EB,
   123  		},
   124  	}
   125  
   126  	for _, test := range tests {
   127  		var v ByteSize
   128  		err := v.Set(test.In)
   129  		if err != nil {
   130  			t.Errorf("Error: %s [%v]", err, test.In)
   131  			continue
   132  		}
   133  
   134  		if v != test.Out {
   135  			t.Errorf("Out: expect '%v' actual '%v'", test.Out, v)
   136  			continue
   137  		}
   138  
   139  		if v.String() != test.OutStr {
   140  			t.Errorf("String: expect '%v' actual '%v'", test.OutStr, v.String())
   141  		}
   142  	}
   143  }