github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/snm/snm_test.go (about) 1 package snm 2 3 import ( 4 "fmt" 5 "slices" 6 "testing" 7 8 "golang.org/x/exp/maps" 9 ) 10 11 func TestSlice(t *testing.T) { 12 want := []int{1, 4, 9, 16} 13 got := Slice(4, func(i int) int { return (i + 1) * (i + 1) }) 14 if !slices.Equal(got, want) { 15 t.Fatalf("Slice((i+1)*(i+1))=%v, want %v", got, want) 16 } 17 } 18 19 func TestSliceToSlice(t *testing.T) { 20 input := []int{1, 4, 9, 16} 21 want := []float64{1.5, 4.5, 9.5, 16.5} 22 got := SliceToSlice(input, func(i int) float64 { 23 return float64(i) + 0.5 24 }) 25 if !slices.Equal(got, want) { 26 t.Fatalf("SliceToSlice(%v)=%v, want %v", input, got, want) 27 } 28 } 29 30 func TestMapToMap(t *testing.T) { 31 input := map[string]string{"a": "bbb", "cccc": "ddddddd"} 32 want := map[int]int{1: 3, 4: 7} 33 got := MapToMap(input, func(k, v string) (int, int) { 34 return len(k), len(v) 35 }) 36 if !maps.Equal(got, want) { 37 t.Fatalf("MapToMap(%v)=%v, want %v", input, got, want) 38 } 39 } 40 41 func TestMapToMap_equalKeys(t *testing.T) { 42 input := map[string]string{"a": "bbb", "cccc": "ddddddd", "e": "ff"} 43 want1 := map[int]int{1: 3, 4: 7} 44 want2 := map[int]int{1: 2, 4: 7} 45 got := MapToMap(input, func(k, v string) (int, int) { 46 return len(k), len(v) 47 }) 48 if !maps.Equal(got, want1) && !maps.Equal(got, want2) { 49 t.Fatalf("MapToMap(%v)=%v, want %v or %v", input, got, want1, want2) 50 } 51 } 52 53 func TestDefaultMap(t *testing.T) { 54 m := NewDefaultMap[int, string](func(i int) string { 55 return fmt.Sprint(i + 1) 56 }) 57 if got, want := m.Get(2), "3"; got != want { 58 t.Fatalf("Get(%d)=%s, want %s", 2, got, want) 59 } 60 if got, want := m.Get(6), "7"; got != want { 61 t.Fatalf("Get(%d)=%s, want %s", 6, got, want) 62 } 63 m.Set(2, "a") 64 if got, want := m.Get(2), "a"; got != want { 65 t.Fatalf("Get(%d)=%s, want %s", 2, got, want) 66 } 67 if got, want := m.Get(6), "7"; got != want { 68 t.Fatalf("Get(%d)=%s, want %s", 6, got, want) 69 } 70 if got, want := len(m.M), 2; got != want { 71 t.Fatalf("Len=%d, want %d", got, want) 72 } 73 } 74 75 func TestCompare(t *testing.T) { 76 input := []int{3, 4, 2, 1, 5} 77 want := []int{1, 2, 3, 4, 5} 78 wantr := []int{5, 4, 3, 2, 1} 79 80 cp := slices.Clone(input) 81 slices.SortFunc(cp, Compare) 82 if !slices.Equal(cp, want) { 83 t.Errorf("SortFunc(%v, Compare)=%v, want %v", 84 input, cp, want) 85 } 86 87 cp = slices.Clone(input) 88 slices.SortFunc(cp, CompareReverse) 89 if !slices.Equal(cp, wantr) { 90 t.Errorf("SortFunc(%v, Compare)=%v, want %v", 91 input, cp, wantr) 92 } 93 } 94 95 func ExampleSortedKeys() { 96 ages := map[string]int{ 97 "Alice": 30, 98 "Bob": 20, 99 "Charlie": 25, 100 } 101 for _, name := range SortedKeys(ages) { 102 fmt.Printf("%s: %d\n", name, ages[name]) 103 } 104 // Output: 105 // Bob: 20 106 // Charlie: 25 107 // Alice: 30 108 } 109 110 func ExampleSortedKeysFunc_reverse() { 111 ages := map[string]int{ 112 "Alice": 30, 113 "Bob": 20, 114 "Charlie": 25, 115 } 116 // Sort by reverse natural order. 117 for _, name := range SortedKeysFunc(ages, CompareReverse) { 118 fmt.Printf("%s: %d\n", name, ages[name]) 119 } 120 // Output: 121 // Alice: 30 122 // Charlie: 25 123 // Bob: 20 124 }