-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
17 changed files
with
787 additions
and
578 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
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
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
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
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,20 @@ | ||
package internal | ||
|
||
import "encoding/json" | ||
|
||
func EncodedSize[T any](obj T) int { | ||
enc, err := json.Marshal(obj) | ||
if err != nil { | ||
return 0 | ||
} | ||
return len(enc) | ||
} | ||
|
||
func RemoveIthElement[T any](slice []T, i int) []T { | ||
if i < 0 || i >= len(slice) { | ||
return slice // Return the original slice if index is out of bounds | ||
} | ||
newSlice := make([]T, 0, len(slice)-1) | ||
newSlice = append(newSlice, slice[:i]...) | ||
return append(newSlice, slice[i+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,31 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRemoveIthElement(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
slice []int | ||
index int | ||
expected []int | ||
}{ | ||
{"Remove middle element", []int{1, 2, 3, 4, 5}, 2, []int{1, 2, 4, 5}}, | ||
{"Remove first element", []int{1, 2, 3, 4, 5}, 0, []int{2, 3, 4, 5}}, | ||
{"Remove last element", []int{1, 2, 3, 4, 5}, 4, []int{1, 2, 3, 4}}, | ||
{"Index out of bounds (negative)", []int{1, 2, 3, 4, 5}, -1, []int{1, 2, 3, 4, 5}}, | ||
{"Index out of bounds (too large)", []int{1, 2, 3, 4, 5}, 5, []int{1, 2, 3, 4, 5}}, | ||
{"Single element slice", []int{1}, 0, []int{}}, | ||
{"Empty slice", []int{}, 0, []int{}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := RemoveIthElement(tt.slice, tt.index) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.