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 MaxLT(x, y) {
29  if (x < y) return y;
30  return x;
31}
32
33function MaxLE(x, y) {
34  if (x <= y) return y;
35  return x;
36}
37
38function MaxGE(x, y) {
39  if (x >= y) return x;
40  return y;
41}
42
43function MaxGT(x, y) {
44  if (x > y) return x;
45  return y;
46}
47
48
49// First test primitive values.
50function TestPrimitive(max, x, y) {
51  assertEquals(max, MaxLT(x, y), "MaxLT - primitive");
52  assertEquals(max, MaxLE(x, y), "MaxLE - primitive");
53  assertEquals(max, MaxGE(x, y), "MaxGE - primitive");
54  assertEquals(max, MaxGT(x, y), "MaxGT - primitive");
55}
56
57TestPrimitive(1, 0, 1);
58TestPrimitive(1, 1, 0);
59TestPrimitive(4, 3, 4);
60TestPrimitive(4, 4, 3);
61TestPrimitive(0, -1, 0);
62TestPrimitive(0, 0, -1)
63TestPrimitive(-2, -2, -3);
64TestPrimitive(-2, -3, -2);
65
66TestPrimitive(1, 0.1, 1);
67TestPrimitive(1, 1, 0.1);
68TestPrimitive(4, 3.1, 4);
69TestPrimitive(4, 4, 3.1);
70TestPrimitive(0, -1.1, 0);
71TestPrimitive(0, 0, -1.1)
72TestPrimitive(-2, -2, -3.1);
73TestPrimitive(-2, -3.1, -2);
74
75
76// Test non-primitive values and watch for valueOf call order.
77function TestNonPrimitive(order, f) {
78  var result = "";
79  var x = { valueOf: function() { result += "x"; } };
80  var y = { valueOf: function() { result += "y"; } };
81  f(x, y);
82  assertEquals(order, result);
83}
84
85TestNonPrimitive("xy", MaxLT);
86TestNonPrimitive("xy", MaxLE);
87TestNonPrimitive("xy", MaxGE);
88TestNonPrimitive("xy", MaxGT);
89
90// Test compare in case of aliased registers.
91function CmpX(x) { if (x == x) return 42; }
92assertEquals(42, CmpX(0));
93
94function CmpXY(x) { var y = x; if (x == y) return 42; }
95assertEquals(42, CmpXY(0));
96
97
98// Test compare against null.
99function CmpNullValue(x) { return x == null; }
100assertEquals(false, CmpNullValue(42));
101
102function CmpNullTest(x) { if (x == null) return 42; return 0; }
103assertEquals(42, CmpNullTest(null));
104
105var g1 = 0;
106function CmpNullEffect() { (g1 = 42) == null; }
107CmpNullEffect();
108assertEquals(42, g1);
109