Source file src/simd/archsimd/_gen/unify/filter_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package unify
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestValueFilter(t *testing.T) {
    13  	read := func(s string) *Value {
    14  		t.Helper()
    15  		c, err := Read(strings.NewReader(s), "<test>", ReadOpts{})
    16  		if err != nil {
    17  			t.Fatal(err)
    18  		}
    19  		return c.val
    20  	}
    21  
    22  	for _, test := range []struct {
    23  		probe, v string
    24  		want     bool
    25  	}{
    26  		{"{a: x}", "{a: x}", true},
    27  		{"{a: x}", "{a: y}", false},
    28  		{"{a: x}", `{a: "x|y"}`, true},
    29  		{"{a: x}", `{a: "y|z"}`, false},
    30  		{`{a: "y|z"}`, "{a: x}", false},
    31  		// Unify doesn't check whether regexps intersect.
    32  		{`{a: "w|x"}`, `{a: "y|z"}`, true},
    33  		// Fields only on one side don't matter.
    34  		{"{a: x, b: y}", "{b: z, c: x}", false},
    35  		{"{a: x}", "{b: y}", true},
    36  		// Top matches anything.
    37  		{"{a: _, b: x}", "{a: {c: d}, b: y}", false},
    38  		{"{a: x, b: y}", "{a: _, b: z}", false},
    39  		// Unifying a with a non-String could bind variables or fail with an
    40  		// error, so the filter can't predict anything about b.
    41  		{"{a: x, b: y}", "{a: {c: d}, b: z}", true},
    42  		{"{a: $v, b: y}", "{a: x, b: z}", true},
    43  		{"{a: [x], b: y}", "{a: [x], b: z}", true},
    44  		// But it can reject based on earlier fields.
    45  		{"{a: x, b: {c: d}}", "{a: y, b: z}", false},
    46  		// The filter only applies to Defs.
    47  		{"{a: x}", "x", true},
    48  		{"{a: x}", "$v", true},
    49  	} {
    50  		f := newValueFilter(read(test.probe))
    51  		if got := f.mayUnify(read(test.v)); got != test.want {
    52  			t.Errorf("filter %s: mayUnify(%s) = %v, want %v", test.probe, test.v, got, test.want)
    53  		}
    54  	}
    55  }
    56  
    57  // TestValueFilterError checks that filtering doesn't hide errors from fields
    58  // that unification visits before a mismatched field.
    59  func TestValueFilterError(t *testing.T) {
    60  	read := func(s string) Closure {
    61  		t.Helper()
    62  		c, err := Read(strings.NewReader(s), "<test>", ReadOpts{})
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  		return c
    67  	}
    68  	_, err := Unify(read("!sum [{a: x, b: y}, {a: {c: d}, b: z}]"), read("{a: x, b: y}"))
    69  	if err == nil || !strings.Contains(err.Error(), "kind mismatch") {
    70  		t.Errorf("want kind mismatch error, got %v", err)
    71  	}
    72  }
    73  

View as plain text