core_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package core
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/evilsocket/islazy/fs"
  6. )
  7. func hasInt(a []int, v int) bool {
  8. for _, n := range a {
  9. if n == v {
  10. return true
  11. }
  12. }
  13. return false
  14. }
  15. func sameInts(a []int, b []int, ordered bool) bool {
  16. if len(a) != len(b) {
  17. return false
  18. }
  19. if ordered {
  20. for i, v := range a {
  21. if v != b[i] {
  22. return false
  23. }
  24. }
  25. } else {
  26. for _, v := range a {
  27. if !hasInt(b, v) {
  28. return false
  29. }
  30. }
  31. }
  32. return true
  33. }
  34. func TestCoreUniqueIntsUnsorted(t *testing.T) {
  35. var units = []struct {
  36. from []int
  37. to []int
  38. }{
  39. {[]int{}, []int{}},
  40. {[]int{1, 1, 1, 1, 1}, []int{1}},
  41. {[]int{1, 2, 1, 2, 3, 4}, []int{1, 2, 3, 4}},
  42. {[]int{4, 3, 4, 3, 2, 2}, []int{4, 3, 2}},
  43. {[]int{8, 3, 8, 4, 6, 1}, []int{8, 3, 4, 6, 1}},
  44. }
  45. for _, u := range units {
  46. got := UniqueInts(u.from, false)
  47. if !sameInts(got, u.to, false) {
  48. t.Fatalf("expected '%v', got '%v'", u.to, got)
  49. }
  50. }
  51. }
  52. func TestCoreUniqueIntsSorted(t *testing.T) {
  53. var units = []struct {
  54. from []int
  55. to []int
  56. }{
  57. {[]int{}, []int{}},
  58. {[]int{1, 1, 1, 1, 1}, []int{1}},
  59. {[]int{1, 2, 1, 2, 3, 4}, []int{1, 2, 3, 4}},
  60. {[]int{4, 3, 4, 3, 2, 2}, []int{2, 3, 4}},
  61. {[]int{8, 3, 8, 4, 6, 1}, []int{1, 3, 4, 6, 8}},
  62. }
  63. for _, u := range units {
  64. got := UniqueInts(u.from, true)
  65. if !sameInts(got, u.to, true) {
  66. t.Fatalf("expected '%v', got '%v'", u.to, got)
  67. }
  68. }
  69. }
  70. func TestCoreExists(t *testing.T) {
  71. var units = []struct {
  72. what string
  73. exists bool
  74. }{
  75. {".", true},
  76. {"/", true},
  77. {"wuuut", false},
  78. {"/wuuu.t", false},
  79. {os.Args[0], true},
  80. }
  81. for _, u := range units {
  82. got := fs.Exists(u.what)
  83. if got != u.exists {
  84. t.Fatalf("expected '%v', got '%v'", u.exists, got)
  85. }
  86. }
  87. }