1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.core.math;
18
19import static org.junit.Assert.assertEquals;
20
21import android.support.test.filters.SmallTest;
22import android.support.test.runner.AndroidJUnit4;
23
24import org.junit.Test;
25import org.junit.runner.RunWith;
26
27@RunWith(AndroidJUnit4.class)
28@SmallTest
29public class MathUtilsTest {
30
31    @Test
32    public void testClamp() {
33        // Int
34        assertEquals(0, MathUtils.clamp(-4, 0, 7));
35        assertEquals(3, MathUtils.clamp(3, -2, 7));
36        assertEquals(0, MathUtils.clamp(0, 0, 7));
37        assertEquals(7, MathUtils.clamp(7, 0, 7));
38        assertEquals(7, MathUtils.clamp(8, -2, 7));
39
40        // Double
41        assertEquals(0.0, MathUtils.clamp(-0.4, 0.0, 7.0), 0.0);
42        assertEquals(3.0, MathUtils.clamp(3.0, 0.0, 7.0), 0.0);
43        assertEquals(0.1, MathUtils.clamp(0.1, 0.0, 7.0), 0.0);
44        assertEquals(7.0, MathUtils.clamp(7.0, 0.0, 7.0), 0.0);
45        assertEquals(-0.6, MathUtils.clamp(-0.7, -0.6, 7.0), 0.0);
46
47        // Float
48        assertEquals(0.0f, MathUtils.clamp(-0.4f, 0.0f, 7.0f), 0.0f);
49        assertEquals(3.0f, MathUtils.clamp(3.0f, 0.0f, 7.0f), 0.0f);
50        assertEquals(0.1f, MathUtils.clamp(0.1f, 0.0f, 7.0f), 0.0f);
51        assertEquals(7.0f, MathUtils.clamp(7.0f, 0.0f, 7.0f), 0.0f);
52        assertEquals(-0.6f, MathUtils.clamp(-0.7f, -0.6f, 7.0f), 0.0f);
53    }
54}
55