diff --git a/codec.go b/codec.go index 9d9e9c1f..6d83c001 100644 --- a/codec.go +++ b/codec.go @@ -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 @@ -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 @@ -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 @@ -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) -} diff --git a/codec_test.go b/codec_test.go index aca79b6d..dcbd6652 100644 --- a/codec_test.go +++ b/codec_test.go @@ -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: ©ingByteCodec, + testName: "CopyingByteCodec", + codec: &CopyingByteCodec{}, + checkCopy: true, }, { - testName: "StringCodec", - codec: &stringCodec, + testName: "StringCodec", + codec: &stringCodec, + checkCopy: true, }, } @@ -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") } }