OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <!-- | |
3 Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
4 Use of this source code is governed by a BSD-style license that can be | |
5 found in the LICENSE file. | |
6 --> | |
7 <link rel="import" href="/tracing/base/iteration_helpers.html"> | |
8 <script> | |
9 'use strict'; | |
10 | |
11 tr.b.unittest.testSuite(function() { | |
12 const compareArrays = tr.b.compareArrays; | |
13 const getOnlyElement = tr.b.getOnlyElement; | |
14 const getFirstElement = tr.b.getFirstElement; | |
15 | |
16 test('setsEqual', function() { | |
17 assert.isTrue(tr.b.setsEqual(new Set(), new Set())); | |
18 assert.isTrue(tr.b.setsEqual(new Set(['a']), new Set(['a']))); | |
19 assert.isFalse(tr.b.setsEqual(new Set(), undefined)); | |
20 assert.isFalse(tr.b.setsEqual(new Set(), new Set(['a']))); | |
21 assert.isFalse(tr.b.setsEqual(new Set(['a']), new Set(['b']))); | |
22 }); | |
23 | |
24 test('compareArrays', function() { | |
25 function cmp(x, y) { | |
26 assert.isDefined(x); | |
27 assert.isDefined(y); | |
28 return x - y; | |
29 } | |
30 | |
31 assert.isBelow(compareArrays([1], [2], cmp), 0); | |
32 assert.isAbove(compareArrays([2], [1], cmp), 0); | |
33 | |
34 assert.isBelow(compareArrays([1], [1, 2], cmp), 0); | |
35 assert.isAbove(compareArrays([1, 2], [1], cmp), 0); | |
36 | |
37 assert.isBelow(compareArrays([], [1], cmp), 0); | |
38 assert.isAbove(compareArrays([1], [], cmp), 0); | |
39 | |
40 assert.isAbove(compareArrays([2], [1], cmp), 0); | |
41 | |
42 assert.strictEqual(compareArrays([], [], cmp), 0); | |
43 assert.strictEqual(compareArrays([1], [1], cmp), 0); | |
44 }); | |
45 | |
46 test('getOnlyElement_throwsIfEmpty', function() { | |
47 assert.throws(() => getOnlyElement([]), | |
48 'getOnlyElement was passed an empty iterable.'); | |
49 }); | |
50 | |
51 test('getOnlyElement_oneItem', function() { | |
52 assert.strictEqual(getOnlyElement([1]), 1); | |
53 }); | |
54 | |
55 test('getOnlyElement_twoItems', function() { | |
56 assert.throws(() => getOnlyElement([1, 2]), | |
57 'getOnlyElement was passed an iterable with multiple elements.'); | |
58 }); | |
59 | |
60 test('getFirstElement_throwsIfEmpty', function() { | |
61 assert.throws(() => getFirstElement([]), | |
62 'getFirstElement was passed an empty iterable.'); | |
63 }); | |
64 | |
65 test('getFirstElement_oneItem', function() { | |
66 assert.strictEqual(getFirstElement([1]), 1); | |
67 }); | |
68 | |
69 test('getFirstElement_twoItems', function() { | |
70 assert.strictEqual(getFirstElement([1, 2]), 1); | |
71 }); | |
72 | |
73 test('groupIntoMap', function() { | |
74 // Empty array | |
75 let srcArray = []; | |
76 const fn = function(curr) { return (curr % 2); }; | |
77 const dstDict = {}; | |
78 | |
79 assert.deepEqual(tr.b.groupIntoMap(srcArray, fn), dstDict); | |
80 | |
81 // Non-empty array | |
82 srcArray = [0, 1, 2, 3, 4, 5, 6]; | |
83 const dstMap = new Map([ | |
84 [0, [0, 2, 4, 6]], | |
85 [1, [1, 3, 5]] | |
86 ]); | |
87 | |
88 assert.deepEqual(tr.b.groupIntoMap(srcArray, fn), dstMap); | |
89 }); | |
90 | |
91 test('inPlaceFilter_simple', function() { | |
92 const someThisArg = {}; | |
93 const list = [1, 2, 3, 4]; | |
94 tr.b.inPlaceFilter(list, function(item) { | |
95 assert.strictEqual(this, someThisArg); | |
96 return item % 2 === 0; | |
97 }, someThisArg); | |
98 assert.deepEqual(list, [2, 4]); | |
99 }); | |
100 | |
101 test('invertArrayOfDicts_defaultGetter', function() { | |
102 const array = [ | |
103 {a: 6, b: 5}, | |
104 undefined, | |
105 {a: 4, b: 3, c: 2}, | |
106 {b: 1, c: 0} | |
107 ]; | |
108 const dict = tr.b.invertArrayOfDicts(array); | |
109 assert.sameMembers(Object.keys(dict), ['a', 'b', 'c']); | |
110 assert.deepEqual(Array.from(dict.a), [6, undefined, 4, undefined]); | |
111 assert.deepEqual(Array.from(dict.b), [5, undefined, 3, 1]); | |
112 assert.deepEqual(Array.from(dict.c), [undefined, undefined, 2, 0]); | |
113 }); | |
114 | |
115 test('invertArrayOfDicts_customGetter', function() { | |
116 const fakeThis = { itemToDict: JSON.parse }; | |
117 const array = [ | |
118 '{"a": "test", "b": true}', | |
119 '{}', | |
120 '{invalid-json}', | |
121 '{"a": 42, "c": false}' | |
122 ]; | |
123 const dict = tr.b.invertArrayOfDicts(array, function(item) { | |
124 try { | |
125 return this.itemToDict(item); | |
126 } catch (e) { | |
127 return undefined; | |
128 } | |
129 }, fakeThis); | |
130 assert.sameMembers(Object.keys(dict), ['a', 'b', 'c']); | |
131 assert.deepEqual( | |
132 Array.from(dict.a), ['test', undefined, undefined, 42]); | |
133 assert.deepEqual( | |
134 Array.from(dict.b), [true, undefined, undefined, undefined]); | |
135 assert.deepEqual( | |
136 Array.from(dict.c), [undefined, undefined, undefined, false]); | |
137 }); | |
138 }); | |
139 </script> | |
OLD | NEW |