Advertisement
Guest User

UtilsTest1ES

a guest
Apr 7th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. /*
  2. * Name:
  3. *
  4. * Course: CS-12
  5. *
  6. * Date: 03/28/17
  7. *
  8. * Filename: UtilsTest1ES.java
  9. *
  10. * Purpose: Test program for age algorithm correctness of PRGM10
  11. * (instructor-provided).
  12. * A series of test birthdates are provided, along with
  13. * the expected age, and age results compared against expected.
  14. *
  15. * This program has been generalized, so that it should work for either
  16. * spring or fall semesters, just by toggling one flag.
  17. *
  18. * =====================================================================
  19. * INSTRUCTIONS: Do global replace of utility class name:
  20. * in jGRASP use: Edit: Find/Replace
  21. * or: Ctrl-F
  22. * change UtilsES ==> your Utils class name
  23. * Then run this program and inspect pass/fail results.
  24. * =====================================================================
  25. * Do not make any other changes to this file - it must run as-provided!
  26. * =====================================================================
  27. */
  28.  
  29. import java.util.Random;
  30.  
  31. public class UtilsTest1ES {
  32.  
  33. public static void main(String [] args) {
  34.  
  35. // declarations
  36. CS12Date bd, ref, now;
  37. int month, day, year;
  38.  
  39. // semester flag, T = spring, F = fall
  40. boolean semFlag = true;
  41.  
  42. // test program control settings
  43. boolean doAgeTests = true;
  44.  
  45. // date initializations
  46. bd = new CS12Date();
  47. ref = new CS12Date();
  48. final int CURR_YEAR = bd.getYear();
  49. final int CURR_MONTH = bd.getMonth();
  50.  
  51. // perform age tests
  52. if (doAgeTests) {
  53.  
  54. // program output header
  55. System.out.println("=============================");
  56. System.out.println(" Age test results ");
  57. System.out.println("(test driver client provided)");
  58. System.out.println("=============================");
  59.  
  60. // test for birthdays BEFORE current date
  61. System.out.println("Birthdays BEFORE current date:");
  62.  
  63. // BD a day before current date
  64. bd = UtilsES.today();
  65. bd.priorDate(1);
  66. bd.setYear(CURR_YEAR - 30);
  67. test1(bd, 30);
  68.  
  69. // BD a day before current date
  70. bd = UtilsES.today();
  71. bd.priorDate(1);
  72. test1(bd, 0);
  73.  
  74. // prior month, early day in month
  75. bd.setDate(priorMonth(semFlag), 1, CURR_YEAR - 35);
  76. test1(bd, 35);
  77.  
  78. // prior month, early day in month, THIS year
  79. bd.setDate(priorMonth(semFlag), 9, CURR_YEAR);
  80. test1(bd, 0);
  81.  
  82. // prior month, late day in month
  83. bd.setDate(priorMonth(semFlag), 28, CURR_YEAR - 27);
  84. test1(bd, 27);
  85.  
  86. // prior month, late day in month, THIS year
  87. bd.setDate(priorMonth(semFlag), 27, CURR_YEAR);
  88. test1(bd, 0);
  89.  
  90. // this month, earlier day in month
  91. bd.setDate(CURR_MONTH, 1, CURR_YEAR - 82);
  92. test1(bd, 82);
  93.  
  94. //this month, earlier day in month, THIS year
  95. bd.setDate(CURR_MONTH, 1, CURR_YEAR);
  96. test1(bd, 0);
  97.  
  98. // test for birthdays AFTER current date
  99. System.out.println("\nBirthdays AFTER current date:");
  100.  
  101. // tomorrow is BD
  102. bd = UtilsES.today();
  103. bd.laterDate(1);
  104. bd.setYear(CURR_YEAR - 47);
  105. test1(bd, 47 - 1);
  106.  
  107. // future month, early day in month
  108. bd.setDate(futureMonth(semFlag), 9, CURR_YEAR - 16);
  109. test1(bd, 16 - 1);
  110.  
  111. // future month, early day in month
  112. bd.setDate(futureMonth(semFlag), 4, CURR_YEAR - 32);
  113. test1(bd, 32 - 1);
  114.  
  115. // future month, late day in month
  116. bd.setDate(futureMonth(semFlag), 30, CURR_YEAR - 21);
  117. test1(bd, 21 - 1);
  118.  
  119. // future month, late day in month
  120. bd.setDate(futureMonth(semFlag), 29, CURR_YEAR - 3);
  121. test1(bd, 3 - 1);
  122.  
  123. // test for birthdays ON current date
  124. System.out.println("\nBirthdays ON current date:");
  125.  
  126. // today, born today
  127. bd = UtilsES.today();
  128. test1(bd, 0);
  129.  
  130. // today, kids or teens
  131. bd = UtilsES.today();
  132. bd.setYear(CURR_YEAR - 8);
  133. test1(bd, 8);
  134.  
  135. // today, millenials
  136. bd = UtilsES.today();
  137. bd.setYear(CURR_YEAR - 22);
  138. test1(bd, 22);
  139.  
  140. // today, baby boomers
  141. bd = UtilsES.today();
  142. bd.setYear(CURR_YEAR - 52);
  143. test1(bd, 52);
  144.  
  145. // today, seniors
  146. bd = UtilsES.today();
  147. bd.setYear(CURR_YEAR - 72);
  148. test1(bd, 72);
  149.  
  150. // test for future dates
  151. System.out.println("\nBirthdays in FUTURE:");
  152. System.out.println("==> error message to user **generated by getAge()** must display for EACH future date\n");
  153.  
  154. // tomorrow
  155. bd = UtilsES.today();
  156. bd.nextDay();
  157. test1(bd, -1);
  158.  
  159. // future date in a week or so
  160. bd = UtilsES.today();
  161. bd.laterDate(10);
  162. test1(bd, -1);
  163.  
  164. // future month this year, early date
  165. bd = UtilsES.today();
  166. bd.setDate(futureMonth(semFlag), 1, CURR_YEAR);
  167. test1(bd, -1);
  168.  
  169. // future month this year, late date
  170. bd = UtilsES.today();
  171. bd.setDate(futureMonth(semFlag), 28, CURR_YEAR);
  172. test1(bd, -1);
  173.  
  174. // future year, early month
  175. bd.setDate(2, 15, CURR_YEAR + 1);
  176. test1(bd, -1);
  177.  
  178. // future year, late month
  179. bd.setDate(11, 9, CURR_YEAR + 2);
  180. test1(bd, -1);
  181.  
  182. // test age on milestone dates
  183. System.out.println("\nPrior birthdays (dead or alive) on future milestone dates:");
  184.  
  185. // George Washington 300th
  186. bd.setDate(2, 22, 1732);
  187. ref.setDate(2, 22, 2032);
  188. test2(bd, ref, 300, "George Washington 300th");
  189. ref.setDate(2, 21, 2032);
  190. test2(bd, ref, 299, "George Washington 300th, day before");
  191.  
  192. // instructor 65th
  193. bd.setDate(7, 18, 1963);
  194. ref.setDate(7, 18, 2028);
  195. test2(bd, ref, 65, "instructor 65th");
  196. ref.setDate(7, 17, 2028);
  197. test2(bd, ref, 64, "instructor 65th, day before");
  198.  
  199. } // end age tests
  200.  
  201. } // end main
  202.  
  203. //-----------------------------------------------------------------------------
  204.  
  205. // generate a future month, depending upon whether spring or fall semester
  206. private static int futureMonth(boolean isSpring) {
  207.  
  208. Random rand = new Random();
  209. int month;
  210.  
  211. // spring or fall semester?
  212. if (isSpring) {
  213. month = rand.nextInt(7) + 6; // June-Dec
  214. }
  215. else {
  216. month = 12; // Dec only
  217. }
  218.  
  219. return month;
  220. }
  221.  
  222. // generate a prior month, depending upon whether spring or fall semester
  223. private static int priorMonth(boolean isSpring) {
  224.  
  225. Random rand = new Random();
  226. int month;
  227.  
  228. // spring or fall semester?
  229. if (isSpring) {
  230. month = rand.nextInt(2) + 1; // Jan-Feb
  231. }
  232. else {
  233. month = rand.nextInt(9) + 1; // Jan-Sept
  234. }
  235.  
  236. return month;
  237. }
  238.  
  239. //-----------------------------------------------------------------------------
  240.  
  241. // test the single-input age form with a BD and an expected age
  242. private static void test1(CS12Date dateBd, int expectedAge) {
  243.  
  244. // test the 1-input form (age as of TODAY)
  245. int age = UtilsES.getAge(bd);
  246.  
  247. // display test results
  248. //System.out.println("bd: " + bd +
  249. // "\tas of: " + UtilsES.today() +
  250. // "\tage: " + age +
  251. // "\texpected: " + expectedAge +
  252. // "\tpass: " + (age==expectedAge ? true : false));
  253.  
  254. // rewritten using format specifiers to avoid tabbing
  255. System.out.printf("%3s%11s%9s%11s%8s%4d%11s%4d%8s%-5b\n",
  256. "bd: ", bd,
  257. "as of:", UtilsES.today(),
  258. "age:", age,
  259. "expected:", expectedAge,
  260. "pass: ", (age==expectedAge ? true : false));
  261.  
  262. } // end test1
  263.  
  264. //-----------------------------------------------------------------------------
  265.  
  266. // test the two-input age form with a BD, milestone date, and an expected age
  267. private static void test2(CS12Date bd, CS12Date ref, int expectedAge, String text) {
  268.  
  269. // test the 2-input form (age as of some reference date)
  270. int age = UtilsES.getAge(bd, ref);
  271.  
  272. // display test results
  273. //System.out.println("bd: " + bd +
  274. // "\ton: " + ref +
  275. // "\tage: " + age +
  276. // "\texpected: " + expectedAge +
  277. // "\tpass: " + (age==expectedAge ? true : false) +
  278. // "\t" + text);
  279.  
  280. // rewritten using format specifiers to avoid tabbing
  281. System.out.printf("%3s%11s%9s%11s%8s%4d%11s%4d%8s%-8b%s\n",
  282. "bd: ", bd,
  283. "on:", ref,
  284. "age:", age,
  285. "expected:", expectedAge,
  286. "pass: ", (age==expectedAge ? true : false),
  287. text);
  288.  
  289. } // end test2
  290.  
  291. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement