1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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// mjsunit/regress/regress-2444.js
16var flags;
17
18function resetFlags(size) {
19    flags = Array(size);
20    while (size--) flags[size] = 0;
21}
22
23function assertFlags(array) {
24    if (flags.length != array.length) {
25        print("array != flags");
26        return false
27    } else{
28        for (var i = 0; i < flags.length; ++i) {
29            if (flags[i] != array[i]) {
30                print("array != flags");
31                return false
32            }
33        }
34        return true
35    }
36}
37
38function object_factory(flag_index, value, expected_flags) {
39    var obj = {};
40    obj.valueOf = function() {
41        assertFlags(expected_flags);
42        flags[flag_index]++;
43        return value;
44    }
45    return obj;
46}
47
48// assertEquals(-Infinity, Math.max());
49print(Math.max());
50
51resetFlags(1);
52// assertEquals(NaN,
53print(Math.max(object_factory(0, NaN, [0])));
54assertFlags([1]);
55
56resetFlags(2);
57// assertEquals(NaN,
58print(Math.max(object_factory(0, NaN, [0, 0]),
59               object_factory(1,   0, [1, 0])));
60assertFlags([1, 1]);
61
62resetFlags(3);
63// assertEquals(NaN,
64print(Math.max(object_factory(0, NaN, [0, 0, 0]),
65               object_factory(1,   0, [1, 0, 0]),
66               object_factory(2,   1, [1, 1, 0])));
67assertFlags([1, 1, 1]);
68
69resetFlags(3);
70// assertEquals(NaN,
71print(Math.max(object_factory(0,   2, [0, 0, 0]),
72               object_factory(1,   0, [1, 0, 0]),
73               object_factory(2, NaN, [1, 1, 0])));
74assertFlags([1, 1, 1]);
75
76resetFlags(3);
77// assertEquals(2,
78print(Math.max(object_factory(0,   2, [0, 0, 0]),
79               object_factory(1,   0, [1, 0, 0]),
80               object_factory(2,   1, [1, 1, 0])));
81assertFlags([1, 1, 1]);
82
83// assertEquals(+Infinity, Math.min());
84print(Math.min());
85
86resetFlags(1);
87// assertEquals(NaN,
88print(Math.min(object_factory(0, NaN, [0])));
89assertFlags([1]);
90
91resetFlags(2);
92// assertEquals(NaN,
93print(Math.min(object_factory(0, NaN, [0, 0]),
94               object_factory(1,   0, [1, 0])));
95assertFlags([1, 1]);
96
97resetFlags(3);
98// assertEquals(NaN,
99print(Math.min(object_factory(0, NaN, [0, 0, 0]),
100               object_factory(1,   0, [1, 0, 0]),
101               object_factory(2,   1, [1, 1, 0])));
102assertFlags([1, 1, 1]);
103
104resetFlags(3);
105// assertEquals(NaN,
106print(Math.min(object_factory(0,   2, [0, 0, 0]),
107               object_factory(1,   0, [1, 0, 0]),
108               object_factory(2, NaN, [1, 1, 0])));
109assertFlags([1, 1, 1]);
110
111resetFlags(3);
112// assertEquals(0,
113print(Math.min(object_factory(0,   2, [0, 0, 0]),
114               object_factory(1,   0, [1, 0, 0]),
115               object_factory(2,   1, [1, 1, 0])));
116assertFlags([1, 1, 1]);
117
118print(Math.max(2, 465, 30.4302, -34.444));
119print(Math.max(2, 465, undefined, -34.444));
120print(Math.max(2, 465, undefined, new Date()));
121
122print(Math.min(2, 465, 30.4302, -34.444));
123print(Math.min(2, 465, undefined, -34.444));
124print(Math.min(2, 465, undefined, new Date()));