Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/tests/typedObjects.js

Issue 6213754488356864: Issue 258 - [Typed objects] Implement a garbage collection mechanism (Closed)
Patch Set: Updated inline documentation Created April 28, 2014, 8:22 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/typedObjects/utils.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 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 foo: uint8, 56 foo: uint8,
57 bar: float32, 57 bar: float32,
58 mtd: function() { 58 mtd: function() {
59 return this.foo * 2; 59 return this.foo * 2;
60 } 60 }
61 }, {bufferSize: 8}); 61 }, {bufferSize: 8});
62 ok(type, "Type created"); 62 ok(type, "Type created");
63 63
64 equal(typeof type.typeId, "number"); 64 equal(typeof type.typeId, "number");
65 equal(typeof type.byteLength, "number"); 65 equal(typeof type.byteLength, "number");
66 equal(type.byteLength, 8);
67 66
68 // Create an object and check default properties 67 // Create an object and check default properties
69 let objects = []; 68 let objects = [];
70 objects.push(type()); 69 objects.push(type());
71 ok(objects[0], "Object created"); 70 ok(objects[0], "Object created");
72 71
73 equal(typeof objects[0].typeId, "number"); 72 equal(typeof objects[0].typeId, "number");
74 equal(objects[0].typeId, type.typeId); 73 equal(objects[0].typeId, type.typeId);
75 74
76 equal(typeof objects[0].bufferIndex, "number"); 75 equal(typeof objects[0].bufferIndex, "number");
77 equal(objects[0].bufferIndex, 0); 76 equal(objects[0].bufferIndex, 0);
78 77
79 equal(typeof objects[0].byteOffset, "number"); 78 equal(typeof objects[0].byteOffset, "number");
80 equal(objects[0].byteOffset, 0); 79 equal(objects[0].byteOffset, 0);
81 80
82 // The first 8 objects should go into the same buffer 81 // The first 8 objects should go into the same buffer
83 for (let i = 1; i < 8; i++) 82 for (let i = 1; i < 8; i++)
84 { 83 {
85 objects.push(type()); 84 objects.push(type());
86 equal(objects[i].bufferIndex, 0); 85 equal(objects[i].bufferIndex, 0);
87 equal(objects[i].byteOffset, 8 * i); 86 equal(objects[i].byteOffset, type.byteLength * i);
88 } 87 }
89 88
90 // Properties should persist and methods should be able to access them 89 // Properties should persist and methods should be able to access them
91 for (let i = 0; i < objects.length; i++) 90 for (let i = 0; i < objects.length; i++)
92 { 91 {
93 objects[i].foo = i; 92 objects[i].foo = i;
94 objects[i].bar = 8.5 - objects[i].foo; 93 objects[i].bar = 8.5 - objects[i].foo;
95 } 94 }
96 ok(true, "Setting properties succeeded"); 95 ok(true, "Setting properties succeeded");
97 96
98 for (let i = 0; i < objects.length; i++) 97 for (let i = 0; i < objects.length; i++)
99 { 98 {
100 equal(objects[i].foo, i); 99 equal(objects[i].foo, i);
101 equal(objects[i].bar, 8.5 - objects[i].foo); 100 equal(objects[i].bar, 8.5 - objects[i].foo);
102 equal(objects[i].mtd(), i * 2); 101 equal(objects[i].mtd(), i * 2);
103 } 102 }
104 103
105 // Next objects should go into a new buffer 104 // Next objects should go into a new buffer
106 let obj = type(); 105 let obj = type();
107 equal(obj.bufferIndex, 1); 106 equal(obj.bufferIndex, 1);
108 equal(obj.byteOffset, 0); 107 equal(obj.byteOffset, 0);
109 108
110 obj = type(); 109 obj = type();
111 equal(obj.bufferIndex, 1); 110 equal(obj.bufferIndex, 1);
112 equal(obj.byteOffset, 8); 111 equal(obj.byteOffset, type.byteLength);
113 }); 112 });
114 113
115 test("Object constructors", function() 114 test("Object constructors", function()
116 { 115 {
117 let {ObjectType, uint8, float32} = require("typedObjects"); 116 let {ObjectType, uint8, float32} = require("typedObjects");
118 let type = new ObjectType({ 117 let type = new ObjectType({
119 foo: uint8, 118 foo: uint8,
120 bar: float32 119 bar: float32
121 }, { 120 }, {
122 constructor: function(a, b) 121 constructor: function(a, b)
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 throws(function() 291 throws(function()
293 { 292 {
294 type3.extend({foo: uint8}); 293 type3.extend({foo: uint8});
295 }, "Property masks method"); 294 }, "Property masks method");
296 295
297 throws(function() 296 throws(function()
298 { 297 {
299 type3.extend({x: function() {}}); 298 type3.extend({x: function() {}});
300 }, "Method masks property"); 299 }, "Method masks property");
301 }); 300 });
301
302 test("Garbage collection", function()
303 {
304 let {ObjectType, uint8, float32} = require("typedObjects");
305
306 let destroyed;
307
308 let type1 = new ObjectType({
309 foo: uint8
310 }, {
311 constructor: function(foo)
312 {
313 this.foo = foo;
314 },
315 destructor: function()
316 {
317 destroyed.push(["type1", this.foo]);
318 }
319 });
320
321 // Single release() call
322 destroyed = [];
323 type1(1).release();
324 deepEqual(destroyed, [["type1", 1]], "Destructor called after release()");
325
326 // retain() and multiple release() calls
327 destroyed = [];
328 let obj2 = type1(2);
329 equal(obj2.bufferIndex, 0, "New object replaces the destroyed one");
330 equal(obj2.byteOffset, 0, "New object replaces the destroyed one");
331
332 obj2.retain();
333 obj2.release();
334 deepEqual(destroyed, [], "Destructor not called after release() if retain() was called");
335 obj2.release();
336 deepEqual(destroyed, [["type1", 2]], "Destructor called after second release ()");
337
338 // References holding object
339 let type2 = type1.extend({
340 bar: type1
341 }, {
342 destructor: function(super_)
343 {
344 super_();
345 destroyed.push(["type2", this.foo]);
346 }
347 });
348
349 destroyed = [];
350 let obj3 = type1(3);
351 let obj4 = type2(4);
352 obj4.bar = obj3;
353 obj3.release();
354 deepEqual(destroyed, [], "Destructor not called if references to object exis t");
355 obj4.bar = null;
356 deepEqual(destroyed, [["type1", 3]], "Destructor called after reference is c leared");
357
358 // Recursive destruction
359 destroyed = [];
360 let obj5 = type1(5);
361 obj4.bar = obj5;
362 obj5.release();
363 deepEqual(destroyed, [], "Destructor not called if references to object exis t");
364 obj4.release();
365 deepEqual(destroyed, [["type1", 4], ["type2", 4], ["type1", 5]], "Destroying an object released its references");
366
367 // Misbehaving destructors
368 let type3 = type1.extend({}, {
369 destructor: function(super_)
370 {
371 this.retain();
372 }
373 });
374 throws(function()
375 {
376 type3(0).release();
377 }, "Retaining reference in destructor is prohibited");
378
379 let type4 = type1.extend({}, {
380 destructor: function(super_)
381 {
382 this.release();
383 }
384 });
385 throws(function()
386 {
387 type4(0).release();
388 }, "Releasing reference in destructor is prohibited");
389
390 let type5 = type1.extend({}, {
391 destructor: function(super_)
392 {
393 this.retain();
394 this.release();
395 }
396 });
397 type5(0).release();
398 ok(true, "Temporarily retaining reference in destructor is allowed");
399 });
302 })(); 400 })();
OLDNEW
« no previous file with comments | « lib/typedObjects/utils.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld