github.com/EagleQL/Xray-core@v1.4.3/common/buf/buffer_test.go (about) 1 package buf_test 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "testing" 7 8 "github.com/google/go-cmp/cmp" 9 "github.com/xtls/xray-core/common" 10 . "github.com/xtls/xray-core/common/buf" 11 ) 12 13 func TestBufferClear(t *testing.T) { 14 buffer := New() 15 defer buffer.Release() 16 17 payload := "Bytes" 18 buffer.Write([]byte(payload)) 19 if diff := cmp.Diff(buffer.Bytes(), []byte(payload)); diff != "" { 20 t.Error(diff) 21 } 22 23 buffer.Clear() 24 if buffer.Len() != 0 { 25 t.Error("expect 0 length, but got ", buffer.Len()) 26 } 27 } 28 29 func TestBufferIsEmpty(t *testing.T) { 30 buffer := New() 31 defer buffer.Release() 32 33 if buffer.IsEmpty() != true { 34 t.Error("expect empty buffer, but not") 35 } 36 } 37 38 func TestBufferString(t *testing.T) { 39 buffer := New() 40 defer buffer.Release() 41 42 const payload = "Test String" 43 common.Must2(buffer.WriteString(payload)) 44 if buffer.String() != payload { 45 t.Error("expect buffer content as ", payload, " but actually ", buffer.String()) 46 } 47 } 48 49 func TestBufferByte(t *testing.T) { 50 { 51 buffer := New() 52 common.Must(buffer.WriteByte('m')) 53 if buffer.String() != "m" { 54 t.Error("expect buffer content as ", "m", " but actually ", buffer.String()) 55 } 56 buffer.Release() 57 } 58 { 59 buffer := StackNew() 60 common.Must(buffer.WriteByte('n')) 61 if buffer.String() != "n" { 62 t.Error("expect buffer content as ", "n", " but actually ", buffer.String()) 63 } 64 buffer.Release() 65 } 66 { 67 buffer := StackNew() 68 common.Must2(buffer.WriteString("HELLOWORLD")) 69 if b := buffer.Byte(5); b != 'W' { 70 t.Error("unexpected byte ", b) 71 } 72 73 buffer.SetByte(5, 'M') 74 if buffer.String() != "HELLOMORLD" { 75 t.Error("expect buffer content as ", "n", " but actually ", buffer.String()) 76 } 77 buffer.Release() 78 } 79 } 80 func TestBufferResize(t *testing.T) { 81 buffer := New() 82 defer buffer.Release() 83 84 const payload = "Test String" 85 common.Must2(buffer.WriteString(payload)) 86 if buffer.String() != payload { 87 t.Error("expect buffer content as ", payload, " but actually ", buffer.String()) 88 } 89 90 buffer.Resize(-6, -3) 91 if l := buffer.Len(); int(l) != 3 { 92 t.Error("len error ", l) 93 } 94 95 if s := buffer.String(); s != "Str" { 96 t.Error("unexpect buffer ", s) 97 } 98 99 buffer.Resize(int32(len(payload)), 200) 100 if l := buffer.Len(); int(l) != 200-len(payload) { 101 t.Error("len error ", l) 102 } 103 } 104 105 func TestBufferSlice(t *testing.T) { 106 { 107 b := New() 108 common.Must2(b.Write([]byte("abcd"))) 109 bytes := b.BytesFrom(-2) 110 if diff := cmp.Diff(bytes, []byte{'c', 'd'}); diff != "" { 111 t.Error(diff) 112 } 113 } 114 115 { 116 b := New() 117 common.Must2(b.Write([]byte("abcd"))) 118 bytes := b.BytesTo(-2) 119 if diff := cmp.Diff(bytes, []byte{'a', 'b'}); diff != "" { 120 t.Error(diff) 121 } 122 } 123 124 { 125 b := New() 126 common.Must2(b.Write([]byte("abcd"))) 127 bytes := b.BytesRange(-3, -1) 128 if diff := cmp.Diff(bytes, []byte{'b', 'c'}); diff != "" { 129 t.Error(diff) 130 } 131 } 132 } 133 134 func TestBufferReadFullFrom(t *testing.T) { 135 payload := make([]byte, 1024) 136 common.Must2(rand.Read(payload)) 137 138 reader := bytes.NewReader(payload) 139 b := New() 140 n, err := b.ReadFullFrom(reader, 1024) 141 common.Must(err) 142 if n != 1024 { 143 t.Error("expect reading 1024 bytes, but actually ", n) 144 } 145 146 if diff := cmp.Diff(payload, b.Bytes()); diff != "" { 147 t.Error(diff) 148 } 149 } 150 151 func BenchmarkNewBuffer(b *testing.B) { 152 for i := 0; i < b.N; i++ { 153 buffer := New() 154 buffer.Release() 155 } 156 } 157 158 func BenchmarkNewBufferStack(b *testing.B) { 159 for i := 0; i < b.N; i++ { 160 buffer := StackNew() 161 buffer.Release() 162 } 163 } 164 165 func BenchmarkWrite2(b *testing.B) { 166 buffer := New() 167 168 b.ResetTimer() 169 for i := 0; i < b.N; i++ { 170 _, _ = buffer.Write([]byte{'a', 'b'}) 171 buffer.Clear() 172 } 173 } 174 175 func BenchmarkWrite8(b *testing.B) { 176 buffer := New() 177 178 b.ResetTimer() 179 for i := 0; i < b.N; i++ { 180 _, _ = buffer.Write([]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}) 181 buffer.Clear() 182 } 183 } 184 185 func BenchmarkWrite32(b *testing.B) { 186 buffer := New() 187 payload := make([]byte, 32) 188 rand.Read(payload) 189 190 b.ResetTimer() 191 for i := 0; i < b.N; i++ { 192 _, _ = buffer.Write(payload) 193 buffer.Clear() 194 } 195 } 196 197 func BenchmarkWriteByte2(b *testing.B) { 198 buffer := New() 199 200 b.ResetTimer() 201 for i := 0; i < b.N; i++ { 202 _ = buffer.WriteByte('a') 203 _ = buffer.WriteByte('b') 204 buffer.Clear() 205 } 206 } 207 208 func BenchmarkWriteByte8(b *testing.B) { 209 buffer := New() 210 211 b.ResetTimer() 212 for i := 0; i < b.N; i++ { 213 _ = buffer.WriteByte('a') 214 _ = buffer.WriteByte('b') 215 _ = buffer.WriteByte('c') 216 _ = buffer.WriteByte('d') 217 _ = buffer.WriteByte('e') 218 _ = buffer.WriteByte('f') 219 _ = buffer.WriteByte('g') 220 _ = buffer.WriteByte('h') 221 buffer.Clear() 222 } 223 }