github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/android/util_test.go (about)

     1  // Copyright 2017 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package android
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  var firstUniqueStringsTestCases = []struct {
    23  	in  []string
    24  	out []string
    25  }{
    26  	{
    27  		in:  []string{"a"},
    28  		out: []string{"a"},
    29  	},
    30  	{
    31  		in:  []string{"a", "b"},
    32  		out: []string{"a", "b"},
    33  	},
    34  	{
    35  		in:  []string{"a", "a"},
    36  		out: []string{"a"},
    37  	},
    38  	{
    39  		in:  []string{"a", "b", "a"},
    40  		out: []string{"a", "b"},
    41  	},
    42  	{
    43  		in:  []string{"b", "a", "a"},
    44  		out: []string{"b", "a"},
    45  	},
    46  	{
    47  		in:  []string{"a", "a", "b"},
    48  		out: []string{"a", "b"},
    49  	},
    50  	{
    51  		in:  []string{"a", "b", "a", "b"},
    52  		out: []string{"a", "b"},
    53  	},
    54  	{
    55  		in:  []string{"liblog", "libdl", "libc++", "libdl", "libc", "libm"},
    56  		out: []string{"liblog", "libdl", "libc++", "libc", "libm"},
    57  	},
    58  }
    59  
    60  func TestFirstUniqueStrings(t *testing.T) {
    61  	for _, testCase := range firstUniqueStringsTestCases {
    62  		out := FirstUniqueStrings(testCase.in)
    63  		if !reflect.DeepEqual(out, testCase.out) {
    64  			t.Errorf("incorrect output:")
    65  			t.Errorf("     input: %#v", testCase.in)
    66  			t.Errorf("  expected: %#v", testCase.out)
    67  			t.Errorf("       got: %#v", out)
    68  		}
    69  	}
    70  }
    71  
    72  var lastUniqueStringsTestCases = []struct {
    73  	in  []string
    74  	out []string
    75  }{
    76  	{
    77  		in:  []string{"a"},
    78  		out: []string{"a"},
    79  	},
    80  	{
    81  		in:  []string{"a", "b"},
    82  		out: []string{"a", "b"},
    83  	},
    84  	{
    85  		in:  []string{"a", "a"},
    86  		out: []string{"a"},
    87  	},
    88  	{
    89  		in:  []string{"a", "b", "a"},
    90  		out: []string{"b", "a"},
    91  	},
    92  	{
    93  		in:  []string{"b", "a", "a"},
    94  		out: []string{"b", "a"},
    95  	},
    96  	{
    97  		in:  []string{"a", "a", "b"},
    98  		out: []string{"a", "b"},
    99  	},
   100  	{
   101  		in:  []string{"a", "b", "a", "b"},
   102  		out: []string{"a", "b"},
   103  	},
   104  	{
   105  		in:  []string{"liblog", "libdl", "libc++", "libdl", "libc", "libm"},
   106  		out: []string{"liblog", "libc++", "libdl", "libc", "libm"},
   107  	},
   108  }
   109  
   110  func TestLastUniqueStrings(t *testing.T) {
   111  	for _, testCase := range lastUniqueStringsTestCases {
   112  		out := LastUniqueStrings(testCase.in)
   113  		if !reflect.DeepEqual(out, testCase.out) {
   114  			t.Errorf("incorrect output:")
   115  			t.Errorf("     input: %#v", testCase.in)
   116  			t.Errorf("  expected: %#v", testCase.out)
   117  			t.Errorf("       got: %#v", out)
   118  		}
   119  	}
   120  }
   121  
   122  func TestJoinWithPrefix(t *testing.T) {
   123  	testcases := []struct {
   124  		name     string
   125  		input    []string
   126  		expected string
   127  	}{
   128  		{
   129  			name:     "zero_inputs",
   130  			input:    []string{},
   131  			expected: "",
   132  		},
   133  		{
   134  			name:     "one_input",
   135  			input:    []string{"a"},
   136  			expected: "prefix:a",
   137  		},
   138  		{
   139  			name:     "two_inputs",
   140  			input:    []string{"a", "b"},
   141  			expected: "prefix:a prefix:b",
   142  		},
   143  	}
   144  
   145  	prefix := "prefix:"
   146  
   147  	for _, testCase := range testcases {
   148  		t.Run(testCase.name, func(t *testing.T) {
   149  			out := JoinWithPrefix(testCase.input, prefix)
   150  			if out != testCase.expected {
   151  				t.Errorf("incorrect output:")
   152  				t.Errorf("     input: %#v", testCase.input)
   153  				t.Errorf("    prefix: %#v", prefix)
   154  				t.Errorf("  expected: %#v", testCase.expected)
   155  				t.Errorf("       got: %#v", out)
   156  			}
   157  		})
   158  	}
   159  }
   160  
   161  func TestIndexList(t *testing.T) {
   162  	input := []string{"a", "b", "c"}
   163  
   164  	testcases := []struct {
   165  		key      string
   166  		expected int
   167  	}{
   168  		{
   169  			key:      "a",
   170  			expected: 0,
   171  		},
   172  		{
   173  			key:      "b",
   174  			expected: 1,
   175  		},
   176  		{
   177  			key:      "c",
   178  			expected: 2,
   179  		},
   180  		{
   181  			key:      "X",
   182  			expected: -1,
   183  		},
   184  	}
   185  
   186  	for _, testCase := range testcases {
   187  		t.Run(testCase.key, func(t *testing.T) {
   188  			out := IndexList(testCase.key, input)
   189  			if out != testCase.expected {
   190  				t.Errorf("incorrect output:")
   191  				t.Errorf("       key: %#v", testCase.key)
   192  				t.Errorf("     input: %#v", input)
   193  				t.Errorf("  expected: %#v", testCase.expected)
   194  				t.Errorf("       got: %#v", out)
   195  			}
   196  		})
   197  	}
   198  }
   199  
   200  func TestInList(t *testing.T) {
   201  	input := []string{"a"}
   202  
   203  	testcases := []struct {
   204  		key      string
   205  		expected bool
   206  	}{
   207  		{
   208  			key:      "a",
   209  			expected: true,
   210  		},
   211  		{
   212  			key:      "X",
   213  			expected: false,
   214  		},
   215  	}
   216  
   217  	for _, testCase := range testcases {
   218  		t.Run(testCase.key, func(t *testing.T) {
   219  			out := InList(testCase.key, input)
   220  			if out != testCase.expected {
   221  				t.Errorf("incorrect output:")
   222  				t.Errorf("       key: %#v", testCase.key)
   223  				t.Errorf("     input: %#v", input)
   224  				t.Errorf("  expected: %#v", testCase.expected)
   225  				t.Errorf("       got: %#v", out)
   226  			}
   227  		})
   228  	}
   229  }
   230  
   231  func TestPrefixInList(t *testing.T) {
   232  	prefixes := []string{"a", "b"}
   233  
   234  	testcases := []struct {
   235  		str      string
   236  		expected bool
   237  	}{
   238  		{
   239  			str:      "a-example",
   240  			expected: true,
   241  		},
   242  		{
   243  			str:      "b-example",
   244  			expected: true,
   245  		},
   246  		{
   247  			str:      "X-example",
   248  			expected: false,
   249  		},
   250  	}
   251  
   252  	for _, testCase := range testcases {
   253  		t.Run(testCase.str, func(t *testing.T) {
   254  			out := PrefixInList(testCase.str, prefixes)
   255  			if out != testCase.expected {
   256  				t.Errorf("incorrect output:")
   257  				t.Errorf("       str: %#v", testCase.str)
   258  				t.Errorf("  prefixes: %#v", prefixes)
   259  				t.Errorf("  expected: %#v", testCase.expected)
   260  				t.Errorf("       got: %#v", out)
   261  			}
   262  		})
   263  	}
   264  }
   265  
   266  func TestFilterList(t *testing.T) {
   267  	input := []string{"a", "b", "c", "c", "b", "d", "a"}
   268  	filter := []string{"a", "c"}
   269  	remainder, filtered := FilterList(input, filter)
   270  
   271  	expected := []string{"b", "b", "d"}
   272  	if !reflect.DeepEqual(remainder, expected) {
   273  		t.Errorf("incorrect remainder output:")
   274  		t.Errorf("     input: %#v", input)
   275  		t.Errorf("    filter: %#v", filter)
   276  		t.Errorf("  expected: %#v", expected)
   277  		t.Errorf("       got: %#v", remainder)
   278  	}
   279  
   280  	expected = []string{"a", "c", "c", "a"}
   281  	if !reflect.DeepEqual(filtered, expected) {
   282  		t.Errorf("incorrect filtered output:")
   283  		t.Errorf("     input: %#v", input)
   284  		t.Errorf("    filter: %#v", filter)
   285  		t.Errorf("  expected: %#v", expected)
   286  		t.Errorf("       got: %#v", filtered)
   287  	}
   288  }
   289  
   290  func TestRemoveListFromList(t *testing.T) {
   291  	input := []string{"a", "b", "c", "d", "a", "c", "d"}
   292  	filter := []string{"a", "c"}
   293  	expected := []string{"b", "d", "d"}
   294  	out := RemoveListFromList(input, filter)
   295  	if !reflect.DeepEqual(out, expected) {
   296  		t.Errorf("incorrect output:")
   297  		t.Errorf("     input: %#v", input)
   298  		t.Errorf("    filter: %#v", filter)
   299  		t.Errorf("  expected: %#v", expected)
   300  		t.Errorf("       got: %#v", out)
   301  	}
   302  }
   303  
   304  func TestRemoveFromList(t *testing.T) {
   305  	testcases := []struct {
   306  		name          string
   307  		key           string
   308  		input         []string
   309  		expectedFound bool
   310  		expectedOut   []string
   311  	}{
   312  		{
   313  			name:          "remove_one_match",
   314  			key:           "a",
   315  			input:         []string{"a", "b", "c"},
   316  			expectedFound: true,
   317  			expectedOut:   []string{"b", "c"},
   318  		},
   319  		{
   320  			name:          "remove_three_matches",
   321  			key:           "a",
   322  			input:         []string{"a", "b", "a", "c", "a"},
   323  			expectedFound: true,
   324  			expectedOut:   []string{"b", "c"},
   325  		},
   326  		{
   327  			name:          "remove_zero_matches",
   328  			key:           "X",
   329  			input:         []string{"a", "b", "a", "c", "a"},
   330  			expectedFound: false,
   331  			expectedOut:   []string{"a", "b", "a", "c", "a"},
   332  		},
   333  		{
   334  			name:          "remove_all_matches",
   335  			key:           "a",
   336  			input:         []string{"a", "a", "a", "a"},
   337  			expectedFound: true,
   338  			expectedOut:   []string{},
   339  		},
   340  	}
   341  
   342  	for _, testCase := range testcases {
   343  		t.Run(testCase.name, func(t *testing.T) {
   344  			found, out := RemoveFromList(testCase.key, testCase.input)
   345  			if found != testCase.expectedFound {
   346  				t.Errorf("incorrect output:")
   347  				t.Errorf("       key: %#v", testCase.key)
   348  				t.Errorf("     input: %#v", testCase.input)
   349  				t.Errorf("  expected: %#v", testCase.expectedFound)
   350  				t.Errorf("       got: %#v", found)
   351  			}
   352  			if !reflect.DeepEqual(out, testCase.expectedOut) {
   353  				t.Errorf("incorrect output:")
   354  				t.Errorf("       key: %#v", testCase.key)
   355  				t.Errorf("     input: %#v", testCase.input)
   356  				t.Errorf("  expected: %#v", testCase.expectedOut)
   357  				t.Errorf("       got: %#v", out)
   358  			}
   359  		})
   360  	}
   361  }