|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Sony Interactive Entertainment Inc. |
| 3 | + * Copyright (C) 2020 Apple Inc. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +function testBasicCases() { |
| 28 | + shouldBe(undefined ?? 3, 3); |
| 29 | + shouldBe(null ?? 3, 3); |
| 30 | + shouldBe(true ?? 3, true); |
| 31 | + shouldBe(false ?? 3, false); |
| 32 | + shouldBe(0 ?? 3, 0); |
| 33 | + shouldBe(1 ?? 3, 1); |
| 34 | + shouldBe('' ?? 3, ''); |
| 35 | + shouldBe('hi' ?? 3, 'hi'); |
| 36 | + shouldBe(({} ?? 3) instanceof Object, true); |
| 37 | + shouldBe(({ x: 'hi' } ?? 3).x, 'hi'); |
| 38 | + shouldBe(([] ?? 3) instanceof Array, true); |
| 39 | + shouldBe((['hi'] ?? 3)[0], 'hi'); |
| 40 | + shouldBe((makeMasquerader() ?? 3) == null, true); |
| 41 | +} |
| 42 | +noInline(testBasicCases); |
| 43 | + |
| 44 | +for (let i = 0; i < 1e5; i++) |
| 45 | + testBasicCases(); |
| 46 | + |
| 47 | +shouldBe(1 | null ?? 3, 1); |
| 48 | +shouldBe(1 ^ null ?? 3, 1); |
| 49 | +shouldBe(1 & null ?? 3, 0); |
| 50 | +shouldBe(3 == null ?? 3, false); |
| 51 | +shouldBe(3 != null ?? 3, true); |
| 52 | +shouldBe(3 === null ?? 3, false); |
| 53 | +shouldBe(3 !== null ?? 3, true); |
| 54 | +shouldBe(1 < null ?? 3, false); |
| 55 | +shouldBe(1 > null ?? 3, true); |
| 56 | +shouldBe(1 <= null ?? 3, false); |
| 57 | +shouldBe(1 >= null ?? 3, true); |
| 58 | +shouldBe(1 << null ?? 3, 1); |
| 59 | +shouldBe(1 >> null ?? 3, 1); |
| 60 | +shouldBe(1 >>> null ?? 3, 1); |
| 61 | +shouldBe(1 + null ?? 3, 1); |
| 62 | +shouldBe(1 - null ?? 3, 1); |
| 63 | +shouldBe(1 * null ?? 3, 0); |
| 64 | +shouldBe(1 / null ?? 3, Infinity); |
| 65 | +shouldBe(isNaN(1 % null ?? 3), true); |
| 66 | +shouldBe(1 ** null ?? 3, 1); |
| 67 | + |
| 68 | +const obj = { |
| 69 | + count: 0, |
| 70 | + get x() { this.count++; return 'x'; } |
| 71 | +}; |
| 72 | +false ?? obj.x; |
| 73 | +shouldBe(obj.count, 0); |
| 74 | +null ?? obj.x; |
| 75 | +shouldBe(obj.count, 1); |
| 76 | +obj.x ?? obj.x; |
| 77 | +shouldBe(obj.count, 2); |
| 78 | + |
| 79 | +(0 || 1) ?? 2; |
| 80 | +0 || (1 ?? 2); |
| 81 | +(0 && 1) ?? 2; |
| 82 | +0 && (1 ?? 2); |
| 83 | +(0 ?? 1) || 2; |
| 84 | +0 ?? (1 || 2); |
| 85 | +(0 ?? 1) && 2; |
| 86 | +0 ?? (1 && 2); |
| 87 | + |
| 88 | +0 || 1 && 2 | 3 ^ 4 & 5 == 6 != 7 === 8 !== 9 < 0 > 1 <= 2 >= 3 << 4 >> 5 >>> 6 + 7 - 8 * 9 / 0 % 1 ** 2 |
0 commit comments