• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28function Test() {
29  this.result = 0;
30  this.x = 0;
31  this.y = 0;
32  this.z = 0;
33}
34var a = 1;
35var b = 2;
36var c = 4;
37var d = 8;
38
39// Test operations expected to stay on the fast path.  Enumerate all binary
40// trees with <= 4 leaves.
41Test.prototype.test0 = function () {
42  this.result = a | b;
43};
44
45Test.prototype.test1 = function() {
46  this.result = (a | b) | c;
47};
48
49Test.prototype.test2 = function() {
50  this.result = a | (b | c);
51};
52
53Test.prototype.test3 = function() {
54  this.result = ((a | b) | c) | d;
55};
56
57Test.prototype.test4 = function() {
58  this.result = (a | (b | c)) | d;
59};
60
61Test.prototype.test5 = function() {
62  this.result = (a | b) | (c | d);
63};
64
65Test.prototype.test6 = function() {
66  this.result = a | ((b | c) | d);
67};
68
69Test.prototype.test7 = function() {
70  this.result = a | (b | (c | d));
71};
72
73// These tests should fail if we bailed out to the beginning of the full
74// code.
75Test.prototype.test8 = function () {
76  // If this.x = 1 and a = 1.1:
77  this.y = this.x | b;  // Should be (1 | 2) == 3.
78  this.x = c;           // Should be 4.
79  this.z = this.x | a;  // Should be (4 | 1.1) == 5.
80};
81
82Test.prototype.test9 = function() {
83  // If this.x = 2 and a = 1.1:
84  this.z =              // (14 | 1.1) == 15
85      (this.x =         // (6 | 8) == 14
86           (this.y =    // (2 | 4) == 6
87                this.x  // 2
88                | c)    // 4
89            | d)        // 8
90       | a;             // 1.1
91}
92
93Test.prototype.test10 = function() {
94  this.z = (a >> b) | (c >> c);
95}
96
97Test.prototype.test11 = function(x) {
98  this.z = x >> x;
99}
100
101var t = new Test();
102
103t.test0();
104assertEquals(3, t.result);
105
106t.test1();
107assertEquals(7, t.result);
108t.test2();
109assertEquals(7, t.result);
110
111t.test3();
112assertEquals(15, t.result);
113t.test4();
114assertEquals(15, t.result);
115t.test5();
116assertEquals(15, t.result);
117t.test6();
118assertEquals(15, t.result);
119t.test7();
120assertEquals(15, t.result);
121
122a = 1.1;
123t.x = 1;
124t.test8();
125assertEquals(4, t.x);
126assertEquals(3, t.y);
127assertEquals(5, t.z);
128
129t.x = 2;
130t.test9();
131assertEquals(14, t.x);
132assertEquals(6, t.y);
133assertEquals(15, t.z);
134
135a = "2";
136t.test11(a);
137assertEquals(0, t.z);
138
139a = 4;
140b = "1";
141c = 2;
142t.test10();
143assertEquals(2, t.z);
144