|
| 1 | +package byteformats_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/sagernet/sing/common/byteformats" |
| 7 | + "github.com/sagernet/sing/common/json" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNetworkBytes(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + testMap := map[string]uint64{ |
| 15 | + "1 Bps": byteformats.Byte, |
| 16 | + "1 Kbps": byteformats.KByte / 8, |
| 17 | + "1 KBps": byteformats.KByte, |
| 18 | + "1 Mbps": byteformats.MByte / 8, |
| 19 | + "1 MBps": byteformats.MByte, |
| 20 | + "1 Gbps": byteformats.GByte / 8, |
| 21 | + "1 GBps": byteformats.GByte, |
| 22 | + "1 Tbps": byteformats.TByte / 8, |
| 23 | + "1 TBps": byteformats.TByte, |
| 24 | + "1 Pbps": byteformats.PByte / 8, |
| 25 | + "1 PBps": byteformats.PByte, |
| 26 | + "1k": byteformats.KByte, |
| 27 | + "1m": byteformats.MByte, |
| 28 | + } |
| 29 | + for k, v := range testMap { |
| 30 | + t.Run(k, func(t *testing.T) { |
| 31 | + var nb byteformats.NetworkBytesCompat |
| 32 | + require.NoError(t, json.Unmarshal([]byte("\""+k+"\""), &nb)) |
| 33 | + require.Equal(t, v, nb.Value) |
| 34 | + b, err := json.Marshal(nb) |
| 35 | + require.NoError(t, err) |
| 36 | + require.Equal(t, "\""+k+"\"", string(b)) |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestMemoryBytes(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + testMap := map[string]uint64{ |
| 44 | + "1 B": byteformats.Byte, |
| 45 | + "1 KB": byteformats.KiByte, |
| 46 | + "1 MB": byteformats.MiByte, |
| 47 | + "1 GB": byteformats.GiByte, |
| 48 | + "1 TB": byteformats.TiByte, |
| 49 | + "1 PB": byteformats.PiByte, |
| 50 | + } |
| 51 | + for k, v := range testMap { |
| 52 | + t.Run(k, func(t *testing.T) { |
| 53 | + var mb byteformats.MemoryBytes |
| 54 | + require.NoError(t, json.Unmarshal([]byte("\""+k+"\""), &mb)) |
| 55 | + require.Equal(t, v, mb.Value) |
| 56 | + b, err := json.Marshal(mb) |
| 57 | + require.NoError(t, err) |
| 58 | + require.Equal(t, "\""+k+"\"", string(b)) |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestDefaultBytes(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + testMap := map[string]uint64{ |
| 66 | + "1 B": byteformats.Byte, |
| 67 | + "1 KB": byteformats.KByte, |
| 68 | + "1 KiB": byteformats.KiByte, |
| 69 | + "1 MB": byteformats.MByte, |
| 70 | + "1 MiB": byteformats.MiByte, |
| 71 | + "1 GB": byteformats.GByte, |
| 72 | + "1 GiB": byteformats.GiByte, |
| 73 | + "1 TB": byteformats.TByte, |
| 74 | + "1 TiB": byteformats.TiByte, |
| 75 | + "1 PB": byteformats.PByte, |
| 76 | + "1 PiB": byteformats.PiByte, |
| 77 | + "1 EB": byteformats.EByte, |
| 78 | + "1 EiB": byteformats.EiByte, |
| 79 | + "1k": byteformats.KByte, |
| 80 | + "1m": byteformats.MByte, |
| 81 | + "1g": byteformats.GByte, |
| 82 | + "1t": byteformats.TByte, |
| 83 | + "1p": byteformats.PByte, |
| 84 | + "1e": byteformats.EByte, |
| 85 | + "1K": byteformats.KByte, |
| 86 | + "1M": byteformats.MByte, |
| 87 | + "1G": byteformats.GByte, |
| 88 | + "1T": byteformats.TByte, |
| 89 | + "1P": byteformats.PByte, |
| 90 | + "1E": byteformats.EByte, |
| 91 | + "1Ki": byteformats.KiByte, |
| 92 | + "1Mi": byteformats.MiByte, |
| 93 | + "1Gi": byteformats.GiByte, |
| 94 | + "1Ti": byteformats.TiByte, |
| 95 | + "1Pi": byteformats.PiByte, |
| 96 | + "1Ei": byteformats.EiByte, |
| 97 | + "1KiB": byteformats.KiByte, |
| 98 | + "1MiB": byteformats.MiByte, |
| 99 | + "1GiB": byteformats.GiByte, |
| 100 | + "1TiB": byteformats.TiByte, |
| 101 | + "1PiB": byteformats.PiByte, |
| 102 | + "1EiB": byteformats.EiByte, |
| 103 | + "1kB": byteformats.KByte, |
| 104 | + "1mB": byteformats.MByte, |
| 105 | + "1gB": byteformats.GByte, |
| 106 | + "1tB": byteformats.TByte, |
| 107 | + "1pB": byteformats.PByte, |
| 108 | + "1eB": byteformats.EByte, |
| 109 | + } |
| 110 | + for k, v := range testMap { |
| 111 | + t.Run(k, func(t *testing.T) { |
| 112 | + var mb byteformats.Bytes |
| 113 | + require.NoError(t, json.Unmarshal([]byte("\""+k+"\""), &mb)) |
| 114 | + require.Equal(t, v, mb.Value) |
| 115 | + b, err := json.Marshal(mb) |
| 116 | + require.NoError(t, err) |
| 117 | + require.Equal(t, "\""+k+"\"", string(b)) |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments