Пример #1
0
func testReencode(t *testing.T, codec pkm.Codec) {
	content := make([]byte, 256)
	for i := range content {
		content[i] = byte(i)
	}
	var decodedbuf bytes.Buffer
	var resultbuf bytes.Buffer

	n, err := codec.Decode(&decodedbuf, bytes.NewReader(content))
	if err != nil {
		t.Errorf("Re-encode test: Decode: unexpected error '%s'", err)
		return
	}
	decoded := decodedbuf.Bytes()[:n]
	n, err = codec.Encode(&resultbuf, bytes.NewReader(decoded))
	if err != nil {
		t.Errorf("Re-encode test: Encode: unexpected error '%s'", err)
		return
	}
	result := resultbuf.Bytes()[:n]
	if !bytes.Equal(content, result) {
		var n int
		if len(content) < len(result) {
			n = len(content)
		} else {
			n = len(result)
		}
		for i := 0; i < n; i++ {
			if result[i] != content[i] {
				t.Errorf("Re-encode test: result does not match original (@%d: 0x%02X != 0x%02X)", i, result[i], content[i])
				return
			}
		}
		t.Errorf("Re-encode test: result length does not match original length (%d != %d)", len(result), len(content))
	}
}
Пример #2
0
func testIO(t *testing.T, codec pkm.Codec, testName string, readerTmpl *dummyIO) {
	var reader *dummyIO
	var writer *dummyIO
	var n int
	var err error

	// Test codec.Decode.
	writer = &dummyIO{}
	reader = readerTmpl.Copy()
	n, err = codec.Decode(writer, reader)
	// Check if the correct number of bytes were returned.
	if writer.writeCalled && n != writer.writtenBytes {
		t.Errorf("test writer decode: '%s' failed: expected %d bytes written, got %d", testName, writer.writtenBytes, n)
	} else if !writer.writeCalled && n != 0 {
		t.Errorf("test writer decode: '%s' failed: expected %d bytes written, got %d", testName, 0, n)
	}
	// Check if an error expected from the reader is returned.
	if reader.err != nil && err == nil {
		t.Errorf("test reader decode: '%s' failed: expected error", testName)
	} else if reader.err == nil && err != nil {
		t.Errorf("test reader decode: '%s' failed: expected no error, got '%s'", testName, err)
	}

	// Test codec.Encode.
	writer = &dummyIO{}
	reader = readerTmpl.Copy()
	n, err = codec.Encode(writer, reader)
	// Check if the correct number of bytes were returned.
	if writer.writeCalled && n != writer.writtenBytes {
		t.Errorf("test writer encode: '%s' failed: expected %d bytes written, got %d", testName, writer.writtenBytes, n)
	} else if !writer.writeCalled && n != 0 {
		t.Errorf("test writer encode: '%s' failed: expected %d bytes written, got %d", testName, 0, n)
	}
	// Check if an error expected from the reader is returned.
	if reader.err != nil && err == nil {
		t.Errorf("test reader decode: '%s' failed: expected error", testName)
	} else if reader.err == nil && err != nil {
		t.Errorf("test reader decode: '%s' failed: expected no error, got '%s'", testName, err)
	}

	// Test codec.Decode with an erroring writer.
	writer = &dummyIO{err: errors.New("writer error")}
	reader = readerTmpl.Copy()
	n, err = codec.Decode(writer, reader)
	// Check if the correct number of bytes were returned.
	if writer.writeCalled && n != writer.writtenBytes {
		t.Errorf("test error writer decode: '%s' failed: expected %d bytes written, got %d", testName, writer.writtenBytes, n)
	} else if !writer.writeCalled && n != 0 {
		t.Errorf("test error writer decode: '%s' failed: expected %d bytes written, got %d", testName, 0, n)
	}
	// Check if an error expected from the writer is returned.
	if reader.err == nil && writer.writeCalled && err == nil {
		t.Errorf("test error writer decode: '%s' failed: expected error", testName)
	} else if reader.err != nil && writer.writeCalled && err == nil {
		t.Errorf("test error writer decode: '%s' failed: expected reader error", testName)
	}

	// Test codec.Encode with an erroring writer.
	writer = &dummyIO{err: errors.New("writer error")}
	reader = readerTmpl.Copy()
	n, err = codec.Encode(writer, reader)
	// Check if the correct number of bytes were returned.
	if writer.writeCalled && n != writer.writtenBytes {
		t.Errorf("test error writer encode: '%s' failed: expected %d bytes written, got %d", testName, writer.writtenBytes, n)
	} else if !writer.writeCalled && n != 0 {
		t.Errorf("test error writer encode: '%s' failed: expected %d bytes written, got %d", testName, 0, n)
	}
	// Check if an error expected from the writer is returned.
	if reader.err == nil && writer.writeCalled && err == nil {
		t.Errorf("test error writer encode: '%s' failed: expected error", testName)
	} else if reader.err != nil && writer.writeCalled && err == nil {
		t.Errorf("test error writer encode: '%s' failed: expected reader error", testName)
	}
}