| Index: test/tests/typedObjects.js |
| =================================================================== |
| --- a/test/tests/typedObjects.js |
| +++ b/test/tests/typedObjects.js |
| @@ -803,9 +803,89 @@ |
| let s12 = string(s8, 4); |
| ok(s12, "String created as typed substring without length parameter"); |
| equal(s12.length, 10, "String length set correctly"); |
| equal(s12.toString(), "longstring", "JavaScript representation is correct"); |
| s12.release(); |
| s8.release(); |
| }); |
| + |
| + test("Hash function", function() |
| + { |
| + let {calculateHash} = require("typedObjects/hash"); |
| + let {string} = require("typedObjects/stringType"); |
| + |
| + equal(calculateHash(string("test")), 1223973025, "Hash of 'test'") |
| + equal(calculateHash(string("test1")), 1743059138, "Hash of 'test1'") |
| + equal(calculateHash(string("foobar")), 711351050, "Hash of 'foobar'") |
| + equal(calculateHash(string("foobars")), 632350465, "Hash of 'foobars'") |
| + equal(calculateHash(string("\1\2\3\4\5\u1234\u4321\u8253")), 3898237100, "Hash of '\1\2\3\4\5\u1234\u4321\u8253'") |
| + equal(calculateHash(string("\u0442\u0435\u0441\u0442")), 3649301257, "Hash of '\u0442\u0435\u0441\u0442'") |
| + }); |
| + |
| + test("Dictionaries of primitive types", function() |
| + { |
| + let {ObjectType, uint32, string} = require("typedObjects"); |
| + |
| + let dictDestroyed = false; |
| + let uint32Dict = uint32.Dictionary({ |
| + customProp: uint32, |
| + setCustomProp: function(customProp) |
| + { |
| + this.customProp = customProp; |
| + } |
| + }, { |
| + constructor: function(capacity) |
| + { |
| + this.capacity = capacity; |
| + }, |
| + destructor: function() |
| + { |
| + dictDestroyed = true; |
| + } |
| + }); |
| + ok(uint32Dict, "Dictionary type created"); |
| + |
| + let dict1 = uint32Dict(3); |
| + ok(dict1, "Array created"); |
| + equal(dict1.capacity, 3, "Constructor set capacity to 3"); |
| + equal(dict1.groups, 1, "Group count was set accordingly"); |
| + |
| + // Custom properties/methods |
| + equal(typeof dict1.customProp, "number", "Custom dictionary property created"); |
| + equal(typeof dict1.setCustomProp, "function", "Custom dictionary method created"); |
| + |
| + dict1.setCustomProp(12); |
| + equal(dict1.customProp, 12, "Method could set custom property"); |
| + |
| + // Setting/reading dictionary elements |
| + for (var i = 0; i < 3; i++) |
| + dict1.set(string("test" + i), i * 2); |
| + for (var i = 0; i < 3; i++) |
| + { |
| + ok(dict1.has(string("test" + i)), "Dictionary element is reported as existing"); |
| + equal(dict1.get(string("test" + i)), i * 2, "Dictionary element value persisted"); |
| + } |
| + |
| + equal(dict1.capacity, 6, "Dictionary capacity increased automatically"); |
| + equal(dict1.groups, 2, "Groups count was set accordingly"); |
| + |
| + // Unknown keys |
| + ok(!dict1.has(string("foo")), "Unknown key is reported as missing"); |
| + throws(() => dict1.get(string("foo")), "Getting unknown key throws"); |
| + |
| + // Using dictionary as a property type |
| + let type1 = ObjectType({ |
| + foo: uint32Dict |
| + }); |
| + let obj1 = type1(); |
| + obj1.foo = dict1; |
| + ok(dict1.equals(obj1.foo), "Dictionary assigned to object property correctly"); |
| + |
| + ok(!dictDestroyed, "Dictionary not destroyed at this point"); |
| + dict1.release(); |
| + ok(!dictDestroyed, "Dictionary not destroyed after releasing if referenced in an object"); |
| + |
| + obj1.release(); |
| + ok(dictDestroyed, "Dictionary destroyed after releasing object holding a reference to it"); |
| + }); |
| })(); |