This commit is contained in:
+2
-2
@@ -11,8 +11,8 @@ type Queue[T any] struct {
|
|||||||
container []T
|
container []T
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueueFixed -> QueueFixed[Type](size) creates a Queue with no fixed size
|
// NewQueueFixed -> NewQueueFixed[Type](size) creates a Queue with no fixed size
|
||||||
func QueueFixed[T any](value uint8) *Queue[T] {
|
func NewQueueFixed[T any](value uint8) *Queue[T] {
|
||||||
return &Queue[T]{
|
return &Queue[T]{
|
||||||
capacity: value,
|
capacity: value,
|
||||||
container: make([]T, 0, value),
|
container: make([]T, 0, value),
|
||||||
|
|||||||
+2
-2
@@ -10,8 +10,8 @@ type Stack[T any] struct {
|
|||||||
container []T
|
container []T
|
||||||
}
|
}
|
||||||
|
|
||||||
// StackFixed -> creates Stack with a fixed size
|
// NewStackFixed -> creates Stack with a fixed size
|
||||||
func StackFixed[T any](value int) *Stack[T] {
|
func NewStackFixed[T any](value int) *Stack[T] {
|
||||||
return &Stack[T]{
|
return &Stack[T]{
|
||||||
capacity: value,
|
capacity: value,
|
||||||
container: make([]T, 0, value),
|
container: make([]T, 0, value),
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
// --- map type ---
|
// --- map type ---
|
||||||
|
|
||||||
func TestQueueWithMaps(t *testing.T) {
|
func TestQueueWithMaps(t *testing.T) {
|
||||||
q := linear.QueueFixed[map[string]int](3)
|
q := linear.NewQueueFixed[map[string]int](3)
|
||||||
|
|
||||||
m1 := map[string]int{"a": 1}
|
m1 := map[string]int{"a": 1}
|
||||||
m2 := map[string]int{"b": 2, "c": 3}
|
m2 := map[string]int{"b": 2, "c": 3}
|
||||||
@@ -39,7 +39,7 @@ func TestQueueWithMaps(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueMapMutationAfterEnqueue(t *testing.T) {
|
func TestQueueMapMutationAfterEnqueue(t *testing.T) {
|
||||||
q := linear.QueueFixed[map[string]int](2)
|
q := linear.NewQueueFixed[map[string]int](2)
|
||||||
|
|
||||||
m := map[string]int{"x": 10}
|
m := map[string]int{"x": 10}
|
||||||
q.Push(m)
|
q.Push(m)
|
||||||
@@ -56,7 +56,7 @@ func TestQueueMapMutationAfterEnqueue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueMapOverCapacity(t *testing.T) {
|
func TestQueueMapOverCapacity(t *testing.T) {
|
||||||
q := linear.QueueFixed[map[string]int](1)
|
q := linear.NewQueueFixed[map[string]int](1)
|
||||||
q.Push(map[string]int{"a": 1})
|
q.Push(map[string]int{"a": 1})
|
||||||
|
|
||||||
err := q.Push(map[string]int{"b": 2})
|
err := q.Push(map[string]int{"b": 2})
|
||||||
@@ -73,7 +73,7 @@ type Person struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueWithStructs(t *testing.T) {
|
func TestQueueWithStructs(t *testing.T) {
|
||||||
q := linear.QueueFixed[Person](3)
|
q := linear.NewQueueFixed[Person](3)
|
||||||
|
|
||||||
q.Push(Person{"Alice", 30})
|
q.Push(Person{"Alice", 30})
|
||||||
q.Push(Person{"Bob", 25})
|
q.Push(Person{"Bob", 25})
|
||||||
@@ -95,7 +95,7 @@ func TestQueueWithStructs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueStructOverCapacity(t *testing.T) {
|
func TestQueueStructOverCapacity(t *testing.T) {
|
||||||
q := linear.QueueFixed[Person](2)
|
q := linear.NewQueueFixed[Person](2)
|
||||||
q.Push(Person{"A", 1})
|
q.Push(Person{"A", 1})
|
||||||
q.Push(Person{"B", 2})
|
q.Push(Person{"B", 2})
|
||||||
|
|
||||||
@@ -108,7 +108,7 @@ func TestQueueStructOverCapacity(t *testing.T) {
|
|||||||
// --- pointer type ---
|
// --- pointer type ---
|
||||||
|
|
||||||
func TestQueueWithPointers(t *testing.T) {
|
func TestQueueWithPointers(t *testing.T) {
|
||||||
q := linear.QueueFixed[*Person](3)
|
q := linear.NewQueueFixed[*Person](3)
|
||||||
|
|
||||||
p1 := &Person{"Alice", 30}
|
p1 := &Person{"Alice", 30}
|
||||||
p2 := &Person{"Bob", 25}
|
p2 := &Person{"Bob", 25}
|
||||||
@@ -128,7 +128,7 @@ func TestQueueWithPointers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueNilPointerPull(t *testing.T) {
|
func TestQueueNilPointerPull(t *testing.T) {
|
||||||
q := linear.QueueFixed[*Person](2)
|
q := linear.NewQueueFixed[*Person](2)
|
||||||
q.Push(nil)
|
q.Push(nil)
|
||||||
|
|
||||||
got, err := q.Pop()
|
got, err := q.Pop()
|
||||||
@@ -141,7 +141,7 @@ func TestQueueNilPointerPull(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueuePointerMutationAfterEnqueue(t *testing.T) {
|
func TestQueuePointerMutationAfterEnqueue(t *testing.T) {
|
||||||
q := linear.QueueFixed[*Person](1)
|
q := linear.NewQueueFixed[*Person](1)
|
||||||
|
|
||||||
p := &Person{"Alice", 30}
|
p := &Person{"Alice", 30}
|
||||||
q.Push(p)
|
q.Push(p)
|
||||||
@@ -157,7 +157,7 @@ func TestQueuePointerMutationAfterEnqueue(t *testing.T) {
|
|||||||
// --- slice type ---
|
// --- slice type ---
|
||||||
|
|
||||||
func TestQueueWithSlices(t *testing.T) {
|
func TestQueueWithSlices(t *testing.T) {
|
||||||
q := linear.QueueFixed[[]int](3)
|
q := linear.NewQueueFixed[[]int](3)
|
||||||
|
|
||||||
q.Push([]int{1, 2, 3})
|
q.Push([]int{1, 2, 3})
|
||||||
q.Push([]int{}) // empty slice
|
q.Push([]int{}) // empty slice
|
||||||
@@ -174,7 +174,7 @@ func TestQueueWithSlices(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueSliceMutationAfterEnqueue(t *testing.T) {
|
func TestQueueSliceMutationAfterEnqueue(t *testing.T) {
|
||||||
q := linear.QueueFixed[[]int](1)
|
q := linear.NewQueueFixed[[]int](1)
|
||||||
|
|
||||||
s := []int{1, 2, 3}
|
s := []int{1, 2, 3}
|
||||||
q.Push(s)
|
q.Push(s)
|
||||||
@@ -190,7 +190,7 @@ func TestQueueSliceMutationAfterEnqueue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueSliceOverCapacity(t *testing.T) {
|
func TestQueueSliceOverCapacity(t *testing.T) {
|
||||||
q := linear.QueueFixed[[]int](1)
|
q := linear.NewQueueFixed[[]int](1)
|
||||||
q.Push([]int{1})
|
q.Push([]int{1})
|
||||||
|
|
||||||
err := q.Push([]int{2})
|
err := q.Push([]int{2})
|
||||||
@@ -202,7 +202,7 @@ func TestQueueSliceOverCapacity(t *testing.T) {
|
|||||||
// --- empty queue edge cases across all types ---
|
// --- empty queue edge cases across all types ---
|
||||||
|
|
||||||
func TestQueuePullEmptyMap(t *testing.T) {
|
func TestQueuePullEmptyMap(t *testing.T) {
|
||||||
q := linear.QueueFixed[map[string]int](2)
|
q := linear.NewQueueFixed[map[string]int](2)
|
||||||
_, err := q.Pop()
|
_, err := q.Pop()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error pulling from empty map queue")
|
t.Error("expected error pulling from empty map queue")
|
||||||
@@ -210,7 +210,7 @@ func TestQueuePullEmptyMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueuePullEmptyPointer(t *testing.T) {
|
func TestQueuePullEmptyPointer(t *testing.T) {
|
||||||
q := linear.QueueFixed[*Person](2)
|
q := linear.NewQueueFixed[*Person](2)
|
||||||
_, err := q.Pop()
|
_, err := q.Pop()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error pulling from empty pointer queue")
|
t.Error("expected error pulling from empty pointer queue")
|
||||||
@@ -218,7 +218,7 @@ func TestQueuePullEmptyPointer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueuePullEmptySlice(t *testing.T) {
|
func TestQueuePullEmptySlice(t *testing.T) {
|
||||||
q := linear.QueueFixed[[]int](2)
|
q := linear.NewQueueFixed[[]int](2)
|
||||||
_, err := q.Pop()
|
_, err := q.Pop()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error pulling from empty slice queue")
|
t.Error("expected error pulling from empty slice queue")
|
||||||
@@ -226,7 +226,7 @@ func TestQueuePullEmptySlice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueueCullEmptyStruct(t *testing.T) {
|
func TestQueueCullEmptyStruct(t *testing.T) {
|
||||||
q := linear.QueueFixed[Person](2)
|
q := linear.NewQueueFixed[Person](2)
|
||||||
_, err := q.Cull()
|
_, err := q.Cull()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error culling from empty struct queue")
|
t.Error("expected error culling from empty struct queue")
|
||||||
@@ -234,7 +234,7 @@ func TestQueueCullEmptyStruct(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueuePeekEmptyPointer(t *testing.T) {
|
func TestQueuePeekEmptyPointer(t *testing.T) {
|
||||||
q := linear.QueueFixed[*Person](2)
|
q := linear.NewQueueFixed[*Person](2)
|
||||||
val, ok := q.Peek()
|
val, ok := q.Peek()
|
||||||
if ok || val != nil {
|
if ok || val != nil {
|
||||||
t.Errorf("expected (nil, false) from empty pointer queue Peek, got (%v, %v)", val, ok)
|
t.Errorf("expected (nil, false) from empty pointer queue Peek, got (%v, %v)", val, ok)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
// --- map type ---
|
// --- map type ---
|
||||||
|
|
||||||
func TestStackWithMaps(t *testing.T) {
|
func TestStackWithMaps(t *testing.T) {
|
||||||
s := linear.StackFixed[map[string]int](3)
|
s := linear.NewStackFixed[map[string]int](3)
|
||||||
|
|
||||||
m1 := map[string]int{"a": 1}
|
m1 := map[string]int{"a": 1}
|
||||||
m2 := map[string]int{"b": 2, "c": 3}
|
m2 := map[string]int{"b": 2, "c": 3}
|
||||||
@@ -39,7 +39,7 @@ func TestStackWithMaps(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackMapMutationAfterPush(t *testing.T) {
|
func TestStackMapMutationAfterPush(t *testing.T) {
|
||||||
s := linear.StackFixed[map[string]int](2)
|
s := linear.NewStackFixed[map[string]int](2)
|
||||||
|
|
||||||
m := map[string]int{"x": 10}
|
m := map[string]int{"x": 10}
|
||||||
s.Push(m)
|
s.Push(m)
|
||||||
@@ -55,7 +55,7 @@ func TestStackMapMutationAfterPush(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackMapOverCapacity(t *testing.T) {
|
func TestStackMapOverCapacity(t *testing.T) {
|
||||||
s := linear.StackFixed[map[string]int](1)
|
s := linear.NewStackFixed[map[string]int](1)
|
||||||
s.Push(map[string]int{"a": 1})
|
s.Push(map[string]int{"a": 1})
|
||||||
|
|
||||||
err := s.Push(map[string]int{"b": 2})
|
err := s.Push(map[string]int{"b": 2})
|
||||||
@@ -67,7 +67,7 @@ func TestStackMapOverCapacity(t *testing.T) {
|
|||||||
// --- struct (object) type ---
|
// --- struct (object) type ---
|
||||||
|
|
||||||
func TestStackWithStructs(t *testing.T) {
|
func TestStackWithStructs(t *testing.T) {
|
||||||
s := linear.StackFixed[Person](3)
|
s := linear.NewStackFixed[Person](3)
|
||||||
|
|
||||||
s.Push(Person{"Alice", 30})
|
s.Push(Person{"Alice", 30})
|
||||||
s.Push(Person{"Bob", 25})
|
s.Push(Person{"Bob", 25})
|
||||||
@@ -94,7 +94,7 @@ func TestStackWithStructs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackStructOverCapacity(t *testing.T) {
|
func TestStackStructOverCapacity(t *testing.T) {
|
||||||
s := linear.StackFixed[Person](2)
|
s := linear.NewStackFixed[Person](2)
|
||||||
s.Push(Person{"A", 1})
|
s.Push(Person{"A", 1})
|
||||||
s.Push(Person{"B", 2})
|
s.Push(Person{"B", 2})
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ func TestStackStructOverCapacity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackClearStructs(t *testing.T) {
|
func TestStackClearStructs(t *testing.T) {
|
||||||
s := linear.StackFixed[Person](3)
|
s := linear.NewStackFixed[Person](3)
|
||||||
s.Push(Person{"Alice", 30})
|
s.Push(Person{"Alice", 30})
|
||||||
s.Push(Person{"Bob", 25})
|
s.Push(Person{"Bob", 25})
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ func TestStackClearStructs(t *testing.T) {
|
|||||||
// --- pointer type ---
|
// --- pointer type ---
|
||||||
|
|
||||||
func TestStackWithPointers(t *testing.T) {
|
func TestStackWithPointers(t *testing.T) {
|
||||||
s := linear.StackFixed[*Person](3)
|
s := linear.NewStackFixed[*Person](3)
|
||||||
|
|
||||||
p1 := &Person{"Alice", 30}
|
p1 := &Person{"Alice", 30}
|
||||||
p2 := &Person{"Bob", 25}
|
p2 := &Person{"Bob", 25}
|
||||||
@@ -144,7 +144,7 @@ func TestStackWithPointers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackNilPointerPop(t *testing.T) {
|
func TestStackNilPointerPop(t *testing.T) {
|
||||||
s := linear.StackFixed[*Person](2)
|
s := linear.NewStackFixed[*Person](2)
|
||||||
s.Push(nil)
|
s.Push(nil)
|
||||||
|
|
||||||
got, err := s.Pop()
|
got, err := s.Pop()
|
||||||
@@ -157,7 +157,7 @@ func TestStackNilPointerPop(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackPointerMutationAfterPush(t *testing.T) {
|
func TestStackPointerMutationAfterPush(t *testing.T) {
|
||||||
s := linear.StackFixed[*Person](1)
|
s := linear.NewStackFixed[*Person](1)
|
||||||
|
|
||||||
p := &Person{"Alice", 30}
|
p := &Person{"Alice", 30}
|
||||||
s.Push(p)
|
s.Push(p)
|
||||||
@@ -173,7 +173,7 @@ func TestStackPointerMutationAfterPush(t *testing.T) {
|
|||||||
// --- slice type ---
|
// --- slice type ---
|
||||||
|
|
||||||
func TestStackWithSlices(t *testing.T) {
|
func TestStackWithSlices(t *testing.T) {
|
||||||
s := linear.StackFixed[[]int](3)
|
s := linear.NewStackFixed[[]int](3)
|
||||||
|
|
||||||
s.Push([]int{1, 2, 3})
|
s.Push([]int{1, 2, 3})
|
||||||
s.Push([]int{})
|
s.Push([]int{})
|
||||||
@@ -195,7 +195,7 @@ func TestStackWithSlices(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackSliceMutationAfterPush(t *testing.T) {
|
func TestStackSliceMutationAfterPush(t *testing.T) {
|
||||||
s := linear.StackFixed[[]int](1)
|
s := linear.NewStackFixed[[]int](1)
|
||||||
|
|
||||||
sl := []int{1, 2, 3}
|
sl := []int{1, 2, 3}
|
||||||
s.Push(sl)
|
s.Push(sl)
|
||||||
@@ -211,7 +211,7 @@ func TestStackSliceMutationAfterPush(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackSliceOverCapacity(t *testing.T) {
|
func TestStackSliceOverCapacity(t *testing.T) {
|
||||||
s := linear.StackFixed[[]int](1)
|
s := linear.NewStackFixed[[]int](1)
|
||||||
s.Push([]int{1})
|
s.Push([]int{1})
|
||||||
|
|
||||||
err := s.Push([]int{2})
|
err := s.Push([]int{2})
|
||||||
@@ -223,7 +223,7 @@ func TestStackSliceOverCapacity(t *testing.T) {
|
|||||||
// --- empty stack edge cases ---
|
// --- empty stack edge cases ---
|
||||||
|
|
||||||
func TestStackPopEmpty(t *testing.T) {
|
func TestStackPopEmpty(t *testing.T) {
|
||||||
s := linear.StackFixed[map[string]int](2)
|
s := linear.NewStackFixed[map[string]int](2)
|
||||||
_, err := s.Pop()
|
_, err := s.Pop()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error popping from empty map stack")
|
t.Error("expected error popping from empty map stack")
|
||||||
@@ -231,7 +231,7 @@ func TestStackPopEmpty(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackPopEmptyPointer(t *testing.T) {
|
func TestStackPopEmptyPointer(t *testing.T) {
|
||||||
s := linear.StackFixed[*Person](2)
|
s := linear.NewStackFixed[*Person](2)
|
||||||
_, err := s.Pop()
|
_, err := s.Pop()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error popping from empty pointer stack")
|
t.Error("expected error popping from empty pointer stack")
|
||||||
@@ -239,7 +239,7 @@ func TestStackPopEmptyPointer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackPopEmptySlice(t *testing.T) {
|
func TestStackPopEmptySlice(t *testing.T) {
|
||||||
s := linear.StackFixed[[]int](2)
|
s := linear.NewStackFixed[[]int](2)
|
||||||
_, err := s.Pop()
|
_, err := s.Pop()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error popping from empty slice stack")
|
t.Error("expected error popping from empty slice stack")
|
||||||
@@ -247,7 +247,7 @@ func TestStackPopEmptySlice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStackPopEmptyStruct(t *testing.T) {
|
func TestStackPopEmptyStruct(t *testing.T) {
|
||||||
s := linear.StackFixed[Person](2)
|
s := linear.NewStackFixed[Person](2)
|
||||||
_, err := s.Pop()
|
_, err := s.Pop()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error popping from empty struct stack")
|
t.Error("expected error popping from empty struct stack")
|
||||||
@@ -263,7 +263,7 @@ func TestStackPeekEmptyPanics(t *testing.T) {
|
|||||||
t.Error("expected panic from Peek on empty stack, got none")
|
t.Error("expected panic from Peek on empty stack, got none")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
s := linear.StackFixed[*Person](2)
|
s := linear.NewStackFixed[*Person](2)
|
||||||
s.Peek()
|
s.Peek()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +273,6 @@ func TestStackFirstEmptyPanics(t *testing.T) {
|
|||||||
t.Error("expected panic from First on empty stack, got none")
|
t.Error("expected panic from First on empty stack, got none")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
s := linear.StackFixed[*Person](2)
|
s := linear.NewStackFixed[*Person](2)
|
||||||
s.First()
|
s.First()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user