github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_unit_size_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gfile_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/os/gfile"
    13  	"github.com/wangyougui/gf/v2/test/gtest"
    14  	"github.com/wangyougui/gf/v2/util/gconv"
    15  )
    16  
    17  func Test_Size(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		var (
    20  			paths1 string = "/testfile_t1.txt"
    21  			sizes  int64
    22  		)
    23  
    24  		createTestFile(paths1, "abcdefghijklmn")
    25  		defer delTestFiles(paths1)
    26  
    27  		sizes = gfile.Size(testpath() + paths1)
    28  		t.Assert(sizes, 14)
    29  
    30  		sizes = gfile.Size("")
    31  		t.Assert(sizes, 0)
    32  
    33  	})
    34  }
    35  
    36  func Test_SizeFormat(t *testing.T) {
    37  	gtest.C(t, func(t *gtest.T) {
    38  		var (
    39  			paths1 = "/testfile_t1.txt"
    40  			sizes  string
    41  		)
    42  
    43  		createTestFile(paths1, "abcdefghijklmn")
    44  		defer delTestFiles(paths1)
    45  
    46  		sizes = gfile.SizeFormat(testpath() + paths1)
    47  		t.Assert(sizes, "14.00B")
    48  
    49  		sizes = gfile.SizeFormat("")
    50  		t.Assert(sizes, "0.00B")
    51  
    52  	})
    53  }
    54  
    55  func Test_StrToSize(t *testing.T) {
    56  	gtest.C(t, func(t *gtest.T) {
    57  		t.Assert(gfile.StrToSize("0.00B"), 0)
    58  		t.Assert(gfile.StrToSize("16.00B"), 16)
    59  		t.Assert(gfile.StrToSize("1.00K"), 1024)
    60  		t.Assert(gfile.StrToSize("1.00KB"), 1024)
    61  		t.Assert(gfile.StrToSize("1.00KiloByte"), 1024)
    62  		t.Assert(gfile.StrToSize("15.26M"), gconv.Int64(15.26*1024*1024))
    63  		t.Assert(gfile.StrToSize("15.26MB"), gconv.Int64(15.26*1024*1024))
    64  		t.Assert(gfile.StrToSize("1.49G"), gconv.Int64(1.49*1024*1024*1024))
    65  		t.Assert(gfile.StrToSize("1.49GB"), gconv.Int64(1.49*1024*1024*1024))
    66  		t.Assert(gfile.StrToSize("8.73T"), gconv.Int64(8.73*1024*1024*1024*1024))
    67  		t.Assert(gfile.StrToSize("8.73TB"), gconv.Int64(8.73*1024*1024*1024*1024))
    68  		t.Assert(gfile.StrToSize("8.53P"), gconv.Int64(8.53*1024*1024*1024*1024*1024))
    69  		t.Assert(gfile.StrToSize("8.53PB"), gconv.Int64(8.53*1024*1024*1024*1024*1024))
    70  		t.Assert(gfile.StrToSize("8.01EB"), gconv.Int64(8.01*1024*1024*1024*1024*1024*1024))
    71  		t.Assert(gfile.StrToSize("0.01ZB"), gconv.Int64(0.01*1024*1024*1024*1024*1024*1024*1024))
    72  		t.Assert(gfile.StrToSize("0.01YB"), gconv.Int64(0.01*1024*1024*1024*1024*1024*1024*1024*1024))
    73  		t.Assert(gfile.StrToSize("0.01BB"), gconv.Int64(0.01*1024*1024*1024*1024*1024*1024*1024*1024*1024))
    74  		t.Assert(gfile.StrToSize("0.01AB"), gconv.Int64(-1))
    75  		t.Assert(gfile.StrToSize("123456789"), 123456789)
    76  	})
    77  }
    78  
    79  func Test_FormatSize(t *testing.T) {
    80  	gtest.C(t, func(t *gtest.T) {
    81  		t.Assert(gfile.FormatSize(0), "0.00B")
    82  		t.Assert(gfile.FormatSize(16), "16.00B")
    83  
    84  		t.Assert(gfile.FormatSize(1024), "1.00K")
    85  
    86  		t.Assert(gfile.FormatSize(16000000), "15.26M")
    87  
    88  		t.Assert(gfile.FormatSize(1600000000), "1.49G")
    89  
    90  		t.Assert(gfile.FormatSize(9600000000000), "8.73T")
    91  		t.Assert(gfile.FormatSize(9600000000000000), "8.53P")
    92  	})
    93  }
    94  
    95  func Test_ReadableSize(t *testing.T) {
    96  	gtest.C(t, func(t *gtest.T) {
    97  
    98  		var (
    99  			paths1 string = "/testfile_t1.txt"
   100  		)
   101  		createTestFile(paths1, "abcdefghijklmn")
   102  		defer delTestFiles(paths1)
   103  		t.Assert(gfile.ReadableSize(testpath()+paths1), "14.00B")
   104  		t.Assert(gfile.ReadableSize(""), "0.00B")
   105  
   106  	})
   107  }