-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests add groupby add groupbyordered
- Loading branch information
Showing
12 changed files
with
230 additions
and
211 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,33 @@ | ||
package orderedmap | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestOrderedMap(t *testing.T) { | ||
t.Parallel() | ||
om := New[string, int]() | ||
|
||
alphabet := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} | ||
for index, letter := range alphabet { | ||
om.Set(letter, index) | ||
} | ||
|
||
assert.Equal(t, alphabet, om.Keys()) | ||
|
||
c, ok := om.Get("c") | ||
assert.True(t, ok) | ||
assert.Equal(t, 2, c) | ||
|
||
for i, entry := range om.Entries() { | ||
if i == 5 { | ||
assert.Equal(t, "f", entry.Key) | ||
} | ||
} | ||
|
||
om.Delete("c") | ||
value, ok := om.Get("c") | ||
assert.False(t, ok) | ||
assert.Equal(t, 0, value) | ||
} |
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,102 @@ | ||
package h | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestUnique(t *testing.T) { | ||
t.Parallel() | ||
slice := []string{"a", "b", "b", "c", "d", "d", "x"} | ||
unique := Unique(slice, func(item string) string { | ||
return item | ||
}) | ||
assert.Equal(t, []string{"a", "b", "c", "d", "x"}, unique) | ||
} | ||
|
||
func TestFilter(t *testing.T) { | ||
t.Parallel() | ||
slice := []string{"a", "b", "b", "c", "d", "d", "x"} | ||
filtered := Filter(slice, func(item string) bool { | ||
return item == "b" | ||
}) | ||
assert.Equal(t, []string{"b", "b"}, filtered) | ||
} | ||
|
||
func TestMap(t *testing.T) { | ||
t.Parallel() | ||
slice := []string{"a", "b", "c"} | ||
mapped := Map(slice, func(item string) string { | ||
return strings.ToUpper(item) | ||
}) | ||
assert.Equal(t, []string{"A", "B", "C"}, mapped) | ||
} | ||
|
||
func TestGroupBy(t *testing.T) { | ||
t.Parallel() | ||
|
||
type Item struct { | ||
Name string | ||
Job string | ||
} | ||
|
||
items := []Item{ | ||
{Name: "Alice", Job: "Developer"}, | ||
{Name: "Bob", Job: "Designer"}, | ||
{Name: "Charlie", Job: "Developer"}, | ||
{Name: "David", Job: "Designer"}, | ||
{Name: "Eve", Job: "Developer"}, | ||
{Name: "Frank", Job: "Product Manager"}, | ||
} | ||
|
||
grouped := GroupBy(items, func(item Item) string { | ||
return item.Job | ||
}) | ||
|
||
assert.Equal(t, 3, len(grouped)) | ||
assert.Equal(t, 3, len(grouped["Developer"])) | ||
assert.Equal(t, 2, len(grouped["Designer"])) | ||
assert.Equal(t, 1, len(grouped["Product Manager"])) | ||
} | ||
|
||
func TestGroupByOrdered(t *testing.T) { | ||
t.Parallel() | ||
|
||
type Item struct { | ||
Name string | ||
Job string | ||
} | ||
|
||
items := []Item{ | ||
{Name: "Alice", Job: "Developer"}, | ||
{Name: "Bob", Job: "Designer"}, | ||
{Name: "Charlie", Job: "Developer"}, | ||
{Name: "David", Job: "Designer"}, | ||
{Name: "Eve", Job: "Developer"}, | ||
{Name: "Frank", Job: "Product Manager"}, | ||
} | ||
|
||
grouped := GroupByOrdered(items, func(item Item) string { | ||
return item.Job | ||
}) | ||
|
||
keys := []string{"Developer", "Designer", "Product Manager"} | ||
assert.Equal(t, keys, grouped.Keys()) | ||
|
||
devs, ok := grouped.Get("Developer") | ||
assert.True(t, ok) | ||
assert.Equal(t, 3, len(devs)) | ||
assert.Equal(t, "Alice", devs[0].Name) | ||
assert.Equal(t, "Charlie", devs[1].Name) | ||
assert.Equal(t, "Eve", devs[2].Name) | ||
} | ||
|
||
func TestFind(t *testing.T) { | ||
t.Parallel() | ||
slice := []string{"a", "b", "c"} | ||
found := Find(slice, func(item *string) bool { | ||
return *item == "b" | ||
}) | ||
assert.Equal(t, "b", *found) | ||
} |
Oops, something went wrong.