-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0207d48
commit a70778a
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package utils | ||
|
||
// InsertInSlice inserts a value into a slice at a given position | ||
// | ||
// If the position gives is out of bounds then it resorts to inserting to the end of the slice | ||
func InsertInSlice[T any](in []T, valueToAdd T, index int) []T { | ||
if index > len(in) { | ||
index = len(in) | ||
} | ||
return append(in[:index], append([]T{valueToAdd}, in[index:]...)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils_test | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/internal/assert" | ||
"github.com/aidenwallis/go-utils/utils" | ||
) | ||
|
||
func TestInsertInSlice(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
in []int | ||
out []int | ||
valueToAdd int | ||
index int | ||
}{ | ||
"normal operation": { | ||
in: []int{0, 1, 2, 3, 4}, | ||
out: []int{0, 1, 2000, 2, 3, 4}, | ||
valueToAdd: 2000, | ||
index: 2, | ||
}, | ||
|
||
"out of bounds": { | ||
in: []int{0, 1, 2, 3}, | ||
out: []int{0, 1, 2, 3, 2000}, | ||
valueToAdd: 2000, | ||
index: 2000, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
result := utils.InsertInSlice(testCase.in, testCase.valueToAdd, testCase.index) | ||
log.Println(result) | ||
|
||
assert.Equal(t, len(testCase.out), len(result)) | ||
for i, v := range result { | ||
assert.Equal(t, testCase.out[i], v) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package utils | ||
|
||
// MoveInSlice moves a value within the slice to a specified new position | ||
// | ||
// If either the old or new position is out of bounds, the existing slice contents is returned. | ||
func MoveInSlice[T any](in []T, oldPosition, newPosition int) []T { | ||
if oldPosition >= len(in) { | ||
return in | ||
} | ||
value := in[oldPosition] | ||
return InsertInSlice(RemoveFromSlice(in, oldPosition), value, newPosition) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/internal/assert" | ||
"github.com/aidenwallis/go-utils/utils" | ||
) | ||
|
||
func TestMoveInSlice(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
in []int | ||
out []int | ||
oldPosition int | ||
newPosition int | ||
}{ | ||
"normal operation": { | ||
in: []int{0, 1, 2, 3, 4}, | ||
out: []int{0, 3, 1, 2, 4}, | ||
oldPosition: 3, | ||
newPosition: 1, | ||
}, | ||
|
||
"out of bounds in old position": { | ||
in: []int{0, 1, 2, 3, 4}, | ||
out: []int{0, 1, 2, 3, 4}, | ||
oldPosition: 5, | ||
newPosition: 0, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
result := utils.MoveInSlice(testCase.in, testCase.oldPosition, testCase.newPosition) | ||
|
||
assert.Equal(t, len(testCase.out), len(result)) | ||
for i, v := range result { | ||
assert.Equal(t, testCase.out[i], v) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package utils | ||
|
||
// RemoveFromSlice removes a value from slice at a given index | ||
func RemoveFromSlice[T any](in []T, index int) []T { | ||
if index >= len(in) { | ||
return in | ||
} | ||
return append(in[:index], in[index+1:]...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/internal/assert" | ||
"github.com/aidenwallis/go-utils/utils" | ||
) | ||
|
||
func TestRemoveFromSlice(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
in []int | ||
out []int | ||
indexToRemove int | ||
}{ | ||
"normal operation": { | ||
in: []int{0, 1, 2, 3}, | ||
out: []int{0, 1, 3}, | ||
indexToRemove: 2, | ||
}, | ||
|
||
"does not panic when out of bounds": { | ||
in: []int{0, 1, 2}, | ||
out: []int{0, 1, 2}, | ||
indexToRemove: 4, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
result := utils.RemoveFromSlice(testCase.in, testCase.indexToRemove) | ||
assert.Equal(t, len(testCase.out), len(result)) | ||
for i, v := range result { | ||
assert.Equal(t, testCase.out[i], v) | ||
} | ||
}) | ||
} | ||
} |