MathUtilitiesTest.java 2.0KB

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