OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <!-- | 2 <!-- |
3 Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 4 Use of this source code is governed by a BSD-style license that can be |
5 found in the LICENSE file. | 5 found in the LICENSE file. |
6 --> | 6 --> |
7 <link rel="import" href="/tracing/base/utils.html"> | 7 <link rel="import" href="/tracing/base/utils.html"> |
8 | 8 |
9 <script> | 9 <script> |
10 'use strict'; | 10 'use strict'; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 assert.isFalse(tr.b.isUrl(null)); | 51 assert.isFalse(tr.b.isUrl(null)); |
52 assert.isFalse(tr.b.isUrl(42)); | 52 assert.isFalse(tr.b.isUrl(42)); |
53 assert.isFalse(tr.b.isUrl([])); | 53 assert.isFalse(tr.b.isUrl([])); |
54 assert.isFalse(tr.b.isUrl({})); | 54 assert.isFalse(tr.b.isUrl({})); |
55 assert.isFalse(tr.b.isUrl('')); | 55 assert.isFalse(tr.b.isUrl('')); |
56 assert.isFalse(tr.b.isUrl('data:,')); | 56 assert.isFalse(tr.b.isUrl('data:,')); |
57 assert.isFalse(tr.b.isUrl('ftp://user:password@host:port/path')); | 57 assert.isFalse(tr.b.isUrl('ftp://user:password@host:port/path')); |
58 assert.isTrue(tr.b.isUrl('http://google.com/')); | 58 assert.isTrue(tr.b.isUrl('http://google.com/')); |
59 assert.isTrue(tr.b.isUrl('https://www.google.com/')); | 59 assert.isTrue(tr.b.isUrl('https://www.google.com/')); |
60 }); | 60 }); |
| 61 |
| 62 test('setsEqual', function() { |
| 63 assert.isTrue(tr.b.setsEqual(new Set(), new Set())); |
| 64 assert.isTrue(tr.b.setsEqual(new Set(['a']), new Set(['a']))); |
| 65 assert.isFalse(tr.b.setsEqual(new Set(), undefined)); |
| 66 assert.isFalse(tr.b.setsEqual(new Set(), new Set(['a']))); |
| 67 assert.isFalse(tr.b.setsEqual(new Set(['a']), new Set(['b']))); |
| 68 }); |
| 69 |
| 70 test('compareArrays', function() { |
| 71 function cmp(x, y) { |
| 72 assert.isDefined(x); |
| 73 assert.isDefined(y); |
| 74 return x - y; |
| 75 } |
| 76 |
| 77 assert.isBelow(tr.b.compareArrays([1], [2], cmp), 0); |
| 78 assert.isAbove(tr.b.compareArrays([2], [1], cmp), 0); |
| 79 |
| 80 assert.isBelow(tr.b.compareArrays([1], [1, 2], cmp), 0); |
| 81 assert.isAbove(tr.b.compareArrays([1, 2], [1], cmp), 0); |
| 82 |
| 83 assert.isBelow(tr.b.compareArrays([], [1], cmp), 0); |
| 84 assert.isAbove(tr.b.compareArrays([1], [], cmp), 0); |
| 85 |
| 86 assert.isAbove(tr.b.compareArrays([2], [1], cmp), 0); |
| 87 |
| 88 assert.strictEqual(tr.b.compareArrays([], [], cmp), 0); |
| 89 assert.strictEqual(tr.b.compareArrays([1], [1], cmp), 0); |
| 90 }); |
| 91 |
| 92 test('getOnlyElement_throwsIfEmpty', function() { |
| 93 assert.throws(() => tr.b.getOnlyElement([]), |
| 94 'getOnlyElement was passed an empty iterable.'); |
| 95 }); |
| 96 |
| 97 test('getOnlyElement_oneItem', function() { |
| 98 assert.strictEqual(tr.b.getOnlyElement([1]), 1); |
| 99 }); |
| 100 |
| 101 test('getOnlyElement_twoItems', function() { |
| 102 assert.throws(() => tr.b.getOnlyElement([1, 2]), |
| 103 'getOnlyElement was passed an iterable with multiple elements.'); |
| 104 }); |
| 105 |
| 106 test('getFirstElement_throwsIfEmpty', function() { |
| 107 assert.throws(() => tr.b.getFirstElement([]), |
| 108 'getFirstElement was passed an empty iterable.'); |
| 109 }); |
| 110 |
| 111 test('getFirstElement_oneItem', function() { |
| 112 assert.strictEqual(tr.b.getFirstElement([1]), 1); |
| 113 }); |
| 114 |
| 115 test('getFirstElement_twoItems', function() { |
| 116 assert.strictEqual(tr.b.getFirstElement([1, 2]), 1); |
| 117 }); |
| 118 |
| 119 test('groupIntoMap', function() { |
| 120 // Empty array |
| 121 let srcArray = []; |
| 122 const fn = function(curr) { return (curr % 2); }; |
| 123 const dstDict = {}; |
| 124 |
| 125 assert.deepEqual(tr.b.groupIntoMap(srcArray, fn), dstDict); |
| 126 |
| 127 // Non-empty array |
| 128 srcArray = [0, 1, 2, 3, 4, 5, 6]; |
| 129 const dstMap = new Map([ |
| 130 [0, [0, 2, 4, 6]], |
| 131 [1, [1, 3, 5]] |
| 132 ]); |
| 133 |
| 134 assert.deepEqual(tr.b.groupIntoMap(srcArray, fn), dstMap); |
| 135 }); |
| 136 |
| 137 test('inPlaceFilter_simple', function() { |
| 138 const someThisArg = {}; |
| 139 const list = [1, 2, 3, 4]; |
| 140 tr.b.inPlaceFilter(list, function(item) { |
| 141 assert.strictEqual(this, someThisArg); |
| 142 return item % 2 === 0; |
| 143 }, someThisArg); |
| 144 assert.deepEqual(list, [2, 4]); |
| 145 }); |
| 146 |
| 147 test('invertArrayOfDicts_defaultGetter', function() { |
| 148 const array = [ |
| 149 {a: 6, b: 5}, |
| 150 undefined, |
| 151 {a: 4, b: 3, c: 2}, |
| 152 {b: 1, c: 0} |
| 153 ]; |
| 154 const dict = tr.b.invertArrayOfDicts(array); |
| 155 assert.sameMembers(Object.keys(dict), ['a', 'b', 'c']); |
| 156 assert.deepEqual(Array.from(dict.a), [6, undefined, 4, undefined]); |
| 157 assert.deepEqual(Array.from(dict.b), [5, undefined, 3, 1]); |
| 158 assert.deepEqual(Array.from(dict.c), [undefined, undefined, 2, 0]); |
| 159 }); |
| 160 |
| 161 test('invertArrayOfDicts_customGetter', function() { |
| 162 const fakeThis = { itemToDict: JSON.parse }; |
| 163 const array = [ |
| 164 '{"a": "test", "b": true}', |
| 165 '{}', |
| 166 '{invalid-json}', |
| 167 '{"a": 42, "c": false}' |
| 168 ]; |
| 169 const dict = tr.b.invertArrayOfDicts(array, function(item) { |
| 170 try { |
| 171 return this.itemToDict(item); |
| 172 } catch (e) { |
| 173 return undefined; |
| 174 } |
| 175 }, fakeThis); |
| 176 assert.sameMembers(Object.keys(dict), ['a', 'b', 'c']); |
| 177 assert.deepEqual( |
| 178 Array.from(dict.a), ['test', undefined, undefined, 42]); |
| 179 assert.deepEqual( |
| 180 Array.from(dict.b), [true, undefined, undefined, undefined]); |
| 181 assert.deepEqual( |
| 182 Array.from(dict.c), [undefined, undefined, undefined, false]); |
| 183 }); |
61 }); | 184 }); |
62 </script> | 185 </script> |
OLD | NEW |