kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/datasize/datasize_test.go (about)

     1  /*
     2   * Copyright 2015 The Kythe Authors. 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 datasize
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"kythe.io/kythe/go/test/testutil"
    24  )
    25  
    26  func TestParse(t *testing.T) {
    27  	tests := []struct {
    28  		str string
    29  		sz  Size
    30  	}{
    31  		{"0", 0},
    32  		{"1024", 1024 * Byte},
    33  		{"100", 100 * Byte},
    34  		{"0tb", 0},
    35  		{"1b", Byte},
    36  		{"1kb", Kilobyte},
    37  		{"1mb", Megabyte},
    38  		{"1Gb", Gigabyte},
    39  		{"1tb", Terabyte},
    40  		{"1Pb", Petabyte},
    41  		{"1KiB", Kibibyte},
    42  		{"1mib", Mebibyte},
    43  		{"1gib", Gibibyte},
    44  		{"1tib", Tebibyte},
    45  		{"1PiB", Pebibyte},
    46  		{"4TB", 4 * Terabyte},
    47  		{"43.5MB", Size(43.5 * float64(Megabyte))},
    48  	}
    49  
    50  	for _, test := range tests {
    51  		found, err := Parse(test.str)
    52  		testutil.Fatalf(t, "Unexpected error: %v", err)
    53  		if found != test.sz {
    54  			t.Errorf("Parse(%q): expected: %s; found: %s", test.str, test.sz, found)
    55  		}
    56  	}
    57  }
    58  
    59  func TestString(t *testing.T) {
    60  	tests := []struct {
    61  		str string
    62  		sz  Size
    63  	}{
    64  		{"0B", 0},
    65  		{"1B", Byte},
    66  		{"256B", 256 * Byte},
    67  		{"256.50KiB", Size(256.5 * float64(Kibibyte))},
    68  
    69  		{"1KiB", Kibibyte},
    70  		{"1MiB", Mebibyte},
    71  		{"1GiB", Gibibyte},
    72  		{"1TiB", Tebibyte},
    73  		{"1PiB", Pebibyte},
    74  
    75  		{"1kB", Kilobyte},
    76  		{"1MB", Megabyte},
    77  		{"1GB", Gigabyte},
    78  		{"1TB", Terabyte},
    79  		{"1PB", Petabyte},
    80  		{"2PB", 2 * Petabyte},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		if found := test.sz.String(); found != test.str {
    85  			t.Errorf("%d.String(): expected: %s; found: %s", test.sz, test.str, found)
    86  		}
    87  	}
    88  }
    89  
    90  func mustParse(s string) Size {
    91  	sz, err := Parse(s)
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  	return sz
    96  }
    97  
    98  func TestRoundtrip(t *testing.T) {
    99  	// Roundtripping through String() works for values when at most 2 decimal
   100  	// place precision is needed.
   101  	tests := []Size{
   102  		0,
   103  		1023,
   104  		mustParse("1.02KiB"),
   105  		mustParse("1.12MiB"),
   106  	}
   107  
   108  	for _, unit := range allUnits {
   109  		tests = append(tests, unit)
   110  	}
   111  
   112  	for _, size := range tests {
   113  		str := size.String()
   114  		if found, err := Parse(str); err != nil {
   115  			t.Errorf("Parse(%q.String()): error: %v", size, err)
   116  		} else if found != size {
   117  			t.Errorf("Parse(Size(%d).String()): expected: %d; found: %d", size, size, found)
   118  		}
   119  	}
   120  }
   121  
   122  func TestUnits(t *testing.T) {
   123  	for i := 0; i < len(allUnits); i++ {
   124  		if expected := unitSuffix(allUnits[i]); !strings.HasSuffix(allUnits[i].String(), expected) {
   125  			t.Errorf("expected suffix: %s; found: %s", expected, allUnits[i])
   126  		}
   127  		if i != len(allUnits)-1 && allUnits[i] <= allUnits[i+1] {
   128  			t.Errorf("%s >= %s", allUnits[i], allUnits[i+1])
   129  		}
   130  	}
   131  }
   132  
   133  func TestFloor(t *testing.T) {
   134  	type test struct {
   135  		sz       Size
   136  		expected Size
   137  	}
   138  	tests := []test{
   139  		{0, 0},
   140  		{10, 10},
   141  
   142  		{mustParse("1.02KiB"), Kibibyte},
   143  		{mustParse("1.45MiB"), Mebibyte},
   144  		{mustParse("1.32GiB"), Gibibyte},
   145  		{mustParse("1.6PiB"), Pebibyte},
   146  
   147  		{mustParse("1.02KB"), Kilobyte},
   148  		{mustParse("1.02MB"), Megabyte},
   149  		{mustParse("1.02GB"), Gigabyte},
   150  		{mustParse("1.02PB"), Petabyte},
   151  	}
   152  
   153  	for _, unit := range allUnits {
   154  		tests = append(tests, test{unit, unit})
   155  	}
   156  
   157  	for _, test := range tests {
   158  		found := test.sz.Floor()
   159  		if found != test.expected {
   160  			t.Errorf("%d.Floor(): expected: %s; found: %s", test.sz, test.expected, found)
   161  		}
   162  		if strings.Contains(found.String(), ".") {
   163  			t.Errorf("%d.Floor() == %s: contains decimal", test.sz, found)
   164  		}
   165  	}
   166  }
   167  
   168  func TestRound(t *testing.T) {
   169  	type test struct {
   170  		sz       Size
   171  		expected Size
   172  	}
   173  	tests := []test{
   174  		{0, 0},
   175  		{mustParse("1.02KiB"), Kibibyte},
   176  		{mustParse("1.9KiB"), 2 * Kibibyte},
   177  		{mustParse("1.45MiB"), Mebibyte},
   178  		{mustParse("4.32GiB"), 4 * Gibibyte},
   179  		{mustParse("4.72GiB"), 5 * Gibibyte},
   180  		{mustParse("2.62GiB"), 3 * Gibibyte},
   181  		{mustParse("1.4PiB"), Pebibyte},
   182  		{mustParse("1.6PiB"), 2 * Pebibyte},
   183  	}
   184  
   185  	for _, unit := range allUnits {
   186  		tests = append(tests, test{unit, unit})
   187  	}
   188  
   189  	for _, test := range tests {
   190  		found := test.sz.Round()
   191  		if found != test.expected {
   192  			t.Errorf("%d.Round(): expected: %s; found: %s", test.sz, test.expected, found)
   193  		}
   194  		if strings.Contains(found.String(), ".") {
   195  			t.Errorf("%d.Round() == %s: contains decimal", test.sz, found)
   196  		}
   197  	}
   198  }