array_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package arrays
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestIndexOf(t *testing.T) {
  7. type args[T comparable] struct {
  8. a T
  9. vs []T
  10. }
  11. type testCase[T comparable] struct {
  12. name string
  13. args args[T]
  14. want int
  15. }
  16. tests := []testCase[string]{
  17. {"exists", args[string]{a: "a", vs: []string{"a", "b"}}, 0},
  18. {"not exists", args[string]{a: "a", vs: []string{"c", "b"}}, -1},
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.name, func(t *testing.T) {
  22. if got := IndexOf(tt.args.a, tt.args.vs); got != tt.want {
  23. t.Errorf("IndexOf() = %v, want %v", got, tt.want)
  24. }
  25. })
  26. }
  27. }
  28. func TestExists(t *testing.T) {
  29. type args[T comparable] struct {
  30. a T
  31. vs []T
  32. }
  33. type testCase[T comparable] struct {
  34. name string
  35. args args[T]
  36. want bool
  37. }
  38. tests := []testCase[int]{
  39. {"exists", args[int]{a: 1, vs: []int{1, 2}}, true},
  40. {"not exists", args[int]{a: 2, vs: []int{3, 4}}, false},
  41. }
  42. for _, tt := range tests {
  43. t.Run(tt.name, func(t *testing.T) {
  44. if got := Exists(tt.args.a, tt.args.vs); got != tt.want {
  45. t.Errorf("Exists() = %v, want %v", got, tt.want)
  46. }
  47. })
  48. }
  49. }
  50. func TestReverse(t *testing.T) {
  51. type args[T comparable] struct {
  52. s []T
  53. }
  54. type testCase[T comparable] struct {
  55. name string
  56. args args[T]
  57. want []T
  58. }
  59. tests := []testCase[int]{
  60. {"one", args[int]{s: []int{1, 2, 3}}, []int{3, 2, 1}},
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.name, func(t *testing.T) {
  64. if got := Reverse(tt.args.s); !reflect.DeepEqual(got, tt.want) {
  65. t.Errorf("Reverse() = %v, want %v", got, tt.want)
  66. }
  67. })
  68. }
  69. }