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