slice_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package buildin
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestIntersection(t *testing.T) {
  7. // Test case 1
  8. a := []string{"apple", "banana", "orange"}
  9. b := []string{"banana", "grape", "orange"}
  10. expected1 := []string{"banana", "orange"}
  11. result1 := Intersection(a, b)
  12. if !reflect.DeepEqual(result1, expected1) {
  13. t.Errorf("Test case 1 failed. Expected %v, got %v", expected1, result1)
  14. }
  15. // Test case 2
  16. c := []int{1, 2, 3, 4}
  17. d := []int{2, 3, 4, 5}
  18. expected2 := []int{2, 3, 4}
  19. result2 := Intersection(c, d)
  20. if !reflect.DeepEqual(result2, expected2) {
  21. t.Errorf("Test case 2 failed. Expected %v, got %v", expected2, result2)
  22. }
  23. }
  24. func TestInSlice(t *testing.T) {
  25. // Test case 1: int
  26. slice1 := []int{1, 2, 3, 4, 5}
  27. item1 := 3
  28. expected1 := true
  29. result1 := InSlice(item1, slice1)
  30. if result1 != expected1 {
  31. t.Errorf("Test case 1 failed. Expected %v, got %v", expected1, result1)
  32. }
  33. // Test case 2: string
  34. slice2 := []string{"apple", "banana", "orange"}
  35. item2 := "grape"
  36. expected2 := false
  37. result2 := InSlice(item2, slice2)
  38. if result2 != expected2 {
  39. t.Errorf("Test case 2 failed. Expected %v, got %v", expected2, result2)
  40. }
  41. // Test case 3: bool
  42. slice3 := []bool{true, false}
  43. item3 := true
  44. expected3 := true
  45. result3 := InSlice(item3, slice3)
  46. if result3 != expected3 {
  47. t.Errorf("Test case 3 failed. Expected %v, got %v", expected3, result3)
  48. }
  49. }
  50. func TestRemoveDuplicates(t *testing.T) {
  51. // Test case 1: []string
  52. slice1 := []string{"apple", "banana", "apple", "orange", "banana"}
  53. result1 := RemoveDuplicates(slice1)
  54. expected1 := []string{"apple", "banana", "orange"}
  55. if !reflect.DeepEqual(result1, expected1) {
  56. panic("Test case 1 failed")
  57. }
  58. // Test case 2: []int
  59. slice2 := []int{1, 2, 2, 3, 4, 4, 5}
  60. result2 := RemoveDuplicates(slice2)
  61. expected2 := []int{1, 2, 3, 4, 5}
  62. if !reflect.DeepEqual(result2, expected2) {
  63. panic("Test case 2 failed")
  64. }
  65. }