v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/slice/flatten_test.go (about)

     1  package slice
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestFlatten(t *testing.T) {
     9  	t.Run("test0", func(t *testing.T) {
    10  		in := 1
    11  		want := []int{1}
    12  		got := Flatten[int](in)
    13  		if !reflect.DeepEqual(got, want) {
    14  			t.Errorf("Flatten() = %v, want %v", got, want)
    15  		}
    16  	})
    17  
    18  	t.Run("test0-ptr", func(t *testing.T) {
    19  		in := 1
    20  		want := []int{1}
    21  		got := Flatten[int](&in)
    22  		if !reflect.DeepEqual(got, want) {
    23  			t.Errorf("Flatten() = %v, want %v", got, want)
    24  		}
    25  	})
    26  
    27  	t.Run("test1", func(t *testing.T) {
    28  		in := []int{1, 2, 3, 4, 5}
    29  		want := []int{1, 2, 3, 4, 5}
    30  		got := Flatten[int](in)
    31  		if !reflect.DeepEqual(got, want) {
    32  			t.Errorf("Flatten() = %v, want %v", got, want)
    33  		}
    34  	})
    35  
    36  	t.Run("test1-ptr", func(t *testing.T) {
    37  		in := []int{1, 2, 3, 4, 5}
    38  		want := []int{1, 2, 3, 4, 5}
    39  		got := Flatten[int](&in)
    40  		if !reflect.DeepEqual(got, want) {
    41  			t.Errorf("Flatten() = %v, want %v", got, want)
    42  		}
    43  	})
    44  
    45  	t.Run("test1-ptr2", func(t *testing.T) {
    46  		in := []int{1, 2, 3, 4, 5}
    47  		want := []int{1, 2, 3, 4, 5}
    48  		inptr := &in
    49  		got := Flatten[int](&inptr)
    50  		if !reflect.DeepEqual(got, want) {
    51  			t.Errorf("Flatten() = %v, want %v", got, want)
    52  		}
    53  	})
    54  
    55  	t.Run("test2", func(t *testing.T) {
    56  		in := [][]int{{1, 2, 3}, {4, 5, 6}}
    57  		want := []int{1, 2, 3, 4, 5, 6}
    58  		got := Flatten[int](in)
    59  		if !reflect.DeepEqual(got, want) {
    60  			t.Errorf("Flatten() = %v, want %v", got, want)
    61  		}
    62  	})
    63  
    64  	t.Run("test2-ptr", func(t *testing.T) {
    65  		in := [][]int{{1, 2, 3}, {4, 5, 6}}
    66  		want := []int{1, 2, 3, 4, 5, 6}
    67  		got := Flatten[int](&in)
    68  		if !reflect.DeepEqual(got, want) {
    69  			t.Errorf("Flatten() = %v, want %v", got, want)
    70  		}
    71  	})
    72  
    73  	t.Run("test3", func(t *testing.T) {
    74  		in := [][][]int{{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}
    75  		want := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
    76  		got := Flatten[int](in)
    77  		if !reflect.DeepEqual(got, want) {
    78  			t.Errorf("Flatten() = %v, want %v", got, want)
    79  		}
    80  	})
    81  
    82  	t.Run("test3-ptr", func(t *testing.T) {
    83  		in := [][][]int{{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}
    84  		want := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
    85  		got := Flatten[int](&in)
    86  		if !reflect.DeepEqual(got, want) {
    87  			t.Errorf("Flatten() = %v, want %v", got, want)
    88  		}
    89  	})
    90  
    91  	t.Run("test3-ptr2", func(t *testing.T) {
    92  		in := [][][]int{{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}
    93  		want := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
    94  		inptr := &in
    95  		got := Flatten[int](&inptr)
    96  		if !reflect.DeepEqual(got, want) {
    97  			t.Errorf("Flatten() = %v, want %v", got, want)
    98  		}
    99  	})
   100  
   101  	t.Run("test1-array", func(t *testing.T) {
   102  		in := [...]int{1, 2, 3, 4, 5}
   103  		want := []int{1, 2, 3, 4, 5}
   104  		got := Flatten[int](in)
   105  		if !reflect.DeepEqual(got, want) {
   106  			t.Errorf("Flatten() = %v, want %v", got, want)
   107  		}
   108  	})
   109  
   110  	t.Run("test1-array-ptr", func(t *testing.T) {
   111  		in := [...]int{1, 2, 3, 4, 5}
   112  		want := []int{1, 2, 3, 4, 5}
   113  		got := Flatten[int](&in)
   114  		if !reflect.DeepEqual(got, want) {
   115  			t.Errorf("Flatten() = %v, want %v", got, want)
   116  		}
   117  	})
   118  
   119  	t.Run("test1-array-ptr2", func(t *testing.T) {
   120  		in := [...]int{1, 2, 3, 4, 5}
   121  		want := []int{1, 2, 3, 4, 5}
   122  		inptr := &in
   123  		got := Flatten[int](&inptr)
   124  		if !reflect.DeepEqual(got, want) {
   125  			t.Errorf("Flatten() = %v, want %v", got, want)
   126  		}
   127  	})
   128  
   129  	t.Run("test2-array", func(t *testing.T) {
   130  		in := [...][]int{{1, 2, 3}, {4, 5, 6}}
   131  		want := []int{1, 2, 3, 4, 5, 6}
   132  		got := Flatten[int](in)
   133  		if !reflect.DeepEqual(got, want) {
   134  			t.Errorf("Flatten() = %v, want %v", got, want)
   135  		}
   136  	})
   137  
   138  	t.Run("test2-array-ptr", func(t *testing.T) {
   139  		in := [...][]int{{1, 2, 3}, {4, 5, 6}}
   140  		want := []int{1, 2, 3, 4, 5, 6}
   141  		got := Flatten[int](&in)
   142  		if !reflect.DeepEqual(got, want) {
   143  			t.Errorf("Flatten() = %v, want %v", got, want)
   144  		}
   145  	})
   146  
   147  	t.Run("test2-array2", func(t *testing.T) {
   148  		in := [...][3]int{{1, 2, 3}, {4, 5, 6}}
   149  		want := []int{1, 2, 3, 4, 5, 6}
   150  		got := Flatten[int](in)
   151  		if !reflect.DeepEqual(got, want) {
   152  			t.Errorf("Flatten() = %v, want %v", got, want)
   153  		}
   154  	})
   155  
   156  	t.Run("test2-array2-ptr", func(t *testing.T) {
   157  		in := [...][3]int{{1, 2, 3}, {4, 5, 6}}
   158  		want := []int{1, 2, 3, 4, 5, 6}
   159  		got := Flatten[int](&in)
   160  		if !reflect.DeepEqual(got, want) {
   161  			t.Errorf("Flatten() = %v, want %v", got, want)
   162  		}
   163  	})
   164  
   165  	t.Run("invalid-type", func(t *testing.T) {
   166  		in := complex(0, 0)
   167  		want := []int(nil)
   168  		got := Flatten[int](in)
   169  		if !reflect.DeepEqual(got, want) {
   170  			t.Errorf("Flatten() = %v, want %v", got, want)
   171  		}
   172  	})
   173  
   174  	t.Run("struct", func(t *testing.T) {
   175  		type S struct {
   176  			A int
   177  			B int
   178  		}
   179  		in := S{1, 2}
   180  		want := []int{1, 2}
   181  		got := Flatten[int](in)
   182  		if !reflect.DeepEqual(got, want) {
   183  			t.Errorf("Flatten() = %v, want %v", got, want)
   184  		}
   185  	})
   186  
   187  	t.Run("struct-ptr", func(t *testing.T) {
   188  		type S struct {
   189  			A int
   190  			B int
   191  		}
   192  		in := S{1, 2}
   193  		want := []int{1, 2}
   194  		got := Flatten[int](&in)
   195  		if !reflect.DeepEqual(got, want) {
   196  			t.Errorf("Flatten() = %v, want %v", got, want)
   197  		}
   198  	})
   199  
   200  	t.Run("struct2", func(t *testing.T) {
   201  		type S struct {
   202  			A int
   203  			B struct {
   204  				C           int
   205  				D           int
   206  				InvalidType complex128
   207  			}
   208  		}
   209  		in := S{1, struct {
   210  			C           int
   211  			D           int
   212  			InvalidType complex128
   213  		}{
   214  			C:           2,
   215  			D:           3,
   216  			InvalidType: 10 + 10i,
   217  		},
   218  		}
   219  		want := []int{1, 2, 3}
   220  		got := Flatten[int](in)
   221  		if !reflect.DeepEqual(got, want) {
   222  			t.Errorf("Flatten() = %v, want %v", got, want)
   223  		}
   224  	})
   225  
   226  	t.Run("map0", func(t *testing.T) {
   227  		in := map[int]int{}
   228  		want := []int(nil)
   229  		got := Flatten[int](in)
   230  		if !reflect.DeepEqual(got, want) {
   231  			t.Errorf("Flatten() = %v, want %v", got, want)
   232  		}
   233  	})
   234  
   235  	t.Run("map1", func(t *testing.T) {
   236  		in := map[int]int{1: 2}
   237  		want := []int{1, 2}
   238  		got := Flatten[int](in)
   239  		if !reflect.DeepEqual(got, want) {
   240  			t.Errorf("Flatten() = %v, want %v", got, want)
   241  		}
   242  	})
   243  
   244  	t.Run("map1-ptr", func(t *testing.T) {
   245  		in := map[int]int{1: 2}
   246  		want := []int{1, 2}
   247  		got := Flatten[int](&in)
   248  		if !reflect.DeepEqual(got, want) {
   249  			t.Errorf("Flatten() = %v, want %v", got, want)
   250  		}
   251  	})
   252  }