Use generics for DepSets
Use Go's generics for DepSets so they don't require a type-specific
wrapper and reflection.
Test: depsets_test.go
Change-Id: I22ba0b7d680d37d2cd05230b0f560d166c4dd20b
diff --git a/android/depset_test.go b/android/depset_test.go
index 955ccb0..376dffa 100644
--- a/android/depset_test.go
+++ b/android/depset_test.go
@@ -17,51 +17,40 @@
import (
"fmt"
"reflect"
- "strconv"
"strings"
"testing"
)
func ExampleDepSet_ToList_postordered() {
- a := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("a")).Build()
- b := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("b")).Transitive(a).Build()
- c := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("c")).Transitive(a).Build()
- d := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("d")).Transitive(b, c).Build()
+ a := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("a")).Build()
+ b := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("b")).Transitive(a).Build()
+ c := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("c")).Transitive(a).Build()
+ d := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("d")).Transitive(b, c).Build()
- fmt.Println(d.ToList().Strings())
+ fmt.Println(Paths(d.ToList()).Strings())
// Output: [a b c d]
}
func ExampleDepSet_ToList_preordered() {
- a := NewDepSetBuilder(PREORDER).Direct(PathForTesting("a")).Build()
- b := NewDepSetBuilder(PREORDER).Direct(PathForTesting("b")).Transitive(a).Build()
- c := NewDepSetBuilder(PREORDER).Direct(PathForTesting("c")).Transitive(a).Build()
- d := NewDepSetBuilder(PREORDER).Direct(PathForTesting("d")).Transitive(b, c).Build()
+ a := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("a")).Build()
+ b := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("b")).Transitive(a).Build()
+ c := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("c")).Transitive(a).Build()
+ d := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("d")).Transitive(b, c).Build()
- fmt.Println(d.ToList().Strings())
+ fmt.Println(Paths(d.ToList()).Strings())
// Output: [d b a c]
}
func ExampleDepSet_ToList_topological() {
- a := NewDepSetBuilder(TOPOLOGICAL).Direct(PathForTesting("a")).Build()
- b := NewDepSetBuilder(TOPOLOGICAL).Direct(PathForTesting("b")).Transitive(a).Build()
- c := NewDepSetBuilder(TOPOLOGICAL).Direct(PathForTesting("c")).Transitive(a).Build()
- d := NewDepSetBuilder(TOPOLOGICAL).Direct(PathForTesting("d")).Transitive(b, c).Build()
+ a := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("a")).Build()
+ b := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("b")).Transitive(a).Build()
+ c := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("c")).Transitive(a).Build()
+ d := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("d")).Transitive(b, c).Build()
- fmt.Println(d.ToList().Strings())
+ fmt.Println(Paths(d.ToList()).Strings())
// Output: [d b c a]
}
-func ExampleDepSet_ToSortedList() {
- a := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("a")).Build()
- b := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("b")).Transitive(a).Build()
- c := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("c")).Transitive(a).Build()
- d := NewDepSetBuilder(POSTORDER).Direct(PathForTesting("d")).Transitive(b, c).Build()
-
- fmt.Println(d.ToSortedList().Strings())
- // Output: [a b c d]
-}
-
// Tests based on Bazel's ExpanderTestBase.java to ensure compatibility
// https://github.com/bazelbuild/bazel/blob/master/src/test/java/com/google/devtools/build/lib/collect/nestedset/ExpanderTestBase.java
func TestDepSet(t *testing.T) {
@@ -74,13 +63,13 @@
tests := []struct {
name string
- depSet func(t *testing.T, order DepSetOrder) *DepSet
+ depSet func(t *testing.T, order DepSetOrder) *DepSet[Path]
postorder, preorder, topological []string
}{
{
name: "simple",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- return NewDepSet(order, Paths{c, a, b}, nil)
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ return NewDepSet[Path](order, Paths{c, a, b}, nil)
},
postorder: []string{"c", "a", "b"},
preorder: []string{"c", "a", "b"},
@@ -88,8 +77,8 @@
},
{
name: "simpleNoDuplicates",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- return NewDepSet(order, Paths{c, a, a, a, b}, nil)
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ return NewDepSet[Path](order, Paths{c, a, a, a, b}, nil)
},
postorder: []string{"c", "a", "b"},
preorder: []string{"c", "a", "b"},
@@ -97,9 +86,9 @@
},
{
name: "nesting",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- subset := NewDepSet(order, Paths{c, a, e}, nil)
- return NewDepSet(order, Paths{b, d}, []*DepSet{subset})
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ subset := NewDepSet[Path](order, Paths{c, a, e}, nil)
+ return NewDepSet[Path](order, Paths{b, d}, []*DepSet[Path]{subset})
},
postorder: []string{"c", "a", "e", "b", "d"},
preorder: []string{"b", "d", "c", "a", "e"},
@@ -107,14 +96,14 @@
},
{
name: "builderReuse",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
assertEquals := func(t *testing.T, w, g Paths) {
t.Helper()
if !reflect.DeepEqual(w, g) {
t.Errorf("want %q, got %q", w, g)
}
}
- builder := NewDepSetBuilder(order)
+ builder := NewDepSetBuilder[Path](order)
assertEquals(t, nil, builder.Build().ToList())
builder.Direct(b)
@@ -123,7 +112,7 @@
builder.Direct(d)
assertEquals(t, Paths{b, d}, builder.Build().ToList())
- child := NewDepSetBuilder(order).Direct(c, a, e).Build()
+ child := NewDepSetBuilder[Path](order).Direct(c, a, e).Build()
builder.Transitive(child)
return builder.Build()
},
@@ -133,9 +122,9 @@
},
{
name: "builderChaining",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- return NewDepSetBuilder(order).Direct(b).Direct(d).
- Transitive(NewDepSetBuilder(order).Direct(c, a, e).Build()).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ return NewDepSetBuilder[Path](order).Direct(b).Direct(d).
+ Transitive(NewDepSetBuilder[Path](order).Direct(c, a, e).Build()).Build()
},
postorder: []string{"c", "a", "e", "b", "d"},
preorder: []string{"b", "d", "c", "a", "e"},
@@ -143,9 +132,9 @@
},
{
name: "transitiveDepsHandledSeparately",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- subset := NewDepSetBuilder(order).Direct(c, a, e).Build()
- builder := NewDepSetBuilder(order)
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ subset := NewDepSetBuilder[Path](order).Direct(c, a, e).Build()
+ builder := NewDepSetBuilder[Path](order)
// The fact that we add the transitive subset between the Direct(b) and Direct(d)
// calls should not change the result.
builder.Direct(b)
@@ -159,9 +148,9 @@
},
{
name: "nestingNoDuplicates",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- subset := NewDepSetBuilder(order).Direct(c, a, e).Build()
- return NewDepSetBuilder(order).Direct(b, d, e).Transitive(subset).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ subset := NewDepSetBuilder[Path](order).Direct(c, a, e).Build()
+ return NewDepSetBuilder[Path](order).Direct(b, d, e).Transitive(subset).Build()
},
postorder: []string{"c", "a", "e", "b", "d"},
preorder: []string{"b", "d", "e", "c", "a"},
@@ -169,10 +158,10 @@
},
{
name: "chain",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- c := NewDepSetBuilder(order).Direct(c).Build()
- b := NewDepSetBuilder(order).Direct(b).Transitive(c).Build()
- a := NewDepSetBuilder(order).Direct(a).Transitive(b).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ c := NewDepSetBuilder[Path](order).Direct(c).Build()
+ b := NewDepSetBuilder[Path](order).Direct(b).Transitive(c).Build()
+ a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Build()
return a
},
@@ -182,11 +171,11 @@
},
{
name: "diamond",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- d := NewDepSetBuilder(order).Direct(d).Build()
- c := NewDepSetBuilder(order).Direct(c).Transitive(d).Build()
- b := NewDepSetBuilder(order).Direct(b).Transitive(d).Build()
- a := NewDepSetBuilder(order).Direct(a).Transitive(b).Transitive(c).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ d := NewDepSetBuilder[Path](order).Direct(d).Build()
+ c := NewDepSetBuilder[Path](order).Direct(c).Transitive(d).Build()
+ b := NewDepSetBuilder[Path](order).Direct(b).Transitive(d).Build()
+ a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Transitive(c).Build()
return a
},
@@ -196,12 +185,12 @@
},
{
name: "extendedDiamond",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- d := NewDepSetBuilder(order).Direct(d).Build()
- e := NewDepSetBuilder(order).Direct(e).Build()
- b := NewDepSetBuilder(order).Direct(b).Transitive(d).Transitive(e).Build()
- c := NewDepSetBuilder(order).Direct(c).Transitive(e).Transitive(d).Build()
- a := NewDepSetBuilder(order).Direct(a).Transitive(b).Transitive(c).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ d := NewDepSetBuilder[Path](order).Direct(d).Build()
+ e := NewDepSetBuilder[Path](order).Direct(e).Build()
+ b := NewDepSetBuilder[Path](order).Direct(b).Transitive(d).Transitive(e).Build()
+ c := NewDepSetBuilder[Path](order).Direct(c).Transitive(e).Transitive(d).Build()
+ a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Transitive(c).Build()
return a
},
postorder: []string{"d", "e", "b", "c", "a"},
@@ -210,13 +199,13 @@
},
{
name: "extendedDiamondRightArm",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- d := NewDepSetBuilder(order).Direct(d).Build()
- e := NewDepSetBuilder(order).Direct(e).Build()
- b := NewDepSetBuilder(order).Direct(b).Transitive(d).Transitive(e).Build()
- c2 := NewDepSetBuilder(order).Direct(c2).Transitive(e).Transitive(d).Build()
- c := NewDepSetBuilder(order).Direct(c).Transitive(c2).Build()
- a := NewDepSetBuilder(order).Direct(a).Transitive(b).Transitive(c).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ d := NewDepSetBuilder[Path](order).Direct(d).Build()
+ e := NewDepSetBuilder[Path](order).Direct(e).Build()
+ b := NewDepSetBuilder[Path](order).Direct(b).Transitive(d).Transitive(e).Build()
+ c2 := NewDepSetBuilder[Path](order).Direct(c2).Transitive(e).Transitive(d).Build()
+ c := NewDepSetBuilder[Path](order).Direct(c).Transitive(c2).Build()
+ a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Transitive(c).Build()
return a
},
postorder: []string{"d", "e", "b", "c2", "c", "a"},
@@ -225,10 +214,10 @@
},
{
name: "orderConflict",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- child1 := NewDepSetBuilder(order).Direct(a, b).Build()
- child2 := NewDepSetBuilder(order).Direct(b, a).Build()
- parent := NewDepSetBuilder(order).Transitive(child1).Transitive(child2).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ child1 := NewDepSetBuilder[Path](order).Direct(a, b).Build()
+ child2 := NewDepSetBuilder[Path](order).Direct(b, a).Build()
+ parent := NewDepSetBuilder[Path](order).Transitive(child1).Transitive(child2).Build()
return parent
},
postorder: []string{"a", "b"},
@@ -237,12 +226,12 @@
},
{
name: "orderConflictNested",
- depSet: func(t *testing.T, order DepSetOrder) *DepSet {
- a := NewDepSetBuilder(order).Direct(a).Build()
- b := NewDepSetBuilder(order).Direct(b).Build()
- child1 := NewDepSetBuilder(order).Transitive(a).Transitive(b).Build()
- child2 := NewDepSetBuilder(order).Transitive(b).Transitive(a).Build()
- parent := NewDepSetBuilder(order).Transitive(child1).Transitive(child2).Build()
+ depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
+ a := NewDepSetBuilder[Path](order).Direct(a).Build()
+ b := NewDepSetBuilder[Path](order).Direct(b).Build()
+ child1 := NewDepSetBuilder[Path](order).Transitive(a).Transitive(b).Build()
+ child2 := NewDepSetBuilder[Path](order).Transitive(b).Transitive(a).Build()
+ parent := NewDepSetBuilder[Path](order).Transitive(child1).Transitive(child2).Build()
return parent
},
postorder: []string{"a", "b"},
@@ -255,19 +244,19 @@
t.Run(tt.name, func(t *testing.T) {
t.Run("postorder", func(t *testing.T) {
depSet := tt.depSet(t, POSTORDER)
- if g, w := depSet.ToList().Strings(), tt.postorder; !reflect.DeepEqual(g, w) {
+ if g, w := Paths(depSet.ToList()).Strings(), tt.postorder; !reflect.DeepEqual(g, w) {
t.Errorf("expected ToList() = %q, got %q", w, g)
}
})
t.Run("preorder", func(t *testing.T) {
depSet := tt.depSet(t, PREORDER)
- if g, w := depSet.ToList().Strings(), tt.preorder; !reflect.DeepEqual(g, w) {
+ if g, w := Paths(depSet.ToList()).Strings(), tt.preorder; !reflect.DeepEqual(g, w) {
t.Errorf("expected ToList() = %q, got %q", w, g)
}
})
t.Run("topological", func(t *testing.T) {
depSet := tt.depSet(t, TOPOLOGICAL)
- if g, w := depSet.ToList().Strings(), tt.topological; !reflect.DeepEqual(g, w) {
+ if g, w := Paths(depSet.ToList()).Strings(), tt.topological; !reflect.DeepEqual(g, w) {
t.Errorf("expected ToList() = %q, got %q", w, g)
}
})
@@ -288,7 +277,7 @@
}
}
}()
- NewDepSet(order1, nil, []*DepSet{NewDepSet(order2, nil, nil)})
+ NewDepSet(order1, nil, []*DepSet[Path]{NewDepSet[Path](order2, nil, nil)})
t.Fatal("expected panic")
}
@@ -304,87 +293,3 @@
})
}
}
-
-func Test_firstUnique(t *testing.T) {
- f := func(t *testing.T, imp func([]string) []string, in, want []string) {
- t.Helper()
- out := imp(in)
- if !reflect.DeepEqual(out, want) {
- t.Errorf("incorrect output:")
- t.Errorf(" input: %#v", in)
- t.Errorf(" expected: %#v", want)
- t.Errorf(" got: %#v", out)
- }
- }
-
- for _, testCase := range firstUniqueStringsTestCases {
- t.Run("list", func(t *testing.T) {
- f(t, func(s []string) []string {
- return firstUniqueList(s).([]string)
- }, testCase.in, testCase.out)
- })
- t.Run("map", func(t *testing.T) {
- f(t, func(s []string) []string {
- return firstUniqueMap(s).([]string)
- }, testCase.in, testCase.out)
- })
- }
-}
-
-func Benchmark_firstUnique(b *testing.B) {
- implementations := []struct {
- name string
- f func([]string) []string
- }{
- {
- name: "list",
- f: func(slice []string) []string {
- return firstUniqueList(slice).([]string)
- },
- },
- {
- name: "map",
- f: func(slice []string) []string {
- return firstUniqueMap(slice).([]string)
- },
- },
- {
- name: "optimal",
- f: func(slice []string) []string {
- return firstUnique(slice).([]string)
- },
- },
- }
- const maxSize = 1024
- uniqueStrings := make([]string, maxSize)
- for i := range uniqueStrings {
- uniqueStrings[i] = strconv.Itoa(i)
- }
- sameString := make([]string, maxSize)
- for i := range sameString {
- sameString[i] = uniqueStrings[0]
- }
-
- f := func(b *testing.B, imp func([]string) []string, s []string) {
- for i := 0; i < b.N; i++ {
- b.ReportAllocs()
- s = append([]string(nil), s...)
- imp(s)
- }
- }
-
- for n := 1; n <= maxSize; n <<= 1 {
- b.Run(strconv.Itoa(n), func(b *testing.B) {
- for _, implementation := range implementations {
- b.Run(implementation.name, func(b *testing.B) {
- b.Run("same", func(b *testing.B) {
- f(b, implementation.f, sameString[:n])
- })
- b.Run("unique", func(b *testing.B) {
- f(b, implementation.f, uniqueStrings[:n])
- })
- })
- }
- })
- }
-}