blob: e51a761400c3be8aaac7822f1fc2b914a5d10eb6 [file] [log] [blame]
Sean Condon39c2a2d2017-03-21 14:23:19 +00001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16package org.onosproject.incubator.net.l2monitoring.soam;
17
18import static org.junit.Assert.assertEquals;
19
20import java.text.DecimalFormat;
21import java.text.NumberFormat;
22
23import org.junit.Test;
24
25public class MilliPctTest {
26
27 @Test
28 public void testIntValue() {
29 assertEquals(100, MilliPct.ofMilliPct(100).intValue());
30 assertEquals(-100, MilliPct.ofMilliPct(-100).intValue());
31 }
32
33 @Test
34 public void testLongValue() {
35 assertEquals(100, MilliPct.ofMilliPct(100).longValue());
36 assertEquals(-100, MilliPct.ofMilliPct(-100).longValue());
37 }
38
39 @Test
40 public void testFloatValue() {
41 assertEquals(100f, MilliPct.ofMilliPct(100).floatValue(), 0.0001f);
42 assertEquals(-100f, MilliPct.ofMilliPct(-100).floatValue(), 0.0001f);
43 }
44
45 @Test
46 public void testDoubleValue() {
47 assertEquals(100f, MilliPct.ofMilliPct(100).doubleValue(), 0.0001f);
48 assertEquals(-100f, MilliPct.ofMilliPct(-100).doubleValue(), 0.0001f);
49 }
50
51 @Test
52 public void testOfPercent() {
53 assertEquals(63563, MilliPct.ofPercent(63.563f).intValue());
54 assertEquals(-63563, MilliPct.ofPercent(-63.563f).intValue());
55 }
56
57 @Test
58 public void testOfRatio() {
59 assertEquals(43211, MilliPct.ofRatio(0.43211f).intValue());
60 assertEquals(-43211, MilliPct.ofRatio(-0.43211f).intValue());
61 }
62
63 @Test
64 public void testPercentValue() {
65 assertEquals(0.1f, MilliPct.ofMilliPct(100).percentValue(), 0.0001f);
66 assertEquals(-0.1f, MilliPct.ofMilliPct(-100).percentValue(), 0.0001f);
67 }
68
69 @Test
70 public void testRatioValue() {
71 assertEquals(0.002f, MilliPct.ofMilliPct(200).ratioValue(), 0.0001f);
72 assertEquals(-0.002f, MilliPct.ofMilliPct(-200).ratioValue(), 0.0001f);
73 }
74
75 @Test
76 public void testToString() {
77 NumberFormat pf = DecimalFormat.getPercentInstance(); //Varies by machine
78 pf.setMaximumFractionDigits(3);
79 assertEquals(pf.format(0.12345f), MilliPct.ofMilliPct(12345).toString());
80 assertEquals(pf.format(-0.12345f), MilliPct.ofMilliPct(-12345).toString());
81 }
82
83}