github.com/safing/portbase@v0.19.5/formats/varint/varint_test.go (about) 1 //nolint:gocognit 2 package varint 3 4 import ( 5 "bytes" 6 "testing" 7 ) 8 9 func TestConversion(t *testing.T) { 10 t.Parallel() 11 12 subjects := []struct { 13 intType uint8 14 bytes []byte 15 integer uint64 16 }{ 17 {8, []byte{0x00}, 0}, 18 {8, []byte{0x01}, 1}, 19 {8, []byte{0x7F}, 127}, 20 {8, []byte{0x80, 0x01}, 128}, 21 {8, []byte{0xFF, 0x01}, 255}, 22 23 {16, []byte{0x80, 0x02}, 256}, 24 {16, []byte{0xFF, 0x7F}, 16383}, 25 {16, []byte{0x80, 0x80, 0x01}, 16384}, 26 {16, []byte{0xFF, 0xFF, 0x03}, 65535}, 27 28 {32, []byte{0x80, 0x80, 0x04}, 65536}, 29 {32, []byte{0xFF, 0xFF, 0x7F}, 2097151}, 30 {32, []byte{0x80, 0x80, 0x80, 0x01}, 2097152}, 31 {32, []byte{0xFF, 0xFF, 0xFF, 0x07}, 16777215}, 32 {32, []byte{0x80, 0x80, 0x80, 0x08}, 16777216}, 33 {32, []byte{0xFF, 0xFF, 0xFF, 0x7F}, 268435455}, 34 {32, []byte{0x80, 0x80, 0x80, 0x80, 0x01}, 268435456}, 35 {32, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x0F}, 4294967295}, 36 37 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x10}, 4294967296}, 38 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x7F}, 34359738367}, 39 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x01}, 34359738368}, 40 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F}, 1099511627775}, 41 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x20}, 1099511627776}, 42 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}, 4398046511103}, 43 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}, 4398046511104}, 44 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F}, 281474976710655}, 45 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x40}, 281474976710656}, 46 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}, 562949953421311}, 47 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}, 562949953421312}, 48 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}, 72057594037927935}, 49 50 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}, 72057594037927936}, 51 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}, 9223372036854775807}, 52 53 {64, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}, 9223372036854775808}, 54 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01}, 18446744073709551615}, 55 } 56 57 for _, subject := range subjects { 58 59 actualInteger, _, err := Unpack64(subject.bytes) 60 if err != nil || actualInteger != subject.integer { 61 t.Errorf("Unpack64 %d: expected %d, actual %d", subject.bytes, subject.integer, actualInteger) 62 } 63 actualBytes := Pack64(subject.integer) 64 if err != nil || !bytes.Equal(actualBytes, subject.bytes) { 65 t.Errorf("Pack64 %d: expected %d, actual %d", subject.integer, subject.bytes, actualBytes) 66 } 67 68 if subject.intType <= 32 { 69 actualInteger, _, err := Unpack32(subject.bytes) 70 if err != nil || actualInteger != uint32(subject.integer) { 71 t.Errorf("Unpack32 %d: expected %d, actual %d", subject.bytes, subject.integer, actualInteger) 72 } 73 actualBytes := Pack32(uint32(subject.integer)) 74 if err != nil || !bytes.Equal(actualBytes, subject.bytes) { 75 t.Errorf("Pack32 %d: expected %d, actual %d", subject.integer, subject.bytes, actualBytes) 76 } 77 } 78 79 if subject.intType <= 16 { 80 actualInteger, _, err := Unpack16(subject.bytes) 81 if err != nil || actualInteger != uint16(subject.integer) { 82 t.Errorf("Unpack16 %d: expected %d, actual %d", subject.bytes, subject.integer, actualInteger) 83 } 84 actualBytes := Pack16(uint16(subject.integer)) 85 if err != nil || !bytes.Equal(actualBytes, subject.bytes) { 86 t.Errorf("Pack16 %d: expected %d, actual %d", subject.integer, subject.bytes, actualBytes) 87 } 88 } 89 90 if subject.intType <= 8 { 91 actualInteger, _, err := Unpack8(subject.bytes) 92 if err != nil || actualInteger != uint8(subject.integer) { 93 t.Errorf("Unpack8 %d: expected %d, actual %d", subject.bytes, subject.integer, actualInteger) 94 } 95 actualBytes := Pack8(uint8(subject.integer)) 96 if err != nil || !bytes.Equal(actualBytes, subject.bytes) { 97 t.Errorf("Pack8 %d: expected %d, actual %d", subject.integer, subject.bytes, actualBytes) 98 } 99 } 100 101 } 102 } 103 104 func TestFails(t *testing.T) { 105 t.Parallel() 106 107 subjects := []struct { 108 intType uint8 109 bytes []byte 110 }{ 111 {32, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01}}, 112 {64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02}}, 113 {64, []byte{0xFF}}, 114 } 115 116 for _, subject := range subjects { 117 118 if subject.intType == 64 { 119 _, _, err := Unpack64(subject.bytes) 120 if err == nil { 121 t.Errorf("Unpack64 %d: expected error while unpacking.", subject.bytes) 122 } 123 } 124 125 _, _, err := Unpack32(subject.bytes) 126 if err == nil { 127 t.Errorf("Unpack32 %d: expected error while unpacking.", subject.bytes) 128 } 129 130 _, _, err = Unpack16(subject.bytes) 131 if err == nil { 132 t.Errorf("Unpack16 %d: expected error while unpacking.", subject.bytes) 133 } 134 135 _, _, err = Unpack8(subject.bytes) 136 if err == nil { 137 t.Errorf("Unpack8 %d: expected error while unpacking.", subject.bytes) 138 } 139 140 } 141 }