Skip to content

Commit dd357a6

Browse files
committed
add tests for unit ConvertUtils.
1 parent a9d65db commit dd357a6

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed
Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,25 @@
11
package com.lcl.lclmeasurementtool.Utils;
22

3+
/**
4+
* Utilities that will be used to convert units.
5+
*/
36
public class ConvertUtils {
47

58
/**
6-
* the conversion rate between megabit and megabyte.
7-
*/
8-
public static final int CONVERSION_RATE = 8;
9-
10-
/**
11-
* Convert data in MBps to Mbps.
12-
*
13-
* @param MBps the data to be converted in MBps.
14-
* @throws IllegalArgumentException if MBps is less than 0.
15-
* @return corresponding data in Mbps.
16-
*/
17-
public static double toMbps(double MBps) {
18-
if (MBps < 0) {
19-
throw new IllegalArgumentException("the input parameter MBps should be greater than 0");
20-
}
21-
return MBps * CONVERSION_RATE;
22-
}
23-
24-
/**
25-
* Convert data in Mbps to MBps.
9+
* Convert data from one unit to the other.
2610
*
27-
* @param Mbps the data to be converted in Mbps.
28-
* @throws IllegalArgumentException if Mbps is less than 0.
29-
* @return corresponding data in MBps.
11+
* @param from the base unit to be converted from.
12+
* @param to the destination unit to be converted to.
13+
* @param data the data whose unit will be converted.
14+
* @throws IllegalArgumentException if input data is less than 0.
15+
* @return a double in the the destination unit.
3016
*/
31-
public static double toMBps(double Mbps) {
32-
if (Mbps < 0) {
33-
throw new IllegalArgumentException("the input parameter Mbps should be greater than 0");
34-
}
35-
return Mbps / CONVERSION_RATE;
36-
}
37-
3817
public static double convert(DataTransferRateUnit from,
3918
DataTransferRateUnit to,
4019
double data) {
20+
if (data < 0) {
21+
throw new IllegalArgumentException("the input parameter Mbps should be greater than 0");
22+
}
4123

4224
double unitConversionRate = 1.0;
4325
if (!from.getUnit().equals(to.getUnit())) {
@@ -53,6 +35,5 @@ public static double convert(DataTransferRateUnit from,
5335
}
5436

5537
return data * unitConversionRate * magnitudeConversionRate;
56-
5738
}
5839
}

app/src/test/java/com/lcl/lclmeasurementtool/ConvertUtilUnitTest.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@
1616
public class ConvertUtilUnitTest {
1717
@Test
1818
public void testUnitConversion() {
19-
assertEquals(
20-
ConvertUtils.convert(DataTransferRateUnit.Kilobit, DataTransferRateUnit.Kilobyte, 1000),
21-
125, 0.1
22-
);
23-
24-
assertEquals(
25-
ConvertUtils.convert(DataTransferRateUnit.Kilobit, DataTransferRateUnit.Megabit, 1000),
26-
1, 0.1
27-
);
28-
29-
assertEquals(
30-
ConvertUtils.convert(DataTransferRateUnit.Kilobit, DataTransferRateUnit.Megabit, 1000),
31-
1, 0.1
32-
);
19+
double data = 100.0;
20+
double[] actual = new double[] {
21+
100, 12.5, 0.1, 0.0125, 0.0001, 0.0000125, // kb to all other
22+
800, 100, 0.8, 0.1, 0.0008, 0.0001, // kB to all other
23+
100000, 12500, 100, 12.5, 0.1, 0.0125, // mb to all other
24+
800000, 100000, 800, 100, 0.8, 0.1, // mB to all other
25+
100000000, 12500000, 100000, 12500, 100, 12.5, // gb to all other
26+
800000000, 100000000, 800000, 100000, 800, 100 // gB to all other
27+
};
3328

3429
List<List<DataTransferRateUnit>> res = findAllCombination();
3530

36-
for (List<DataTransferRateUnit> list : res) {
37-
System.out.println(list);
31+
for (int i = 0; i < res.size(); i++) {
32+
List<DataTransferRateUnit> list = res.get(i);
33+
assertEquals(
34+
ConvertUtils.convert(list.get(0), list.get(1), data), actual[i], 0.001
35+
);
3836
}
37+
}
3938

39+
@Test(expected = IllegalArgumentException.class)
40+
public void testInvalidData() {
41+
ConvertUtils.convert(DataTransferRateUnit.Kilobit, DataTransferRateUnit.Megabit, -100);
4042
}
4143

44+
// TODO: should also include tests for very large/small numbers
45+
4246
private List<List<DataTransferRateUnit>> findAllCombination() {
4347
List<List<DataTransferRateUnit>> res = new ArrayList<>();
4448
find(0, DataTransferRateUnit.values(), new ArrayList<>(), res);

0 commit comments

Comments
 (0)