github.com/gogf/gf@v1.16.9/os/gfile/gfile_z_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/gogf/gf. 6 7 package gfile_test 8 9 import ( 10 "github.com/gogf/gf/util/gconv" 11 "testing" 12 13 "github.com/gogf/gf/os/gfile" 14 "github.com/gogf/gf/test/gtest" 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 }) 72 } 73 74 func Test_FormatSize(t *testing.T) { 75 gtest.C(t, func(t *gtest.T) { 76 t.Assert(gfile.FormatSize(0), "0.00B") 77 t.Assert(gfile.FormatSize(16), "16.00B") 78 79 t.Assert(gfile.FormatSize(1024), "1.00K") 80 81 t.Assert(gfile.FormatSize(16000000), "15.26M") 82 83 t.Assert(gfile.FormatSize(1600000000), "1.49G") 84 85 t.Assert(gfile.FormatSize(9600000000000), "8.73T") 86 t.Assert(gfile.FormatSize(9600000000000000), "8.53P") 87 }) 88 } 89 90 func Test_ReadableSize(t *testing.T) { 91 gtest.C(t, func(t *gtest.T) { 92 93 var ( 94 paths1 string = "/testfile_t1.txt" 95 ) 96 createTestFile(paths1, "abcdefghijklmn") 97 defer delTestFiles(paths1) 98 t.Assert(gfile.ReadableSize(testpath()+paths1), "14.00B") 99 t.Assert(gfile.ReadableSize(""), "0.00B") 100 101 }) 102 }