1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.require('tvcm.statistics');
9 tvcm.unittest.testSuite('tvcm.statistics_test', function() {
10 var Statistics = tvcm.Statistics;
12 test('sumBasic', function() {
13 assertEquals(6, Statistics.sum([1, 2, 3]));
15 test('sumWithFunctor', function() {
18 assertEquals(12, Statistics.sum(ary, function(x, i) {
19 assertEquals(this, ctx);
20 assertEquals(ary[i], x);
25 test('minMaxWithFunctor', function() {
29 assertEquals(this, ctx);
30 assertEquals(ary[i], x);
33 assertEquals(3, Statistics.max(ary, func, ctx));
34 assertEquals(1, Statistics.min(ary, func, ctx));
36 var range = Statistics.range(ary, func, ctx);
37 assertFalse(range.isEmpty);
38 assertEquals(1, range.min);
39 assertEquals(3, range.max);
42 test('maxExtrema', function() {
43 assertEquals(-Infinity, Statistics.max([]));
44 assertEquals(Infinity, Statistics.min([]));
47 test('mean', function() {
48 assertEquals(2, Statistics.mean([1, 2, 3]));
51 test('varianceAndStdDev', function() {
57 var v = Statistics.mean(ary,
59 assertEquals(ctx, this);
65 test('percentile', function() {
78 assertEquals(ctx, this);
81 assertEquals(0, Statistics.percentile(ary, 0, func, ctx));
82 assertEquals(4, Statistics.percentile(ary, .5, func, ctx));
83 assertEquals(6, Statistics.percentile(ary, .75, func, ctx));
84 assertEquals(9, Statistics.percentile(ary, 1, func, ctx));