Skip to content

Commit

Permalink
codec: remove GetBytes() from Codec, clean up test
Browse files Browse the repository at this point in the history
  • Loading branch information
willgreenberg committed Jul 22, 2019
1 parent 1a2ed2b commit befe473
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
16 changes: 0 additions & 16 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package galaxycache
type Codec interface {
MarshalBinary() ([]byte, error)
UnmarshalBinary(data []byte) error
GetBytes() []byte // for testing
}

// Note: to ensure that unmarshaling is a read-only operation, bytes
Expand All @@ -47,11 +46,6 @@ func (c *ByteCodec) UnmarshalBinary(data []byte) error {
return nil
}

// GetBytes returns the underlying byte slice of ByteCodec
func (c *ByteCodec) GetBytes() []byte {
return *c
}

// CopyingByteCodec is a byte slice type that implements Codec
// and returns a copy of the bytes when marshaled
type CopyingByteCodec []byte
Expand All @@ -68,11 +62,6 @@ func (c *CopyingByteCodec) UnmarshalBinary(data []byte) error {
return nil
}

// GetBytes returns the underlying byte slice of CopyingByteCodec
func (c *CopyingByteCodec) GetBytes() []byte {
return *c
}

// StringCodec is a string type that implements Codec
type StringCodec string

Expand All @@ -88,8 +77,3 @@ func (c *StringCodec) UnmarshalBinary(data []byte) error {
*c = StringCodec(cloneBytes(data))
return nil
}

// GetBytes returns the underlying byte slice of StringCodec
func (c *StringCodec) GetBytes() []byte {
return []byte(*c)
}
46 changes: 24 additions & 22 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,35 @@ limitations under the License.

package galaxycache

import "testing"
import (
"bytes"
"testing"
)

const testBytes = "some bytes"

func TestCodec(t *testing.T) {
var byteCodec ByteCodec
var copyingByteCodec CopyingByteCodec
var stringCodec StringCodec

testCases := []struct {
testName string
codec Codec
testName string
codec Codec
checkCopy bool
}{
{
testName: "ByteCodec",
codec: &byteCodec,
testName: "ByteCodec",
codec: &ByteCodec{},
checkCopy: false,
},
{
testName: "CopyingByteCodec",
codec: &copyingByteCodec,
testName: "CopyingByteCodec",
codec: &CopyingByteCodec{},
checkCopy: true,
},
{
testName: "StringCodec",
codec: &stringCodec,
testName: "StringCodec",
codec: &stringCodec,
checkCopy: true,
},
}

Expand All @@ -48,24 +53,21 @@ func TestCodec(t *testing.T) {
inBytes := []byte(testBytes)
tc.codec.UnmarshalBinary(inBytes)
inBytes[0] = 'a' // change the original byte slice to ensure copy was made
if string(tc.codec.GetBytes()) != testBytes {
t.Errorf("UnmarshalBinary resulted in %q; want %q", tc.codec, testBytes)
}

marshaledBytes, err := tc.codec.MarshalBinary()
if err != nil {
t.Errorf("Error marshaling from byteCodec: %s", err)
}
if string(marshaledBytes) != testBytes {
t.Errorf("MarshalBinary resulted in %q; want %q", marshaledBytes, testBytes)
}
if &inBytes[0] == &marshaledBytes[0] {
t.Errorf("inBytes and marshaledBytes share memory")
t.Errorf("Unmarshal/Marshal resulted in %q; want %q", marshaledBytes, testBytes)
}

if tc.testName == "CopyingByteCodec" {
codecBytes := tc.codec.GetBytes()
if &marshaledBytes[0] == &codecBytes[0] {
if tc.checkCopy {
marshaledBytes[0] = 'a'
secondMarshaledBytes, errM := tc.codec.MarshalBinary()
if errM != nil {
t.Errorf("Error marshaling from byteCodec: %s", errM)
}
if bytes.Equal(marshaledBytes, secondMarshaledBytes) {
t.Errorf("Marshaling did not copy the bytes")
}
}
Expand Down

0 comments on commit befe473

Please sign in to comment.