finishing initial set of tests

main
RageCage64 2 years ago
parent 337e85e173
commit fdf625a5a7

@ -48,7 +48,7 @@ func (head *LLNode[T]) Range() []T {
return result return result
} }
func (head *LLNode[T]) AddToEnd(val T) *LLNode[T] { func (head *LLNode[T]) AddToTail(val T) *LLNode[T] {
curr := head curr := head
for curr.Next != nil { for curr.Next != nil {
curr = curr.Next curr = curr.Next
@ -58,7 +58,7 @@ func (head *LLNode[T]) AddToEnd(val T) *LLNode[T] {
return node return node
} }
func (head *LLNode[T]) AddToStart(val T) *LLNode[T] { func (head *LLNode[T]) AddToHead(val T) *LLNode[T] {
node := &LLNode[T]{Value: val, Next: head} node := &LLNode[T]{Value: val, Next: head}
head.Prev = node head.Prev = node
return node return node
@ -82,7 +82,7 @@ func (q *Queue[T]) Enqueue(val T) {
if q.Head == nil { if q.Head == nil {
q.Head = &LLNode[T]{Value: val} q.Head = &LLNode[T]{Value: val}
} else { } else {
q.Head.AddToEnd(val) q.Head.AddToTail(val)
} }
} }
@ -106,7 +106,7 @@ func (s *Stack[T]) Push(val T) {
if s.Head == nil { if s.Head == nil {
s.Head = &LLNode[T]{Value: val} s.Head = &LLNode[T]{Value: val}
} else { } else {
node := s.Head.AddToStart(val) node := s.Head.AddToHead(val)
s.Head = node s.Head = node
} }
} }

@ -0,0 +1,71 @@
// Copyright (c) 2023 Braydon Kains
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package collections_test
import (
"testing"
"github.com/RageCage64/collections-go"
"github.com/RageCage64/go-assert"
)
func TestLinkedList(t *testing.T) {
head := &collections.LLNode[int]{Value: 1}
head.AddToTail(2)
head.AddToTail(3)
llSlice := head.Range()
assert.SliceEqual(t, []int{1, 2, 3}, llSlice)
head = head.AddToHead(0)
llSlice = head.Range()
assert.SliceEqual(t, []int{0, 1, 2, 3}, llSlice)
head.Next.RemoveSelf()
llSlice = head.Range()
assert.SliceEqual(t, []int{0, 2, 3}, llSlice)
}
func TestQueue(t *testing.T) {
q := &collections.Queue[int]{}
q.Enqueue(1)
q.Enqueue(2)
q.Enqueue(3)
assert.Equal(t, 3, q.Head.Len())
for i := 1; i <= 3; i++ {
x, err := q.Dequeue()
assert.NilErr(t, err)
assert.Equal(t, i, x)
}
}
func TestStack(t *testing.T) {
q := &collections.Stack[int]{}
q.Push(1)
q.Push(2)
q.Push(3)
assert.Equal(t, 3, q.Head.Len())
for i := 3; i >= 1; i-- {
x, err := q.Pop()
assert.NilErr(t, err)
assert.Equal(t, i, x)
}
}

@ -33,7 +33,7 @@ type UnaryOperator[T any] func(T) T
// Reducer is a function that takes an accumulator and the current // Reducer is a function that takes an accumulator and the current
// element of the slice. // element of the slice.
type Reducer[Acc any, T any] func(accumulator Acc, current T) type Reducer[Acc any, T any] func(accumulator Acc, current T) Acc
/***************** /*****************
The alias API, which allows for receiver-style calling convention. The alias API, which allows for receiver-style calling convention.
@ -191,7 +191,7 @@ func SliceMap[T any](sl []T, op UnaryOperator[T]) []T {
// Run a predicate on every element in the slice, and return a slice // Run a predicate on every element in the slice, and return a slice
// that contains every element for which the predicate was true. // that contains every element for which the predicate was true.
// //
// Sometimes known by other names: Where, // Sometimes known by other names: Where
// //
// Time Complexity: O(n * m) (where m = complexity of predicate) // Time Complexity: O(n * m) (where m = complexity of predicate)
// Space Complexity: O(n) // Space Complexity: O(n)
@ -224,10 +224,10 @@ func SliceFilter[T any](sl []T, pred UnaryPredicate[T]) []T {
func SliceReduce[Acc any, T any]( func SliceReduce[Acc any, T any](
sl []T, sl []T,
accumulator Acc, accumulator Acc,
folder Reducer[Acc, T], reducer Reducer[Acc, T],
) Acc { ) Acc {
for i := 0; i < len(sl); i++ { for i := 0; i < len(sl); i++ {
folder(accumulator, sl[i]) accumulator = reducer(accumulator, sl[i])
} }
return accumulator return accumulator
} }

@ -27,6 +27,101 @@ import (
) )
func TestSliceContains(t *testing.T) { func TestSliceContains(t *testing.T) {
x := []int{1, 2, 3} haystack := []int{1, 2, 3}
assert.Assert(t, collections.SliceContains(x, 1), "it didn't") needle := 1
assert.Assert(
t,
collections.SliceContains(haystack, needle),
"expected %v to contain %d", haystack, needle,
)
}
func TestSliceContainsEach(t *testing.T) {
haystack := []int{1, 2, 3, 4, 5, 6, 7, 8}
needles := []int{5, 3, 4, 6}
assert.Assert(
t,
collections.SliceContainsEach(haystack, needles),
"expected %v to contain each of %v", haystack, needles,
)
}
func TestSliceSubset(t *testing.T) {
haystack := []int{1, 2, 3, 4, 5, 6, 7, 8}
subset := []int{3, 4, 5}
assert.Assert(
t,
collections.SliceSubset(haystack, subset),
"expected %v to contain subset %v", haystack, subset,
)
}
func TestSliceSubsetEqualSlices(t *testing.T) {
haystack := []int{1, 2, 3, 4, 5, 6, 7, 8}
subset := []int{1, 2, 3, 4, 5, 6, 7, 8}
assert.Assert(
t,
collections.SliceSubset(haystack, subset),
"expected %v to contain subset %v", haystack, subset,
)
}
func TestSliceSubsetOutOfOrderFails(t *testing.T) {
haystack := []int{1, 2, 3, 4, 5, 6, 7, 8}
subset := []int{4, 3, 5}
assert.Assert(
t,
!collections.SliceSubset(haystack, subset),
"expected %v not to contain out of order subset %v", haystack, subset,
)
}
func TestSliceSubsetStrict(t *testing.T) {
haystack := []int{1, 2, 3, 4, 5, 6, 7, 8}
subset := []int{5, 6, 7}
assert.Assert(
t,
collections.SliceSubsetStrict(haystack, subset),
"expected %v to contain strict subset %v", haystack, subset,
)
}
func TestSliceSubsetStrictEqualSetFails(t *testing.T) {
haystack := []int{1, 2, 3, 4, 5, 6, 7, 8}
subset := []int{1, 2, 3, 4, 5, 6, 7, 8}
assert.Assert(
t,
!collections.SliceSubsetStrict(haystack, subset),
"expected %v not to have strict subset %v", haystack, subset,
)
}
func TestMap(t *testing.T) {
sl := collections.Slice[int]{1, 2, 3}
result := sl.Map(
func(x int) int {
return x + 1
},
)
assert.SliceEqual(t, []int{2, 3, 4}, result)
}
func TestFilter(t *testing.T) {
sl := collections.Slice[int]{1, 2, 3, 4}
result := sl.Filter(
func(x int) bool {
return x%2 == 0
},
)
assert.SliceEqual(t, []int{2, 4}, result)
}
func TestReduce(t *testing.T) {
sl := collections.Slice[int]{1, 2, 3, 4, 5}
result := collections.SliceReduce(sl, 0,
func(accumulator int, current int) int {
return accumulator + current
},
)
assert.Equal(t, 15, result)
} }

Loading…
Cancel
Save