OLD | NEW |
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 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 equal(s11.toString(), "", "JavaScript representation is correct"); | 801 equal(s11.toString(), "", "JavaScript representation is correct"); |
802 s11.release(); | 802 s11.release(); |
803 | 803 |
804 let s12 = string(s8, 4); | 804 let s12 = string(s8, 4); |
805 ok(s12, "String created as typed substring without length parameter"); | 805 ok(s12, "String created as typed substring without length parameter"); |
806 equal(s12.length, 10, "String length set correctly"); | 806 equal(s12.length, 10, "String length set correctly"); |
807 equal(s12.toString(), "longstring", "JavaScript representation is correct"); | 807 equal(s12.toString(), "longstring", "JavaScript representation is correct"); |
808 s12.release(); | 808 s12.release(); |
809 s8.release(); | 809 s8.release(); |
810 }); | 810 }); |
| 811 |
| 812 test("Hash function", function() |
| 813 { |
| 814 let {calculateHash} = require("typedObjects/hash"); |
| 815 let {string} = require("typedObjects/stringType"); |
| 816 |
| 817 equal(calculateHash(string("test")), 1223973025, "Hash of 'test'") |
| 818 equal(calculateHash(string("test1")), 1743059138, "Hash of 'test1'") |
| 819 equal(calculateHash(string("foobar")), 711351050, "Hash of 'foobar'") |
| 820 equal(calculateHash(string("foobars")), 632350465, "Hash of 'foobars'") |
| 821 equal(calculateHash(string("\1\2\3\4\5\u1234\u4321\u8253")), 3898237100, "Ha
sh of '\1\2\3\4\5\u1234\u4321\u8253'") |
| 822 equal(calculateHash(string("\u0442\u0435\u0441\u0442")), 3649301257, "Hash o
f '\u0442\u0435\u0441\u0442'") |
| 823 }); |
| 824 |
| 825 test("Dictionaries of primitive types", function() |
| 826 { |
| 827 let {ObjectType, uint32, string} = require("typedObjects"); |
| 828 |
| 829 let dictDestroyed = false; |
| 830 let uint32Dict = uint32.Dictionary({ |
| 831 customProp: uint32, |
| 832 setCustomProp: function(customProp) |
| 833 { |
| 834 this.customProp = customProp; |
| 835 } |
| 836 }, { |
| 837 constructor: function(capacity) |
| 838 { |
| 839 this.capacity = capacity; |
| 840 }, |
| 841 destructor: function() |
| 842 { |
| 843 dictDestroyed = true; |
| 844 } |
| 845 }); |
| 846 ok(uint32Dict, "Dictionary type created"); |
| 847 |
| 848 let dict1 = uint32Dict(3); |
| 849 ok(dict1, "Array created"); |
| 850 equal(dict1.capacity, 3, "Constructor set capacity to 3"); |
| 851 equal(dict1.groups, 1, "Group count was set accordingly"); |
| 852 |
| 853 // Custom properties/methods |
| 854 equal(typeof dict1.customProp, "number", "Custom dictionary property created
"); |
| 855 equal(typeof dict1.setCustomProp, "function", "Custom dictionary method crea
ted"); |
| 856 |
| 857 dict1.setCustomProp(12); |
| 858 equal(dict1.customProp, 12, "Method could set custom property"); |
| 859 |
| 860 // Setting/reading dictionary elements |
| 861 for (var i = 0; i < 3; i++) |
| 862 dict1.set(string("test" + i), i * 2); |
| 863 for (var i = 0; i < 3; i++) |
| 864 { |
| 865 ok(dict1.has(string("test" + i)), "Dictionary element is reported as exist
ing"); |
| 866 equal(dict1.get(string("test" + i)), i * 2, "Dictionary element value pers
isted"); |
| 867 } |
| 868 |
| 869 equal(dict1.capacity, 6, "Dictionary capacity increased automatically"); |
| 870 equal(dict1.groups, 2, "Groups count was set accordingly"); |
| 871 |
| 872 // Unknown keys |
| 873 ok(!dict1.has(string("foo")), "Unknown key is reported as missing"); |
| 874 throws(() => dict1.get(string("foo")), "Getting unknown key throws"); |
| 875 |
| 876 // Using dictionary as a property type |
| 877 let type1 = ObjectType({ |
| 878 foo: uint32Dict |
| 879 }); |
| 880 let obj1 = type1(); |
| 881 obj1.foo = dict1; |
| 882 ok(dict1.equals(obj1.foo), "Dictionary assigned to object property correctly
"); |
| 883 |
| 884 ok(!dictDestroyed, "Dictionary not destroyed at this point"); |
| 885 dict1.release(); |
| 886 ok(!dictDestroyed, "Dictionary not destroyed after releasing if referenced i
n an object"); |
| 887 |
| 888 obj1.release(); |
| 889 ok(dictDestroyed, "Dictionary destroyed after releasing object holding a ref
erence to it"); |
| 890 }); |
811 })(); | 891 })(); |
OLD | NEW |