|
| 1 | +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) |
| 2 | +// go-aah/essentials source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package ess |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "aahframework.org/test/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestBStrToBytes(t *testing.T) { |
| 14 | + checkBytesValue(t, "2b", int64(2)) |
| 15 | + checkBytesValue(t, "2B", int64(2)) |
| 16 | + checkBytesValue(t, "2b", int64(2)) |
| 17 | + checkBytesValue(t, "2B", int64(2)) |
| 18 | +} |
| 19 | + |
| 20 | +func TestKBStrToBytes(t *testing.T) { |
| 21 | + checkBytesValue(t, "2kb", int64(2048)) |
| 22 | + checkBytesValue(t, "2KB", int64(2048)) |
| 23 | + checkBytesValue(t, "2kib", int64(2048)) |
| 24 | + checkBytesValue(t, "2KiB", int64(2048)) |
| 25 | +} |
| 26 | + |
| 27 | +func TestMBStrToBytes(t *testing.T) { |
| 28 | + checkBytesValue(t, "2mb", int64(2097152)) |
| 29 | + checkBytesValue(t, "2MB", int64(2097152)) |
| 30 | + checkBytesValue(t, "2mib", int64(2097152)) |
| 31 | + checkBytesValue(t, "2MiB", int64(2097152)) |
| 32 | +} |
| 33 | + |
| 34 | +func TestGBStrToBytes(t *testing.T) { |
| 35 | + checkBytesValue(t, "2gb", int64(2147483648)) |
| 36 | + checkBytesValue(t, "2GB", int64(2147483648)) |
| 37 | + checkBytesValue(t, "2Gib", int64(2147483648)) |
| 38 | + checkBytesValue(t, "2GiB", int64(2147483648)) |
| 39 | +} |
| 40 | + |
| 41 | +func TestTBStrToBytes(t *testing.T) { |
| 42 | + checkBytesValue(t, "2tb", int64(2199023255552)) |
| 43 | + checkBytesValue(t, "2TB", int64(2199023255552)) |
| 44 | + checkBytesValue(t, "2Tib", int64(2199023255552)) |
| 45 | + checkBytesValue(t, "2TiB", int64(2199023255552)) |
| 46 | +} |
| 47 | + |
| 48 | +func TestErrStrToBytes(t *testing.T) { |
| 49 | + v1, err := StrToBytes("2") |
| 50 | + assert.NotNil(t, err) |
| 51 | + assert.Equal(t, "format: invalid input '2'", err.Error()) |
| 52 | + assert.Equal(t, int64(0), v1) |
| 53 | +} |
| 54 | + |
| 55 | +func checkBytesValue(t *testing.T, value string, expt int64) { |
| 56 | + v, err := StrToBytes(value) |
| 57 | + assert.Nil(t, err) |
| 58 | + assert.Equal(t, expt, v) |
| 59 | +} |
0 commit comments