• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var two = 2
17var three = 3
18
19// Precedence (right-to-left) tests
20assert(2 ** 3 ** 2 === 512)
21assert((2 ** 3) ** 2 === 64)
22assert(2 ** (3 ** 2) === 512)
23assert(two ** three ** two === 512)
24assert((two ** three) ** two === 64)
25assert(two ** (three ** two) === 512)
26
27assert(2 ** 2 * 3 ** 3 === 4 * 27)
28assert(two ** two * three ** three === 4 * 27)
29
30var a = 3
31assert((a **= 3) === 27)
32assert(a === 27)
33
34a = 2
35assert((a **= a **= 2) === 16)
36assert(a === 16)
37
38function assertThrows(src) {
39  try {
40    eval(src)
41    assert(false)
42  } catch (e) {
43    assert(e instanceof SyntaxError)
44  }
45}
46
47assertThrows("-2 ** 2")
48assertThrows("+a ** 2")
49assertThrows("!a ** 2")
50assertThrows("~a ** 2")
51assertThrows("void a ** 2")
52assertThrows("typeof a ** 2")
53assertThrows("delete a ** 2")
54assertThrows("!(-a) ** 2")
55
56assert((-2) ** 2 === 4)
57
58a = 3
59assert((-+-a) ** 3 === 27)
60
61a = 0
62assert((!a) ** 2 === 1)
63
64a = 0
65assert(isNaN((void a) ** 3))
66
67a = -4
68assert(++a ** 2 === 9)
69assert(a === -3)
70
71a = -2
72assert(a-- ** 2 === 4)
73assert(a === -3)
74