Source file src/cmd/compile/internal/types2/termlist_test.go

     1  // Copyright 2021 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 types2
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  // maketl makes a term list from a string of the term list.
    13  func maketl(s string) termlist {
    14  	s = strings.ReplaceAll(s, " ", "")
    15  	names := strings.Split(s, "|")
    16  	r := make(termlist, len(names))
    17  	for i, n := range names {
    18  		r[i] = testTerm(n)
    19  	}
    20  	return r
    21  }
    22  
    23  func TestTermlistAll(t *testing.T) {
    24  	if !allTermlist.isAll() {
    25  		t.Errorf("allTermlist is not the set of all types")
    26  	}
    27  }
    28  
    29  func TestTermlistString(t *testing.T) {
    30  	for _, want := range []string{
    31  		"∅",
    32  		"𝓤",
    33  		"int",
    34  		"~int",
    35  		"myInt",
    36  		"∅ | ∅",
    37  		"𝓤 | 𝓤",
    38  		"∅ | 𝓤 | int",
    39  		"∅ | 𝓤 | int | myInt",
    40  	} {
    41  		if got := maketl(want).String(); got != want {
    42  			t.Errorf("(%v).String() == %v", want, got)
    43  		}
    44  	}
    45  }
    46  
    47  func TestTermlistIsEmpty(t *testing.T) {
    48  	for test, want := range map[string]bool{
    49  		"∅":             true,
    50  		"∅ | ∅":         true,
    51  		"∅ | ∅ | 𝓤":     false,
    52  		"∅ | ∅ | myInt": false,
    53  		"𝓤":             false,
    54  		"𝓤 | int":       false,
    55  		"𝓤 | myInt | ∅": false,
    56  	} {
    57  		xl := maketl(test)
    58  		got := xl.isEmpty()
    59  		if got != want {
    60  			t.Errorf("(%v).isEmpty() == %v; want %v", test, got, want)
    61  		}
    62  	}
    63  }
    64  
    65  func TestTermlistIsAll(t *testing.T) {
    66  	for test, want := range map[string]bool{
    67  		"∅":             false,
    68  		"∅ | ∅":         false,
    69  		"int | ~string": false,
    70  		"~int | myInt":  false,
    71  		"∅ | ∅ | 𝓤":     true,
    72  		"𝓤":             true,
    73  		"𝓤 | int":       true,
    74  		"myInt | 𝓤":     true,
    75  	} {
    76  		xl := maketl(test)
    77  		got := xl.isAll()
    78  		if got != want {
    79  			t.Errorf("(%v).isAll() == %v; want %v", test, got, want)
    80  		}
    81  	}
    82  }
    83  
    84  func TestTermlistNorm(t *testing.T) {
    85  	for _, test := range []struct {
    86  		xl, want string
    87  	}{
    88  		{"∅", "∅"},
    89  		{"∅ | ∅", "∅"},
    90  		{"∅ | int", "int"},
    91  		{"∅ | myInt", "myInt"},
    92  		{"𝓤 | int", "𝓤"},
    93  		{"𝓤 | myInt", "𝓤"},
    94  		{"int | myInt", "int | myInt"},
    95  		{"~int | int", "~int"},
    96  		{"~int | myInt", "~int"},
    97  		{"int | ~string | int", "int | ~string"},
    98  		{"~int | string | 𝓤 | ~string | int", "𝓤"},
    99  		{"~int | string | myInt | ~string | int", "~int | ~string"},
   100  	} {
   101  		xl := maketl(test.xl)
   102  		got := maketl(test.xl).norm()
   103  		if got.String() != test.want {
   104  			t.Errorf("(%v).norm() = %v; want %v", xl, got, test.want)
   105  		}
   106  	}
   107  }
   108  
   109  func TestTermlistUnion(t *testing.T) {
   110  	for _, test := range []struct {
   111  		xl, yl, want string
   112  	}{
   113  
   114  		{"∅", "∅", "∅"},
   115  		{"∅", "𝓤", "𝓤"},
   116  		{"∅", "int", "int"},
   117  		{"𝓤", "~int", "𝓤"},
   118  		{"int", "~int", "~int"},
   119  		{"int", "string", "int | string"},
   120  		{"int", "myInt", "int | myInt"},
   121  		{"~int", "myInt", "~int"},
   122  		{"int | string", "~string", "int | ~string"},
   123  		{"~int | string", "~string | int", "~int | ~string"},
   124  		{"~int | string | ∅", "~string | int", "~int | ~string"},
   125  		{"~int | myInt | ∅", "~string | int", "~int | ~string"},
   126  		{"~int | string | 𝓤", "~string | int", "𝓤"},
   127  		{"~int | string | myInt", "~string | int", "~int | ~string"},
   128  	} {
   129  		xl := maketl(test.xl)
   130  		yl := maketl(test.yl)
   131  		got := xl.union(yl).String()
   132  		if got != test.want {
   133  			t.Errorf("(%v).union(%v) = %v; want %v", test.xl, test.yl, got, test.want)
   134  		}
   135  	}
   136  }
   137  
   138  func TestTermlistIntersect(t *testing.T) {
   139  	for _, test := range []struct {
   140  		xl, yl, want string
   141  	}{
   142  
   143  		{"∅", "∅", "∅"},
   144  		{"∅", "𝓤", "∅"},
   145  		{"∅", "int", "∅"},
   146  		{"∅", "myInt", "∅"},
   147  		{"𝓤", "~int", "~int"},
   148  		{"𝓤", "myInt", "myInt"},
   149  		{"int", "~int", "int"},
   150  		{"int", "string", "∅"},
   151  		{"int", "myInt", "∅"},
   152  		{"~int", "myInt", "myInt"},
   153  		{"int | string", "~string", "string"},
   154  		{"~int | string", "~string | int", "int | string"},
   155  		{"~int | string | ∅", "~string | int", "int | string"},
   156  		{"~int | myInt | ∅", "~string | int", "int"},
   157  		{"~int | string | 𝓤", "~string | int", "int | ~string"},
   158  		{"~int | string | myInt", "~string | int", "int | string"},
   159  	} {
   160  		xl := maketl(test.xl)
   161  		yl := maketl(test.yl)
   162  		got := xl.intersect(yl).String()
   163  		if got != test.want {
   164  			t.Errorf("(%v).intersect(%v) = %v; want %v", test.xl, test.yl, got, test.want)
   165  		}
   166  	}
   167  }
   168  
   169  func TestTermlistEqual(t *testing.T) {
   170  	for _, test := range []struct {
   171  		xl, yl string
   172  		want   bool
   173  	}{
   174  		{"∅", "∅", true},
   175  		{"∅", "𝓤", false},
   176  		{"𝓤", "𝓤", true},
   177  		{"𝓤 | int", "𝓤", true},
   178  		{"𝓤 | int", "string | 𝓤", true},
   179  		{"𝓤 | myInt", "string | 𝓤", true},
   180  		{"int | ~string", "string | int", false},
   181  		{"~int | string", "string | myInt", false},
   182  		{"int | ~string | ∅", "string | int | ~string", true},
   183  	} {
   184  		xl := maketl(test.xl)
   185  		yl := maketl(test.yl)
   186  		got := xl.equal(yl)
   187  		if got != test.want {
   188  			t.Errorf("(%v).equal(%v) = %v; want %v", test.xl, test.yl, got, test.want)
   189  		}
   190  	}
   191  }
   192  
   193  func TestTermlistIncludes(t *testing.T) {
   194  	for _, test := range []struct {
   195  		xl, typ string
   196  		want    bool
   197  	}{
   198  		{"∅", "int", false},
   199  		{"𝓤", "int", true},
   200  		{"~int", "int", true},
   201  		{"int", "string", false},
   202  		{"~int", "string", false},
   203  		{"~int", "myInt", true},
   204  		{"int | string", "string", true},
   205  		{"~int | string", "int", true},
   206  		{"~int | string", "myInt", true},
   207  		{"~int | myInt | ∅", "myInt", true},
   208  		{"myInt | ∅ | 𝓤", "int", true},
   209  	} {
   210  		xl := maketl(test.xl)
   211  		yl := testTerm(test.typ).typ
   212  		got := xl.includes(yl)
   213  		if got != test.want {
   214  			t.Errorf("(%v).includes(%v) = %v; want %v", test.xl, yl, got, test.want)
   215  		}
   216  	}
   217  }
   218  
   219  func TestTermlistSupersetOf(t *testing.T) {
   220  	for _, test := range []struct {
   221  		xl, typ string
   222  		want    bool
   223  	}{
   224  		{"∅", "∅", true},
   225  		{"∅", "𝓤", false},
   226  		{"∅", "int", false},
   227  		{"𝓤", "∅", true},
   228  		{"𝓤", "𝓤", true},
   229  		{"𝓤", "int", true},
   230  		{"𝓤", "~int", true},
   231  		{"𝓤", "myInt", true},
   232  		{"~int", "int", true},
   233  		{"~int", "~int", true},
   234  		{"~int", "myInt", true},
   235  		{"int", "~int", false},
   236  		{"myInt", "~int", false},
   237  		{"int", "string", false},
   238  		{"~int", "string", false},
   239  		{"int | string", "string", true},
   240  		{"int | string", "~string", false},
   241  		{"~int | string", "int", true},
   242  		{"~int | string", "myInt", true},
   243  		{"~int | string | ∅", "string", true},
   244  		{"~string | ∅ | 𝓤", "myInt", true},
   245  	} {
   246  		xl := maketl(test.xl)
   247  		y := testTerm(test.typ)
   248  		got := xl.supersetOf(y)
   249  		if got != test.want {
   250  			t.Errorf("(%v).supersetOf(%v) = %v; want %v", test.xl, y, got, test.want)
   251  		}
   252  	}
   253  }
   254  
   255  func TestTermlistSubsetOf(t *testing.T) {
   256  	for _, test := range []struct {
   257  		xl, yl string
   258  		want   bool
   259  	}{
   260  		{"∅", "∅", true},
   261  		{"∅", "𝓤", true},
   262  		{"𝓤", "∅", false},
   263  		{"𝓤", "𝓤", true},
   264  		{"int", "int | string", true},
   265  		{"~int", "int | string", false},
   266  		{"~int", "myInt | string", false},
   267  		{"myInt", "~int | string", true},
   268  		{"~int", "string | string | int | ~int", true},
   269  		{"myInt", "string | string | ~int", true},
   270  		{"int | string", "string", false},
   271  		{"int | string", "string | int", true},
   272  		{"int | ~string", "string | int", false},
   273  		{"myInt | ~string", "string | int | 𝓤", true},
   274  		{"int | ~string", "string | int | ∅ | string", false},
   275  		{"int | myInt", "string | ~int | ∅ | string", true},
   276  	} {
   277  		xl := maketl(test.xl)
   278  		yl := maketl(test.yl)
   279  		got := xl.subsetOf(yl)
   280  		if got != test.want {
   281  			t.Errorf("(%v).subsetOf(%v) = %v; want %v", test.xl, test.yl, got, test.want)
   282  		}
   283  	}
   284  }
   285  

View as plain text