• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import static org.chromium.base.MathUtils.EPSILON;
8 
9 import org.junit.Assert;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 
13 import org.chromium.base.test.BaseRobolectricTestRunner;
14 
15 /** Unit tests for {@link org.chromium.base.MathUtils}. */
16 @RunWith(BaseRobolectricTestRunner.class)
17 public class MathUtilsTest {
18     private static final String ROUND_FAILURE =
19             "Failure to correctly round value to two decimal places.";
20     private static final String CLAMP_FAILURE = "Failure to correctly clamp value to range.";
21     private static final String MODULO_FAILURE =
22             "Failure to correctly return a positive modulo value.";
23     private static final String SMOOTH_STEP_FAILURE = "Failure to smooth step between 0 and 1.";
24 
25     @Test
testRoundTwoDecimalPlaces()26     public void testRoundTwoDecimalPlaces() {
27         Assert.assertEquals(ROUND_FAILURE, 2.12, MathUtils.roundTwoDecimalPlaces(2.123), EPSILON);
28         Assert.assertEquals(ROUND_FAILURE, 2.13, MathUtils.roundTwoDecimalPlaces(2.127), EPSILON);
29         Assert.assertEquals(ROUND_FAILURE, -2.12, MathUtils.roundTwoDecimalPlaces(-2.123), EPSILON);
30         Assert.assertEquals(ROUND_FAILURE, -2.13, MathUtils.roundTwoDecimalPlaces(-2.127), EPSILON);
31     }
32 
33     @Test
testClampInt()34     public void testClampInt() {
35         int min = 1;
36         int max = 9;
37         Assert.assertEquals(CLAMP_FAILURE, 4, MathUtils.clamp(4, min, max));
38         Assert.assertEquals(CLAMP_FAILURE, 4, MathUtils.clamp(4, max, min));
39 
40         Assert.assertEquals(CLAMP_FAILURE, 1, MathUtils.clamp(-1, min, max));
41         Assert.assertEquals(CLAMP_FAILURE, 1, MathUtils.clamp(0, max, min));
42 
43         Assert.assertEquals(CLAMP_FAILURE, 9, MathUtils.clamp(10, min, max));
44         Assert.assertEquals(CLAMP_FAILURE, 9, MathUtils.clamp(30, max, min));
45     }
46 
47     @Test
testClampLong()48     public void testClampLong() {
49         long min = 1L;
50         long max = 9L;
51         Assert.assertEquals(CLAMP_FAILURE, 4, MathUtils.clamp(4, min, max), EPSILON);
52         Assert.assertEquals(CLAMP_FAILURE, 4, MathUtils.clamp(4, max, min), EPSILON);
53 
54         Assert.assertEquals(CLAMP_FAILURE, 1, MathUtils.clamp(-1, min, max), EPSILON);
55         Assert.assertEquals(CLAMP_FAILURE, 1, MathUtils.clamp(0, max, min), EPSILON);
56 
57         Assert.assertEquals(CLAMP_FAILURE, 9, MathUtils.clamp(10, min, max), EPSILON);
58         Assert.assertEquals(CLAMP_FAILURE, 9, MathUtils.clamp(30, max, min), EPSILON);
59     }
60 
61     @Test
testClampFloat()62     public void testClampFloat() {
63         float min = 1.0f;
64         float max = 9.0f;
65         Assert.assertEquals(CLAMP_FAILURE, 4.8f, MathUtils.clamp(4.8f, min, max), EPSILON);
66         Assert.assertEquals(CLAMP_FAILURE, 4.8f, MathUtils.clamp(4.8f, max, min), EPSILON);
67 
68         Assert.assertEquals(CLAMP_FAILURE, 1f, MathUtils.clamp(-1.7f, min, max), EPSILON);
69         Assert.assertEquals(CLAMP_FAILURE, 1f, MathUtils.clamp(0.003f, max, min), EPSILON);
70 
71         Assert.assertEquals(CLAMP_FAILURE, 9f, MathUtils.clamp(10.9f, min, max), EPSILON);
72         Assert.assertEquals(CLAMP_FAILURE, 9f, MathUtils.clamp(30.1f, max, min), EPSILON);
73     }
74 
75     @Test
testPositiveModulo()76     public void testPositiveModulo() {
77         Assert.assertEquals(MODULO_FAILURE, 1, MathUtils.positiveModulo(3, 2));
78         Assert.assertEquals(MODULO_FAILURE, 1, MathUtils.positiveModulo(3, -2));
79         Assert.assertEquals(MODULO_FAILURE, 1, MathUtils.positiveModulo(-3, 2));
80     }
81 
82     @Test
testSmoothStep()83     public void testSmoothStep() {
84         Assert.assertEquals(SMOOTH_STEP_FAILURE, 0f, MathUtils.smoothstep(0f), EPSILON);
85         Assert.assertEquals(SMOOTH_STEP_FAILURE, 1f, MathUtils.smoothstep(1f), EPSILON);
86         Assert.assertEquals(SMOOTH_STEP_FAILURE, 0.648f, MathUtils.smoothstep(0.6f), EPSILON);
87         Assert.assertEquals(SMOOTH_STEP_FAILURE, 0.216f, MathUtils.smoothstep(0.3f), EPSILON);
88     }
89 }
90