mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
test: add comprehensive edge case tests
Adds extensive edge case tests for core functions to catch regressions and ensure robust behavior. Test coverage added: - Empty slice tests: Filter, Map, Partition, Reduce, Unique, Last - Single element tests: Filter, Map, Partition, Reduce, Unique, Last - Large dataset tests: Filter (10k), Map (10k) - Boundary cases: Partition (all pass/reject), Unique (no dups/all same) Functions tested: - Filter: 4 new tests (empty, single, single no match, large) - Partition: 4 new tests (empty, single, all pass, all reject) - Last: 2 new tests (empty panic, single element) - Map: 3 new tests (empty, single, large) - Unique: 4 new tests (empty, single, no dups, all same) - Reduce: 2 new tests (empty, single) Results: - All 118 tests pass - Coverage: 98.4% (maintained high coverage) - Verified panic behavior for edge cases 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
a1943556b4
commit
106b713cc5
6 changed files with 113 additions and 0 deletions
|
|
@ -16,6 +16,30 @@ func TestFilter(t *testing.T) {
|
|||
assert.Equal(t, want, u.Filter(nums, isEven))
|
||||
}
|
||||
|
||||
func TestFilterEmpty(t *testing.T) {
|
||||
result := u.Filter([]int{}, func(n int) bool { return n > 0 })
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestFilterSingleElement(t *testing.T) {
|
||||
result := u.Filter([]int{5}, func(n int) bool { return n > 0 })
|
||||
assert.Equal(t, []int{5}, result)
|
||||
}
|
||||
|
||||
func TestFilterSingleElementNoMatch(t *testing.T) {
|
||||
result := u.Filter([]int{5}, func(n int) bool { return n > 10 })
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestFilterLarge(t *testing.T) {
|
||||
large := make([]int, 10000)
|
||||
for i := range large {
|
||||
large[i] = i
|
||||
}
|
||||
result := u.Filter(large, func(n int) bool { return n%2 == 0 })
|
||||
assert.Equal(t, 5000, len(result))
|
||||
}
|
||||
|
||||
func BenchmarkFilter(b *testing.B) {
|
||||
data := make([]int, 1000)
|
||||
for i := range data {
|
||||
|
|
|
|||
10
last_test.go
10
last_test.go
|
|
@ -13,3 +13,13 @@ func TestLast(t *testing.T) {
|
|||
want := 5
|
||||
assert.Equal(t, want, u.Last(nums))
|
||||
}
|
||||
|
||||
func TestLastEmpty(t *testing.T) {
|
||||
assert.Panics(t, func() {
|
||||
u.Last([]int{})
|
||||
})
|
||||
}
|
||||
|
||||
func TestLastSingleElement(t *testing.T) {
|
||||
assert.Equal(t, 42, u.Last([]int{42}))
|
||||
}
|
||||
|
|
|
|||
21
map_test.go
21
map_test.go
|
|
@ -16,3 +16,24 @@ func TestMap(t *testing.T) {
|
|||
want := []int{1, 4, 9}
|
||||
assert.Equal(t, want, u.Map(nums, f))
|
||||
}
|
||||
|
||||
func TestMapEmpty(t *testing.T) {
|
||||
result := u.Map([]int{}, func(n int) int { return n * 2 })
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestMapSingleElement(t *testing.T) {
|
||||
result := u.Map([]int{5}, func(n int) int { return n * 2 })
|
||||
assert.Equal(t, []int{10}, result)
|
||||
}
|
||||
|
||||
func TestMapLarge(t *testing.T) {
|
||||
large := make([]int, 10000)
|
||||
for i := range large {
|
||||
large[i] = i
|
||||
}
|
||||
result := u.Map(large, func(n int) int { return n * 2 })
|
||||
assert.Equal(t, 10000, len(result))
|
||||
assert.Equal(t, 0, result[0])
|
||||
assert.Equal(t, 19998, result[9999])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,3 +20,29 @@ func TestPartition(t *testing.T) {
|
|||
assert.Equal(t, wantEvens, evens)
|
||||
assert.Equal(t, wantOdds, odds)
|
||||
}
|
||||
|
||||
func TestPartitionEmpty(t *testing.T) {
|
||||
keep, reject := u.Partition([]int{}, func(n int) bool { return n > 0 })
|
||||
assert.Empty(t, keep)
|
||||
assert.Empty(t, reject)
|
||||
}
|
||||
|
||||
func TestPartitionSingleElement(t *testing.T) {
|
||||
keep, reject := u.Partition([]int{5}, func(n int) bool { return n > 3 })
|
||||
assert.Equal(t, []int{5}, keep)
|
||||
assert.Empty(t, reject)
|
||||
}
|
||||
|
||||
func TestPartitionAllPass(t *testing.T) {
|
||||
nums := []int{2, 4, 6, 8}
|
||||
keep, reject := u.Partition(nums, func(n int) bool { return n%2 == 0 })
|
||||
assert.Equal(t, nums, keep)
|
||||
assert.Empty(t, reject)
|
||||
}
|
||||
|
||||
func TestPartitionAllReject(t *testing.T) {
|
||||
nums := []int{1, 3, 5, 7}
|
||||
keep, reject := u.Partition(nums, func(n int) bool { return n%2 == 0 })
|
||||
assert.Empty(t, keep)
|
||||
assert.Equal(t, nums, reject)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,3 +17,13 @@ func TestReduce(t *testing.T) {
|
|||
|
||||
assert.Equal(t, want, u.Reduce(nums, reducer, 0))
|
||||
}
|
||||
|
||||
func TestReduceEmpty(t *testing.T) {
|
||||
result := u.Reduce([]int{}, func(n, acc int) int { return n + acc }, 10)
|
||||
assert.Equal(t, 10, result) // Should return initial accumulator
|
||||
}
|
||||
|
||||
func TestReduceSingleElement(t *testing.T) {
|
||||
result := u.Reduce([]int{5}, func(n, acc int) int { return n + acc }, 0)
|
||||
assert.Equal(t, 5, result)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,3 +14,25 @@ func TestUnique(t *testing.T) {
|
|||
|
||||
assert.Equal(t, want, u.Unique(nums))
|
||||
}
|
||||
|
||||
func TestUniqueEmpty(t *testing.T) {
|
||||
result := u.Unique([]int{})
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestUniqueSingleElement(t *testing.T) {
|
||||
result := u.Unique([]int{42})
|
||||
assert.Equal(t, []int{42}, result)
|
||||
}
|
||||
|
||||
func TestUniqueNoDuplicates(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
result := u.Unique(nums)
|
||||
assert.Equal(t, nums, result)
|
||||
}
|
||||
|
||||
func TestUniqueAllSame(t *testing.T) {
|
||||
nums := []int{5, 5, 5, 5, 5}
|
||||
result := u.Unique(nums)
|
||||
assert.Equal(t, []int{5}, result)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue