mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-11 06:27:54 +00:00
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:
@@ -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, "]")
|
||||
}
|
||||
|
||||
@@ -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]`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user