src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

unique_test.go (1655B)


      1 package unique
      2 
      3 import "testing"
      4 
      5 func TestString(t *testing.T) {
      6 	var tests = []struct {
      7 		Input    []string
      8 		Expected []string
      9 	}{
     10 		{
     11 			Input:    nil,
     12 			Expected: nil,
     13 		},
     14 		{
     15 			Input:    []string{},
     16 			Expected: []string{},
     17 		},
     18 		{
     19 			Input:    []string{"a", "b", "c", "d"},
     20 			Expected: []string{"a", "b", "c", "d"},
     21 		},
     22 		{
     23 			Input: []string{
     24 				"a", "b", "c", "d", "a", "b", "c", "d",
     25 			},
     26 			Expected: []string{"a", "b", "c", "d"},
     27 		},
     28 	}
     29 
     30 	for _, test := range tests {
     31 		res := Unique(test.Input)
     32 
     33 		if len(res) != len(test.Expected) {
     34 			t.Errorf(
     35 				"expected %d values, but got %d",
     36 				len(test.Expected), len(res),
     37 			)
     38 			return
     39 		}
     40 		for i := range test.Expected {
     41 			if res[i] != test.Expected[i] {
     42 				t.Errorf(
     43 					"expected value %v but got %v",
     44 					res[i], test.Expected[i],
     45 				)
     46 				return
     47 			}
     48 		}
     49 
     50 		t.Logf("%v %v", res, test.Expected)
     51 	}
     52 
     53 }
     54 
     55 func TestInt(t *testing.T) {
     56 	var tests = []struct {
     57 		Input    []int
     58 		Expected []int
     59 	}{
     60 		{
     61 			Input:    nil,
     62 			Expected: nil,
     63 		},
     64 		{
     65 			Input:    []int{},
     66 			Expected: []int{},
     67 		},
     68 		{
     69 			Input:    []int{1, 2, 3, 4},
     70 			Expected: []int{1, 2, 3, 4},
     71 		},
     72 		{
     73 			Input:    []int{1, 2, 3, 4, 1, 2, 3, 4},
     74 			Expected: []int{1, 2, 3, 4},
     75 		},
     76 	}
     77 
     78 	for _, test := range tests {
     79 		res := Unique(test.Input)
     80 
     81 		if len(res) != len(test.Expected) {
     82 			t.Errorf(
     83 				"expected %d values, but got %d",
     84 				len(test.Expected), len(res),
     85 			)
     86 			return
     87 		}
     88 		for i := range test.Expected {
     89 			if res[i] != test.Expected[i] {
     90 				t.Errorf(
     91 					"expected value %v but got %v",
     92 					res[i], test.Expected[i],
     93 				)
     94 				return
     95 			}
     96 		}
     97 
     98 		t.Logf("%v %v", res, test.Expected)
     99 	}
    100 
    101 }