Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: tracing/tracing/base/math/math_test.html

Issue 2771723003: [tracing] Move math utilities from base into their own subdirectory (attempt 2) (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/math/math.html">
8
9 <script>
10 'use strict';
11
12 tr.b.unittest.testSuite(function() {
13 test('erf', function() {
14 assert.closeTo(-1, tr.b.math.erf(-1e10), 1e-6);
15 assert.closeTo(-0.8427, tr.b.math.erf(-1), 1e-6);
16 assert.closeTo(-0.5205, tr.b.math.erf(-0.5), 1e-6);
17 assert.closeTo(0, tr.b.math.erf(0), 1e-6);
18 assert.closeTo(0.5205, tr.b.math.erf(0.5), 1e-6);
19 assert.closeTo(0.8427, tr.b.math.erf(1), 1e-6);
20 assert.closeTo(1, tr.b.math.erf(1e10), 1e-6);
21 });
22
23 test('clamping', function() {
24 assert.equal(tr.b.math.clamp(2, 1, 3), 2);
25 assert.equal(tr.b.math.clamp(1, 1, 3), 1);
26 assert.equal(tr.b.math.clamp(0, 1, 3), 1);
27 assert.equal(tr.b.math.clamp(3, 1, 3), 3);
28 assert.equal(tr.b.math.clamp(4, 1, 3), 3);
29 });
30
31 test('interpolatePiecewiseFunction', function() {
32 var points = [[0, 0], [0.1, 0.5], [1, 1]];
33 assert.equal(0, vec2.interpolatePiecewiseFunction(points, -1));
34 assert.equal(0, vec2.interpolatePiecewiseFunction(points, 0));
35 assert.equal(0.25, vec2.interpolatePiecewiseFunction(points, 0.05));
36 assert.equal(0.5, vec2.interpolatePiecewiseFunction(points, 0.1));
37 assert.equal(0.75, vec2.interpolatePiecewiseFunction(points, 0.55));
38 assert.equal(1, vec2.interpolatePiecewiseFunction(points, 1));
39 assert.equal(1, vec2.interpolatePiecewiseFunction(points, 2));
40 });
41
42 test('powers', function() {
43 assert.strictEqual(0.01, tr.b.math.lesserPower(0.05));
44 assert.strictEqual(0.1, tr.b.math.greaterPower(0.05));
45 assert.strictEqual(0.1, tr.b.math.lesserPower(0.5));
46 assert.strictEqual(1, tr.b.math.greaterPower(0.5));
47 assert.strictEqual(1, tr.b.math.lesserPower(5));
48 assert.strictEqual(10, tr.b.math.greaterPower(5));
49 assert.strictEqual(10, tr.b.math.lesserPower(50));
50 assert.strictEqual(100, tr.b.math.greaterPower(50));
51
52 assert.strictEqual(0, tr.b.math.lesserPower(0));
53 assert.strictEqual(0, tr.b.math.greaterPower(0));
54 assert.isTrue(isNaN(tr.b.math.lesserPower(-1)));
55 assert.isTrue(isNaN(tr.b.math.greaterPower(-1)));
56
57 assert.strictEqual(0.25, tr.b.math.lesserPower(0.3, 2));
58 assert.strictEqual(0.5, tr.b.math.greaterPower(0.3, 2));
59 assert.strictEqual(0.5, tr.b.math.lesserPower(0.8, 2));
60 assert.strictEqual(1, tr.b.math.greaterPower(0.8, 2));
61 assert.strictEqual(1, tr.b.math.lesserPower(1.5, 2));
62 assert.strictEqual(2, tr.b.math.greaterPower(1.5, 2));
63 assert.strictEqual(2, tr.b.math.lesserPower(3, 2));
64 assert.strictEqual(4, tr.b.math.greaterPower(3, 2));
65 assert.strictEqual(4, tr.b.math.lesserPower(5, 2));
66 assert.strictEqual(8, tr.b.math.greaterPower(5, 2));
67
68 assert.strictEqual(0, tr.b.math.lesserPower(0, 2));
69 assert.strictEqual(0, tr.b.math.greaterPower(0, 2));
70 assert.isTrue(isNaN(tr.b.math.lesserPower(-1, 2)));
71 assert.isTrue(isNaN(tr.b.math.greaterPower(-1, 2)));
72 });
73
74 test('lesserWholeNumber', function() {
75 // Use powers of 2 less than 10 to prevent float rounding errors from
76 // breaking Math.floor().
77 for (const i of [1, 2, 4, 8]) {
78 assert.strictEqual(-i, tr.b.math.lesserWholeNumber(-i));
79 assert.strictEqual(-i * 10, tr.b.math.lesserWholeNumber(-i * 10));
80 assert.strictEqual(-i / 10, tr.b.math.lesserWholeNumber(-i / 10));
81 assert.strictEqual(-i * 100, tr.b.math.lesserWholeNumber(-i * 100));
82 assert.strictEqual(-i / 100, tr.b.math.lesserWholeNumber(-i / 100));
83
84 assert.strictEqual(i, tr.b.math.lesserWholeNumber(i));
85 assert.strictEqual(i * 10, tr.b.math.lesserWholeNumber(i * 10));
86 assert.strictEqual(i / 10, tr.b.math.lesserWholeNumber(i / 10));
87 assert.strictEqual(i * 100, tr.b.math.lesserWholeNumber(i * 100));
88 assert.strictEqual(i / 100, tr.b.math.lesserWholeNumber(i / 100));
89
90 const x = i * 1.01;
91 assert.strictEqual(-i, tr.b.math.lesserWholeNumber(-x));
92 assert.strictEqual(-i * 10, tr.b.math.lesserWholeNumber(-x * 10));
93 assert.strictEqual(-i / 10, tr.b.math.lesserWholeNumber(-x / 10));
94 assert.strictEqual(-i * 100, tr.b.math.lesserWholeNumber(-x * 100));
95 assert.strictEqual(-i / 100, tr.b.math.lesserWholeNumber(-x / 100));
96
97 assert.strictEqual(i, tr.b.math.lesserWholeNumber(x));
98 assert.strictEqual(i * 10, tr.b.math.lesserWholeNumber(x * 10));
99 assert.strictEqual(i / 10, tr.b.math.lesserWholeNumber(x / 10));
100 assert.strictEqual(i * 100, tr.b.math.lesserWholeNumber(x * 100));
101 assert.strictEqual(i / 100, tr.b.math.lesserWholeNumber(x / 100));
102 }
103 });
104
105 test('greaterWholeNumber', function() {
106 // Use powers of 2 great than 10 to prevent float rounding errors from
107 // breaking Math.floor().
108 for (const i of [1, 2, 4, 8]) {
109 assert.strictEqual(-i, tr.b.math.greaterWholeNumber(-i));
110 assert.strictEqual(-i * 10, tr.b.math.greaterWholeNumber(-i * 10));
111 assert.strictEqual(-i / 10, tr.b.math.greaterWholeNumber(-i / 10));
112 assert.strictEqual(-i * 100, tr.b.math.greaterWholeNumber(-i * 100));
113 assert.strictEqual(-i / 100, tr.b.math.greaterWholeNumber(-i / 100));
114
115 assert.strictEqual(i, tr.b.math.greaterWholeNumber(i));
116 assert.strictEqual(i * 10, tr.b.math.greaterWholeNumber(i * 10));
117 assert.strictEqual(i / 10, tr.b.math.greaterWholeNumber(i / 10));
118 assert.strictEqual(i * 100, tr.b.math.greaterWholeNumber(i * 100));
119 assert.strictEqual(i / 100, tr.b.math.greaterWholeNumber(i / 100));
120
121 const x = i * 0.99;
122 assert.strictEqual(-i, tr.b.math.greaterWholeNumber(-x));
123 assert.strictEqual(-i * 10, tr.b.math.greaterWholeNumber(-x * 10));
124 assert.strictEqual(-i / 10, tr.b.math.greaterWholeNumber(-x / 10));
125 assert.strictEqual(-i * 100, tr.b.math.greaterWholeNumber(-x * 100));
126 assert.strictEqual(-i / 100, tr.b.math.greaterWholeNumber(-x / 100));
127
128 assert.strictEqual(i, tr.b.math.greaterWholeNumber(x));
129 assert.strictEqual(i * 10, tr.b.math.greaterWholeNumber(x * 10));
130 assert.strictEqual(i / 10, tr.b.math.greaterWholeNumber(x / 10));
131 assert.strictEqual(i * 100, tr.b.math.greaterWholeNumber(x * 100));
132 assert.strictEqual(i / 100, tr.b.math.greaterWholeNumber(x / 100));
133 }
134 });
135
136 test('preferedNumberLargerThanMin', function() {
137 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(0), 0);
138 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(1), 1);
139 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(2), 2);
140 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(3), 5);
141 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(7), 10);
142 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(0.03), 0.05);
143 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(-1), -1);
144 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(237538), 500000);
145 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(46.13246), 50);
146 assert.strictEqual(tr.b.math.preferredNumberLargerThanMin(-823.34561),
147 -1000);
148 });
149 });
150 </script>
OLDNEW
« no previous file with comments | « tracing/tracing/base/math/math.html ('k') | tracing/tracing/base/math/piecewise_linear_function.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698