diff --git a/setutil.go b/setutil.go index 3e8c01dd..2fe81514 100644 --- a/setutil.go +++ b/setutil.go @@ -14,6 +14,7 @@ func equal(a, b []uint16) bool { func difference(set1 []uint16, set2 []uint16, buffer []uint16) int { if 0 == len(set2) { + buffer = buffer[:len(set1)] for k := 0; k < len(set1); k++ { buffer[k] = set1[k] } diff --git a/setutil_test.go b/setutil_test.go index b037e02a..de5fee77 100644 --- a/setutil_test.go +++ b/setutil_test.go @@ -22,6 +22,23 @@ func TestSetUtilDifference(t *testing.T) { result = result[:nl] assert.Equal(t, expectedresult, result) + + // empty set2 + data2 = []uint16{} + expectedresult = []uint16{0, 1, 2, 3, 4, 9} + nl = difference(data1, data2, result) + result = result[:nl] + + assert.Equal(t, expectedresult, result) + + // empty set 1 + data1 = []uint16{} + data2 = []uint16{2, 3, 4, 5, 8, 9, 11} + expectedresult = []uint16{} + nl = difference(data1, data2, result) + result = result[:nl] + + assert.Equal(t, expectedresult, result) } func TestSetUtilUnion(t *testing.T) {