github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/gadget/quantity/size_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package quantity_test
    21  
    22  import (
    23  	"fmt"
    24  	"testing"
    25  
    26  	. "gopkg.in/check.v1"
    27  	"gopkg.in/yaml.v2"
    28  
    29  	"github.com/snapcore/snapd/gadget/quantity"
    30  )
    31  
    32  func TestRun(t *testing.T) { TestingT(t) }
    33  
    34  type sizeTestSuite struct{}
    35  
    36  var _ = Suite(&sizeTestSuite{})
    37  
    38  func (s *sizeTestSuite) TestIECString(c *C) {
    39  	for _, tc := range []struct {
    40  		size quantity.Size
    41  		exp  string
    42  	}{
    43  		{512, "512 B"},
    44  		{1000, "1000 B"},
    45  		{1030, "1.01 KiB"},
    46  		{quantity.SizeKiB + 512, "1.50 KiB"},
    47  		{123 * quantity.SizeKiB, "123 KiB"},
    48  		{512 * quantity.SizeKiB, "512 KiB"},
    49  		{578 * quantity.SizeMiB, "578 MiB"},
    50  		{1*quantity.SizeGiB + 123*quantity.SizeMiB, "1.12 GiB"},
    51  		{1024 * quantity.SizeGiB, "1 TiB"},
    52  		{2 * 1024 * 1024 * 1024 * quantity.SizeGiB, "2048 PiB"},
    53  	} {
    54  		c.Check(tc.size.IECString(), Equals, tc.exp)
    55  	}
    56  }
    57  
    58  func (s *sizeTestSuite) TestUnmarshalYAMLSize(c *C) {
    59  	type foo struct {
    60  		Size quantity.Size `yaml:"size"`
    61  	}
    62  
    63  	for i, tc := range []struct {
    64  		s   string
    65  		sz  quantity.Size
    66  		err string
    67  	}{
    68  		{"1234", 1234, ""},
    69  		{"1234M", 1234 * quantity.SizeMiB, ""},
    70  		{"1234G", 1234 * quantity.SizeGiB, ""},
    71  		{"0", 0, ""},
    72  		{"a0M", 0, `cannot parse size "a0M": no numerical prefix.*`},
    73  		{"-123", 0, `cannot parse size "-123": size cannot be negative`},
    74  		{"123a", 0, `cannot parse size "123a": invalid suffix "a"`},
    75  	} {
    76  		c.Logf("tc: %v", i)
    77  
    78  		var f foo
    79  		err := yaml.Unmarshal([]byte(fmt.Sprintf("size: %s", tc.s)), &f)
    80  		if tc.err != "" {
    81  			c.Check(err, ErrorMatches, tc.err)
    82  		} else {
    83  			c.Check(err, IsNil)
    84  			c.Check(f.Size, Equals, tc.sz)
    85  		}
    86  	}
    87  }
    88  
    89  func (s *sizeTestSuite) TestSizeString(c *C) {
    90  	var pSize *quantity.Size
    91  	c.Check(pSize.String(), Equals, "unspecified")
    92  
    93  	for _, tc := range []struct {
    94  		size quantity.Size
    95  		exp  string
    96  	}{
    97  		{512, "512"},
    98  		{1030, "1030"},
    99  		{quantity.SizeKiB + 512, "1536"},
   100  		{578 * quantity.SizeMiB, "606076928"},
   101  	} {
   102  		c.Check(tc.size.String(), Equals, tc.exp)
   103  	}
   104  }