Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: test/mjsunit/debug-sourceinfo.js

Issue 118371: Change locationFromPosition() and locationFromLine() to use a binary search t... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/messages.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 20 matching lines...) Expand all
31 function b() { 31 function b() {
32 c(true); 32 c(true);
33 }; 33 };
34 function c(x) { 34 function c(x) {
35 if (x) { 35 if (x) {
36 return 1; 36 return 1;
37 } else { 37 } else {
38 return 1; 38 return 1;
39 } 39 }
40 }; 40 };
41 function d(x) {
42 x = 1 ;
43 x = 2 ;
44 x = 3 ;
45 x = 4 ;
46 x = 5 ;
47 x = 6 ;
48 x = 7 ;
49 x = 8 ;
50 x = 9 ;
51 x = 10;
52 x = 11;
53 x = 12;
54 x = 13;
55 x = 14;
56 x = 15;
57 }
41 58
42 // Get the Debug object exposed from the debug context global object. 59 // Get the Debug object exposed from the debug context global object.
43 Debug = debug.Debug 60 Debug = debug.Debug
44 61
45 // This is the number of comment lines above the first test function. 62 // This is the number of comment lines above the first test function.
46 var comment_lines = 29; 63 var comment_lines = 29;
47 64
65 // This is the last position in the entire file (note: this equals
66 // file size of <debug-sourceinfo.js> - 1, since starting at 0).
67 var last_position = 14072;
68 // This is the last line of entire file (note: starting at 0).
69 var last_line = 345;
70 // This is the last column of last line (note: starting at 0 and +2, due
71 // to trailing <CR><LF>).
72 var last_column = 48;
73
48 // This magic number is the length or the first line comment (actually number 74 // This magic number is the length or the first line comment (actually number
49 // of characters before 'function a(...'. 75 // of characters before 'function a(...'.
50 var comment_line_length = 1726; 76 var comment_line_length = 1726;
51 var start_a = 10 + comment_line_length; 77 var start_a = 10 + comment_line_length;
52 var start_b = 37 + comment_line_length; 78 var start_b = 37 + comment_line_length;
53 var start_c = 71 + comment_line_length; 79 var start_c = 71 + comment_line_length;
80 var start_d = 163 + comment_line_length;
81
82 // The position of the first line of d(), i.e. "x = 1 ;".
83 var start_code_d = start_d + 7;
84 // The line # of the first line of d() (note: starting at 0).
85 var start_line_d = 41;
86 var line_length_d = 11;
87 var num_lines_d = 15;
54 88
55 assertEquals(start_a, Debug.sourcePosition(a)); 89 assertEquals(start_a, Debug.sourcePosition(a));
56 assertEquals(start_b, Debug.sourcePosition(b)); 90 assertEquals(start_b, Debug.sourcePosition(b));
57 assertEquals(start_c, Debug.sourcePosition(c)); 91 assertEquals(start_c, Debug.sourcePosition(c));
92 assertEquals(start_d, Debug.sourcePosition(d));
58 93
59 var script = Debug.findScript(a); 94 var script = Debug.findScript(a);
60 assertTrue(script.data === Debug.findScript(b).data); 95 assertTrue(script.data === Debug.findScript(b).data);
61 assertTrue(script.data === Debug.findScript(c).data); 96 assertTrue(script.data === Debug.findScript(c).data);
97 assertTrue(script.data === Debug.findScript(d).data);
62 assertTrue(script.source === Debug.findScript(b).source); 98 assertTrue(script.source === Debug.findScript(b).source);
63 assertTrue(script.source === Debug.findScript(c).source); 99 assertTrue(script.source === Debug.findScript(c).source);
100 assertTrue(script.source === Debug.findScript(d).source);
64 101
65 // Test that when running through source positions the position, line and 102 // Test that when running through source positions the position, line and
66 // column progresses as expected. 103 // column progresses as expected.
67 var position; 104 var position;
68 var line; 105 var line;
69 var column; 106 var column;
70 for (var p = 0; p < 100; p++) { 107 for (var p = 0; p < 100; p++) {
71 var location = script.locationFromPosition(p); 108 var location = script.locationFromPosition(p);
72 if (p > 0) { 109 if (p > 0) {
73 assertEquals(position + 1, location.position); 110 assertEquals(position + 1, location.position);
74 if (line == location.line) { 111 if (line == location.line) {
75 assertEquals(column + 1, location.column); 112 assertEquals(column + 1, location.column);
76 } else { 113 } else {
77 assertEquals(line + 1, location.line); 114 assertEquals(line + 1, location.line);
78 assertEquals(0, location.column); 115 assertEquals(0, location.column);
79 } 116 }
80 } else { 117 } else {
81 assertEquals(0, location.position); 118 assertEquals(0, location.position);
82 assertEquals(0, location.line); 119 assertEquals(0, location.line);
83 assertEquals(0, location.column); 120 assertEquals(0, location.column);
84 } 121 }
85 122
86 // Remember the location. 123 // Remember the location.
87 position = location.position; 124 position = location.position;
88 line = location.line; 125 line = location.line;
89 column = location.column; 126 column = location.column;
90 } 127 }
91 128
129 // Every line of d() is the same length. Verify we can loop through all
130 // positions and find the right line # for each.
131 var p = start_code_d;
132 for (line = 0; line < num_lines_d; line++) {
133 for (column = 0; column < line_length_d; column++) {
134 var location = script.locationFromPosition(p);
135 assertEquals(p, location.position);
136 assertEquals(start_line_d + line, location.line);
137 assertEquals(column, location.column);
138 p++;
139 }
140 }
141
92 // Test first position. 142 // Test first position.
93 assertEquals(0, script.locationFromPosition(0).position); 143 assertEquals(0, script.locationFromPosition(0).position);
94 assertEquals(0, script.locationFromPosition(0).line); 144 assertEquals(0, script.locationFromPosition(0).line);
95 assertEquals(0, script.locationFromPosition(0).column); 145 assertEquals(0, script.locationFromPosition(0).column);
96 146
97 // Test second position. 147 // Test second position.
98 assertEquals(1, script.locationFromPosition(1).position); 148 assertEquals(1, script.locationFromPosition(1).position);
99 assertEquals(0, script.locationFromPosition(1).line); 149 assertEquals(0, script.locationFromPosition(1).line);
100 assertEquals(1, script.locationFromPosition(1).column); 150 assertEquals(1, script.locationFromPosition(1).column);
101 151
102 // Test first position in finction a. 152 // Test first position in function a().
103 assertEquals(start_a, script.locationFromPosition(start_a).position); 153 assertEquals(start_a, script.locationFromPosition(start_a).position);
104 assertEquals(0, script.locationFromPosition(start_a).line - comment_lines); 154 assertEquals(0, script.locationFromPosition(start_a).line - comment_lines);
105 assertEquals(10, script.locationFromPosition(start_a).column); 155 assertEquals(10, script.locationFromPosition(start_a).column);
106 156
107 // Test first position in finction b. 157 // Test first position in function b().
108 assertEquals(start_b, script.locationFromPosition(start_b).position); 158 assertEquals(start_b, script.locationFromPosition(start_b).position);
109 assertEquals(1, script.locationFromPosition(start_b).line - comment_lines); 159 assertEquals(1, script.locationFromPosition(start_b).line - comment_lines);
110 assertEquals(13, script.locationFromPosition(start_b).column); 160 assertEquals(13, script.locationFromPosition(start_b).column);
111 161
112 // Test first position in finction b. 162 // Test first position in function c().
113 assertEquals(start_c, script.locationFromPosition(start_c).position); 163 assertEquals(start_c, script.locationFromPosition(start_c).position);
114 assertEquals(4, script.locationFromPosition(start_c).line - comment_lines); 164 assertEquals(4, script.locationFromPosition(start_c).line - comment_lines);
115 assertEquals(12, script.locationFromPosition(start_c).column); 165 assertEquals(12, script.locationFromPosition(start_c).column);
116 166
167 // Test first position in function d().
168 assertEquals(start_d, script.locationFromPosition(start_d).position);
169 assertEquals(11, script.locationFromPosition(start_d).line - comment_lines);
170 assertEquals(10, script.locationFromPosition(start_d).column);
171
117 // Test first line. 172 // Test first line.
118 assertEquals(0, script.locationFromLine().position); 173 assertEquals(0, script.locationFromLine().position);
119 assertEquals(0, script.locationFromLine().line); 174 assertEquals(0, script.locationFromLine().line);
120 assertEquals(0, script.locationFromLine().column); 175 assertEquals(0, script.locationFromLine().column);
121 assertEquals(0, script.locationFromLine(0).position); 176 assertEquals(0, script.locationFromLine(0).position);
122 assertEquals(0, script.locationFromLine(0).line); 177 assertEquals(0, script.locationFromLine(0).line);
123 assertEquals(0, script.locationFromLine(0).column); 178 assertEquals(0, script.locationFromLine(0).column);
124 179
125 // Test first line column 1 180 // Test first line column 1.
126 assertEquals(1, script.locationFromLine(0, 1).position); 181 assertEquals(1, script.locationFromLine(0, 1).position);
127 assertEquals(0, script.locationFromLine(0, 1).line); 182 assertEquals(0, script.locationFromLine(0, 1).line);
128 assertEquals(1, script.locationFromLine(0, 1).column); 183 assertEquals(1, script.locationFromLine(0, 1).column);
129 184
130 // Test first line offset 1 185 // Test first line offset 1.
131 assertEquals(1, script.locationFromLine(0, 0, 1).position); 186 assertEquals(1, script.locationFromLine(0, 0, 1).position);
132 assertEquals(0, script.locationFromLine(0, 0, 1).line); 187 assertEquals(0, script.locationFromLine(0, 0, 1).line);
133 assertEquals(1, script.locationFromLine(0, 0, 1).column); 188 assertEquals(1, script.locationFromLine(0, 0, 1).column);
134 189
135 // Test offset function a 190 // Test offset function a().
136 assertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position) ; 191 assertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position) ;
137 assertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_ lines); 192 assertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_ lines);
138 assertEquals(10, script.locationFromLine(void 0, void 0, start_a).column); 193 assertEquals(10, script.locationFromLine(void 0, void 0, start_a).column);
139 assertEquals(start_a, script.locationFromLine(0, void 0, start_a).position); 194 assertEquals(start_a, script.locationFromLine(0, void 0, start_a).position);
140 assertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines ); 195 assertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines );
141 assertEquals(10, script.locationFromLine(0, void 0, start_a).column); 196 assertEquals(10, script.locationFromLine(0, void 0, start_a).column);
142 assertEquals(start_a, script.locationFromLine(0, 0, start_a).position); 197 assertEquals(start_a, script.locationFromLine(0, 0, start_a).position);
143 assertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines); 198 assertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines);
144 assertEquals(10, script.locationFromLine(0, 0, start_a).column); 199 assertEquals(10, script.locationFromLine(0, 0, start_a).column);
145 200
146 // Test second line offset function a 201 // Test second line offset function a().
147 assertEquals(start_a + 14, script.locationFromLine(1, 0, start_a).position); 202 assertEquals(start_a + 14, script.locationFromLine(1, 0, start_a).position);
148 assertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines); 203 assertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines);
149 assertEquals(0, script.locationFromLine(1, 0, start_a).column); 204 assertEquals(0, script.locationFromLine(1, 0, start_a).column);
150 205
151 // Test second line column 2 offset function a 206 // Test second line column 2 offset function a().
152 assertEquals(start_a + 14 + 2, script.locationFromLine(1, 2, start_a).position); 207 assertEquals(start_a + 14 + 2, script.locationFromLine(1, 2, start_a).position);
153 assertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines); 208 assertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines);
154 assertEquals(2, script.locationFromLine(1, 2, start_a).column); 209 assertEquals(2, script.locationFromLine(1, 2, start_a).column);
155 210
156 // Test offset function b 211 // Test offset function b().
157 assertEquals(start_b, script.locationFromLine(0, 0, start_b).position); 212 assertEquals(start_b, script.locationFromLine(0, 0, start_b).position);
158 assertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines); 213 assertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines);
159 assertEquals(13, script.locationFromLine(0, 0, start_b).column); 214 assertEquals(13, script.locationFromLine(0, 0, start_b).column);
160 215
161 // Test second line offset function b 216 // Test second line offset function b().
162 assertEquals(start_b + 6, script.locationFromLine(1, 0, start_b).position); 217 assertEquals(start_b + 6, script.locationFromLine(1, 0, start_b).position);
163 assertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines); 218 assertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines);
164 assertEquals(0, script.locationFromLine(1, 0, start_b).column); 219 assertEquals(0, script.locationFromLine(1, 0, start_b).column);
165 220
166 // Test second line column 11 offset function b 221 // Test second line column 11 offset function b().
167 assertEquals(start_b + 6 + 11, script.locationFromLine(1, 11, start_b).position) ; 222 assertEquals(start_b + 6 + 11, script.locationFromLine(1, 11, start_b).position) ;
168 assertEquals(2, script.locationFromLine(1, 11, start_b).line - comment_lines); 223 assertEquals(2, script.locationFromLine(1, 11, start_b).line - comment_lines);
169 assertEquals(11, script.locationFromLine(1, 11, start_b).column); 224 assertEquals(11, script.locationFromLine(1, 11, start_b).column);
170 225
171 // Test second line column 12 offset function b. Second line in b is 11 long 226 // Test second line column 12 offset function b. Second line in b is 11 long
172 // using column 12 wraps to next line. 227 // using column 12 wraps to next line.
173 assertEquals(start_b + 6 + 12, script.locationFromLine(1, 12, start_b).position) ; 228 assertEquals(start_b + 6 + 12, script.locationFromLine(1, 12, start_b).position) ;
174 assertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines); 229 assertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines);
175 assertEquals(0, script.locationFromLine(1, 12, start_b).column); 230 assertEquals(0, script.locationFromLine(1, 12, start_b).column);
176 231
177 // Test the Debug.findSourcePosition which wraps SourceManager. 232 // Test the Debug.findSourcePosition which wraps SourceManager.
178 assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position); 233 assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position);
179 assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position); 234 assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position);
180 assertEquals(6 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position); 235 assertEquals(6 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position);
181 assertEquals(8 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position); 236 assertEquals(8 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position);
182 assertEquals(18 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position); 237 assertEquals(18 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position);
183 assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position); 238 assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position);
184 assertEquals(7 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position); 239 assertEquals(7 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position);
185 assertEquals(21 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position); 240 assertEquals(21 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position);
186 assertEquals(38 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position); 241 assertEquals(38 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position);
187 assertEquals(52 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position); 242 assertEquals(52 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position);
188 assertEquals(69 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position); 243 assertEquals(69 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position);
189 assertEquals(76 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position); 244 assertEquals(76 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position);
245 assertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position);
246 assertEquals(7 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position);
247 for (i = 1; i <= num_lines_d; i++) {
248 assertEquals(7 + (i * line_length_d) + start_d, Debug.findFunctionSourceLocati on(d, (i + 1), 0).position);
249 }
250 assertEquals(175 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position) ;
251
252 // Make sure invalid inputs work properly.
253 assertEquals(0, script.locationFromPosition(-1).line);
254 assertEquals(null, script.locationFromPosition(last_position + 1));
255
256 // Test last position.
257 assertEquals(last_position, script.locationFromPosition(last_position).position) ;
258 assertEquals(last_line, script.locationFromPosition(last_position).line);
259 assertEquals(last_column, script.locationFromPosition(last_position).column);
190 260
191 // Test source line and restriction. All the following tests start from line 1 261 // Test source line and restriction. All the following tests start from line 1
192 // column 2 in function b, which is the call to c. 262 // column 2 in function b, which is the call to c.
193 // c(true); 263 // c(true);
194 // ^ 264 // ^
195 265
196 var location; 266 var location;
197 267
198 location = script.locationFromLine(1, 0, start_b); 268 location = script.locationFromLine(1, 0, start_b);
199 assertEquals(' c(true);', location.sourceText()); 269 assertEquals(' c(true);', location.sourceText());
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 location.restrict(5, 4); 337 location.restrict(5, 4);
268 assertEquals(' c(t', location.sourceText()); 338 assertEquals(' c(t', location.sourceText());
269 339
270 location = script.locationFromLine(1, 0, start_b); 340 location = script.locationFromLine(1, 0, start_b);
271 location.restrict(7, 0); 341 location.restrict(7, 0);
272 assertEquals(' c(tru', location.sourceText()); 342 assertEquals(' c(tru', location.sourceText());
273 343
274 location = script.locationFromLine(1, 0, start_b); 344 location = script.locationFromLine(1, 0, start_b);
275 location.restrict(7, 6); 345 location.restrict(7, 6);
276 assertEquals(' c(tru', location.sourceText()); 346 assertEquals(' c(tru', location.sourceText());
OLDNEW
« no previous file with comments | « src/messages.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698