mirror of
https://github.com/pion/mediadevices.git
synced 2025-10-05 08:36:55 +08:00
Fix wrong required size
This commit is contained in:
@@ -4,7 +4,7 @@ package io
|
|||||||
// InsufficientBufferError.
|
// InsufficientBufferError.
|
||||||
func Copy(dst, src []byte) (n int, err error) {
|
func Copy(dst, src []byte) (n int, err error) {
|
||||||
if len(dst) < len(src) {
|
if len(dst) < len(src) {
|
||||||
return 0, &InsufficientBufferError{len(dst)}
|
return 0, &InsufficientBufferError{len(src)}
|
||||||
}
|
}
|
||||||
|
|
||||||
return copy(dst, src), nil
|
return copy(dst, src), nil
|
||||||
|
45
pkg/io/io_test.go
Normal file
45
pkg/io/io_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package io
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCopy(t *testing.T) {
|
||||||
|
var dst []byte
|
||||||
|
src := make([]byte, 4)
|
||||||
|
|
||||||
|
n, err := Copy(dst, src)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected err to be non-nill")
|
||||||
|
}
|
||||||
|
|
||||||
|
if n != 0 {
|
||||||
|
t.Fatalf("expected n to be 0, but got %d", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
e, ok := err.(*InsufficientBufferError)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected error to be InsufficientBufferError")
|
||||||
|
}
|
||||||
|
|
||||||
|
if e.RequiredSize != len(src) {
|
||||||
|
t.Fatalf("expected required size to be %d, but got %d", len(src), e.RequiredSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
dst = make([]byte, 2*e.RequiredSize)
|
||||||
|
n, err = Copy(dst, src)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected to not get an error after expanding the buffer")
|
||||||
|
}
|
||||||
|
|
||||||
|
if n != len(src) {
|
||||||
|
t.Fatalf("expected n to be %d, but got %d", len(src), n)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(src); i++ {
|
||||||
|
if src[i] != dst[i] {
|
||||||
|
log.Fatalf("expected value at %d to be %d, but got %d", i, src[i], dst[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user