Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

💄 fix toggle typo #57

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions byteslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ func Set(data, setData []byte) ([]byte, error) {
return result, nil
}

// Toogle apply XOR operation on a byte slice with an "toogle" byte slice (must have the same size).
func Toogle(data, toogleData []byte) ([]byte, error) {
// Toggle apply XOR operation on a byte slice with an "toggle" byte slice (must have the same size).
func Toggle(data, toggleData []byte) ([]byte, error) {
dataLength := len(data)
toogleDataLength := len(toogleData)
if dataLength != toogleDataLength {
return nil, errors.New("data and toogleData must have the same size")
toggleDataLength := len(toggleData)
if dataLength != toggleDataLength {
return nil, errors.New("data and toggleData must have the same size")
}

result := make([]byte, dataLength)
for i := 0; i < dataLength; i++ {
result[i] = data[i] ^ toogleData[i]
result[i] = data[i] ^ toggleData[i]
}
return result, nil
}
Expand Down
12 changes: 6 additions & 6 deletions byteslice_bigendian.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ func LSet(data, setData []byte) []byte {
return result
}

// LToogle apply XOR operation on a byte slice with an "toogle" byte slice using big endian order.
func LToogle(data, toogleData []byte) []byte {
// LToggle apply XOR operation on a byte slice with an "toggle" byte slice using big endian order.
func LToggle(data, toggleData []byte) []byte {
dataLength := len(data)
toogleDataLength := len(toogleData)
toggleDataLength := len(toggleData)

operationLength := dataLength
if toogleDataLength > dataLength {
operationLength = toogleDataLength
if toggleDataLength > dataLength {
operationLength = toggleDataLength
}

result, _ := Toogle(RPad(data, operationLength, 0x00), RPad(toogleData, operationLength, 0x00))
result, _ := Toggle(RPad(data, operationLength, 0x00), RPad(toggleData, operationLength, 0x00))
return result
}

Expand Down
24 changes: 12 additions & 12 deletions byteslice_bigendian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,41 @@ func BenchmarkLSet(b *testing.B) {
}
}

var tcLToogle = []struct {
var tcLToggle = []struct {
name string
data []byte
toogleData []byte
toggleData []byte
result []byte
}{
{"equal length slices", []byte{0xDA, 0x99, 0xBA}, []byte{0xAD, 0x11, 0xAB}, []byte{0x77, 0x88, 0x11}},
{"data longer", []byte{0xAD, 0x11, 0xAB, 0x77, 0x88, 0x11}, []byte{0xDA, 0x99, 0xBA}, []byte{0x77, 0x88, 0x11, 0x77, 0x88, 0x11}},
{"toogleData longer", []byte{0xDA, 0x99, 0xBA}, []byte{0xAD, 0x11, 0xAB, 0x77, 0x88, 0x11}, []byte{0x77, 0x88, 0x11, 0x77, 0x88, 0x11}},
{"toggleData longer", []byte{0xDA, 0x99, 0xBA}, []byte{0xAD, 0x11, 0xAB, 0x77, 0x88, 0x11}, []byte{0x77, 0x88, 0x11, 0x77, 0x88, 0x11}},
{"data empty", []byte{}, []byte{0xAD, 0x11, 0xAB}, []byte{0xAD, 0x11, 0xAB}},
{"toogleData empty", []byte{0xDA, 0x99, 0xBA}, []byte{}, []byte{0xDA, 0x99, 0xBA}},
{"toggleData empty", []byte{0xDA, 0x99, 0xBA}, []byte{}, []byte{0xDA, 0x99, 0xBA}},
{"empty slices", []byte{}, []byte{}, []byte{}},
}

func TestLToogle(t *testing.T) {
func TestLToggle(t *testing.T) {
var val []byte
for _, tc := range tcLToogle {
for _, tc := range tcLToggle {
t.Run(tc.name, func(t *testing.T) {
val = LToogle(tc.data, tc.toogleData)
val = LToggle(tc.data, tc.toggleData)
if !reflect.DeepEqual(val, tc.result) {
t.Errorf("LToogle(%x, %x) was %x, should be %x",
tc.data, tc.toogleData,
t.Errorf("LToggle(%x, %x) was %x, should be %x",
tc.data, tc.toggleData,
val,
tc.result)
}
})
}
}

func BenchmarkLToogle(b *testing.B) {
func BenchmarkLToggle(b *testing.B) {
var val []byte
for _, tc := range tcLToogle {
for _, tc := range tcLToggle {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
val = LToogle(tc.data, tc.toogleData)
val = LToggle(tc.data, tc.toggleData)
}
})
}
Expand Down
12 changes: 6 additions & 6 deletions byteslice_littleendian.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ func RSet(data, setData []byte) []byte {
return result
}

// RToogle apply XOR operation on a byte slice with an "toogle" byte slice using little endian order.
func RToogle(data, toogleData []byte) []byte {
// RToggle apply XOR operation on a byte slice with an "toggle" byte slice using little endian order.
func RToggle(data, toggleData []byte) []byte {
dataLength := len(data)
toogleDataLength := len(toogleData)
toggleDataLength := len(toggleData)

operationLength := dataLength
if toogleDataLength > dataLength {
operationLength = toogleDataLength
if toggleDataLength > dataLength {
operationLength = toggleDataLength
}

result, _ := Toogle(LPad(data, operationLength, 0x00), LPad(toogleData, operationLength, 0x00))
result, _ := Toggle(LPad(data, operationLength, 0x00), LPad(toggleData, operationLength, 0x00))
return result
}

Expand Down
24 changes: 12 additions & 12 deletions byteslice_littleendian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,41 @@ func BenchmarkRSet(b *testing.B) {
}
}

var tcRToogle = []struct {
var tcRToggle = []struct {
name string
data []byte
toogleData []byte
toggleData []byte
result []byte
}{
{"equal length slices", []byte{0xDA, 0x99, 0xBA}, []byte{0xAD, 0x11, 0xAB}, []byte{0x77, 0x88, 0x11}},
{"data longer", []byte{0x77, 0x88, 0x11, 0xAD, 0x11, 0xAB}, []byte{0xDA, 0x99, 0xBA}, []byte{0x77, 0x88, 0x11, 0x77, 0x88, 0x11}},
{"toogleData longer", []byte{0xDA, 0x99, 0xBA}, []byte{0x77, 0x88, 0x11, 0xAD, 0x11, 0xAB}, []byte{0x77, 0x88, 0x11, 0x77, 0x88, 0x11}},
{"toggleData longer", []byte{0xDA, 0x99, 0xBA}, []byte{0x77, 0x88, 0x11, 0xAD, 0x11, 0xAB}, []byte{0x77, 0x88, 0x11, 0x77, 0x88, 0x11}},
{"data empty", []byte{}, []byte{0xAD, 0x11, 0xAB}, []byte{0xAD, 0x11, 0xAB}},
{"toogleData empty", []byte{0xDA, 0x99, 0xBA}, []byte{}, []byte{0xDA, 0x99, 0xBA}},
{"toggleData empty", []byte{0xDA, 0x99, 0xBA}, []byte{}, []byte{0xDA, 0x99, 0xBA}},
{"empty slices", []byte{}, []byte{}, []byte{}},
}

func TestRToogle(t *testing.T) {
func TestRToggle(t *testing.T) {
var val []byte
for _, tc := range tcRToogle {
for _, tc := range tcRToggle {
t.Run(tc.name, func(t *testing.T) {
val = RToogle(tc.data, tc.toogleData)
val = RToggle(tc.data, tc.toggleData)
if !reflect.DeepEqual(val, tc.result) {
t.Errorf("RToogle(%x, %x) was %x, should be %x",
tc.data, tc.toogleData,
t.Errorf("RToggle(%x, %x) was %x, should be %x",
tc.data, tc.toggleData,
val,
tc.result)
}
})
}
}

func BenchmarkRToogle(b *testing.B) {
func BenchmarkRToggle(b *testing.B) {
var val []byte
for _, tc := range tcRToogle {
for _, tc := range tcRToggle {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
val = RToogle(tc.data, tc.toogleData)
val = RToggle(tc.data, tc.toggleData)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions byteslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ func TestSetError(t *testing.T) {
}
}

func TestToogleError(t *testing.T) {
val, err := Toogle([]byte{0x00, 0x00}, []byte{0x00})
func TestToggleError(t *testing.T) {
val, err := Toggle([]byte{0x00, 0x00}, []byte{0x00})
if err == nil || val != nil {
t.Errorf("Toogle with two byte slices of different size needs to return an error and no value")
t.Errorf("Toggle with two byte slices of different size needs to return an error and no value")
}
}

Expand Down