Skip to content

Commit

Permalink
sorted_struct_list_test start using list[t] in background
Browse files Browse the repository at this point in the history
  • Loading branch information
jtomasevic committed Aug 1, 2022
1 parent 90bf46b commit 706b3e0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
11 changes: 5 additions & 6 deletions collections/sorted_struct_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func SortedStructList[T comparable](comparableFunction CompareFunction[T]) sorte
}

type sortedStructList[T comparable] struct {
elements []T
elements List[T]
compare CompareFunction[T]
}

Expand All @@ -30,9 +30,7 @@ func (list *sortedStructList[T]) Add(element T) {
high = median - 1
}
}
elems := append([]T{element}, newList[low:]...)
elems = append(newList[:low], elems...)
list.elements = elems
list.elements.Insert(low, element)
}

// Remove element from sorted list. If there is multiple values, removes only first one.
Expand All @@ -53,8 +51,9 @@ func (list *sortedStructList[T]) Remove(element T) bool {
}
}
if low < len(newList) && newList[low] == element {
newList = append(newList[:low], newList[low+1:]...)
list.elements = newList
list.elements.RemoveAt(low)
//newList = append(newList[:low], newList[low+1:]...)
//list.elements = newList
return true
}
return false
Expand Down
80 changes: 40 additions & 40 deletions collections/sorted_struct_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestSortedStructList_Add(t *testing.T) {
sorted := getSortedProductsAsc()

t.Run("Add ", func(t *testing.T) {
require.Equal(t, sorted.Values(), []Product{
require.Equal(t, []Product{
{
Name: "Addidas",
},
Expand All @@ -26,7 +26,7 @@ func TestSortedStructList_Add(t *testing.T) {
{
Name: "Puma",
},
})
}, sorted.Values())
})
}

Expand All @@ -38,7 +38,7 @@ func TestSortedStructList_Remove(t *testing.T) {
Name: "Nike",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Addidas",
},
Expand All @@ -51,14 +51,14 @@ func TestSortedStructList_Remove(t *testing.T) {
{
Name: "Puma",
},
})
}, sorted.elements)
})
t.Run("Remove non existing element", func(t *testing.T) {
result := sorted.Remove(Product{
Name: "Nike",
})
require.False(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Addidas",
},
Expand All @@ -71,14 +71,14 @@ func TestSortedStructList_Remove(t *testing.T) {
{
Name: "Puma",
},
})
}, sorted.elements)
})
t.Run("Remove First", func(t *testing.T) {
result := sorted.Remove(Product{
Name: "Addidas",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Diadora",
},
Expand All @@ -88,21 +88,21 @@ func TestSortedStructList_Remove(t *testing.T) {
{
Name: "Puma",
},
})
}, sorted.elements)
})
t.Run("Remove Last", func(t *testing.T) {
result := sorted.Remove(Product{
Name: "Puma",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Diadora",
},
{
Name: "Nokka",
},
})
}, sorted.elements)
})
t.Run("Remove From empty", func(t *testing.T) {
result := sorted.Remove(Product{
Expand All @@ -113,7 +113,7 @@ func TestSortedStructList_Remove(t *testing.T) {
Name: "Nokka",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{})
require.Equal(t, List[Product]{}, sorted.elements)
result = sorted.Remove(Product{
Name: "Some random product",
})
Expand All @@ -127,7 +127,7 @@ func TestSortedStructList_RemoveAt(t *testing.T) {
t.Run("Remove from middle ", func(t *testing.T) {
result := sorted.RemoveAt(2)
require.True(t, result)
require.Equal(t, []Product{
require.Equal(t, List[Product]{
{
Name: "Addidas",
},
Expand All @@ -146,7 +146,7 @@ func TestSortedStructList_RemoveAt(t *testing.T) {
t.Run("Remove with non existing index", func(t *testing.T) {
result := sorted.RemoveAt(10)
require.False(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Addidas",
},
Expand All @@ -159,14 +159,14 @@ func TestSortedStructList_RemoveAt(t *testing.T) {
{
Name: "Puma",
},
})
}, sorted.elements)
result = sorted.RemoveAt(-1)
require.False(t, result)
})
t.Run("Remove at First", func(t *testing.T) {
result := sorted.RemoveAt(0)
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Diadora",
},
Expand All @@ -176,26 +176,26 @@ func TestSortedStructList_RemoveAt(t *testing.T) {
{
Name: "Puma",
},
})
}, sorted.elements)
})
t.Run("Remove at Last", func(t *testing.T) {
result := sorted.RemoveAt(2)
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Diadora",
},
{
Name: "Nokka",
},
})
}, sorted.elements)
})
t.Run("Remove at From empty", func(t *testing.T) {
result := sorted.RemoveAt(0)
require.True(t, result)
result = sorted.RemoveAt(0)
require.True(t, result)
require.Equal(t, sorted.elements, []Product{})
require.Equal(t, List[Product]{}, sorted.elements)
require.Equal(t, sorted.Size(), 0)

result = sorted.RemoveAt(0)
Expand All @@ -209,7 +209,7 @@ func TestSortedStructListDesc_Add(t *testing.T) {
sorted := getSortedProductsDesc()

t.Run("Add ", func(t *testing.T) {
require.Equal(t, []Product{
require.Equal(t, List[Product]{
{
Name: "Puma",
},
Expand All @@ -225,7 +225,7 @@ func TestSortedStructListDesc_Add(t *testing.T) {
{
Name: "Addidas",
},
}, sorted.Values())
}, sorted.elements)
})
}

Expand All @@ -237,7 +237,7 @@ func TestSortedStructListDesc_Remove(t *testing.T) {
Name: "Nike",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Puma",
},
Expand All @@ -250,14 +250,14 @@ func TestSortedStructListDesc_Remove(t *testing.T) {
{
Name: "Addidas",
},
})
}, sorted.elements)
})
t.Run("Remove non existing element", func(t *testing.T) {
result := sorted.Remove(Product{
Name: "Nike",
})
require.False(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Puma",
},
Expand All @@ -270,14 +270,14 @@ func TestSortedStructListDesc_Remove(t *testing.T) {
{
Name: "Addidas",
},
})
}, sorted.elements)
})
t.Run("Remove First", func(t *testing.T) {
result := sorted.Remove(Product{
Name: "Puma",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Nokka",
},
Expand All @@ -287,21 +287,21 @@ func TestSortedStructListDesc_Remove(t *testing.T) {
{
Name: "Addidas",
},
})
}, sorted.elements)
})
t.Run("Remove Last", func(t *testing.T) {
result := sorted.Remove(Product{
Name: "Addidas",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Nokka",
},
{
Name: "Diadora",
},
})
}, sorted.elements)
})
t.Run("Remove From empty", func(t *testing.T) {
result := sorted.Remove(Product{
Expand All @@ -312,7 +312,7 @@ func TestSortedStructListDesc_Remove(t *testing.T) {
Name: "Nokka",
})
require.True(t, result)
require.Equal(t, sorted.elements, []Product{})
require.Equal(t, List[Product]{}, sorted.elements)
result = sorted.Remove(Product{
Name: "Some random product",
})
Expand All @@ -326,7 +326,7 @@ func TestSortedStructListDesc_RemoveAt(t *testing.T) {
t.Run("Remove from middle ", func(t *testing.T) {
result := sorted.RemoveAt(2)
require.True(t, result)
require.Equal(t, []Product{
require.Equal(t, List[Product]{
{
Name: "Puma",
},
Expand All @@ -345,7 +345,7 @@ func TestSortedStructListDesc_RemoveAt(t *testing.T) {
t.Run("Remove with non existing index", func(t *testing.T) {
result := sorted.RemoveAt(10)
require.False(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Puma",
},
Expand All @@ -358,14 +358,14 @@ func TestSortedStructListDesc_RemoveAt(t *testing.T) {
{
Name: "Addidas",
},
})
}, sorted.elements)
result = sorted.RemoveAt(-1)
require.False(t, result)
})
t.Run("Remove at First", func(t *testing.T) {
result := sorted.RemoveAt(0)
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Nokka",
},
Expand All @@ -375,26 +375,26 @@ func TestSortedStructListDesc_RemoveAt(t *testing.T) {
{
Name: "Addidas",
},
})
}, sorted.elements)
})
t.Run("Remove at Last", func(t *testing.T) {
result := sorted.RemoveAt(2)
require.True(t, result)
require.Equal(t, sorted.elements, []Product{
require.Equal(t, List[Product]{
{
Name: "Nokka",
},
{
Name: "Diadora",
},
})
}, sorted.elements)
})
t.Run("Remove at From empty", func(t *testing.T) {
result := sorted.RemoveAt(0)
require.True(t, result)
result = sorted.RemoveAt(0)
require.True(t, result)
require.Equal(t, sorted.elements, []Product{})
require.Equal(t, List[Product]{}, sorted.elements)
require.Equal(t, sorted.Size(), 0)

result = sorted.RemoveAt(0)
Expand Down Expand Up @@ -430,8 +430,8 @@ type Product struct {
Name string
}

func getProduct() []Product {
return []Product{
func getProduct() List[Product] {
return List[Product]{
{
Name: "Diadora",
},
Expand Down

0 comments on commit 706b3e0

Please sign in to comment.