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

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

Issue 4827963358969856: Issue 147 - [Typed objects] Implement object types (Closed)
Patch Set: Fixed comments and one more issue Created April 22, 2014, 3:10 p.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 | « test/qunit.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
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 (function()
19 {
20 module("Typed objects");
21
22 test("Math utilities", function()
23 {
24 let {nextPow2, ilog2} = require("typedObjects/utils");
25
26 equal(nextPow2(0), 0, "nextPow2(0)");
27 equal(nextPow2(1), 1, "nextPow2(1)");
28 equal(nextPow2(2), 2, "nextPow2(2)");
29 equal(nextPow2(3), 4, "nextPow2(3)");
30 equal(nextPow2(7), 8, "nextPow2(7)");
31 equal(nextPow2(8), 8, "nextPow2(8)");
32 equal(nextPow2(9), 16, "nextPow2(9)");
33 equal(nextPow2(13), 16, "nextPow2(13)");
34 equal(nextPow2(1000), 1024, "nextPow2(1000)");
35 equal(nextPow2(0x5123), 0x8000, "nextPow2(0x5123)");
36 equal(nextPow2(0x31234567), 0x40000000, "nextPow2(0x31234567)");
37
38 equal(ilog2(-1), 0, "ilog2(-1)");
39 equal(ilog2(1), 0, "ilog2(1)");
40 equal(ilog2(2), 1, "ilog2(2)");
41 equal(ilog2(3), 1, "ilog2(3)");
42 equal(ilog2(7), 2, "ilog2(7)");
43 equal(ilog2(8), 3, "ilog2(8)");
44 equal(ilog2(9), 3, "ilog2(9)");
45 equal(ilog2(13), 3, "ilog2(13)");
46 equal(ilog2(1000), 9, "ilog2(1000)");
47 equal(ilog2(0x5123), 14, "ilog2(0x5123)");
48 equal(ilog2(0x31234567), 29, "ilog2(0x31234567)");
49 });
50
51 test("Object creation and property access", function()
52 {
53 // Create a type and check its properties
54 let {ObjectType, uint8, float32} = require("typedObjects");
55 let type = new ObjectType({
56 foo: uint8,
57 bar: float32,
58 mtd: function() {
59 return this.foo * 2;
60 }
61 }, {bufferSize: 8});
62 ok(type, "Type created");
63
64 equal(typeof type.typeId, "number");
65 equal(typeof type.byteLength, "number");
66 equal(type.byteLength, 8);
67
68 // Create an object and check default properties
69 let objects = [];
70 objects.push(type());
71 ok(objects[0], "Object created");
72
73 equal(typeof objects[0].typeId, "number");
74 equal(objects[0].typeId, type.typeId);
75
76 equal(typeof objects[0].bufferIndex, "number");
77 equal(objects[0].bufferIndex, 0);
78
79 equal(typeof objects[0].byteOffset, "number");
80 equal(objects[0].byteOffset, 0);
81
82 // The first 8 objects should go into the same buffer
83 for (let i = 1; i < 8; i++)
84 {
85 objects.push(type());
86 equal(objects[i].bufferIndex, 0);
87 equal(objects[i].byteOffset, 8 * i);
88 }
89
90 // Properties should persist and methods should be able to access them
91 for (let i = 0; i < objects.length; i++)
92 {
93 objects[i].foo = i;
94 objects[i].bar = 8.5 - objects[i].foo;
95 }
96 ok(true, "Setting properties succeeded");
97
98 for (let i = 0; i < objects.length; i++)
99 {
100 equal(objects[i].foo, i);
101 equal(objects[i].bar, 8.5 - objects[i].foo);
102 equal(objects[i].mtd(), i * 2);
103 }
104
105 // Next objects should go into a new buffer
106 let obj = type();
107 equal(obj.bufferIndex, 1);
108 equal(obj.byteOffset, 0);
109
110 obj = type();
111 equal(obj.bufferIndex, 1);
112 equal(obj.byteOffset, 8);
113 });
114
115 test("Object constructors", function()
116 {
117 let {ObjectType, uint8, float32} = require("typedObjects");
118 let type = new ObjectType({
119 foo: uint8,
120 bar: float32
121 }, {
122 constructor: function(a, b)
123 {
124 this.foo = a;
125 this.bar = b;
126 }
127 });
128 ok(type, "Type created");
129
130 let obj = type(4, 12.5);
131 equal(obj.foo, 4);
132 equal(obj.bar, 12.5);
133 });
134
135 test("Object references", function()
136 {
137 let {ObjectType, uint8} = require("typedObjects");
138 let type1 = new ObjectType({
139 foo: uint8
140 });
141 let type2 = new ObjectType({
142 bar: type1
143 });
144 ok(type1 && type2, "Types created");
145
146 let obj1 = type1();
147 let obj2 = type2();
148 ok(obj1 && obj2, "Objects created");
149
150 obj2.bar = obj1;
151 ok(obj2.bar, "Object reference set");
152 equal(obj2.bar.typeId, obj1.typeId);
153 equal(obj2.bar.bufferIndex, obj1.bufferIndex);
154 equal(obj2.bar.byteOffset, obj1.byteOffset);
155
156 obj2.bar = null;
157 ok(!obj2.bar, "Object reference unset");
158
159 let obj3 = type2();
160 obj3.bar = obj1;
161 ok(obj3.bar, "Object reference set on new object");
162 equal(obj3.bar.typeId, obj1.typeId);
163 equal(obj3.bar.bufferIndex, obj1.bufferIndex);
164 equal(obj3.bar.byteOffset, obj1.byteOffset);
165 ok(!obj2.bar);
166 });
167 })();
OLDNEW
« no previous file with comments | « test/qunit.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld