Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -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 com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.junit.ImmutableClassChecker; |
| 21 | |
| 22 | import static org.hamcrest.MatcherAssert.assertThat; |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 23 | import static org.hamcrest.Matchers.greaterThan; |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 24 | import static org.hamcrest.Matchers.is; |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 25 | import static org.hamcrest.Matchers.lessThan; |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 26 | |
| 27 | public class FrequencyTest { |
| 28 | |
| 29 | private final Frequency frequency1 = Frequency.ofMHz(1000); |
| 30 | private final Frequency sameFrequency1 = Frequency.ofMHz(1000); |
| 31 | private final Frequency frequency2 = Frequency.ofGHz(1000); |
| 32 | private final Frequency sameFrequency2 = Frequency.ofGHz(1000); |
| 33 | private final Frequency moreSameFrequency2 = Frequency.ofTHz(1); |
| 34 | private final Frequency frequency3 = Frequency.ofTHz(193.1); |
| 35 | private final Frequency sameFrequency3 = Frequency.ofGHz(193100); |
| 36 | |
| 37 | /** |
| 38 | * Tests immutability of Frequency. |
| 39 | */ |
| 40 | @Test |
| 41 | public void testImmutability() { |
| 42 | ImmutableClassChecker.assertThatClassIsImmutable(Frequency.class); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Tests equality of Frequency instances. |
| 47 | */ |
| 48 | @Test |
| 49 | public void testEquality() { |
| 50 | new EqualsTester() |
| 51 | .addEqualityGroup(frequency1, sameFrequency1) |
| 52 | .addEqualityGroup(frequency2, sameFrequency2, moreSameFrequency2) |
| 53 | .addEqualityGroup(frequency3, sameFrequency3) |
| 54 | .testEquals(); |
| 55 | } |
| 56 | |
| 57 | /** |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 58 | * Tests the first object is less than the second object. |
| 59 | */ |
| 60 | @Test |
| 61 | public void testLessThan() { |
| 62 | assertThat(frequency1, is(lessThan(frequency2))); |
| 63 | assertThat(frequency1.isLessThan(frequency2), is(true)); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testGreaterThan() { |
| 68 | assertThat(frequency2, is(greaterThan(frequency1))); |
| 69 | assertThat(frequency2.isGreaterThan(frequency1), is(true)); |
| 70 | } |
| 71 | |
| 72 | /** |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 73 | * Tests add operation of two Frequencies. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testAdd() { |
| 77 | Frequency low = Frequency.ofMHz(100); |
| 78 | Frequency high = Frequency.ofGHz(1); |
| 79 | Frequency expected = Frequency.ofMHz(1100); |
| 80 | |
| 81 | assertThat(low.add(high), is(expected)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests subtract operation of two Frequencies. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testSubtract() { |
| 89 | Frequency high = Frequency.ofGHz(1); |
| 90 | Frequency low = Frequency.ofMHz(100); |
| 91 | Frequency expected = Frequency.ofMHz(900); |
| 92 | |
| 93 | assertThat(high.subtract(low), is(expected)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests multiply operation of Frequency. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testMultiply() { |
| 101 | Frequency frequency = Frequency.ofMHz(1000); |
| 102 | long factor = 5; |
| 103 | Frequency expected = Frequency.ofGHz(5); |
| 104 | |
| 105 | assertThat(frequency.multiply(5), is(expected)); |
| 106 | } |
| 107 | } |