gf/os/gfile/gfile_z_size_test.go

84 lines
2.4 KiB
Go
Raw Normal View History

// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2019-04-16 22:11:44 +08:00
package gfile_test
2019-04-10 16:22:29 +08:00
import (
"github.com/gogf/gf/util/gconv"
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/test/gtest"
2019-04-10 16:22:29 +08:00
)
func Test_Size(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-12 09:47:55 +08:00
var (
2019-04-13 13:14:30 +08:00
paths1 string = "/testfile_t1.txt"
2019-04-12 09:47:55 +08:00
sizes int64
2019-04-10 16:22:29 +08:00
)
2019-04-13 13:14:30 +08:00
2019-04-16 22:11:44 +08:00
createTestFile(paths1, "abcdefghijklmn")
defer delTestFiles(paths1)
2019-04-13 13:14:30 +08:00
2019-04-16 22:11:44 +08:00
sizes = gfile.Size(testpath() + paths1)
2020-03-19 22:56:12 +08:00
t.Assert(sizes, 14)
2019-04-10 17:54:28 +08:00
2019-04-16 22:11:44 +08:00
sizes = gfile.Size("")
2020-03-19 22:56:12 +08:00
t.Assert(sizes, 0)
2019-04-10 16:22:29 +08:00
})
}
func Test_StrToSize(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gfile.StrToSize("0.00B"), 0)
t.Assert(gfile.StrToSize("16.00B"), 16)
t.Assert(gfile.StrToSize("1.00K"), 1024)
t.Assert(gfile.StrToSize("1.00KB"), 1024)
t.Assert(gfile.StrToSize("1.00KiloByte"), 1024)
t.Assert(gfile.StrToSize("15.26M"), gconv.Int64(15.26*1024*1024))
t.Assert(gfile.StrToSize("15.26MB"), gconv.Int64(15.26*1024*1024))
t.Assert(gfile.StrToSize("1.49G"), gconv.Int64(1.49*1024*1024*1024))
t.Assert(gfile.StrToSize("1.49GB"), gconv.Int64(1.49*1024*1024*1024))
t.Assert(gfile.StrToSize("8.73T"), gconv.Int64(8.73*1024*1024*1024*1024))
t.Assert(gfile.StrToSize("8.73TB"), gconv.Int64(8.73*1024*1024*1024*1024))
t.Assert(gfile.StrToSize("8.53P"), gconv.Int64(8.53*1024*1024*1024*1024*1024))
t.Assert(gfile.StrToSize("8.53PB"), gconv.Int64(8.53*1024*1024*1024*1024*1024))
t.Assert(gfile.StrToSize("8.01EB"), gconv.Int64(8.01*1024*1024*1024*1024*1024*1024))
})
}
func Test_FormatSize(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gfile.FormatSize(0), "0.00B")
t.Assert(gfile.FormatSize(16), "16.00B")
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.FormatSize(1024), "1.00K")
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.FormatSize(16000000), "15.26M")
2019-04-10 16:22:29 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.FormatSize(1600000000), "1.49G")
2019-04-10 17:54:28 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.FormatSize(9600000000000), "8.73T")
t.Assert(gfile.FormatSize(9600000000000000), "8.53P")
2019-04-10 16:22:29 +08:00
})
}
func Test_ReadableSize(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-10 16:22:29 +08:00
2019-04-13 13:14:30 +08:00
var (
paths1 string = "/testfile_t1.txt"
)
2019-04-16 22:11:44 +08:00
createTestFile(paths1, "abcdefghijklmn")
defer delTestFiles(paths1)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.ReadableSize(testpath()+paths1), "14.00B")
t.Assert(gfile.ReadableSize(""), "0.00B")
2019-04-10 16:22:29 +08:00
})
}