1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --allow-natives-syntax --array-bounds-checks-elimination
6
7var a = []
8for (var i = 0; i < 9; i++) a[i] = i + 1;
9
10function test(f, arg1, arg2, expected) {
11  assertEquals(expected, f(arg1));
12  f(arg2);
13  %OptimizeFunctionOnNextCall(f);
14  assertEquals(expected, f(arg1));
15}
16
17test(function f0() {
18  return a[7] * a[6] * a[5] * a[4] * a[3] * a[2] * a[1] * a[0];
19}, 0, 1, 40320);
20
21test(function f1() {
22  return a[7] * a[6] * a[5] * a[4] * a[10] * a[2] * a[1] * a[0];
23}, 0, 1, NaN);
24
25test(function f2() {
26  return a[0] * a[1] * a[2] * a[3] * a[4] * a[5] * a[6] * a[7];
27}, 0, 1, 40320);
28
29test(function f3() {
30  return a[3] * a[0] * a[6] * a[7] * a[5] * a[1] * a[4] * a[2];
31}, 0, 1, 40320);
32
33test(function f4(b) {
34  return a[b+3] * a[0] * a[b+6] * a[7] * a[b+5] * a[1] * a[b+4] * a[2];
35}, 0, 1, 40320);
36
37test(function f5(b) {
38  return a[b+1] * a[0] * a[b+4] * a[7] * a[b+3] * a[1] * a[b+2] * a[2];
39}, 2, 3, 40320);
40
41test(function f6(b) {
42  var c;
43  if (b) c = a[3] * a[0] * a[6] * a[7];
44  return c * a[5] * a[1] * a[4] * a[2];
45}, true, false, 40320);
46
47test(function f7(b) {
48  var c = a[7];
49  if (b) c *= a[3] * a[0] * a[6];
50  return c * a[5] * a[1] * a[4] * a[2];
51}, true, false, 40320);
52
53test(function f8(b) {
54  var c = a[7];
55  if (b) c *= a[3] * a[0] * a[6];
56  return c * a[5] * a[10] * a[4] * a[2];
57}, true, false, NaN);
58
59test(function f9(b) {
60  var c = a[1];
61  if (b) {
62    c *= a[3] * a[0] * a[6];
63  } else {
64    c = a[6] * a[5] * a[4];
65  }
66  return c * a[5] * a[7] * a[4] * a[2];
67}, true, false, 40320);
68
69test(function fa(b) {
70  var c = a[1];
71  if (b) {
72    c = a[6] * a[b+5] * a[4];
73  } else {
74    c *= a[b+3] * a[0] * a[b+6];
75  }
76  return c * a[5] * a[b+7] * a[4] * a[2];
77}, 0, 1, 40320);
78
79test(function fb(b) {
80  var c = a[b-3];
81  if (b != 4) {
82    c = a[6] * a[b+1] * a[4];
83  } else {
84    c *= a[b-1] * a[0] * a[b+2];
85  }
86  return c * a[5] * a[b+3] * a[4] * a[b-2];
87}, 4, 3, 40320);
88
89test(function fc(b) {
90  var c = a[b-3];
91  if (b != 4) {
92    c = a[6] * a[b+1] * a[4];
93  } else {
94    c *= a[b-1] * a[0] * a[b+2];
95  }
96  return c * a[5] * a[b+3] * a[4] * a[b-2];
97}, 6, 3, NaN);
98
99test(function fd(b) {
100  var c = a[b-3];
101  if (b != 4) {
102    c = a[6] * a[b+1] * a[4];
103  } else {
104    c *= a[b-1] * a[0] * a[b+2];
105  }
106  return c * a[5] * a[b+3] * a[4] * a[b-2];
107}, 1, 4, NaN);
108
109test(function fe(b) {
110  var c = 1;
111  for (var i = 1; i < b-1; i++) {
112    c *= a[i-1] * a[i] * a[i+1];
113  }
114  return c;
115}, 8, 4, (40320 / 8 / 7) * (40320 / 8) * (40320 / 2));
116
117test(function ff(b) {
118  var c = 0;
119  for (var i = 0; i < b; i++) {
120    c += a[3] * a[0] * a[6] * a[7] * a[5] * a[1] * a[4] * a[2];
121  }
122  return c;
123}, 100, 4, 40320 * 100);
124