Home | History | Annotate | Download | only in test
      1 // compile
      2 
      3 // Copyright 2012 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 // Various tests for expressions with high complexity.
      8 
      9 package main
     10 
     11 // Concatenate 16 4-bit integers into a 64-bit number.
     12 func concat(s *[16]byte) uint64 {
     13 	r := (((((((((((((((uint64(s[0])<<4|
     14 		uint64(s[1]))<<4|
     15 		uint64(s[2]))<<4|
     16 		uint64(s[3]))<<4|
     17 		uint64(s[4]))<<4|
     18 		uint64(s[5]))<<4|
     19 		uint64(s[6]))<<4|
     20 		uint64(s[7]))<<4|
     21 		uint64(s[8]))<<4|
     22 		uint64(s[9]))<<4|
     23 		uint64(s[10]))<<4|
     24 		uint64(s[11]))<<4|
     25 		uint64(s[12]))<<4|
     26 		uint64(s[13]))<<4|
     27 		uint64(s[14]))<<4 |
     28 		uint64(s[15]))
     29 	return r
     30 }
     31 
     32 // Compute the determinant of a 4x4-matrix by the sum
     33 // over all index permutations.
     34 func determinant(m [4][4]float64) float64 {
     35 	return m[0][0]*m[1][1]*m[2][2]*m[3][3] -
     36 		m[0][0]*m[1][1]*m[2][3]*m[3][2] -
     37 		m[0][0]*m[1][2]*m[2][1]*m[3][3] +
     38 		m[0][0]*m[1][2]*m[2][3]*m[3][1] +
     39 		m[0][0]*m[1][3]*m[2][1]*m[3][2] -
     40 		m[0][0]*m[1][3]*m[2][2]*m[3][1] -
     41 		m[0][1]*m[1][0]*m[2][2]*m[3][3] +
     42 		m[0][1]*m[1][0]*m[2][3]*m[3][2] +
     43 		m[0][1]*m[1][2]*m[2][0]*m[3][3] -
     44 		m[0][1]*m[1][2]*m[2][3]*m[3][0] -
     45 		m[0][1]*m[1][3]*m[2][0]*m[3][2] +
     46 		m[0][1]*m[1][3]*m[2][2]*m[3][0] +
     47 		m[0][2]*m[1][0]*m[2][1]*m[3][3] -
     48 		m[0][2]*m[1][0]*m[2][3]*m[3][1] -
     49 		m[0][2]*m[1][1]*m[2][0]*m[3][3] +
     50 		m[0][2]*m[1][1]*m[2][3]*m[3][0] +
     51 		m[0][2]*m[1][3]*m[2][0]*m[3][1] -
     52 		m[0][2]*m[1][3]*m[2][1]*m[3][0] -
     53 		m[0][3]*m[1][0]*m[2][1]*m[3][2] +
     54 		m[0][3]*m[1][0]*m[2][2]*m[3][1] +
     55 		m[0][3]*m[1][1]*m[2][0]*m[3][2] -
     56 		m[0][3]*m[1][1]*m[2][2]*m[3][0] -
     57 		m[0][3]*m[1][2]*m[2][0]*m[3][1] +
     58 		m[0][3]*m[1][2]*m[2][1]*m[3][0]
     59 }
     60 
     61 // Compute the determinant of a 4x4-matrix by the sum
     62 // over all index permutations.
     63 func determinantInt(m [4][4]int) int {
     64 	return m[0][0]*m[1][1]*m[2][2]*m[3][3] -
     65 		m[0][0]*m[1][1]*m[2][3]*m[3][2] -
     66 		m[0][0]*m[1][2]*m[2][1]*m[3][3] +
     67 		m[0][0]*m[1][2]*m[2][3]*m[3][1] +
     68 		m[0][0]*m[1][3]*m[2][1]*m[3][2] -
     69 		m[0][0]*m[1][3]*m[2][2]*m[3][1] -
     70 		m[0][1]*m[1][0]*m[2][2]*m[3][3] +
     71 		m[0][1]*m[1][0]*m[2][3]*m[3][2] +
     72 		m[0][1]*m[1][2]*m[2][0]*m[3][3] -
     73 		m[0][1]*m[1][2]*m[2][3]*m[3][0] -
     74 		m[0][1]*m[1][3]*m[2][0]*m[3][2] +
     75 		m[0][1]*m[1][3]*m[2][2]*m[3][0] +
     76 		m[0][2]*m[1][0]*m[2][1]*m[3][3] -
     77 		m[0][2]*m[1][0]*m[2][3]*m[3][1] -
     78 		m[0][2]*m[1][1]*m[2][0]*m[3][3] +
     79 		m[0][2]*m[1][1]*m[2][3]*m[3][0] +
     80 		m[0][2]*m[1][3]*m[2][0]*m[3][1] -
     81 		m[0][2]*m[1][3]*m[2][1]*m[3][0] -
     82 		m[0][3]*m[1][0]*m[2][1]*m[3][2] +
     83 		m[0][3]*m[1][0]*m[2][2]*m[3][1] +
     84 		m[0][3]*m[1][1]*m[2][0]*m[3][2] -
     85 		m[0][3]*m[1][1]*m[2][2]*m[3][0] -
     86 		m[0][3]*m[1][2]*m[2][0]*m[3][1] +
     87 		m[0][3]*m[1][2]*m[2][1]*m[3][0]
     88 }
     89 
     90 // Compute the determinant of a 4x4-matrix by the sum
     91 // over all index permutations.
     92 func determinantByte(m [4][4]byte) byte {
     93 	return m[0][0]*m[1][1]*m[2][2]*m[3][3] -
     94 		m[0][0]*m[1][1]*m[2][3]*m[3][2] -
     95 		m[0][0]*m[1][2]*m[2][1]*m[3][3] +
     96 		m[0][0]*m[1][2]*m[2][3]*m[3][1] +
     97 		m[0][0]*m[1][3]*m[2][1]*m[3][2] -
     98 		m[0][0]*m[1][3]*m[2][2]*m[3][1] -
     99 		m[0][1]*m[1][0]*m[2][2]*m[3][3] +
    100 		m[0][1]*m[1][0]*m[2][3]*m[3][2] +
    101 		m[0][1]*m[1][2]*m[2][0]*m[3][3] -
    102 		m[0][1]*m[1][2]*m[2][3]*m[3][0] -
    103 		m[0][1]*m[1][3]*m[2][0]*m[3][2] +
    104 		m[0][1]*m[1][3]*m[2][2]*m[3][0] +
    105 		m[0][2]*m[1][0]*m[2][1]*m[3][3] -
    106 		m[0][2]*m[1][0]*m[2][3]*m[3][1] -
    107 		m[0][2]*m[1][1]*m[2][0]*m[3][3] +
    108 		m[0][2]*m[1][1]*m[2][3]*m[3][0] +
    109 		m[0][2]*m[1][3]*m[2][0]*m[3][1] -
    110 		m[0][2]*m[1][3]*m[2][1]*m[3][0] -
    111 		m[0][3]*m[1][0]*m[2][1]*m[3][2] +
    112 		m[0][3]*m[1][0]*m[2][2]*m[3][1] +
    113 		m[0][3]*m[1][1]*m[2][0]*m[3][2] -
    114 		m[0][3]*m[1][1]*m[2][2]*m[3][0] -
    115 		m[0][3]*m[1][2]*m[2][0]*m[3][1] +
    116 		m[0][3]*m[1][2]*m[2][1]*m[3][0]
    117 }
    118 
    119 type A []A
    120 
    121 // A sequence of constant indexings.
    122 func IndexChain1(s A) A {
    123 	return s[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]
    124 }
    125 
    126 // A sequence of non-constant indexings.
    127 func IndexChain2(s A, i int) A {
    128 	return s[i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][i]
    129 }
    130 
    131 // Another sequence of indexings.
    132 func IndexChain3(s []int) int {
    133 	return s[s[s[s[s[s[s[s[s[s[s[s[s[s[s[s[s[s[s[s[s[0]]]]]]]]]]]]]]]]]]]]]
    134 }
    135 
    136 // A right-leaning tree of byte multiplications.
    137 func righttree(a, b, c, d uint8) uint8 {
    138 	return a * (b * (c * (d *
    139 		(a * (b * (c * (d *
    140 			(a * (b * (c * (d *
    141 				(a * (b * (c * (d *
    142 					(a * (b * (c * (d *
    143 						a * (b * (c * d)))))))))))))))))))))
    144 
    145 }
    146 
    147 // A left-leaning tree of byte multiplications.
    148 func lefttree(a, b, c, d uint8) uint8 {
    149 	return ((((((((((((((((((a * b) * c) * d *
    150 		a) * b) * c) * d *
    151 		a) * b) * c) * d *
    152 		a) * b) * c) * d *
    153 		a) * b) * c) * d *
    154 		a) * b) * c) * d)
    155 }
    156 
    157 type T struct {
    158 	Next I
    159 }
    160 
    161 type I interface{}
    162 
    163 // A chains of type assertions.
    164 func ChainT(t *T) *T {
    165 	return t.
    166 		Next.(*T).
    167 		Next.(*T).
    168 		Next.(*T).
    169 		Next.(*T).
    170 		Next.(*T).
    171 		Next.(*T).
    172 		Next.(*T).
    173 		Next.(*T).
    174 		Next.(*T).
    175 		Next.(*T).
    176 		Next.(*T).
    177 		Next.(*T).
    178 		Next.(*T).
    179 		Next.(*T).
    180 		Next.(*T).
    181 		Next.(*T).
    182 		Next.(*T).
    183 		Next.(*T).
    184 		Next.(*T).
    185 		Next.(*T)
    186 }
    187 
    188 type U struct {
    189 	Children []J
    190 }
    191 
    192 func (u *U) Child(n int) J { return u.Children[n] }
    193 
    194 type J interface {
    195 	Child(n int) J
    196 }
    197 
    198 func ChainUAssert(u *U) *U {
    199 	return u.Child(0).(*U).
    200 		Child(0).(*U).
    201 		Child(0).(*U).
    202 		Child(0).(*U).
    203 		Child(0).(*U).
    204 		Child(0).(*U).
    205 		Child(0).(*U).
    206 		Child(0).(*U).
    207 		Child(0).(*U).
    208 		Child(0).(*U).
    209 		Child(0).(*U).
    210 		Child(0).(*U).
    211 		Child(0).(*U).
    212 		Child(0).(*U).
    213 		Child(0).(*U).
    214 		Child(0).(*U).
    215 		Child(0).(*U).
    216 		Child(0).(*U).
    217 		Child(0).(*U).
    218 		Child(0).(*U).
    219 		Child(0).(*U).
    220 		Child(0).(*U)
    221 }
    222 
    223 func ChainUNoAssert(u *U) *U {
    224 	return u.Child(0).
    225 		Child(0).
    226 		Child(0).
    227 		Child(0).
    228 		Child(0).
    229 		Child(0).
    230 		Child(0).
    231 		Child(0).
    232 		Child(0).
    233 		Child(0).
    234 		Child(0).
    235 		Child(0).
    236 		Child(0).
    237 		Child(0).
    238 		Child(0).
    239 		Child(0).
    240 		Child(0).
    241 		Child(0).
    242 		Child(0).
    243 		Child(0).
    244 		Child(0).
    245 		Child(0).(*U)
    246 }
    247 
    248 // Type assertions and slice indexing. See issue 4207.
    249 func ChainAssertIndex(u *U) J {
    250 	return u.
    251 		Children[0].(*U).
    252 		Children[0].(*U).
    253 		Children[0].(*U).
    254 		Children[0].(*U).
    255 		Children[0].(*U).
    256 		Children[0].(*U).
    257 		Children[0].(*U).
    258 		Children[0].(*U).
    259 		Children[0].(*U).
    260 		Children[0].(*U).
    261 		Children[0].(*U).
    262 		Children[0].(*U).
    263 		Children[0].(*U).
    264 		Children[0]
    265 }
    266 
    267 type UArr struct {
    268 	Children [2]J
    269 }
    270 
    271 func (u *UArr) Child(n int) J { return u.Children[n] }
    272 
    273 func ChainAssertArrayIndex(u *UArr) J {
    274 	return u.
    275 		Children[0].(*UArr).
    276 		Children[0].(*UArr).
    277 		Children[0].(*UArr).
    278 		Children[0].(*UArr).
    279 		Children[0].(*UArr).
    280 		Children[0].(*UArr).
    281 		Children[0].(*UArr).
    282 		Children[0].(*UArr).
    283 		Children[0].(*UArr).
    284 		Children[0].(*UArr).
    285 		Children[0].(*UArr).
    286 		Children[0].(*UArr).
    287 		Children[0].(*UArr).
    288 		Children[0]
    289 }
    290 
    291 type UArrPtr struct {
    292 	Children *[2]J
    293 }
    294 
    295 func (u *UArrPtr) Child(n int) J { return u.Children[n] }
    296 
    297 func ChainAssertArrayptrIndex(u *UArrPtr) J {
    298 	return u.
    299 		Children[0].(*UArrPtr).
    300 		Children[0].(*UArrPtr).
    301 		Children[0].(*UArrPtr).
    302 		Children[0].(*UArrPtr).
    303 		Children[0].(*UArrPtr).
    304 		Children[0].(*UArrPtr).
    305 		Children[0].(*UArrPtr).
    306 		Children[0].(*UArrPtr).
    307 		Children[0].(*UArrPtr).
    308 		Children[0].(*UArrPtr).
    309 		Children[0].(*UArrPtr).
    310 		Children[0].(*UArrPtr).
    311 		Children[0].(*UArrPtr).
    312 		Children[0]
    313 }
    314 
    315 // Chains of divisions. See issue 4201.
    316 
    317 func ChainDiv(a, b int) int {
    318 	return a / b / a / b / a / b / a / b /
    319 		a / b / a / b / a / b / a / b /
    320 		a / b / a / b / a / b / a / b
    321 }
    322 
    323 func ChainDivRight(a, b int) int {
    324 	return a / (b / (a / (b /
    325 		(a / (b / (a / (b /
    326 			(a / (b / (a / (b /
    327 				(a / (b / (a / (b /
    328 					(a / (b / (a / b))))))))))))))))))
    329 }
    330 
    331 func ChainDivConst(a int) int {
    332 	return a / 17 / 17 / 17 /
    333 		17 / 17 / 17 / 17 /
    334 		17 / 17 / 17 / 17
    335 }
    336 
    337 func ChainMulBytes(a, b, c byte) byte {
    338 	return a*(a*(a*(a*(a*(a*(a*(a*(a*b+c)+c)+c)+c)+c)+c)+c)+c) + c
    339 }
    340 
    341 func ChainCap() {
    342 	select {
    343 	case <-make(chan int, cap(make(chan int, cap(make(chan int, cap(make(chan int, cap(make(chan int))))))))):
    344 	default:
    345 	}
    346 }
    347