tools/src/cts/query: Add Tree

A tree of query to data. Has utilities for reducing the tree based on a
custom merger function.

Bug: dawn:1342
Change-Id: If1c0503be05ee04bcf55dd5bdc9aa3caf6fb56ee
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/87222
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2022-04-20 19:10:42 +00:00
parent 4c9b72b4fa
commit 1a10b73552
6 changed files with 1425 additions and 1 deletions

View File

@@ -14,7 +14,10 @@
package container
import "sort"
import (
"fmt"
"sort"
)
// Set is a generic unordered set, which wrap's go's builtin 'map'.
// T is the set key, which must match the 'key' constraint.
@@ -98,3 +101,24 @@ func (s Set[T]) List() []T {
sort.Slice(out, func(i, j int) bool { return out[i] < out[j] })
return out
}
// One returns a random item from the set, or an empty item if the set is empty.
func (s Set[T]) One() T {
for item := range s {
return item
}
var zero T
return zero
}
// Format writes the Target to the fmt.State
func (s Set[T]) Format(f fmt.State, verb rune) {
fmt.Fprint(f, "[")
for i, item := range s.List() {
if i > 0 {
fmt.Fprint(f, ", ")
}
fmt.Fprint(f, item)
}
fmt.Fprint(f, "]")
}

View File

@@ -15,6 +15,7 @@
package container_test
import (
"fmt"
"testing"
"dawn.googlesource.com/dawn/tools/src/container"
@@ -143,3 +144,19 @@ func TestSetRemoveAll(t *testing.T) {
expectEq(t, "len(s)", len(s), 1)
expectEq(t, "s.List()", s.List(), []string{"b"})
}
func TestSetOne(t *testing.T) {
expectEq(t, "NewSet[string]().One()", container.NewSet[string]().One(), "")
expectEq(t, `NewSet("x").One()`, container.NewSet("x").One(), "x")
if got := container.NewSet("x", "y").One(); got != "x" && got != "y" {
t.Errorf(`NewSet("x", "y").One() returned "%v"`, got)
}
}
func TestFormat(t *testing.T) {
expectEq(t, "NewSet[string]()", fmt.Sprint(container.NewSet[string]()), "[]")
expectEq(t, `NewSet("x")`, fmt.Sprint(container.NewSet("x")), `[x]`)
expectEq(t, `NewSet(1)`, fmt.Sprint(container.NewSet(1)), `[1]`)
expectEq(t, `NewSet("y", "x")`, fmt.Sprint(container.NewSet("y", "x")), `[x, y]`)
expectEq(t, `NewSet(3, 1, 2)`, fmt.Sprint(container.NewSet(3, 1, 2)), `[1, 2, 3]`)
}