1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var { cal } = ChromeUtils.import("resource:///modules/calendar/calHashedArray.jsm");
var { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");
XPCOMUtils.defineLazyModuleGetters(this, {
CalEvent: "resource:///modules/CalEvent.jsm",
});
function run_test() {
test_array_base();
test_array_sorted();
test_hashAccessor();
}
/**
* Helper function to create an item that has a sensible hash id, with the given
* title identification.
*
* @param ident The title to identify the item.
* @returns The created item.
*/
function hashedCreateItem(ident) {
let item = new CalEvent();
item.calendar = { id: "test" };
item.id = cal.getUUID();
item.title = ident;
return item;
}
/**
* Comparator function to sort the items by their title
*
* @param a Object to compare.
* @param b Object to compare with.
* @returns 0, -1, or 1 (usual comptor meanings)
*/
function titleComptor(a, b) {
if (a.title > b.title) {
return 1;
} else if (a.title < b.title) {
return -1;
}
return 0;
}
/**
* Checks if the hashed array accessor functions work for the status of the
* items array.
*
* @param har The Hashed Array
* @param testItems The array of test items
* @param itemAccessor The accessor func to retrieve the items
* @throws Exception If the arrays are not the same.
*/
function checkConsistancy(har, testItems, itemAccessor) {
itemAccessor =
itemAccessor ||
function (item) {
return item;
};
for (let idx in testItems) {
let testItem = itemAccessor(testItems[idx]);
equal(itemAccessor(har.itemByIndex(idx)).title, testItem.title);
equal(itemAccessor(har.itemById(testItem.hashId)).title, testItem.title);
equal(har.indexOf(testItems[idx]), idx);
}
}
/**
* Man, this function is really hard to keep general enough, I'm almost tempted
* to duplicate the code. It checks if the remove and modify operations work for
* the given hashed array.
*
* @param har The Hashed Array
* @param testItems The js array with the items
* @param postprocessFunc (optional) The function to call after each
* operation, but before checking consistency.
* @param itemAccessor (optional) The function to access the item for an
* array element.
* @param itemCreator (optional) Function to create a new item for the
* array.
*/
function testRemoveModify(har, testItems, postprocessFunc, itemAccessor, itemCreator) {
postprocessFunc =
postprocessFunc ||
function (a, b) {
return [a, b];
};
itemCreator = itemCreator || (title => hashedCreateItem(title));
itemAccessor =
itemAccessor ||
function (item) {
return item;
};
// Now, delete the second item and check again
har.removeById(itemAccessor(testItems[1]).hashId);
testItems.splice(1, 1);
[har, testItems] = postprocessFunc(har, testItems);
checkConsistancy(har, testItems, itemAccessor);
// Try the same by index
har.removeByIndex(2);
testItems.splice(2, 1);
[har, testItems] = postprocessFunc(har, testItems);
checkConsistancy(har, testItems, itemAccessor);
// Try modifying an item
let newInstance = itemCreator("z-changed");
itemAccessor(newInstance).id = itemAccessor(testItems[0]).id;
testItems[0] = newInstance;
har.modifyItem(newInstance);
[har, testItems] = postprocessFunc(har, testItems);
checkConsistancy(har, testItems, itemAccessor);
}
/**
* Tests the basic cal.HashedArray
*/
function test_array_base() {
let har, testItems;
// Test normal additions
har = new cal.HashedArray();
testItems = ["a", "b", "c", "d"].map(hashedCreateItem);
testItems.forEach(har.addItem, har);
checkConsistancy(har, testItems);
testRemoveModify(har, testItems);
// Test adding in batch mode
har = new cal.HashedArray();
testItems = ["e", "f", "g", "h"].map(hashedCreateItem);
har.startBatch();
testItems.forEach(har.addItem, har);
har.endBatch();
checkConsistancy(har, testItems);
testRemoveModify(har, testItems);
}
/**
* Tests the sorted cal.SortedHashedArray
*/
function test_array_sorted() {
let har, testItems, testItemsSorted;
function sortedPostProcess(harParam, tiParam) {
tiParam = tiParam.sort(titleComptor);
return [harParam, tiParam];
}
// Test normal additions
har = new cal.SortedHashedArray(titleComptor);
testItems = ["d", "c", "a", "b"].map(hashedCreateItem);
testItemsSorted = testItems.sort(titleComptor);
testItems.forEach(har.addItem, har);
checkConsistancy(har, testItemsSorted);
testRemoveModify(har, testItemsSorted, sortedPostProcess);
// Test adding in batch mode
har = new cal.SortedHashedArray(titleComptor);
testItems = ["e", "f", "g", "h"].map(hashedCreateItem);
testItemsSorted = testItems.sort(titleComptor);
har.startBatch();
testItems.forEach(har.addItem, har);
har.endBatch();
checkConsistancy(har, testItemsSorted);
testRemoveModify(har, testItemsSorted, sortedPostProcess);
}
/**
* Tests cal.SortedHashedArray with a custom hashAccessor.
*/
function test_hashAccessor() {
let har, testItems, testItemsSorted;
let comptor = (a, b) => titleComptor(a.item, b.item);
har = new cal.SortedHashedArray(comptor);
har.hashAccessor = function (obj) {
return obj.item.hashId;
};
function itemAccessor(obj) {
if (!obj) {
do_throw("WTF?");
}
return obj.item;
}
function itemCreator(title) {
return { item: hashedCreateItem(title) };
}
function sortedPostProcess(harParam, tiParam) {
tiParam = tiParam.sort(comptor);
return [harParam, tiParam];
}
testItems = ["d", "c", "a", "b"].map(itemCreator);
testItemsSorted = testItems.sort(comptor);
testItems.forEach(har.addItem, har);
checkConsistancy(har, testItemsSorted, itemAccessor);
testRemoveModify(har, testItemsSorted, sortedPostProcess, itemAccessor, itemCreator);
}
|