github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/gc/logic_test.go (about)

     1  package gc
     2  
     3  import "testing"
     4  
     5  // Tests to make sure logic simplification rules are correct.
     6  
     7  func TestLogic64(t *testing.T) {
     8  	// test values to determine function equality
     9  	values := [...]int64{-1 << 63, 1<<63 - 1, -4, -3, -2, -1, 0, 1, 2, 3, 4}
    10  
    11  	// golden functions we use repeatedly
    12  	zero := func(x int64) int64 { return 0 }
    13  	id := func(x int64) int64 { return x }
    14  	or := func(x, y int64) int64 { return x | y }
    15  	and := func(x, y int64) int64 { return x & y }
    16  	y := func(x, y int64) int64 { return y }
    17  
    18  	for _, test := range [...]struct {
    19  		name   string
    20  		f      func(int64) int64
    21  		golden func(int64) int64
    22  	}{
    23  		{"x|x", func(x int64) int64 { return x | x }, id},
    24  		{"x|0", func(x int64) int64 { return x | 0 }, id},
    25  		{"x|-1", func(x int64) int64 { return x | -1 }, func(x int64) int64 { return -1 }},
    26  		{"x&x", func(x int64) int64 { return x & x }, id},
    27  		{"x&0", func(x int64) int64 { return x & 0 }, zero},
    28  		{"x&-1", func(x int64) int64 { return x & -1 }, id},
    29  		{"x^x", func(x int64) int64 { return x ^ x }, zero},
    30  		{"x^0", func(x int64) int64 { return x ^ 0 }, id},
    31  		{"x^-1", func(x int64) int64 { return x ^ -1 }, func(x int64) int64 { return ^x }},
    32  		{"x+0", func(x int64) int64 { return x + 0 }, id},
    33  		{"x-x", func(x int64) int64 { return x - x }, zero},
    34  		{"x*0", func(x int64) int64 { return x * 0 }, zero},
    35  		{"^^x", func(x int64) int64 { return ^^x }, id},
    36  	} {
    37  		for _, v := range values {
    38  			got := test.f(v)
    39  			want := test.golden(v)
    40  			if want != got {
    41  				t.Errorf("[%s](%d)=%d, want %d", test.name, v, got, want)
    42  			}
    43  		}
    44  	}
    45  	for _, test := range [...]struct {
    46  		name   string
    47  		f      func(int64, int64) int64
    48  		golden func(int64, int64) int64
    49  	}{
    50  		{"x|(x|y)", func(x, y int64) int64 { return x | (x | y) }, or},
    51  		{"x|(y|x)", func(x, y int64) int64 { return x | (y | x) }, or},
    52  		{"(x|y)|x", func(x, y int64) int64 { return (x | y) | x }, or},
    53  		{"(y|x)|x", func(x, y int64) int64 { return (y | x) | x }, or},
    54  		{"x&(x&y)", func(x, y int64) int64 { return x & (x & y) }, and},
    55  		{"x&(y&x)", func(x, y int64) int64 { return x & (y & x) }, and},
    56  		{"(x&y)&x", func(x, y int64) int64 { return (x & y) & x }, and},
    57  		{"(y&x)&x", func(x, y int64) int64 { return (y & x) & x }, and},
    58  		{"x^(x^y)", func(x, y int64) int64 { return x ^ (x ^ y) }, y},
    59  		{"x^(y^x)", func(x, y int64) int64 { return x ^ (y ^ x) }, y},
    60  		{"(x^y)^x", func(x, y int64) int64 { return (x ^ y) ^ x }, y},
    61  		{"(y^x)^x", func(x, y int64) int64 { return (y ^ x) ^ x }, y},
    62  		{"-(y-x)", func(x, y int64) int64 { return -(y - x) }, func(x, y int64) int64 { return x - y }},
    63  		{"(x+y)-x", func(x, y int64) int64 { return (x + y) - x }, y},
    64  		{"(y+x)-x", func(x, y int64) int64 { return (y + x) - x }, y},
    65  	} {
    66  		for _, v := range values {
    67  			for _, w := range values {
    68  				got := test.f(v, w)
    69  				want := test.golden(v, w)
    70  				if want != got {
    71  					t.Errorf("[%s](%d,%d)=%d, want %d", test.name, v, w, got, want)
    72  				}
    73  			}
    74  		}
    75  	}
    76  }
    77  
    78  func TestLogic32(t *testing.T) {
    79  	// test values to determine function equality
    80  	values := [...]int32{-1 << 31, 1<<31 - 1, -4, -3, -2, -1, 0, 1, 2, 3, 4}
    81  
    82  	// golden functions we use repeatedly
    83  	zero := func(x int32) int32 { return 0 }
    84  	id := func(x int32) int32 { return x }
    85  	or := func(x, y int32) int32 { return x | y }
    86  	and := func(x, y int32) int32 { return x & y }
    87  	y := func(x, y int32) int32 { return y }
    88  
    89  	for _, test := range [...]struct {
    90  		name   string
    91  		f      func(int32) int32
    92  		golden func(int32) int32
    93  	}{
    94  		{"x|x", func(x int32) int32 { return x | x }, id},
    95  		{"x|0", func(x int32) int32 { return x | 0 }, id},
    96  		{"x|-1", func(x int32) int32 { return x | -1 }, func(x int32) int32 { return -1 }},
    97  		{"x&x", func(x int32) int32 { return x & x }, id},
    98  		{"x&0", func(x int32) int32 { return x & 0 }, zero},
    99  		{"x&-1", func(x int32) int32 { return x & -1 }, id},
   100  		{"x^x", func(x int32) int32 { return x ^ x }, zero},
   101  		{"x^0", func(x int32) int32 { return x ^ 0 }, id},
   102  		{"x^-1", func(x int32) int32 { return x ^ -1 }, func(x int32) int32 { return ^x }},
   103  		{"x+0", func(x int32) int32 { return x + 0 }, id},
   104  		{"x-x", func(x int32) int32 { return x - x }, zero},
   105  		{"x*0", func(x int32) int32 { return x * 0 }, zero},
   106  		{"^^x", func(x int32) int32 { return ^^x }, id},
   107  	} {
   108  		for _, v := range values {
   109  			got := test.f(v)
   110  			want := test.golden(v)
   111  			if want != got {
   112  				t.Errorf("[%s](%d)=%d, want %d", test.name, v, got, want)
   113  			}
   114  		}
   115  	}
   116  	for _, test := range [...]struct {
   117  		name   string
   118  		f      func(int32, int32) int32
   119  		golden func(int32, int32) int32
   120  	}{
   121  		{"x|(x|y)", func(x, y int32) int32 { return x | (x | y) }, or},
   122  		{"x|(y|x)", func(x, y int32) int32 { return x | (y | x) }, or},
   123  		{"(x|y)|x", func(x, y int32) int32 { return (x | y) | x }, or},
   124  		{"(y|x)|x", func(x, y int32) int32 { return (y | x) | x }, or},
   125  		{"x&(x&y)", func(x, y int32) int32 { return x & (x & y) }, and},
   126  		{"x&(y&x)", func(x, y int32) int32 { return x & (y & x) }, and},
   127  		{"(x&y)&x", func(x, y int32) int32 { return (x & y) & x }, and},
   128  		{"(y&x)&x", func(x, y int32) int32 { return (y & x) & x }, and},
   129  		{"x^(x^y)", func(x, y int32) int32 { return x ^ (x ^ y) }, y},
   130  		{"x^(y^x)", func(x, y int32) int32 { return x ^ (y ^ x) }, y},
   131  		{"(x^y)^x", func(x, y int32) int32 { return (x ^ y) ^ x }, y},
   132  		{"(y^x)^x", func(x, y int32) int32 { return (y ^ x) ^ x }, y},
   133  		{"-(y-x)", func(x, y int32) int32 { return -(y - x) }, func(x, y int32) int32 { return x - y }},
   134  		{"(x+y)-x", func(x, y int32) int32 { return (x + y) - x }, y},
   135  		{"(y+x)-x", func(x, y int32) int32 { return (y + x) - x }, y},
   136  	} {
   137  		for _, v := range values {
   138  			for _, w := range values {
   139  				got := test.f(v, w)
   140  				want := test.golden(v, w)
   141  				if want != got {
   142  					t.Errorf("[%s](%d,%d)=%d, want %d", test.name, v, w, got, want)
   143  				}
   144  			}
   145  		}
   146  	}
   147  }
   148  
   149  func TestLogic16(t *testing.T) {
   150  	// test values to determine function equality
   151  	values := [...]int16{-1 << 15, 1<<15 - 1, -4, -3, -2, -1, 0, 1, 2, 3, 4}
   152  
   153  	// golden functions we use repeatedly
   154  	zero := func(x int16) int16 { return 0 }
   155  	id := func(x int16) int16 { return x }
   156  	or := func(x, y int16) int16 { return x | y }
   157  	and := func(x, y int16) int16 { return x & y }
   158  	y := func(x, y int16) int16 { return y }
   159  
   160  	for _, test := range [...]struct {
   161  		name   string
   162  		f      func(int16) int16
   163  		golden func(int16) int16
   164  	}{
   165  		{"x|x", func(x int16) int16 { return x | x }, id},
   166  		{"x|0", func(x int16) int16 { return x | 0 }, id},
   167  		{"x|-1", func(x int16) int16 { return x | -1 }, func(x int16) int16 { return -1 }},
   168  		{"x&x", func(x int16) int16 { return x & x }, id},
   169  		{"x&0", func(x int16) int16 { return x & 0 }, zero},
   170  		{"x&-1", func(x int16) int16 { return x & -1 }, id},
   171  		{"x^x", func(x int16) int16 { return x ^ x }, zero},
   172  		{"x^0", func(x int16) int16 { return x ^ 0 }, id},
   173  		{"x^-1", func(x int16) int16 { return x ^ -1 }, func(x int16) int16 { return ^x }},
   174  		{"x+0", func(x int16) int16 { return x + 0 }, id},
   175  		{"x-x", func(x int16) int16 { return x - x }, zero},
   176  		{"x*0", func(x int16) int16 { return x * 0 }, zero},
   177  		{"^^x", func(x int16) int16 { return ^^x }, id},
   178  	} {
   179  		for _, v := range values {
   180  			got := test.f(v)
   181  			want := test.golden(v)
   182  			if want != got {
   183  				t.Errorf("[%s](%d)=%d, want %d", test.name, v, got, want)
   184  			}
   185  		}
   186  	}
   187  	for _, test := range [...]struct {
   188  		name   string
   189  		f      func(int16, int16) int16
   190  		golden func(int16, int16) int16
   191  	}{
   192  		{"x|(x|y)", func(x, y int16) int16 { return x | (x | y) }, or},
   193  		{"x|(y|x)", func(x, y int16) int16 { return x | (y | x) }, or},
   194  		{"(x|y)|x", func(x, y int16) int16 { return (x | y) | x }, or},
   195  		{"(y|x)|x", func(x, y int16) int16 { return (y | x) | x }, or},
   196  		{"x&(x&y)", func(x, y int16) int16 { return x & (x & y) }, and},
   197  		{"x&(y&x)", func(x, y int16) int16 { return x & (y & x) }, and},
   198  		{"(x&y)&x", func(x, y int16) int16 { return (x & y) & x }, and},
   199  		{"(y&x)&x", func(x, y int16) int16 { return (y & x) & x }, and},
   200  		{"x^(x^y)", func(x, y int16) int16 { return x ^ (x ^ y) }, y},
   201  		{"x^(y^x)", func(x, y int16) int16 { return x ^ (y ^ x) }, y},
   202  		{"(x^y)^x", func(x, y int16) int16 { return (x ^ y) ^ x }, y},
   203  		{"(y^x)^x", func(x, y int16) int16 { return (y ^ x) ^ x }, y},
   204  		{"-(y-x)", func(x, y int16) int16 { return -(y - x) }, func(x, y int16) int16 { return x - y }},
   205  		{"(x+y)-x", func(x, y int16) int16 { return (x + y) - x }, y},
   206  		{"(y+x)-x", func(x, y int16) int16 { return (y + x) - x }, y},
   207  	} {
   208  		for _, v := range values {
   209  			for _, w := range values {
   210  				got := test.f(v, w)
   211  				want := test.golden(v, w)
   212  				if want != got {
   213  					t.Errorf("[%s](%d,%d)=%d, want %d", test.name, v, w, got, want)
   214  				}
   215  			}
   216  		}
   217  	}
   218  }
   219  
   220  func TestLogic8(t *testing.T) {
   221  	// test values to determine function equality
   222  	values := [...]int8{-1 << 7, 1<<7 - 1, -4, -3, -2, -1, 0, 1, 2, 3, 4}
   223  
   224  	// golden functions we use repeatedly
   225  	zero := func(x int8) int8 { return 0 }
   226  	id := func(x int8) int8 { return x }
   227  	or := func(x, y int8) int8 { return x | y }
   228  	and := func(x, y int8) int8 { return x & y }
   229  	y := func(x, y int8) int8 { return y }
   230  
   231  	for _, test := range [...]struct {
   232  		name   string
   233  		f      func(int8) int8
   234  		golden func(int8) int8
   235  	}{
   236  		{"x|x", func(x int8) int8 { return x | x }, id},
   237  		{"x|0", func(x int8) int8 { return x | 0 }, id},
   238  		{"x|-1", func(x int8) int8 { return x | -1 }, func(x int8) int8 { return -1 }},
   239  		{"x&x", func(x int8) int8 { return x & x }, id},
   240  		{"x&0", func(x int8) int8 { return x & 0 }, zero},
   241  		{"x&-1", func(x int8) int8 { return x & -1 }, id},
   242  		{"x^x", func(x int8) int8 { return x ^ x }, zero},
   243  		{"x^0", func(x int8) int8 { return x ^ 0 }, id},
   244  		{"x^-1", func(x int8) int8 { return x ^ -1 }, func(x int8) int8 { return ^x }},
   245  		{"x+0", func(x int8) int8 { return x + 0 }, id},
   246  		{"x-x", func(x int8) int8 { return x - x }, zero},
   247  		{"x*0", func(x int8) int8 { return x * 0 }, zero},
   248  		{"^^x", func(x int8) int8 { return ^^x }, id},
   249  	} {
   250  		for _, v := range values {
   251  			got := test.f(v)
   252  			want := test.golden(v)
   253  			if want != got {
   254  				t.Errorf("[%s](%d)=%d, want %d", test.name, v, got, want)
   255  			}
   256  		}
   257  	}
   258  	for _, test := range [...]struct {
   259  		name   string
   260  		f      func(int8, int8) int8
   261  		golden func(int8, int8) int8
   262  	}{
   263  		{"x|(x|y)", func(x, y int8) int8 { return x | (x | y) }, or},
   264  		{"x|(y|x)", func(x, y int8) int8 { return x | (y | x) }, or},
   265  		{"(x|y)|x", func(x, y int8) int8 { return (x | y) | x }, or},
   266  		{"(y|x)|x", func(x, y int8) int8 { return (y | x) | x }, or},
   267  		{"x&(x&y)", func(x, y int8) int8 { return x & (x & y) }, and},
   268  		{"x&(y&x)", func(x, y int8) int8 { return x & (y & x) }, and},
   269  		{"(x&y)&x", func(x, y int8) int8 { return (x & y) & x }, and},
   270  		{"(y&x)&x", func(x, y int8) int8 { return (y & x) & x }, and},
   271  		{"x^(x^y)", func(x, y int8) int8 { return x ^ (x ^ y) }, y},
   272  		{"x^(y^x)", func(x, y int8) int8 { return x ^ (y ^ x) }, y},
   273  		{"(x^y)^x", func(x, y int8) int8 { return (x ^ y) ^ x }, y},
   274  		{"(y^x)^x", func(x, y int8) int8 { return (y ^ x) ^ x }, y},
   275  		{"-(y-x)", func(x, y int8) int8 { return -(y - x) }, func(x, y int8) int8 { return x - y }},
   276  		{"(x+y)-x", func(x, y int8) int8 { return (x + y) - x }, y},
   277  		{"(y+x)-x", func(x, y int8) int8 { return (y + x) - x }, y},
   278  	} {
   279  		for _, v := range values {
   280  			for _, w := range values {
   281  				got := test.f(v, w)
   282  				want := test.golden(v, w)
   283  				if want != got {
   284  					t.Errorf("[%s](%d,%d)=%d, want %d", test.name, v, w, got, want)
   285  				}
   286  			}
   287  		}
   288  	}
   289  }