github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/ioutils/buffer_test.go (about)

     1  package ioutils
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestFixedBufferCap(t *testing.T) {
     9  	buf := &fixedBuffer{buf: make([]byte, 0, 5)}
    10  
    11  	n := buf.Cap()
    12  	if n != 5 {
    13  		t.Fatalf("expected buffer capacity to be 5 bytes, got %d", n)
    14  	}
    15  }
    16  
    17  func TestFixedBufferLen(t *testing.T) {
    18  	buf := &fixedBuffer{buf: make([]byte, 0, 10)}
    19  
    20  	buf.Write([]byte("hello"))
    21  	l := buf.Len()
    22  	if l != 5 {
    23  		t.Fatalf("expected buffer length to be 5 bytes, got %d", l)
    24  	}
    25  
    26  	buf.Write([]byte("world"))
    27  	l = buf.Len()
    28  	if l != 10 {
    29  		t.Fatalf("expected buffer length to be 10 bytes, got %d", l)
    30  	}
    31  
    32  	// read 5 bytes
    33  	b := make([]byte, 5)
    34  	buf.Read(b)
    35  
    36  	l = buf.Len()
    37  	if l != 5 {
    38  		t.Fatalf("expected buffer length to be 5 bytes, got %d", l)
    39  	}
    40  
    41  	n, err := buf.Write([]byte("i-wont-fit"))
    42  	if n != 0 {
    43  		t.Fatalf("expected no bytes to be written to buffer, got %d", n)
    44  	}
    45  	if err != errBufferFull {
    46  		t.Fatalf("expected errBufferFull, got %v", err)
    47  	}
    48  
    49  	l = buf.Len()
    50  	if l != 5 {
    51  		t.Fatalf("expected buffer length to still be 5 bytes, got %d", l)
    52  	}
    53  
    54  	buf.Reset()
    55  	l = buf.Len()
    56  	if l != 0 {
    57  		t.Fatalf("expected buffer length to still be 0 bytes, got %d", l)
    58  	}
    59  }
    60  
    61  func TestFixedBufferString(t *testing.T) {
    62  	buf := &fixedBuffer{buf: make([]byte, 0, 10)}
    63  
    64  	buf.Write([]byte("hello"))
    65  	buf.Write([]byte("world"))
    66  
    67  	out := buf.String()
    68  	if out != "helloworld" {
    69  		t.Fatalf("expected output to be \"helloworld\", got %q", out)
    70  	}
    71  
    72  	// read 5 bytes
    73  	b := make([]byte, 5)
    74  	buf.Read(b)
    75  
    76  	// test that fixedBuffer.String() only returns the part that hasn't been read
    77  	out = buf.String()
    78  	if out != "world" {
    79  		t.Fatalf("expected output to be \"world\", got %q", out)
    80  	}
    81  }
    82  
    83  func TestFixedBufferWrite(t *testing.T) {
    84  	buf := &fixedBuffer{buf: make([]byte, 0, 64)}
    85  	n, err := buf.Write([]byte("hello"))
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	if n != 5 {
    91  		t.Fatalf("expected 5 bytes written, got %d", n)
    92  	}
    93  
    94  	if string(buf.buf[:5]) != "hello" {
    95  		t.Fatalf("expected \"hello\", got %q", string(buf.buf[:5]))
    96  	}
    97  
    98  	n, err = buf.Write(bytes.Repeat([]byte{1}, 64))
    99  	if n != 59 {
   100  		t.Fatalf("expected 59 bytes written before buffer is full, got %d", n)
   101  	}
   102  	if err != errBufferFull {
   103  		t.Fatalf("expected errBufferFull, got %v - %v", err, buf.buf[:64])
   104  	}
   105  }
   106  
   107  func TestFixedBufferRead(t *testing.T) {
   108  	buf := &fixedBuffer{buf: make([]byte, 0, 64)}
   109  	if _, err := buf.Write([]byte("hello world")); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	b := make([]byte, 5)
   114  	n, err := buf.Read(b)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	if n != 5 {
   120  		t.Fatalf("expected 5 bytes read, got %d - %s", n, buf.String())
   121  	}
   122  
   123  	if string(b) != "hello" {
   124  		t.Fatalf("expected \"hello\", got %q", string(b))
   125  	}
   126  
   127  	n, err = buf.Read(b)
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  
   132  	if n != 5 {
   133  		t.Fatalf("expected 5 bytes read, got %d", n)
   134  	}
   135  
   136  	if string(b) != " worl" {
   137  		t.Fatalf("expected \" worl\", got %s", string(b))
   138  	}
   139  
   140  	b = b[:1]
   141  	n, err = buf.Read(b)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  
   146  	if n != 1 {
   147  		t.Fatalf("expected 1 byte read, got %d - %s", n, buf.String())
   148  	}
   149  
   150  	if string(b) != "d" {
   151  		t.Fatalf("expected \"d\", got %s", string(b))
   152  	}
   153  }