test_BiMap.js (4948B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const { BiMap } = ChromeUtils.importESModule( 6 "chrome://remote/content/shared/BiMap.sys.mjs" 7 ); 8 9 add_task(function test_BiMap_constructor() { 10 const bimap = new BiMap(); 11 ok(bimap, "BiMap instance created"); 12 }); 13 14 add_task(function test_BiMap_clear() { 15 const bimap = new BiMap(); 16 const obj1 = { foo: "bar" }; 17 const obj2 = { baz: "qux" }; 18 19 const id1 = bimap.getOrInsert(obj1); 20 const id2 = bimap.getOrInsert(obj2); 21 22 ok(bimap.hasId(id1), "First id exists before clear"); 23 ok(bimap.hasId(id2), "Second id exists before clear"); 24 25 bimap.clear(); 26 27 ok(!bimap.hasId(id1), "First id removed after clear"); 28 ok(!bimap.hasId(id2), "Second id removed after clear"); 29 ok(!bimap.hasObject(obj1), "First object removed after clear"); 30 ok(!bimap.hasObject(obj2), "Second object removed after clear"); 31 }); 32 33 add_task(function test_BiMap_getOrInsert() { 34 const bimap = new BiMap(); 35 const obj = { foo: "bar" }; 36 37 const regExpUUID = new RegExp( 38 /^[a-f|0-9]{8}-[a-f|0-9]{4}-[a-f|0-9]{4}-[a-f|0-9]{4}-[a-f|0-9]{12}$/g 39 ); 40 41 const id = bimap.getOrInsert(obj); 42 43 ok(regExpUUID.test(id), "Returned a valid UUID"); 44 equal(bimap.getId(obj), id, "Object is mapped to the id"); 45 equal(bimap.getObject(id), obj, "Id is mapped to the object"); 46 }); 47 48 add_task(function test_BiMap_multipleMappings() { 49 const bimap = new BiMap(); 50 const obj1 = { name: "first" }; 51 const obj2 = { name: "second" }; 52 const obj3 = { name: "third" }; 53 54 const id1 = bimap.getOrInsert(obj1); 55 const id2 = bimap.getOrInsert(obj2); 56 const id3 = bimap.getOrInsert(obj3); 57 58 equal(bimap.getId(obj1), id1, "First mapping correct"); 59 equal(bimap.getId(obj2), id2, "Second mapping correct"); 60 equal(bimap.getId(obj3), id3, "Third mapping correct"); 61 62 equal(bimap.getObject(id1), obj1, "First reverse mapping correct"); 63 equal(bimap.getObject(id2), obj2, "Second reverse mapping correct"); 64 equal(bimap.getObject(id3), obj3, "Third reverse mapping correct"); 65 }); 66 67 add_task(function test_BiMap_getId() { 68 const bimap = new BiMap(); 69 const obj = { foo: "bar" }; 70 71 equal(bimap.getId(obj), undefined, "Returns undefined for unknown object"); 72 73 const id = bimap.getOrInsert(obj); 74 75 equal(bimap.getId(obj), id, "Retrieved correct id for object"); 76 }); 77 78 add_task(function test_BiMap_getObject() { 79 const bimap = new BiMap(); 80 const obj = { foo: "bar" }; 81 82 equal( 83 bimap.getObject("unknown-id"), 84 undefined, 85 "Returns undefined for unknown id" 86 ); 87 88 const id = bimap.getOrInsert(obj); 89 90 equal(bimap.getObject(id), obj, "Retrieved correct object for id"); 91 }); 92 93 add_task(function test_BiMap_hasId() { 94 const bimap = new BiMap(); 95 const obj = { foo: "bar" }; 96 97 const id = bimap.getOrInsert(obj); 98 99 ok(!bimap.hasId("unknown-id"), "Returns false for unknown id"); 100 ok(bimap.hasId(id), "Returns true for existing id"); 101 }); 102 103 add_task(function test_BiMap_hasObject() { 104 const bimap = new BiMap(); 105 const obj = { foo: "bar" }; 106 const otherObj = { baz: "qux" }; 107 108 ok(!bimap.hasObject(otherObj), "Returns false for non-existing object"); 109 110 bimap.getOrInsert(obj); 111 112 ok(bimap.hasObject(obj), "Returns true for existing object"); 113 }); 114 115 add_task(function test_BiMap_deleteById() { 116 const bimap = new BiMap(); 117 const obj = { foo: "bar" }; 118 119 const id = bimap.getOrInsert(obj); 120 121 ok(bimap.hasId(id), "Id exists before deletion"); 122 ok(bimap.hasObject(obj), "Object exists before deletion"); 123 124 // Deleting non-existing id should not affect existing mappings. 125 bimap.deleteById("unknown-id"); 126 127 ok(bimap.hasId(id), "Existing id still present"); 128 ok(bimap.hasObject(obj), "Existing object still present"); 129 130 // Delete existing id. 131 bimap.deleteById(id); 132 133 ok(!bimap.hasId(id), "Id removed after deletion"); 134 ok(!bimap.hasObject(obj), "Object removed after deletion"); 135 equal(bimap.getId(obj), undefined, "Object no longer maps to id"); 136 equal(bimap.getObject(id), undefined, "Id no longer maps to object"); 137 }); 138 139 add_task(function test_BiMap_deleteByObject() { 140 const bimap = new BiMap(); 141 const obj = { foo: "bar" }; 142 const otherObj = { baz: "qux" }; 143 144 const id = bimap.getOrInsert(obj); 145 146 ok(bimap.hasId(id), "Id exists before deletion"); 147 ok(bimap.hasObject(obj), "Object exists before deletion"); 148 149 // Deleting non-existing object should not affect existing mappings. 150 bimap.deleteByObject(otherObj); 151 152 ok(bimap.hasId(id), "Existing id still present"); 153 ok(bimap.hasObject(obj), "Existing object still present"); 154 155 // Delete existing object. 156 bimap.deleteByObject(obj); 157 158 ok(!bimap.hasId(id), "Id removed after deletion"); 159 ok(!bimap.hasObject(obj), "Object removed after deletion"); 160 equal(bimap.getId(obj), undefined, "Object no longer maps to id"); 161 equal(bimap.getObject(id), undefined, "Id no longer maps to object"); 162 });