blob: 05f930aefd3706f1ffcd53bef09f827573315fea [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/value/numeric.html">
<link rel="import" href="/tracing/value/unit.html">
<script>
'use strict';
tr.b.unittest.testSuite(function() {
test('nonUnitThrows', function() {
assert.throws(function() { new tr.v.NumericBase('foo', -273.15); });
});
test('nonNumberScalarThrows', function() {
var unit = tr.v.Unit.byName.sizeInBytes;
assert.throws(function() { new tr.v.ScalarNumeric(unit, 'foo'); });
});
test('numericBasic', function() {
var n = new tr.v.Numeric.createLinear(
tr.v.Unit.byName.timeDurationInMs,
tr.b.Range.fromExplicitRange(0, 1000),
10);
assert.equal(n.getBinForValue(250).range.min, 200);
assert.equal(n.getBinForValue(250).range.max, 300);
n.add(-1, 'a');
n.add(0, 'b');
n.add(0, 'c');
n.add(500, 'c');
n.add(999, 'd');
n.add(1000, 'd');
assert.equal(n.underflowBin.count, 1);
assert.equal(n.getBinForValue(0).count, 2);
assert.deepEqual(n.getBinForValue(0).sourceInfos,
['b', 'c']);
assert.equal(n.getBinForValue(500).count, 1);
assert.equal(n.getBinForValue(999).count, 1);
assert.equal(n.overflowBin.count, 1);
assert.equal(n.numValues, 6);
assert.closeTo(n.average, 416.3, 0.1);
});
test('numericNans', function() {
var n = new tr.v.Numeric.createLinear(
tr.v.Unit.byName.timeDurationInMs,
tr.b.Range.fromExplicitRange(0, 1000),
10);
n.add(undefined, 'b');
n.add(NaN, 'c');
assert.equal(n.numNans, 2);
assert.deepEqual(n.nanSourceInfos, ['b', 'c']);
});
test('addNumericsValid', function() {
var n0 = new tr.v.Numeric.createLinear(
tr.v.Unit.byName.timeDurationInMs,
tr.b.Range.fromExplicitRange(0, 1000),
10);
var n1 = new tr.v.Numeric.createLinear(
tr.v.Unit.byName.timeDurationInMs,
tr.b.Range.fromExplicitRange(0, 1000),
10);
n0.add(-1, 'a0');
n0.add(0, 'b0');
n0.add(0, 'c0');
n0.add(500, 'c0');
n0.add(1000, 'd0');
n0.add(NaN, 'e0');
n1.add(-1, 'a1');
n1.add(0, 'b1');
n1.add(0, 'c1');
n1.add(999, 'd1');
n1.add(1000, 'd1');
n1.add(NaN, 'e1');
n0.addNumeric(n1);
assert.equal(n0.numNans, 2);
assert.deepEqual(n0.nanSourceInfos, ['e0', 'e1']);
assert.equal(n0.underflowBin.count, 2);
assert.deepEqual(n0.underflowBin.sourceInfos, ['a0', 'a1']);
assert.equal(n0.getBinForValue(0).count, 4);
assert.deepEqual(n0.getBinForValue(0).sourceInfos,
['b0', 'c0', 'b1', 'c1']);
assert.equal(n0.getBinForValue(500).count, 1);
assert.deepEqual(n0.getBinForValue(500).sourceInfos, ['c0']);
assert.equal(n0.getBinForValue(999).count, 1);
assert.deepEqual(n0.getBinForValue(999).sourceInfos, ['d1']);
assert.equal(n0.overflowBin.count, 2);
assert.deepEqual(n0.overflowBin.sourceInfos, ['d0', 'd1']);
assert.equal(n0.numValues, 10);
assert.closeTo(n0.average, 349.7, 0.1);
assert.equal(2, n0.maxCount);
assert.equal(2, n1.maxCount);
});
test('addNumericsInvalid', function() {
var n0 = new tr.v.Numeric.createLinear(
tr.v.Unit.byName.timeDurationInMs,
tr.b.Range.fromExplicitRange(0, 1000),
10);
var n1 = new tr.v.Numeric.createLinear(
tr.v.Unit.byName.timeDurationInMs,
tr.b.Range.fromExplicitRange(0, 1001),
10);
var n2 = new tr.v.Numeric.createLinear(
tr.v.Unit.byName.timeDurationInMs,
tr.b.Range.fromExplicitRange(0, 1000),
11);
assert.throws(n0.addNumeric.bind(n0, n1), Error);
assert.throws(n0.addNumeric.bind(n0, n1), Error);
});
test('getInterpolateCountAt', function() {
var n = tr.v.Numeric.fromDict({
unit: 'unitless',
min: 0,
max: 100,
centralBinWidth: 10,
underflowBin: {min: -Number.MAX_VALUE, max: 0, count: 11},
centralBins: [
{min: 0, max: 10, count: 10},
{min: 10, max: 20, count: 9},
{min: 20, max: 30, count: 8},
{min: 30, max: 40, count: 7},
{min: 40, max: 50, count: 6},
{min: 50, max: 60, count: 5},
{min: 60, max: 70, count: 4},
{min: 70, max: 80, count: 3},
{min: 80, max: 90, count: 2},
{min: 90, max: 100, count: 1}
],
overflowBin: {min: 100, max: Number.MAX_VALUE, count: 0}
});
assert.equal(11, n.maxCount);
assert.equal(11, n.getInterpolatedCountAt(-1));
assert.equal(0, n.getInterpolatedCountAt(101));
assert.closeTo(10.8, n.getInterpolatedCountAt(1), 1e-3);
assert.closeTo(9.5, n.getInterpolatedCountAt(10), 1e-3);
assert.closeTo(0.2, n.getInterpolatedCountAt(99), 1e-3);
});
test('scalarBasic', function() {
var unit = tr.v.Unit.byName.sizeInBytes;
var d = {
type: 'scalar',
unit: unit.asJSON(),
value: 42
};
assert.deepEqual(d, tr.v.NumericBase.fromDict(d).asDict());
});
});
</script>