Debian Codegraph @

js/src/jit-test/tests/basic/substring-of-rope.js

raw
1 gczeal(0);
2
3 const hasStringRepresentation = typeof stringRepresentation !== "undefined";
4
5 function assertSameAddress(s1, s2) {
6 if (hasStringRepresentation) {
7 var s1Repr = stringRepresentation(s1);
8 var s2Repr = stringRepresentation(s2);
9 var s1Addr = JSON.parse(s1Repr).address;
10 var s2Addr = JSON.parse(s2Repr).address;
11 assertEq(typeof s1Addr, "string");
12 assertEq(s1Addr, s2Addr);
13 }
14 }
15
16 // rope.substring(0) should not allocate a new string or flatten the rope.
17 function testSameSubstring() {
18 var rope = newRope("abcdefghijklmnop", "0123456789");
19 for (var i = 0; i < 5; i++) {
20 var sub = rope.substring(0);
21 assertEq(isRope(sub), true);
22 assertSameAddress(rope, sub);
23 }
24 }
25 testSameSubstring();
26
27 // If the substring is exactly the left or right child, we should return it
28 // without flattening the rope.
29 function testLeftRightSubstring() {
30 var left = "abcdefghijklmnopqrstuvwxyz";
31 var right = "012345678901234567890123456789";
32 var rope = newRope(left, right);
33 for (var i = 0; i < 10; i++) {
34 assertSameAddress(rope.substring(0, left.length), left);
35 assertSameAddress(rope.substring(left.length), right);
36 assertEq(isRope(rope), true); // Still a rope.
37 }
38 }
39 testLeftRightSubstring();
40
41 function testSubstringSpansBoth() {
42 var left = "abcdefghijklmnopqrstuvwxyz";
43 var right = "012345678901234567890123456789";
44 var rope = newRope(left, right);
45
46 // If the substring spans both left and right children but fits in an inline
47 // string, don't flatten the rope.
48 for (var i = 0; i < 10; i++) {
49 var substrInline = rope.substring(left.length - 5, left.length + 5);
50 if (hasStringRepresentation) {
51 assertEq(stringRepresentation(substrInline).includes("InlineString"), true);
52 }
53 assertEq(substrInline, "vwxyz01234");
54 assertEq(isRope(rope), true); // Still a rope.
55 }
56
57 // If the substring doesn't fit in an inline string, we flatten the rope and
58 // return a dependent string.
59 var substrLong = rope.substring(0, rope.length - 1);
60 assertEq(isRope(rope), false);
61 assertEq(isRope(substrLong), false);
62 }
63 testSubstringSpansBoth();