Added a magnitiude addition helper function.
This commit is contained in:
parent
629660c8fc
commit
1e0d63e562
@ -18,4 +18,11 @@ public class Utilities {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static int addMagnitude(int value, int add) {
|
||||
boolean negative = false;
|
||||
if (value < 0) negative = true;
|
||||
int res = Math.abs(value) + add;
|
||||
return negative ? - res : res;
|
||||
}
|
||||
}
|
@ -39,4 +39,13 @@ public class UtilitiesTest {
|
||||
assertEquals(first, expectedFirst);
|
||||
assertEquals(second, expectedSecond);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMagnitudeAddPositive() {
|
||||
assertEquals(2, Utilities.addMagnitude(1, 1));
|
||||
}
|
||||
@Test
|
||||
public void testMagnitudeAddNegative() {
|
||||
assertEquals(-2, Utilities.addMagnitude(-1, 1));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user