Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. //IMPORTANT NOTE:
  4. //These tests are designed to help make sure you are on the correct track. However, they are not a substitute
  5. //for actually testing your code on your own.
  6. //In order to run the tests, you will need to make sure that you have written the appropriate method header for EVERY question that is required
  7. //The tests work by executing your code, so if the method is not there, with the correct spelling, and the correct input/output
  8. //the test cases will not compile!
  9.  
  10. public class AssignmentTwoTests
  11. {
  12. public static void main(String[] args)
  13. {
  14. System.out.println("These tests are not always thorough. You need to do testing on your own as well to be certain your code is correct although these will help make sure you are on the right track!");
  15. testSimpsonsLaw();
  16. testLeadingDigit();
  17. testGenerateBenfordSequence();
  18. testCalculatePercentages();
  19. testCalculateDistance();
  20. System.out.println("No test for main method. (Make sure to test this yourself!)");
  21. }
  22.  
  23. private static void testSimpsonsLaw()
  24. {
  25. System.out.println("Testing simpsons law");
  26. int[] input1 = {12, 48, 104, 411, 183, 582, 45, 140};
  27. validateSimpson(input1, true);
  28.  
  29. int[] input2 = {183, 582, 45, 140, 12, 48, 104, 411};
  30. validateSimpson(input2, true);
  31.  
  32. int[] input3 = {12, 48, 1, 411, 183, 582, 45, 140};
  33. validateSimpson(input3, false);
  34.  
  35. System.out.println("Done testing Simpsons method");
  36. }
  37.  
  38. private static void validateSimpson(int[] input, boolean expectedResult)
  39. {
  40. boolean actualResult = SimpsonsParadox.isSimpsonExample(input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7]);
  41. if (actualResult == expectedResult)
  42. {
  43. System.out.println("PASS");
  44. }
  45. else
  46. {
  47. System.out.print("Calling method with input: " + input[0] + " " + input[1] + " " + input[2] + " " + input[3] + " " + input[4] + " " + input[5] + " " + input[6] + " " + input[7] + "....");
  48. System.out.print("Result is " + actualResult + "....Correct answer is..." + expectedResult + "...");
  49. System.out.println("FAIL");
  50. }
  51. }
  52.  
  53. private static void testLeadingDigit()
  54. {
  55. System.out.println("Testing leading digit method");
  56. validateLeadingDigit(300, 3);
  57. validateLeadingDigit(2,2);
  58. validateLeadingDigit(0,0);
  59. validateLeadingDigit(-53,5);
  60. System.out.println("Done testing leading digit method");
  61. }
  62.  
  63. private static void validateLeadingDigit(int input, int expectedResult)
  64. {
  65. int actualResult = BenfordsLaw.calculateLeadingDigit(input);
  66.  
  67. if (actualResult == expectedResult)
  68. {
  69. System.out.println("PASS");
  70. }
  71. else
  72. {
  73. System.out.print("Calling method with input: " + input + "....");
  74. System.out.print("Result is " + actualResult + ".....Correct answer is " + expectedResult + "....");
  75. System.out.println("FAIL");
  76. }
  77. }
  78.  
  79. private static void testGenerateBenfordSequence()
  80. {
  81. System.out.println("Testing generating the Benford sequence");
  82. double[] correct = {100, 110, 121, 133.1};
  83. validateSequence(100, .1, 4, correct);
  84. System.out.println("Done testing generating the Benford sequence");
  85. }
  86.  
  87. private static void testCalculatePercentages()
  88. {
  89. System.out.println("Testing calculate percentages");
  90. double[] correct = {0, .5, .25, 0, 0, 0, 0, 0, 0, .25};
  91. double[] input = {100, 200.1, 9.3, 10};
  92. validatePercentages(input, correct);
  93. System.out.println("Done testing calculate percentages");
  94. }
  95.  
  96. private static void validateSequence(double initialAmount, double growthRate, int numberPeriods, double[] expectedAnswers)
  97. {
  98. final double tolerance = .00001;
  99. double[] actualAnswers = BenfordsLaw.generateBenfordNumbers(initialAmount, growthRate, numberPeriods);
  100. if (actualAnswers.length != expectedAnswers.length)
  101. {
  102. System.out.println("FAIL : Incorrect number of entries generated with input of initialAmount = " + initialAmount + " growthRate = " + growthRate + " and numberPeriods = " + numberPeriods);
  103. return;
  104. }
  105.  
  106. for (int i = 0; i < expectedAnswers.length; i++)
  107. {
  108. if (Math.abs(expectedAnswers[i] - actualAnswers[i]) > tolerance)
  109. {
  110. System.out.println("FAIL : Incorrect number generated in period number " + (i+1) + " counting from 1, with input of initialAmount = " + initialAmount + " growthRate = " + growthRate + " and numberPeriods = " + numberPeriods + "Expected sequence " + Arrays.toString(expectedAnswers) + " ...got " + Arrays.toString(actualAnswers));
  111. return;
  112. }
  113. }
  114.  
  115. System.out.println("PASS");
  116. }
  117.  
  118. private static void validatePercentages(double[] input, double[] expectedAnswers)
  119. {
  120. final double tolerance = .00001;
  121. double[] actualAnswers = BenfordsLaw.calculateLeadingDigitProportions(input);
  122. if (actualAnswers.length != expectedAnswers.length)
  123. {
  124. System.out.println("FAIL : Incorrect size of array");
  125. return;
  126. }
  127.  
  128. for (int i = 0; i < expectedAnswers.length; i++)
  129. {
  130. if (Math.abs(expectedAnswers[i] - actualAnswers[i]) > tolerance)
  131. {
  132. System.out.println("FAIL : Incorrect percentages generated for digit " + i + ". It occurs as a leading digit" + expectedAnswers[i] + " proportion of time but output is " + actualAnswers[i]);
  133. return;
  134. }
  135. }
  136.  
  137. System.out.println("PASS");
  138. }
  139.  
  140. private static void testCalculateDistance()
  141. {
  142. System.out.println("Testing Calculate Distance");
  143. //check that identical arrays have distance of 0
  144. double[] array1 = {1,2,0,3};
  145. double[] array2= {1,2,0,3};
  146. validateCalculateDistance(array1, array2, 0);
  147.  
  148. double[] array3 = {1,2};
  149. double[] array4= {4,-2};
  150. validateCalculateDistance(array3, array4, 5);
  151.  
  152. System.out.println("Done testing calculate distance");
  153. }
  154.  
  155. private static void validateCalculateDistance(double[] array1, double[] array2, double expected)
  156. {
  157. double actual = BenfordsLaw.calculateDistance(array1, array2);
  158. if (Math.abs(actual - expected) < .001) //.001 to deal with rounding issues
  159. {
  160. System.out.println("PASS");
  161. }
  162. else
  163. {
  164. System.out.println("FAIL on input : " + Arrays.toString(array1) + " and " + Arrays.toString(array2) + " got distance of : " + actual + " and expected distance of " + expected);
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement