| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package functools |
| |
| import ( |
| "math" |
| "testing" |
| |
| "golang.org/x/exp/slices" |
| ) |
| |
| func TestFilter(t *testing.T) { |
| original := []string{"foo", "bar", "baz"} |
| got := Filter(original, func(s string) bool { return s == "bar" }) |
| want := []string{"bar"} |
| if !slices.Equal(got, want) { |
| t.Errorf("Filter returned wrong slice, got %s, wanted %s", got, want) |
| } |
| } |
| |
| func TestSortBy(t *testing.T) { |
| slice := []int{4, -2, 1, 4, -10, 3} |
| |
| SortBy(slice, func(x int) float64 { |
| return math.Abs(float64(x)) |
| }) |
| |
| // The numbers should end up sorted by absolute value. |
| want := []int{1, -2, 3, 4, 4, -10} |
| if !slices.Equal(slice, want) { |
| t.Errorf("SortBy incorrect, got %d, wanted %d", slice, want) |
| } |
| } |