File: iteration.js

package info (click to toggle)
kjs 5.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,020 kB
  • sloc: cpp: 36,704; javascript: 5,079; yacc: 790; perl: 191; sh: 52; makefile: 7
file content (151 lines) | stat: -rw-r--r-- 3,355 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 12.6.1
var count = 0;
do {
  count++;
} while (count < 10);
shouldBe("count", "10");

count = 0;
for (var i = 0; i < 10; i++) {
  if (i == 5)
    break;
  count++;
}
shouldBe("count", "5");

// 12.6.3
count = 0;
for (i = 0; i < 10; i++) {
  count++;
}
shouldBe("count", "10");

// 12.6.4
obj = new Object();
obj.a = 11;
obj.b = 22;

properties = "";
for ( prop in obj )
  properties += (prop + "=" + obj[prop] + ";");

shouldBe("properties", "'a=11;b=22;'");

// now a test verifying the order. not standardized but common.
obj.y = 33;
obj.x = 44;
properties = "";
for ( prop in obj )
  properties += prop;
// shouldBe("properties", "'abyx'");

arr = new Array;
arr[0] = 100;
arr[1] = 101;
list = "";
for ( var j in arr ) {
  list += "[" + j + "]=" + arr[j] + ";";
}
shouldBe("list","'[0]=100;[1]=101;'");

list = "";
for (var a = [1,2,3], length = a.length, i = 0; i < length; i++) {
  list += a[i];
}
shouldBe("list", "'123'");

// completion values
var immediateWhileBreak = eval("while (true) { break; }");
shouldBeUndefined("immediateWhileBreak", "'foo'");
var laterWhileBreak = eval("while (true) { 'foo'; break; }");
shouldBe("laterWhileBreak", "'foo'");
var whileEnd = eval("var y = 0; while (y < 2) { ++y }");
shouldBe("whileEnd", "2");

var immediateDoWhileBreak = eval("do { break; } while(false); ");
shouldBeUndefined("immediateDoWhileBreak", "'foo'");
var laterDoWhileBreak = eval("do { 'bar'; break; } while(true);");
shouldBe("laterDoWhileBreak", "'bar'");
var doWhileEnd = eval("var y = 0; do { ++y } while (y < 3);");
shouldBe("doWhileEnd", "3");

var immediateForBreak = eval("for(;;) { break; }");
shouldBeUndefined("immediateForBreak", "'foo'");
var laterForBreak = eval("for(;;) { 'zilch'; break; }");
shouldBe("laterForBreak", "'zilch'");
var forEnd = eval("for(var y = 0; y < 4; ++y) { 'quark' }");
shouldBe("forEnd", "'quark'");

// Various stuff for control flow and error handling of break/continue
var iters;

// Test whether we handle multiple labels on the same statement right
// Note: this is in a function since konq 3.x just breaks out of the whole thing.
function testLabelNest() {
  iters = 0;
  foo: bar: while (true) {
    ++iters;
    if (iters < 10)
      continue foo;
    else
      break;
  }
}

testLabelNest();
shouldBe('iters', '10');

// Test to make sure doing continue outside a loop
// throws properly. (3.x didn't), and so does a label-less
// break outside of it.
shouldThrow('foo: continue foo; ');
shouldThrow('continue; ');
shouldThrow('break;');

// These ones should be fine OTOH
shouldBeUndefined('foo: break foo; ');
shouldBe('foo: { a = 1; break foo; a = 2; }', '1');

// Make sure we don't break out too far.
function testLocalBreak() {
  l: break l;
}

for (iters = 0; iters < 10; ++iters)
  testLocalBreak();

shouldBe('iters', '10');

// Regression test, bug during FB development.
var o = {a: 0, b: 1}
for (p in o) {
    if (p == "b")
        break;
    continue;
}

testPassed("Didn't crash on continue in for .. in");

/** Regression test for #165847: making sure that break
out of a for ... in cleans stuff up properly from the for ... in stack
*/
var list = [];
for (var i = 0; i < 10; ++i)
  list.push(i);

done = false;
for (i in list) {
  for (j in list) {
    if (i == 0 && j == 1) {
        done = true;
        break;
    }

    if (done)
      break;
  }
}



debug("Done.");