Esempio n. 1
0
func TestResize(t *testing.T) {
	buffer := buffer.New()
	buffer.Write([]byte{1, 2, 3})
	buffer.Seek(1, 0)

	buffer.Write([]byte{7, 7, 7})

	if buffer.Size != 4 {
		t.Errorf("Should have resized to 4, but size is now %d", buffer.Size)
	}

	buf := make([]byte, 4)
	n, _ := buffer.Read(buf)
	if n != 0 {
		t.Errorf("Pointer was at the end, shouldn't have read anything but had %d bytes returned", n)
	}

	buffer.Seek(1, 0)
	n, _ = buffer.Read(buf)
	if n != 3 {
		t.Errorf("Should have read 4, but returned %d", n)
	}

	if string(buf) != string([]byte{7, 7, 7, 0}) {
		t.Errorf("Should have read 7 7 7 0, but got %s", buf)
	}
}
Esempio n. 2
0
func TestNoResize(t *testing.T) {
	buffer := buffer.NewWithSize(4, true)

	n, _ := buffer.Write([]byte{1, 2, 3, 4, 5, 6})
	if n != 4 {
		t.Errorf("Should have wrote 4, but wrote %d", n)
	}
	if buffer.Pointer != 4 {
		t.Errorf("Pointer should be at 4, but got %d", buffer.Pointer)
	}

	if buffer.Size != 4 {
		t.Errorf("Shouldn't have resized, but resized to %d", buffer.Size)
	}

	buffer.Seek(0, 0)

	buf := make([]byte, 10)
	n, _ = buffer.Read(buf)
	if n != 4 {
		t.Errorf("Should have read 4, but got %d", n)
	}

	if string(buf) != string([]byte{1, 2, 3, 4, 0, 0, 0, 0, 0, 0}) {
		t.Errorf("Didn't read what was written...")
	}
}