string-compare-char-string.js (1650B)
1 // Test comparison against single-element character strings. 2 3 function makeComparator(code) { 4 var str = String.fromCharCode(code); 5 6 return Function("ch", "code", ` 7 assertEq(ch == "${str}", code == ${code} && ch.length == 1); 8 assertEq(ch != "${str}", code != ${code} || ch.length != 1); 9 10 assertEq(ch < "${str}", code < ${code} || (code == ${code} && ch.length < 1)); 11 assertEq(ch <= "${str}", code < ${code} || (code == ${code} && ch.length <= 1)); 12 assertEq(ch > "${str}", code > ${code} || (code == ${code} && ch.length > 1)); 13 assertEq(ch >= "${str}", code > ${code} || (code == ${code} && ch.length >= 1)); 14 `); 15 } 16 17 function testCompare(strings, comparator) { 18 // Don't Ion compile to ensure |comparator| won't be inlined. 19 with ({}) ; 20 21 for (let i = 0; i < 1000; ++i) { 22 let str = strings[i % strings.length]; 23 24 comparator("", -1); 25 26 for (let j = 0; j < str.length; ++j) { 27 let ch = str.charAt(j); 28 let code = str.charCodeAt(j); 29 30 comparator(ch, code); 31 comparator(ch + "A", code); 32 } 33 } 34 } 35 36 // Compare against the Latin-1 character U+0062 ('b'). 37 testCompare([ 38 "", 39 "a", "b", "c", 40 "a-", "b-", "c-", 41 "a\u{100}", "b\u{100}", "c\u{100}", 42 ], makeComparator(0x62)); 43 44 // Compare against the maximum Latin-1 character U+00FF. 45 testCompare([ 46 "", 47 "a", "b", "c", 48 "a-", "b-", "c-", 49 "\u{fe}", "\u{ff}", "\u{100}", 50 "\u{fe}-", "\u{ff}-", "\u{100}-", 51 ], makeComparator(0xff)); 52 53 // Compare against the Two-byte character U+0101. 54 testCompare([ 55 "", 56 "a", "b", "c", 57 "a-", "b-", "c-", 58 "\u{100}", "\u{101}", "\u{102}", 59 "\u{100}-", "\u{101}-", "\u{102}-", 60 ], makeComparator(0x101));