Ethan Tai | ef08da2 | 2017-06-21 17:21:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ethan Tai | ef08da2 | 2017-06-21 17:21:46 -0700 | [diff] [blame] | 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 | package org.onlab.util; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import static org.hamcrest.Matchers.greaterThan; |
| 21 | import static org.hamcrest.Matchers.is; |
| 22 | import static org.hamcrest.Matchers.lessThan; |
| 23 | import static org.junit.Assert.*; |
| 24 | |
| 25 | /** |
| 26 | * Unit tests for LongBandwidth. |
| 27 | */ |
| 28 | public class LongBandwidthTest { |
| 29 | private final long billion = 1000000000; |
| 30 | private final long one = 1; |
| 31 | |
| 32 | //These are LongBandwidth objects because if a Bandwidth object is passed a |
| 33 | //long parameter it becomes a LongBandwidth object |
| 34 | Bandwidth notLongSmall = Bandwidth.kbps(1.0); |
| 35 | private final Bandwidth notLongBig = Bandwidth.kbps(1000.0); |
| 36 | private final Bandwidth big = Bandwidth.kbps(billion); |
| 37 | private final Bandwidth small = Bandwidth.kbps(one); |
| 38 | |
| 39 | /** |
| 40 | * Tests the getter method of LongBandwidths. |
| 41 | */ |
| 42 | @Test |
| 43 | public void testBps() { |
| 44 | Bandwidth expected = Bandwidth.bps(one); |
| 45 | |
| 46 | assertEquals(one, expected.bps(), 0.0); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Tests add operation of two LongBandwidths and two Bandwidths. |
| 51 | */ |
| 52 | @Test |
| 53 | public void testAdd() { |
| 54 | final long add = billion + one; |
| 55 | Bandwidth expected = Bandwidth.kbps(add); |
| 56 | |
| 57 | assertThat(big.add(small), is(expected)); |
| 58 | |
| 59 | final double notLongAdd = 1001.0; |
| 60 | Bandwidth notLongExpected = Bandwidth.kbps(notLongAdd); |
| 61 | |
| 62 | assertThat(notLongSmall.add(notLongBig), is(notLongExpected)); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Tests subtract operation of two LongBandwidths and two Bandwidths. |
| 67 | */ |
| 68 | @Test |
| 69 | public void testSubtract() { |
| 70 | final long sub = billion - one; |
| 71 | Bandwidth expected = Bandwidth.kbps(sub); |
| 72 | |
| 73 | assertThat(big.subtract(small), is(expected)); |
| 74 | |
| 75 | final double notLongSubtract = 999.0; |
| 76 | Bandwidth notLongExpected = Bandwidth.kbps(notLongSubtract); |
| 77 | |
| 78 | assertThat(notLongBig.subtract(notLongSmall), is(notLongExpected)); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Tests if the first object is less than the second object, and then it |
| 84 | * tests if the compareTo function works correctly if comparing a smaller |
| 85 | * object to a larger one, and vice versa. |
| 86 | * Also tests the same thing but with Bandwidth objects. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testLessThan() { |
| 90 | |
| 91 | assertThat(small, is(lessThan(big))); |
| 92 | assertThat(small.compareTo(big), is(-1)); |
| 93 | |
| 94 | assertThat(big, is(greaterThan(small))); |
| 95 | assertThat(big.compareTo(small), is(1)); |
| 96 | |
| 97 | assertThat(notLongSmall, is(lessThan(notLongBig))); |
| 98 | assertThat(notLongSmall.compareTo(notLongBig), is(-1)); |
| 99 | |
| 100 | assertThat(notLongBig, is(greaterThan(notLongSmall))); |
| 101 | assertThat(notLongBig.compareTo(notLongSmall), is(1)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests the equals function between itself, another equivalent Bandwidth |
| 106 | * object, a nonequivalent Bandwidth object, and an int. |
| 107 | */ |
| 108 | @Test |
| 109 | public void testEquals() { |
| 110 | Bandwidth expected = Bandwidth.kbps(one); |
| 111 | assertFalse(small.equals(big)); |
| 112 | assertTrue(small.equals(expected)); |
| 113 | assertTrue(small.equals(small)); |
| 114 | assertFalse(small.equals(1000)); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tests the hashcode function of a LongBandwidth. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testHashCode() { |
| 122 | Long expected = (one * 1000); |
| 123 | assertEquals(small.hashCode(), expected.hashCode()); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Tests the toString function of a LongBandwidth. |
| 128 | */ |
| 129 | @Test |
| 130 | public void testToString() { |
| 131 | String expected = "1000"; |
| 132 | assertEquals(small.toString(), expected); |
| 133 | } |
| 134 | } |