min-max-foldsTo-2.js (5568B)
1 with ({}); // Don't inline anything into the top-level script. 2 3 // Fold min(x, min(y, z)) to min(min(x, y), z) with constant min(x, y). 4 for (let x of [-Infinity, -10, 0, 10, Infinity, NaN]) { 5 for (let y of [-Infinity, -10, 0, 10, Infinity, NaN]) { 6 for (let z of [-Infinity, -10, 0, 10, Infinity, NaN]) { 7 let fn = Function("z", `return Math.min(${x}, Math.min(${y}, z))`); 8 for (let i = 0; i < 100; ++i) { 9 let r = fn(z); 10 assertEq(r, Math.min(x, Math.min(y, z))); 11 assertEq(r, Math.min(Math.min(x, y), z)); 12 } 13 14 // Reverse operands. 15 fn = Function("z", `return Math.min(${x}, Math.min(z, ${y}))`); 16 for (let i = 0; i < 100; ++i) { 17 let r = fn(z); 18 assertEq(r, Math.min(x, Math.min(z, y))); 19 assertEq(r, Math.min(Math.min(x, y), z)); 20 } 21 22 // Reverse operands. 23 fn = Function("z", `return Math.min(Math.min(${y}, z), ${x})`); 24 for (let i = 0; i < 100; ++i) { 25 let r = fn(z); 26 assertEq(r, Math.min(Math.min(y, z), x)); 27 assertEq(r, Math.min(Math.min(x, y), z)); 28 } 29 30 // Reverse operands. 31 fn = Function("z", `return Math.min(Math.min(z, ${y}), ${x})`); 32 for (let i = 0; i < 100; ++i) { 33 let r = fn(z); 34 assertEq(r, Math.min(Math.min(z, y), x)); 35 assertEq(r, Math.min(Math.min(x, y), z)); 36 } 37 } 38 } 39 } 40 41 // Fold max(x, max(y, z)) to max(max(x, y), z) with constant max(x, y). 42 for (let x of [-Infinity, -10, 0, 10, Infinity, NaN]) { 43 for (let y of [-Infinity, -10, 0, 10, Infinity, NaN]) { 44 for (let z of [-Infinity, -10, 0, 10, Infinity, NaN]) { 45 let fn = Function("z", `return Math.max(${x}, Math.max(${y}, z))`); 46 for (let i = 0; i < 100; ++i) { 47 let r = fn(z); 48 assertEq(r, Math.max(x, Math.max(y, z))); 49 assertEq(r, Math.max(Math.max(x, y), z)); 50 } 51 52 // Reverse operands. 53 fn = Function("z", `return Math.max(${x}, Math.max(z, ${y}))`); 54 for (let i = 0; i < 100; ++i) { 55 let r = fn(z); 56 assertEq(r, Math.max(x, Math.max(z, y))); 57 assertEq(r, Math.max(Math.max(x, y), z)); 58 } 59 60 // Reverse operands. 61 fn = Function("z", `return Math.max(Math.max(${y}, z), ${x})`); 62 for (let i = 0; i < 100; ++i) { 63 let r = fn(z); 64 assertEq(r, Math.max(Math.max(y, z), x)); 65 assertEq(r, Math.max(Math.max(x, y), z)); 66 } 67 68 // Reverse operands. 69 fn = Function("z", `return Math.max(Math.max(z, ${y}), ${x})`); 70 for (let i = 0; i < 100; ++i) { 71 let r = fn(z); 72 assertEq(r, Math.max(Math.max(z, y), x)); 73 assertEq(r, Math.max(Math.max(x, y), z)); 74 } 75 } 76 } 77 } 78 79 // Fold min(x, max(y, z)) to max(min(x, y), min(x, z)). 80 for (let x of [-Infinity, -10, 0, 10, Infinity, NaN]) { 81 for (let y of [-Infinity, -10, 0, 10, Infinity, NaN]) { 82 for (let z of ["", "asdf", [], [1,2,3], new Int32Array(0), new Int32Array(10)]) { 83 let fn = Function("z", `return Math.min(${x}, Math.max(${y}, z.length))`); 84 for (let i = 0; i < 100; ++i) { 85 let r = fn(z); 86 assertEq(r, Math.min(x, Math.max(y, z.length))); 87 assertEq(r, Math.max(Math.min(x, y), Math.min(x, z.length))); 88 } 89 90 // Reverse operands. 91 fn = Function("z", `return Math.min(${x}, Math.max(z.length, ${y}))`); 92 for (let i = 0; i < 100; ++i) { 93 let r = fn(z); 94 assertEq(r, Math.min(x, Math.max(z.length, y))); 95 assertEq(r, Math.max(Math.min(x, y), Math.min(x, z.length))); 96 } 97 98 // Reverse operands. 99 fn = Function("z", `return Math.min(Math.max(${y}, z.length), ${x})`); 100 for (let i = 0; i < 100; ++i) { 101 let r = fn(z); 102 assertEq(r, Math.min(Math.max(y, z.length), x)); 103 assertEq(r, Math.max(Math.min(x, y), Math.min(x, z.length))); 104 } 105 106 // Reverse operands. 107 fn = Function("z", `return Math.min(Math.max(z.length, ${y}), ${x})`); 108 for (let i = 0; i < 100; ++i) { 109 let r = fn(z); 110 assertEq(r, Math.min(Math.max(z.length, y), x)); 111 assertEq(r, Math.max(Math.min(x, y), Math.min(x, z.length))); 112 } 113 } 114 } 115 } 116 117 // Fold max(x, min(y, z)) to min(max(x, y), max(x, z)). 118 for (let x of [-Infinity, -10, 0, 10, Infinity, NaN]) { 119 for (let y of [-Infinity, -10, 0, 10, Infinity, NaN]) { 120 for (let z of ["", "asdf", [], [1,2,3], new Int32Array(0), new Int32Array(10)]) { 121 let fn = Function("z", `return Math.max(${x}, Math.min(${y}, z.length))`); 122 for (let i = 0; i < 100; ++i) { 123 let r = fn(z); 124 assertEq(r, Math.max(x, Math.min(y, z.length))); 125 assertEq(r, Math.min(Math.max(x, y), Math.max(x, z.length))); 126 } 127 128 // Reverse operands. 129 fn = Function("z", `return Math.max(${x}, Math.min(z.length, ${y}))`); 130 for (let i = 0; i < 100; ++i) { 131 let r = fn(z); 132 assertEq(r, Math.max(x, Math.min(z.length, y))); 133 assertEq(r, Math.min(Math.max(x, y), Math.max(x, z.length))); 134 } 135 136 // Reverse operands. 137 fn = Function("z", `return Math.max(Math.min(${y}, z.length), ${x})`); 138 for (let i = 0; i < 100; ++i) { 139 let r = fn(z); 140 assertEq(r, Math.max(Math.min(y, z.length), x)); 141 assertEq(r, Math.min(Math.max(x, y), Math.max(x, z.length))); 142 } 143 144 // Reverse operands. 145 fn = Function("z", `return Math.max(Math.min(z.length, ${y}), ${x})`); 146 for (let i = 0; i < 100; ++i) { 147 let r = fn(z); 148 assertEq(r, Math.max(Math.min(z.length, y), x)); 149 assertEq(r, Math.min(Math.max(x, y), Math.max(x, z.length))); 150 } 151 } 152 } 153 }