2020-08-16 14:05:52 +00:00
|
|
|
package flagutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBytesSetFailure(t *testing.T) {
|
|
|
|
f := func(value string) {
|
|
|
|
t.Helper()
|
|
|
|
var b Bytes
|
|
|
|
if err := b.Set(value); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error in b.Set(%q)", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("foobar")
|
|
|
|
f("5foobar")
|
|
|
|
f("aKB")
|
|
|
|
f("134xMB")
|
|
|
|
f("2.43sdfGb")
|
|
|
|
f("aKiB")
|
|
|
|
f("134xMiB")
|
|
|
|
f("2.43sdfGIb")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesSetSuccess(t *testing.T) {
|
2022-12-15 03:26:24 +00:00
|
|
|
f := func(value string, expectedResult int64) {
|
2020-08-16 14:05:52 +00:00
|
|
|
t.Helper()
|
|
|
|
var b Bytes
|
|
|
|
if err := b.Set(value); err != nil {
|
|
|
|
t.Fatalf("unexpected error in b.Set(%q): %s", value, err)
|
|
|
|
}
|
|
|
|
if b.N != expectedResult {
|
|
|
|
t.Fatalf("unexpected result; got %d; want %d", b.N, expectedResult)
|
|
|
|
}
|
|
|
|
valueString := b.String()
|
|
|
|
valueExpected := normalizeBytesString(value)
|
|
|
|
if valueString != valueExpected {
|
|
|
|
t.Fatalf("unexpected valueString; got %q; want %q", valueString, valueExpected)
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 18:53:01 +00:00
|
|
|
f("", 0)
|
2020-08-16 14:05:52 +00:00
|
|
|
f("0", 0)
|
|
|
|
f("1", 1)
|
|
|
|
f("-1234", -1234)
|
|
|
|
f("123.456", 123)
|
|
|
|
f("1KiB", 1024)
|
|
|
|
f("1.5kib", 1.5*1024)
|
|
|
|
f("23MiB", 23*1024*1024)
|
2020-08-20 21:27:37 +00:00
|
|
|
f("0.25GiB", 0.25*1024*1024*1024)
|
2022-12-15 01:52:32 +00:00
|
|
|
f("1.25TiB", 1.25*1024*1024*1024*1024)
|
2020-08-16 14:05:52 +00:00
|
|
|
f("1KB", 1000)
|
|
|
|
f("1.5kb", 1.5*1000)
|
|
|
|
f("23MB", 23*1000*1000)
|
2020-08-20 21:27:37 +00:00
|
|
|
f("0.25GB", 0.25*1000*1000*1000)
|
2022-12-15 01:52:32 +00:00
|
|
|
f("1.25TB", 1.25*1000*1000*1000*1000)
|
2020-08-16 14:05:52 +00:00
|
|
|
}
|