Merge lp:~ubuntu-sdk-team/ubuntu-ui-toolkit/getYourCalculusRight into lp:ubuntu-ui-toolkit/staging

Proposed by Cris Dywan
Status: Merged
Approved by: Tim Peeters
Approved revision: 1273
Merged at revision: 1273
Proposed branch: lp:~ubuntu-sdk-team/ubuntu-ui-toolkit/getYourCalculusRight
Merge into: lp:ubuntu-ui-toolkit/staging
Diff against target: 140 lines (+106/-5)
2 files modified
modules/Ubuntu/Components/mathUtils.js (+5/-5)
tests/unit/tst_components/tst_math_utils.qml (+101/-0)
To merge this branch: bzr merge lp:~ubuntu-sdk-team/ubuntu-ui-toolkit/getYourCalculusRight
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Tim Peeters Approve
Review via email: mp+236849@code.launchpad.net

Commit message

Add unit tests for MathUtils API

To post a comment you must log in.
Revision history for this message
Tim Peeters (tpeeters) wrote :

Please also test the other functions in MathUtils besides clamp().

1269. By Cris Dywan

Unit tests for lerp, project and clampAndProject

1270. By Cris Dywan

Don't use global variables for no reason

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Tim Peeters (tpeeters) wrote :

see (3) inline comments below

1271. By Cris Dywan

Better docs for MathUtils functions

1272. By Cris Dywan

Typo: s/clamed/clamped

1273. By Cris Dywan

Tweaks in the space time continuum

Revision history for this message
Tim Peeters (tpeeters) wrote :

thanks, all good!

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'modules/Ubuntu/Components/mathUtils.js'
2--- modules/Ubuntu/Components/mathUtils.js 2013-01-31 20:37:32 +0000
3+++ modules/Ubuntu/Components/mathUtils.js 2014-10-02 16:35:24 +0000
4@@ -16,9 +16,7 @@
5
6 .pragma library
7
8-// FIXME(loicm) It would be better to have these functions available in a global
9-// set of common native C++ functions.
10-
11+// Ensure the value x is between min and max
12 function clamp(x, min, max) {
13 if (min <= max) {
14 return Math.max(min, Math.min(x, max));
15@@ -28,8 +26,9 @@
16 }
17 }
18
19-function lerp(x, a, b) {
20- return ((1.0 - x) * a) + (x * b);
21+// Get the linear interpolation
22+function lerp(delta, from, to) {
23+ return ((1.0 - delta) * from) + (delta * to);
24 }
25
26 // Linearly project a value x from [xmin, xmax] into [ymin, ymax]
27@@ -37,6 +36,7 @@
28 return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin)
29 }
30
31+// Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
32 function clampAndProject(x, xmin, xmax, ymin, ymax) {
33 return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax)
34 }
35
36=== added file 'tests/unit/tst_components/tst_math_utils.qml'
37--- tests/unit/tst_components/tst_math_utils.qml 1970-01-01 00:00:00 +0000
38+++ tests/unit/tst_components/tst_math_utils.qml 2014-10-02 16:35:24 +0000
39@@ -0,0 +1,101 @@
40+/*
41+ * Copyright 2014 Canonical Ltd.
42+ *
43+ * This program is free software; you can redistribute it and/or modify
44+ * it under the terms of the GNU Lesser General Public License as published by
45+ * the Free Software Foundation; version 3.
46+ *
47+ * This program is distributed in the hope that it will be useful,
48+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
49+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50+ * GNU Lesser General Public License for more details.
51+ *
52+ * You should have received a copy of the GNU Lesser General Public License
53+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
54+ */
55+
56+import QtQuick 2.0
57+import QtTest 1.0
58+import Ubuntu.Components 1.1
59+
60+TestCase {
61+ name: "MathUtils"
62+
63+ function test_clamp_positive_lower() {
64+ var minValue = 9
65+ var maxValue = 42
66+ var clampValue = -7
67+
68+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
69+ compare(clamped, minValue, "clamped value not within range")
70+ }
71+
72+ function test_clamp_positive_greater() {
73+ var minValue = 9
74+ var maxValue = 42
75+ var clampValue = 111
76+
77+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
78+ compare(clamped, maxValue, "clamped value not within range")
79+ }
80+
81+ function test_clamp_positive_within() {
82+ var minValue = 9
83+ var maxValue = 53
84+ var clampValue = 42
85+
86+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
87+ compare(clamped, clampValue, "clamped value changed even though it shouldn't have")
88+ }
89+
90+ function test_clamp_positive_on_border() {
91+ var minValue = 9
92+ var maxValue = 42
93+ var clampValue = 9
94+
95+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
96+ compare(clamped, clampValue, "clamped value changed even though it shouldn't have")
97+ }
98+
99+ function test_clamp_negative_lower() {
100+ var minValue = -42
101+ var maxValue = -9
102+ var clampValue = -50
103+
104+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
105+ compare(clamped, minValue, "clamped value not within range")
106+ }
107+
108+ function test_clamp_negative_greater() {
109+ var minValue = -42
110+ var maxValue = -9
111+ var clampValue = 50
112+
113+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
114+ compare(clamped, maxValue, "clamped value not within range")
115+ }
116+
117+ function test_clamp_postive_and_negative_greater() {
118+ var minValue = -42
119+ var maxValue = 9
120+ var clampValue = 50
121+
122+ var clamped = MathUtils.clamp(clampValue, minValue, maxValue)
123+ compare(clamped, maxValue, "clamped value not within range")
124+ }
125+
126+ function test_lerp() {
127+ var lerped = MathUtils.lerp(0.25, 90, 0)
128+ compare(lerped, 67.5)
129+ }
130+
131+ function test_project_value() {
132+ var projectedValue = MathUtils.projectValue(5, 1, 100, 2, 200)
133+ compare(projectedValue, 10)
134+ }
135+
136+ function test_clamp_and_project() {
137+ var clampedAndProjectedValue = MathUtils.clampAndProject(5, 1, 10, 2, 200)
138+ compare(clampedAndProjectedValue, 90)
139+ }
140+}

Subscribers

People subscribed via source and target branches