MathUtilitiesTest.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. public class MathUtilitiesTest
  6. {
  7. private MathUtilities utilities;
  8. private static final double DELTA = 0.009;
  9. @Before
  10. public void setup(){
  11. utilities = new MathUtilities();
  12. }
  13. @Test
  14. public void testAddInteger(){
  15. //Given
  16. int expected = 15;
  17. //When
  18. int actual = utilities.add(7, 8);
  19. //Then
  20. assertEquals(expected, actual);
  21. }
  22. @Test
  23. public void testAddDouble(){
  24. //Given
  25. double expected = 11.22;
  26. //When
  27. double actual = utilities.add(10, 1.22);
  28. //Then
  29. assertEquals(expected, actual, DELTA);
  30. }
  31. @Test
  32. public void testHalfForEven(){
  33. //Given
  34. double expected = 2.0;
  35. //When
  36. double actual = utilities.half(4);
  37. //Then
  38. assertEquals(expected, actual, DELTA);
  39. }
  40. @Test
  41. public void testHalfForOdd(){
  42. //Given
  43. double expected = 12.5;
  44. //When
  45. double actual = utilities.half(25);
  46. //Then
  47. assertEquals(expected, actual, DELTA);
  48. }
  49. @Test
  50. public void testIsOddForOddNumber(){
  51. assertTrue(utilities.isOdd(7));
  52. }
  53. @Test
  54. public void testIsOddForEvenNumber(){
  55. assertFalse(utilities.isOdd(12));
  56. }
  57. @Test
  58. public void testSquareForBaseCase() {
  59. //Given
  60. int expected = 1;
  61. //When
  62. int actual = utilities.square(1);
  63. //Then
  64. assertEquals(expected, actual);
  65. }
  66. @Test
  67. public void testSquareForOneDigit() {
  68. //Given
  69. int expected = 9;
  70. //When
  71. int actual = utilities.square(3);
  72. //Then
  73. assertEquals(expected, actual);
  74. }
  75. @Test
  76. public void testSquareForTwoDigit() {
  77. //Given
  78. int expected = 121;
  79. //When
  80. int actual = utilities.square(11);
  81. //Then
  82. assertEquals(expected, actual);
  83. }
  84. }