CalculatorTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import java.io.*;
  2. import static org.junit.Assert.*;
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. /**
  7. * The test class CalculatorTest.
  8. *
  9. * @author (your name)
  10. * @version (a version number or a date)
  11. */
  12. public class CalculatorTest
  13. {
  14. Calculator calcTest = new Calculator();
  15. double delta = 0.000000000000000000001;
  16. @Test
  17. public void addTest(){
  18. assertEquals(7.5,calcTest.add(2.25,5.25),delta);
  19. }
  20. @Test
  21. public void addTest_NegNum(){
  22. assertEquals(-15,calcTest.add(-5,-10),delta);
  23. }
  24. @Test
  25. public void subtractTest(){
  26. assertEquals(-4.5,calcTest.subtract(5.5,10),delta);
  27. }
  28. @Test
  29. public void subtractTest_NegNums(){
  30. assertEquals(5,calcTest.subtract(-5,-10),delta);
  31. }
  32. @Test
  33. public void multiplyTest(){
  34. assertEquals(12,calcTest.multiply(3,4),delta);
  35. }
  36. @Test
  37. public void multiply_TestDec(){
  38. assertEquals(1.2,calcTest.multiply(.25,4.8),delta);
  39. }
  40. @Test
  41. public void multiplyTest_NegNums(){
  42. assertEquals(15,calcTest.multiply(-3,-5),delta);
  43. }
  44. @Test
  45. public void divideTest(){
  46. assertEquals(-5, calcTest.divide(15,-3),delta);
  47. }
  48. @Test
  49. public void divideTest_Dec() {
  50. assertEquals(5, calcTest.divide(.5,.1),delta);
  51. }
  52. @Test
  53. public void squareTest(){
  54. assertEquals(100,calcTest.square(10),delta);
  55. }
  56. @Test
  57. public void squareTest_NegNum(){
  58. assertEquals(100,calcTest.square(-10),delta);
  59. }
  60. @Test
  61. public void squareTest_Dec(){
  62. assertEquals(.25,calcTest.square(.5),delta);
  63. }
  64. @Test
  65. public void squareRootTest(){
  66. assertEquals(10,calcTest.squareRoot(100),delta);
  67. }
  68. @Test
  69. public void squareRootTest_DecResult(){
  70. assertEquals(Math.sqrt(27),calcTest.squareRoot(27),delta);
  71. }
  72. @Test
  73. public void exponentTest(){
  74. assertEquals(8,calcTest.exponent(2,3),delta);
  75. }
  76. @Test
  77. public void exponentTest_Dec(){
  78. assertEquals(Math.pow(.5,.8),calcTest.exponent(.5,.8),delta);
  79. }
  80. @Test
  81. public void invertTest(){
  82. assertEquals(.05,calcTest.invert(20),delta);
  83. }
  84. @Test
  85. public void invertTest2(){
  86. assertEquals(-.01,calcTest.invert(-100),delta);
  87. }
  88. @Test
  89. public void invertTest3(){
  90. assertEquals(.25,calcTest.invert(4),delta);
  91. }
  92. @Test
  93. public void changeSignTest(){
  94. assertEquals(-3,calcTest.changeSign(3),delta);
  95. }
  96. @Test
  97. public void changeSignTest2(){
  98. assertEquals(3,calcTest.changeSign(-3),delta);
  99. }
  100. @Test
  101. public void storeMemoryTest(){
  102. calcTest.storeMemory(5);
  103. assertEquals(5,calcTest.getMemory(),delta);
  104. }
  105. @Test
  106. public void clearMemoryTest(){
  107. calcTest.storeMemory(20);
  108. assertEquals(0,calcTest.clearMemory(), delta);
  109. }
  110. @Test
  111. public void getMemoryTest(){
  112. calcTest.storeMemory(7);
  113. assertEquals(7,calcTest.getMemory(),delta);
  114. }
  115. }