1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
import (
"reflect"
"strconv"
"testing"
"github.com/google/blueprint/proptools"
)
type printfIntoPropertyTestCase struct {
in string
val interface{}
out string
err bool
}
var printfIntoPropertyTestCases = []printfIntoPropertyTestCase{
{
in: "%d",
val: 0,
out: "0",
},
{
in: "%d",
val: 1,
out: "1",
},
{
in: "%d",
val: 2,
out: "2",
},
{
in: "%d",
val: false,
out: "0",
},
{
in: "%d",
val: true,
out: "1",
},
{
in: "%d",
val: -1,
out: "-1",
},
{
in: "-DA=%d",
val: 1,
out: "-DA=1",
},
{
in: "-DA=%du",
val: 1,
out: "-DA=1u",
},
{
in: "-DA=%s",
val: "abc",
out: "-DA=abc",
},
{
in: `-DA="%s"`,
val: "abc",
out: `-DA="abc"`,
},
{
in: "%%",
err: true,
},
{
in: "%d%s",
err: true,
},
{
in: "%d,%s",
err: true,
},
{
in: "%d",
val: "",
err: true,
},
{
in: "%d",
val: 1.5,
err: true,
},
{
in: "%f",
val: 1.5,
err: true,
},
}
func TestPrintfIntoProperty(t *testing.T) {
for _, testCase := range printfIntoPropertyTestCases {
s := testCase.in
v := reflect.ValueOf(&s).Elem()
err := printfIntoProperty(v, testCase.val)
if err != nil && !testCase.err {
t.Errorf("unexpected error %s", err)
} else if err == nil && testCase.err {
t.Errorf("expected error")
} else if err == nil && v.String() != testCase.out {
t.Errorf("expected %q got %q", testCase.out, v.String())
}
}
}
type testProductVariableModule struct {
ModuleBase
}
func (m *testProductVariableModule) GenerateAndroidBuildActions(ctx ModuleContext) {
}
var testProductVariableProperties = struct {
Product_variables struct {
Eng struct {
Srcs []string
Cflags []string
}
}
}{}
func testProductVariableModuleFactoryFactory(props interface{}) func() Module {
return func() Module {
m := &testProductVariableModule{}
clonedProps := proptools.CloneProperties(reflect.ValueOf(props)).Interface()
m.AddProperties(clonedProps)
// Set a default soongConfigVariableProperties, this will be used as the input to the property struct filter
// for this test module.
m.variableProperties = testProductVariableProperties
InitAndroidModule(m)
return m
}
}
func TestProductVariables(t *testing.T) {
ctx := NewTestContext()
// A module type that has a srcs property but not a cflags property.
ctx.RegisterModuleType("module1", testProductVariableModuleFactoryFactory(&struct {
Srcs []string
}{}))
// A module type that has a cflags property but not a srcs property.
ctx.RegisterModuleType("module2", testProductVariableModuleFactoryFactory(&struct {
Cflags []string
}{}))
// A module type that does not have any properties that match product_variables.
ctx.RegisterModuleType("module3", testProductVariableModuleFactoryFactory(&struct {
Foo []string
}{}))
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("variable", VariableMutator).Parallel()
})
// Test that a module can use one product variable even if it doesn't have all the properties
// supported by that product variable.
bp := `
module1 {
name: "foo",
product_variables: {
eng: {
srcs: ["foo.c"],
},
},
}
module2 {
name: "bar",
product_variables: {
eng: {
cflags: ["-DBAR"],
},
},
}
module3 {
name: "baz",
}
`
config := TestConfig(buildDir, nil, bp, nil)
config.TestProductVariables.Eng = proptools.BoolPtr(true)
ctx.Register(config)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
}
var testProductVariableDefaultsProperties = struct {
Product_variables struct {
Eng struct {
Foo []string
Bar []string
}
}
}{}
type productVariablesDefaultsTestProperties struct {
Foo []string
}
type productVariablesDefaultsTestProperties2 struct {
Foo []string
Bar []string
}
type productVariablesDefaultsTestModule struct {
ModuleBase
DefaultableModuleBase
properties productVariablesDefaultsTestProperties
}
func (d *productVariablesDefaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
ctx.Build(pctx, BuildParams{
Rule: Touch,
Output: PathForModuleOut(ctx, "out"),
})
}
func productVariablesDefaultsTestModuleFactory() Module {
module := &productVariablesDefaultsTestModule{}
module.AddProperties(&module.properties)
module.variableProperties = testProductVariableDefaultsProperties
InitAndroidModule(module)
InitDefaultableModule(module)
return module
}
type productVariablesDefaultsTestDefaults struct {
ModuleBase
DefaultsModuleBase
}
func productVariablesDefaultsTestDefaultsFactory() Module {
defaults := &productVariablesDefaultsTestDefaults{}
defaults.AddProperties(&productVariablesDefaultsTestProperties{})
defaults.AddProperties(&productVariablesDefaultsTestProperties2{})
defaults.variableProperties = testProductVariableDefaultsProperties
InitDefaultsModule(defaults)
return defaults
}
// Test a defaults module that supports more product variable properties than the target module.
func TestProductVariablesDefaults(t *testing.T) {
bp := `
defaults {
name: "defaults",
product_variables: {
eng: {
foo: ["product_variable_defaults"],
bar: ["product_variable_defaults"],
},
},
foo: ["defaults"],
bar: ["defaults"],
}
test {
name: "foo",
defaults: ["defaults"],
foo: ["module"],
product_variables: {
eng: {
foo: ["product_variable_module"],
},
},
}
`
config := TestConfig(buildDir, nil, bp, nil)
config.TestProductVariables.Eng = boolPtr(true)
ctx := NewTestContext()
ctx.RegisterModuleType("test", productVariablesDefaultsTestModuleFactory)
ctx.RegisterModuleType("defaults", productVariablesDefaultsTestDefaultsFactory)
ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("variable", VariableMutator).Parallel()
})
ctx.Register(config)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
foo := ctx.ModuleForTests("foo", "").Module().(*productVariablesDefaultsTestModule)
want := []string{"defaults", "module", "product_variable_defaults", "product_variable_module"}
if g, w := foo.properties.Foo, want; !reflect.DeepEqual(g, w) {
t.Errorf("expected foo %q, got %q", w, g)
}
}
func BenchmarkSliceToTypeArray(b *testing.B) {
for _, n := range []int{1, 2, 4, 8, 100} {
var propStructs []interface{}
for i := 0; i < n; i++ {
propStructs = append(propStructs, &struct {
A *string
B string
}{})
}
b.Run(strconv.Itoa(n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sliceToTypeArray(propStructs)
}
})
}
}
|