Source file src/go/types/typeterm_test.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  
     3  // Copyright 2021 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package types
     8  
     9  import (
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  var myInt = func() Type {
    15  	tname := NewTypeName(nopos, nil, "myInt", nil)
    16  	return NewNamed(tname, Typ[Int], nil)
    17  }()
    18  
    19  var testTerms = map[string]*term{
    20  	"∅":       nil,
    21  	"𝓤":       {},
    22  	"int":     {false, Typ[Int]},
    23  	"~int":    {true, Typ[Int]},
    24  	"string":  {false, Typ[String]},
    25  	"~string": {true, Typ[String]},
    26  	"myInt":   {false, myInt},
    27  }
    28  
    29  func TestTermString(t *testing.T) {
    30  	for want, x := range testTerms {
    31  		if got := x.String(); got != want {
    32  			t.Errorf("%v.String() == %v; want %v", x, got, want)
    33  		}
    34  	}
    35  }
    36  
    37  func split(s string, n int) []string {
    38  	r := strings.Split(s, " ")
    39  	if len(r) != n {
    40  		panic("invalid test case: " + s)
    41  	}
    42  	return r
    43  }
    44  
    45  func testTerm(name string) *term {
    46  	r, ok := testTerms[name]
    47  	if !ok {
    48  		panic("invalid test argument: " + name)
    49  	}
    50  	return r
    51  }
    52  
    53  func TestTermEqual(t *testing.T) {
    54  	for _, test := range []string{
    55  		"∅ ∅ T",
    56  		"𝓤 𝓤 T",
    57  		"int int T",
    58  		"~int ~int T",
    59  		"myInt myInt T",
    60  		"∅ 𝓤 F",
    61  		"∅ int F",
    62  		"∅ ~int F",
    63  		"𝓤 int F",
    64  		"𝓤 ~int F",
    65  		"𝓤 myInt F",
    66  		"int ~int F",
    67  		"int myInt F",
    68  		"~int myInt F",
    69  	} {
    70  		args := split(test, 3)
    71  		x := testTerm(args[0])
    72  		y := testTerm(args[1])
    73  		want := args[2] == "T"
    74  		if got := x.equal(y); got != want {
    75  			t.Errorf("%v.equal(%v) = %v; want %v", x, y, got, want)
    76  		}
    77  		// equal is symmetric
    78  		x, y = y, x
    79  		if got := x.equal(y); got != want {
    80  			t.Errorf("%v.equal(%v) = %v; want %v", x, y, got, want)
    81  		}
    82  	}
    83  }
    84  
    85  func TestTermUnion(t *testing.T) {
    86  	for _, test := range []string{
    87  		"∅ ∅ ∅ ∅",
    88  		"∅ 𝓤 𝓤 ∅",
    89  		"∅ int int ∅",
    90  		"∅ ~int ~int ∅",
    91  		"∅ myInt myInt ∅",
    92  		"𝓤 𝓤 𝓤 ∅",
    93  		"𝓤 int 𝓤 ∅",
    94  		"𝓤 ~int 𝓤 ∅",
    95  		"𝓤 myInt 𝓤 ∅",
    96  		"int int int ∅",
    97  		"int ~int ~int ∅",
    98  		"int string int string",
    99  		"int ~string int ~string",
   100  		"int myInt int myInt",
   101  		"~int ~string ~int ~string",
   102  		"~int myInt ~int ∅",
   103  
   104  		// union is symmetric, but the result order isn't - repeat symmetric cases explicitly
   105  		"𝓤 ∅ 𝓤 ∅",
   106  		"int ∅ int ∅",
   107  		"~int ∅ ~int ∅",
   108  		"myInt ∅ myInt ∅",
   109  		"int 𝓤 𝓤 ∅",
   110  		"~int 𝓤 𝓤 ∅",
   111  		"myInt 𝓤 𝓤 ∅",
   112  		"~int int ~int ∅",
   113  		"string int string int",
   114  		"~string int ~string int",
   115  		"myInt int myInt int",
   116  		"~string ~int ~string ~int",
   117  		"myInt ~int ~int ∅",
   118  	} {
   119  		args := split(test, 4)
   120  		x := testTerm(args[0])
   121  		y := testTerm(args[1])
   122  		want1 := testTerm(args[2])
   123  		want2 := testTerm(args[3])
   124  		if got1, got2 := x.union(y); !got1.equal(want1) || !got2.equal(want2) {
   125  			t.Errorf("%v.union(%v) = %v, %v; want %v, %v", x, y, got1, got2, want1, want2)
   126  		}
   127  	}
   128  }
   129  
   130  func TestTermIntersection(t *testing.T) {
   131  	for _, test := range []string{
   132  		"∅ ∅ ∅",
   133  		"∅ 𝓤 ∅",
   134  		"∅ int ∅",
   135  		"∅ ~int ∅",
   136  		"∅ myInt ∅",
   137  		"𝓤 𝓤 𝓤",
   138  		"𝓤 int int",
   139  		"𝓤 ~int ~int",
   140  		"𝓤 myInt myInt",
   141  		"int int int",
   142  		"int ~int int",
   143  		"int string ∅",
   144  		"int ~string ∅",
   145  		"int string ∅",
   146  		"~int ~string ∅",
   147  		"~int myInt myInt",
   148  	} {
   149  		args := split(test, 3)
   150  		x := testTerm(args[0])
   151  		y := testTerm(args[1])
   152  		want := testTerm(args[2])
   153  		if got := x.intersect(y); !got.equal(want) {
   154  			t.Errorf("%v.intersect(%v) = %v; want %v", x, y, got, want)
   155  		}
   156  		// intersect is symmetric
   157  		x, y = y, x
   158  		if got := x.intersect(y); !got.equal(want) {
   159  			t.Errorf("%v.intersect(%v) = %v; want %v", x, y, got, want)
   160  		}
   161  	}
   162  }
   163  
   164  func TestTermIncludes(t *testing.T) {
   165  	for _, test := range []string{
   166  		"∅ int F",
   167  		"𝓤 int T",
   168  		"int int T",
   169  		"~int int T",
   170  		"~int myInt T",
   171  		"string int F",
   172  		"~string int F",
   173  		"myInt int F",
   174  	} {
   175  		args := split(test, 3)
   176  		x := testTerm(args[0])
   177  		y := testTerm(args[1]).typ
   178  		want := args[2] == "T"
   179  		if got := x.includes(y); got != want {
   180  			t.Errorf("%v.includes(%v) = %v; want %v", x, y, got, want)
   181  		}
   182  	}
   183  }
   184  
   185  func TestTermSubsetOf(t *testing.T) {
   186  	for _, test := range []string{
   187  		"∅ ∅ T",
   188  		"𝓤 𝓤 T",
   189  		"int int T",
   190  		"~int ~int T",
   191  		"myInt myInt T",
   192  		"∅ 𝓤 T",
   193  		"∅ int T",
   194  		"∅ ~int T",
   195  		"∅ myInt T",
   196  		"𝓤 int F",
   197  		"𝓤 ~int F",
   198  		"𝓤 myInt F",
   199  		"int ~int T",
   200  		"int myInt F",
   201  		"~int myInt F",
   202  		"myInt int F",
   203  		"myInt ~int T",
   204  	} {
   205  		args := split(test, 3)
   206  		x := testTerm(args[0])
   207  		y := testTerm(args[1])
   208  		want := args[2] == "T"
   209  		if got := x.subsetOf(y); got != want {
   210  			t.Errorf("%v.subsetOf(%v) = %v; want %v", x, y, got, want)
   211  		}
   212  	}
   213  }
   214  
   215  func TestTermDisjoint(t *testing.T) {
   216  	for _, test := range []string{
   217  		"int int F",
   218  		"~int ~int F",
   219  		"int ~int F",
   220  		"int string T",
   221  		"int ~string T",
   222  		"int myInt T",
   223  		"~int ~string T",
   224  		"~int myInt F",
   225  		"string myInt T",
   226  		"~string myInt T",
   227  	} {
   228  		args := split(test, 3)
   229  		x := testTerm(args[0])
   230  		y := testTerm(args[1])
   231  		want := args[2] == "T"
   232  		if got := x.disjoint(y); got != want {
   233  			t.Errorf("%v.disjoint(%v) = %v; want %v", x, y, got, want)
   234  		}
   235  		// disjoint is symmetric
   236  		x, y = y, x
   237  		if got := x.disjoint(y); got != want {
   238  			t.Errorf("%v.disjoint(%v) = %v; want %v", x, y, got, want)
   239  		}
   240  	}
   241  }
   242  

View as plain text