|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | +// Dart test for testing Math.min and Math.max. |
| 5 | + |
| 6 | +// VMOptions=--optimization-counter-threshold=50 --no-background-compilation |
| 7 | + |
| 8 | +import "package:expect/expect.dart"; |
| 9 | +import "dart:math" as math; |
| 10 | + |
| 11 | +var inf = double.infinity; |
| 12 | +var nan = double.nan; |
| 13 | + |
| 14 | +@pragma("vm:never-inline") |
| 15 | +int smi_min(int a, int b) => math.min(a, b); |
| 16 | + |
| 17 | +@pragma("vm:never-inline") |
| 18 | +int smi_max(int a, int b) => math.max(a, b); |
| 19 | + |
| 20 | +@pragma("vm:never-inline") |
| 21 | +int mint_min(int a, int b) => math.min(a, b); |
| 22 | + |
| 23 | +@pragma("vm:never-inline") |
| 24 | +int mint_max(int a, int b) => math.max(a, b); |
| 25 | + |
| 26 | +@pragma("vm:never-inline") |
| 27 | +double double_min(double a, double b) => math.min(a, b); |
| 28 | + |
| 29 | +@pragma("vm:never-inline") |
| 30 | +double double_max(double a, double b) => math.max(a, b); |
| 31 | + |
| 32 | +testSmiMin() { |
| 33 | + Expect.equals(-2, smi_min(-2, -2)); |
| 34 | + Expect.equals(-2, smi_min(-1, -2)); |
| 35 | + Expect.equals(-2, smi_min(0, -2)); |
| 36 | + Expect.equals(-2, smi_min(1, -2)); |
| 37 | + Expect.equals(-2, smi_min(2, -2)); |
| 38 | + |
| 39 | + Expect.equals(-2, smi_min(-2, -1)); |
| 40 | + Expect.equals(-1, smi_min(-1, -1)); |
| 41 | + Expect.equals(-1, smi_min(0, -1)); |
| 42 | + Expect.equals(-1, smi_min(1, -1)); |
| 43 | + Expect.equals(-1, smi_min(2, -1)); |
| 44 | + |
| 45 | + Expect.equals(-2, smi_min(-2, 0)); |
| 46 | + Expect.equals(-1, smi_min(-1, 0)); |
| 47 | + Expect.equals(0, smi_min(0, 0)); |
| 48 | + Expect.equals(0, smi_min(1, 0)); |
| 49 | + Expect.equals(0, smi_min(2, 0)); |
| 50 | + |
| 51 | + Expect.equals(-2, smi_min(-2, 1)); |
| 52 | + Expect.equals(-1, smi_min(-1, 1)); |
| 53 | + Expect.equals(0, smi_min(0, 1)); |
| 54 | + Expect.equals(1, smi_min(1, 1)); |
| 55 | + Expect.equals(1, smi_min(2, 1)); |
| 56 | + |
| 57 | + Expect.equals(-2, smi_min(-2, 2)); |
| 58 | + Expect.equals(-1, smi_min(-1, 2)); |
| 59 | + Expect.equals(0, smi_min(0, 2)); |
| 60 | + Expect.equals(1, smi_min(1, 2)); |
| 61 | + Expect.equals(2, smi_min(2, 2)); |
| 62 | +} |
| 63 | + |
| 64 | +testSmiMax() { |
| 65 | + Expect.equals(-2, smi_max(-2, -2)); |
| 66 | + Expect.equals(-1, smi_max(-1, -2)); |
| 67 | + Expect.equals(0, smi_max(0, -2)); |
| 68 | + Expect.equals(1, smi_max(1, -2)); |
| 69 | + Expect.equals(2, smi_max(2, -2)); |
| 70 | + |
| 71 | + Expect.equals(-1, smi_max(-2, -1)); |
| 72 | + Expect.equals(-1, smi_max(-1, -1)); |
| 73 | + Expect.equals(0, smi_max(0, -1)); |
| 74 | + Expect.equals(1, smi_max(1, -1)); |
| 75 | + Expect.equals(2, smi_max(2, -1)); |
| 76 | + |
| 77 | + Expect.equals(0, smi_max(-2, 0)); |
| 78 | + Expect.equals(0, smi_max(-1, 0)); |
| 79 | + Expect.equals(0, smi_max(0, 0)); |
| 80 | + Expect.equals(1, smi_max(1, 0)); |
| 81 | + Expect.equals(2, smi_max(2, 0)); |
| 82 | + |
| 83 | + Expect.equals(1, smi_max(-2, 1)); |
| 84 | + Expect.equals(1, smi_max(-1, 1)); |
| 85 | + Expect.equals(1, smi_max(0, 1)); |
| 86 | + Expect.equals(1, smi_max(1, 1)); |
| 87 | + Expect.equals(2, smi_max(2, 1)); |
| 88 | + |
| 89 | + Expect.equals(2, smi_max(-2, 2)); |
| 90 | + Expect.equals(2, smi_max(-1, 2)); |
| 91 | + Expect.equals(2, smi_max(0, 2)); |
| 92 | + Expect.equals(2, smi_max(1, 2)); |
| 93 | + Expect.equals(2, smi_max(2, 2)); |
| 94 | +} |
| 95 | + |
| 96 | +testMintMin() { |
| 97 | + Expect.equals(0x8000000000000000, mint_min(0x8000000000000000, 0x8000000000000000)); |
| 98 | + Expect.equals(0x8000000000000000, mint_min(-1, 0x8000000000000000)); |
| 99 | + Expect.equals(0x8000000000000000, mint_min(0, 0x8000000000000000)); |
| 100 | + Expect.equals(0x8000000000000000, mint_min(1, 0x8000000000000000)); |
| 101 | + Expect.equals(0x8000000000000000, mint_min(0x7FFFFFFFFFFFFFFF, 0x8000000000000000)); |
| 102 | + |
| 103 | + Expect.equals(0x8000000000000000, mint_min(0x8000000000000000, -1)); |
| 104 | + Expect.equals(-1, mint_min(-1, -1)); |
| 105 | + Expect.equals(-1, mint_min(0, -1)); |
| 106 | + Expect.equals(-1, mint_min(1, -1)); |
| 107 | + Expect.equals(-1, mint_min(0x7FFFFFFFFFFFFFFF, -1)); |
| 108 | + |
| 109 | + Expect.equals(0x8000000000000000, mint_min(0x8000000000000000, 0)); |
| 110 | + Expect.equals(-1, mint_min(-1, 0)); |
| 111 | + Expect.equals(0, mint_min(0, 0)); |
| 112 | + Expect.equals(0, mint_min(1, 0)); |
| 113 | + Expect.equals(0, mint_min(0x7FFFFFFFFFFFFFFF, 0)); |
| 114 | + |
| 115 | + Expect.equals(0x8000000000000000, mint_min(0x8000000000000000, 1)); |
| 116 | + Expect.equals(-1, mint_min(-1, 1)); |
| 117 | + Expect.equals(0, mint_min(0, 1)); |
| 118 | + Expect.equals(1, mint_min(1, 1)); |
| 119 | + Expect.equals(1, mint_min(0x7FFFFFFFFFFFFFFF, 1)); |
| 120 | + |
| 121 | + Expect.equals(0x8000000000000000, mint_min(0x8000000000000000, 0x7FFFFFFFFFFFFFFF)); |
| 122 | + Expect.equals(-1, mint_min(-1, 0x7FFFFFFFFFFFFFFF)); |
| 123 | + Expect.equals(0, mint_min(0, 0x7FFFFFFFFFFFFFFF)); |
| 124 | + Expect.equals(1, mint_min(1, 0x7FFFFFFFFFFFFFFF)); |
| 125 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_min(0x7FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF)); |
| 126 | +} |
| 127 | + |
| 128 | +testMintMax() { |
| 129 | + Expect.equals(0x8000000000000000, mint_max(0x8000000000000000, 0x8000000000000000)); |
| 130 | + Expect.equals(-1, mint_max(-1, 0x8000000000000000)); |
| 131 | + Expect.equals(0, mint_max(0, 0x8000000000000000)); |
| 132 | + Expect.equals(1, mint_max(1, 0x8000000000000000)); |
| 133 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(0x7FFFFFFFFFFFFFFF, 0x8000000000000000)); |
| 134 | + |
| 135 | + Expect.equals(-1, mint_max(0x8000000000000000, -1)); |
| 136 | + Expect.equals(-1, mint_max(-1, -1)); |
| 137 | + Expect.equals(0, mint_max(0, -1)); |
| 138 | + Expect.equals(1, mint_max(1, -1)); |
| 139 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(0x7FFFFFFFFFFFFFFF, -1)); |
| 140 | + |
| 141 | + Expect.equals(0, mint_max(0x8000000000000000, 0)); |
| 142 | + Expect.equals(0, mint_max(-1, 0)); |
| 143 | + Expect.equals(0, mint_max(0, 0)); |
| 144 | + Expect.equals(1, mint_max(1, 0)); |
| 145 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(0x7FFFFFFFFFFFFFFF, 0)); |
| 146 | + |
| 147 | + Expect.equals(1, mint_max(0x8000000000000000, 1)); |
| 148 | + Expect.equals(1, mint_max(-1, 1)); |
| 149 | + Expect.equals(1, mint_max(0, 1)); |
| 150 | + Expect.equals(1, mint_max(1, 1)); |
| 151 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(0x7FFFFFFFFFFFFFFF, 1)); |
| 152 | + |
| 153 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(0x8000000000000000, 0x7FFFFFFFFFFFFFFF)); |
| 154 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(-1, 0x7FFFFFFFFFFFFFFF)); |
| 155 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(0, 0x7FFFFFFFFFFFFFFF)); |
| 156 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(1, 0x7FFFFFFFFFFFFFFF)); |
| 157 | + Expect.equals(0x7FFFFFFFFFFFFFFF, mint_max(0x7FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF)); |
| 158 | +} |
| 159 | + |
| 160 | +testDoubleMin() { |
| 161 | + Expect.equals(-inf, double_min(-inf, -inf)); |
| 162 | + Expect.equals(-inf, double_min(-1.0, -inf)); |
| 163 | + Expect.equals(-inf, double_min(0.0, -inf)); |
| 164 | + Expect.equals(-inf, double_min(1.0, -inf)); |
| 165 | + Expect.equals(-inf, double_min(inf, -inf)); |
| 166 | + |
| 167 | + Expect.equals(-inf, double_min(-inf, -1.0)); |
| 168 | + Expect.equals(-1.0, double_min(-1.0, -1.0)); |
| 169 | + Expect.equals(-1.0, double_min(0.0, -1.0)); |
| 170 | + Expect.equals(-1.0, double_min(1.0, -1.0)); |
| 171 | + Expect.equals(-1.0, double_min(inf, -1.0)); |
| 172 | + |
| 173 | + Expect.equals(-inf, double_min(-inf, 0.0)); |
| 174 | + Expect.equals(-1.0, double_min(-1.0, 0.0)); |
| 175 | + Expect.equals(0.0, double_min(0.0, 0.0)); |
| 176 | + Expect.equals(0.0, double_min(1.0, 0.0)); |
| 177 | + Expect.equals(0.0, double_min(inf, 0.0)); |
| 178 | + |
| 179 | + Expect.equals(-inf, double_min(-inf, 1.0)); |
| 180 | + Expect.equals(-1.0, double_min(-1.0, 1.0)); |
| 181 | + Expect.equals(0.0, double_min(0.0, 1.0)); |
| 182 | + Expect.equals(1.0, double_min(1.0, 1.0)); |
| 183 | + Expect.equals(1.0, double_min(inf, 1.0)); |
| 184 | + |
| 185 | + Expect.equals(-inf, double_min(-inf, inf)); |
| 186 | + Expect.equals(-1.0, double_min(-1.0, inf)); |
| 187 | + Expect.equals(0.0, double_min(0.0, inf)); |
| 188 | + Expect.equals(1.0, double_min(1.0, inf)); |
| 189 | + Expect.equals(inf, double_min(inf, inf)); |
| 190 | + |
| 191 | + Expect.identical(-0.0, double_min(0.0, -0.0)); |
| 192 | + Expect.identical(-0.0, double_min(-0.0, 0.0)); |
| 193 | + |
| 194 | + Expect.isTrue(double_min(nan, nan).isNaN); |
| 195 | + Expect.isTrue(double_min(nan, -inf).isNaN); |
| 196 | + Expect.isTrue(double_min(nan, 1.0).isNaN); |
| 197 | + Expect.isTrue(double_min(nan, inf).isNaN); |
| 198 | + Expect.isTrue(double_min(nan, nan).isNaN); |
| 199 | + Expect.isTrue(double_min(-inf, nan).isNaN); |
| 200 | + Expect.isTrue(double_min(1.0, nan).isNaN); |
| 201 | + Expect.isTrue(double_min(inf, nan).isNaN); |
| 202 | +} |
| 203 | + |
| 204 | +testDoubleMax() { |
| 205 | + Expect.equals(-inf, double_max(-inf, -inf)); |
| 206 | + Expect.equals(-1.0, double_max(-1.0, -inf)); |
| 207 | + Expect.equals(0.0, double_max(0.0, -inf)); |
| 208 | + Expect.equals(1.0, double_max(1.0, -inf)); |
| 209 | + Expect.equals(inf, double_max(inf, -inf)); |
| 210 | + |
| 211 | + Expect.equals(-1.0, double_max(-inf, -1.0)); |
| 212 | + Expect.equals(-1.0, double_max(-1.0, -1.0)); |
| 213 | + Expect.equals(0.0, double_max(0.0, -1.0)); |
| 214 | + Expect.equals(1.0, double_max(1.0, -1.0)); |
| 215 | + Expect.equals(inf, double_max(inf, -1.0)); |
| 216 | + |
| 217 | + Expect.equals(0.0, double_max(-inf, 0.0)); |
| 218 | + Expect.equals(0.0, double_max(-1.0, 0.0)); |
| 219 | + Expect.equals(0.0, double_max(0.0, 0.0)); |
| 220 | + Expect.equals(1.0, double_max(1.0, 0.0)); |
| 221 | + Expect.equals(inf, double_max(inf, 0.0)); |
| 222 | + |
| 223 | + Expect.equals(1.0, double_max(-inf, 1.0)); |
| 224 | + Expect.equals(1.0, double_max(-1.0, 1.0)); |
| 225 | + Expect.equals(1.0, double_max(0.0, 1.0)); |
| 226 | + Expect.equals(1.0, double_max(1.0, 1.0)); |
| 227 | + Expect.equals(inf, double_max(inf, 1.0)); |
| 228 | + |
| 229 | + Expect.equals(inf, double_max(-inf, inf)); |
| 230 | + Expect.equals(inf, double_max(-1.0, inf)); |
| 231 | + Expect.equals(inf, double_max(0.0, inf)); |
| 232 | + Expect.equals(inf, double_max(1.0, inf)); |
| 233 | + Expect.equals(inf, double_max(inf, inf)); |
| 234 | + |
| 235 | + Expect.identical(0.0, double_max(0.0, -0.0)); |
| 236 | + Expect.identical(0.0, double_max(-0.0, 0.0)); |
| 237 | + |
| 238 | + Expect.isTrue(double_max(nan, nan).isNaN); |
| 239 | + Expect.isTrue(double_max(nan, -inf).isNaN); |
| 240 | + Expect.isTrue(double_max(nan, 1.0).isNaN); |
| 241 | + Expect.isTrue(double_max(nan, inf).isNaN); |
| 242 | + Expect.isTrue(double_max(-inf, nan).isNaN); |
| 243 | + Expect.isTrue(double_max(1.0, nan).isNaN); |
| 244 | + Expect.isTrue(double_max(inf, nan).isNaN); |
| 245 | +} |
| 246 | + |
| 247 | +main() { |
| 248 | + for (int i = 0; i < 100; i++) { |
| 249 | + testSmiMin(); |
| 250 | + testSmiMax(); |
| 251 | + testMintMin(); |
| 252 | + testMintMax(); |
| 253 | + testDoubleMin(); |
| 254 | + testDoubleMax(); |
| 255 | + } |
| 256 | +} |
0 commit comments