Left: | ||
Right: |
OLD | NEW |
---|---|
(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 {ceilLog2, log2} = require("typedObjects/utils"); | |
25 | |
26 equal(ceilLog2(0), 0, "ceilLog2(0)"); | |
27 equal(ceilLog2(1), 1, "ceilLog2(1)"); | |
28 equal(ceilLog2(2), 2, "ceilLog2(2)"); | |
29 equal(ceilLog2(3), 4, "ceilLog2(3)"); | |
30 equal(ceilLog2(7), 8, "ceilLog2(7)"); | |
31 equal(ceilLog2(8), 8, "ceilLog2(8)"); | |
32 equal(ceilLog2(9), 16, "ceilLog2(9)"); | |
33 equal(ceilLog2(13), 16, "ceilLog2(16)"); | |
René Jeschke
2014/04/21 17:24:12
I think this should read "ceilLog2(13)" instead of
| |
34 equal(ceilLog2(1000), 1024, "ceilLog2(1000)"); | |
35 equal(ceilLog2(0x5123), 0x8000, "ceilLog2(0x5123)"); | |
36 equal(ceilLog2(0x31234567), 0x40000000, "ceilLog2(0x31234567)"); | |
37 | |
38 equal(log2(1), 0, "log2(1)"); | |
39 equal(log2(2), 1, "log2(2)"); | |
40 equal(log2(3), 1, "log2(3)"); | |
41 equal(log2(7), 2, "log2(7)"); | |
42 equal(log2(8), 3, "log2(8)"); | |
43 equal(log2(9), 3, "log2(9)"); | |
44 equal(log2(13), 3, "log2(16)"); | |
René Jeschke
2014/04/21 17:24:12
I think this should read "log2(13)" instead of "lo
| |
45 equal(log2(1000), 9, "log2(1000)"); | |
46 equal(log2(0x5123), 14, "log2(0x5123)"); | |
47 equal(log2(0x31234567), 29, "log2(0x31234567)"); | |
48 }); | |
49 | |
50 test("Object creation and property access", function() | |
51 { | |
52 // Create a type and check its properties | |
53 let {ObjectType, uint8, float32} = require("typedObjects"); | |
54 let type = new ObjectType({ | |
55 foo: uint8, | |
56 bar: float32, | |
57 mtd: function() { | |
58 return this.foo * 2; | |
59 } | |
60 }, {bufferSize: 8}); | |
61 ok(type, "Type created"); | |
62 | |
63 equal(typeof type.typeId, "number"); | |
64 equal(typeof type.byteLength, "number"); | |
65 equal(type.byteLength, 8); | |
66 | |
67 // Create an object and check default properties | |
68 let objects = []; | |
69 objects.push(type()); | |
70 ok(objects[0], "Object created"); | |
71 | |
72 equal(typeof objects[0].typeId, "number"); | |
73 equal(objects[0].typeId, type.typeId); | |
74 | |
75 equal(typeof objects[0].bufferIndex, "number"); | |
76 equal(objects[0].bufferIndex, 0); | |
77 | |
78 equal(typeof objects[0].byteOffset, "number"); | |
79 equal(objects[0].byteOffset, 0); | |
80 | |
81 // The first 8 objects should go into the same buffer | |
82 for (let i = 1; i < 8; i++) | |
83 { | |
84 objects.push(type()); | |
85 equal(objects[i].bufferIndex, 0); | |
86 equal(objects[i].byteOffset, 8 * i); | |
87 } | |
88 | |
89 // Properties should persist and methods should be able to access them | |
90 for (let i = 0; i < objects.length; i++) | |
91 { | |
92 objects[i].foo = i; | |
93 objects[i].bar = 8.5 - objects[i].foo; | |
94 } | |
95 ok(true, "Setting properties succeeded"); | |
96 | |
97 for (let i = 0; i < objects.length; i++) | |
98 { | |
99 equal(objects[i].foo, i); | |
100 equal(objects[i].bar, 8.5 - objects[i].foo); | |
101 equal(objects[i].mtd(), i * 2); | |
102 } | |
103 | |
104 // Next objects should go into a new buffer | |
105 let obj = type(); | |
106 equal(obj.bufferIndex, 1); | |
107 equal(obj.byteOffset, 0); | |
108 | |
109 obj = type(); | |
110 equal(obj.bufferIndex, 1); | |
111 equal(obj.byteOffset, 8); | |
112 }); | |
113 | |
114 test("Object constructors", function() | |
115 { | |
116 let {ObjectType, uint8, float32} = require("typedObjects"); | |
117 let type = new ObjectType({ | |
118 foo: uint8, | |
119 bar: float32 | |
120 }, { | |
121 constructor: function(a, b) | |
122 { | |
123 this.foo = a; | |
124 this.bar = b; | |
125 } | |
126 }); | |
127 ok(type, "Type created"); | |
128 | |
129 let obj = type(4, 12.5); | |
130 equal(obj.foo, 4); | |
131 equal(obj.bar, 12.5); | |
132 }); | |
133 | |
134 test("Object references", function() | |
135 { | |
136 let {ObjectType, uint8} = require("typedObjects"); | |
137 let type1 = new ObjectType({ | |
138 foo: uint8 | |
139 }); | |
140 let type2 = new ObjectType({ | |
141 bar: type1 | |
142 }); | |
143 ok(type1 && type2, "Types created"); | |
144 | |
145 let obj1 = type1(); | |
146 let obj2 = type2(); | |
147 ok(obj1 && obj2, "Objects created"); | |
148 | |
149 obj2.bar = obj1; | |
150 ok(obj2.bar, "Object reference set"); | |
151 equal(obj2.bar.typeId, obj1.typeId); | |
152 equal(obj2.bar.bufferIndex, obj1.bufferIndex); | |
153 equal(obj2.bar.byteOffset, obj1.byteOffset); | |
154 | |
155 obj2.bar = null; | |
156 ok(!obj2.bar, "Object reference unset"); | |
157 | |
158 let obj3 = type2(); | |
159 obj3.bar = obj1; | |
160 ok(obj3.bar, "Object reference set on new object"); | |
161 equal(obj3.bar.typeId, obj1.typeId); | |
162 equal(obj3.bar.bufferIndex, obj1.bufferIndex); | |
163 equal(obj3.bar.byteOffset, obj1.byteOffset); | |
164 ok(!obj2.bar); | |
165 }); | |
166 })(); | |
OLD | NEW |