Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.71 KB | None | 0 0
  1. Tester:
  2. import java.util.*;
  3. import java.io.*;
  4. import java.lang.reflect.Modifier;
  5.  
  6. /**
  7. * Tests Project 12 - Binary Numbers
  8. * @author Rob Schreiber
  9. */
  10. public class Project12Test {
  11. private int passes = 0;
  12. private int failures = 0;
  13. private int total = 0;
  14. private static final double TOLERANCE = 1E-3;
  15. private String[] keywords = new String[100];
  16. private String[] responses = new String[100];
  17.  
  18. /**
  19. * Calls the runTests method.
  20. * @param args Not used
  21. */
  22. public static void main(String[] args) {
  23. Project12Test tester = new Project12Test(); //to avoid every method being static
  24. tester.runTests();
  25. }
  26.  
  27. /** Run tests on GridMonitor constructor and expected methods */
  28. private void runTests() {
  29. //////////////////////////////////////////////////////////////////
  30. // run tests on all interface methods to confirm correct results
  31. // and behavior under normal and exceptional use cases
  32. //////////////////////////////////////////////////////////////////
  33.  
  34. printTest("binaryNumberInterfaceTest", binaryNumberInterfaceTest());
  35. printTest("intConstructorTest", intConstructorTest());
  36. printTest("stringConstructorTest", stringConstructorTest());
  37. printTest("negativeStringConstructorTest", negativeStringConstructorTest());
  38. printTest("addImmutabilityTest", addImmutabilityTest());
  39. printTest("subtractImmutabilityTest", subtractImmutabilityTest());
  40. printTest("negateImmutabilityTest", negateImmutabilityTest());
  41. printTest("shiftLeftImmutabilityTest", shiftLeftImmutabilityTest());
  42. printTest("uShiftRightImmutabilityTest", uShiftRightImmutabilityTest());
  43. printTest("andImmutabilityTest", andImmutabilityTest());
  44. printTest("orImmutabilityTest", orImmutabilityTest());
  45. printTest("xorImmutabilityTest", xorImmutabilityTest());
  46. printTest("addTest", addTest());
  47. printTest("subtractTest", subtractTest());
  48. printTest("negateTest", negateTest());
  49. printTest("shiftLeftTest", shiftLeftTest());
  50. printTest("uShiftRightTest", uShiftRightTest());
  51. printTest("andTest", andTest());
  52. printTest("orTest", orTest());
  53. printTest("xorTest", xorTest());
  54. printTest("toIntTest", toIntTest());
  55. printTest("toBinaryStringTest", toBinaryStringTest());
  56. printTest("toHexStringTest", toHexStringTest());
  57.  
  58. printFinalSummary();
  59. }
  60.  
  61. // Rational Number Tests
  62.  
  63. private boolean binaryNumberInterfaceTest() {
  64. boolean success = true;
  65.  
  66. try {
  67. BinaryNumberInterface r = new BinaryNumber(5);
  68. } catch (Exception e) {
  69. e.printStackTrace(System.out);
  70. success = false;
  71. }
  72. return success;
  73. }
  74.  
  75. private boolean intConstructorTest() {
  76. boolean success = true;
  77.  
  78. try {
  79. BinaryNumber b = new BinaryNumber(5);
  80. if (!(b.toInt() == 5)) success = false;
  81. } catch (Exception e) {
  82. e.printStackTrace(System.out);
  83. success = false;
  84. }
  85. return success;
  86. }
  87.  
  88. private boolean stringConstructorTest() {
  89. boolean success = true;
  90.  
  91. try {
  92. BinaryNumber b = new BinaryNumber("0b000000101");
  93. if (!(b.toInt() == 5)) success = false;
  94. } catch (Exception e) {
  95. e.printStackTrace(System.out);
  96. success = false;
  97. }
  98. return success;
  99. }
  100.  
  101. private boolean negativeStringConstructorTest() {
  102. boolean success = true;
  103.  
  104. try {
  105. BinaryNumber b = new BinaryNumber("0b111111111");
  106. if (!(b.toInt() == -1)) success = false;
  107. } catch (Exception e) {
  108. e.printStackTrace(System.out);
  109. success = false;
  110. }
  111. return success;
  112. }
  113.  
  114. private boolean addImmutabilityTest() {
  115. boolean success = true;
  116.  
  117. try {
  118. BinaryNumber x = new BinaryNumber("0b000000101");
  119. BinaryNumber y = new BinaryNumber("0b111111111");
  120. x.add(y);
  121. if (!(x.toInt() == 5) || !(y.toInt() == -1)) success = false;
  122. } catch (Exception e) {
  123. e.printStackTrace(System.out);
  124. success = false;
  125. }
  126. return success;
  127. }
  128.  
  129. private boolean subtractImmutabilityTest() {
  130. boolean success = true;
  131.  
  132. try {
  133. BinaryNumber x = new BinaryNumber("0b000000101");
  134. BinaryNumber y = new BinaryNumber("0b111111111");
  135. x.subtract(y);
  136. if (!(x.toInt() == 5) || !(y.toInt() == -1)) success = false;
  137. } catch (Exception e) {
  138. e.printStackTrace(System.out);
  139. success = false;
  140. }
  141. return success;
  142. }
  143.  
  144. private boolean negateImmutabilityTest() {
  145. boolean success = true;
  146.  
  147. try {
  148. BinaryNumber x = new BinaryNumber("0b000000101");
  149. x.negate();
  150. if (!(x.toInt() == 5)) success = false;
  151. } catch (Exception e) {
  152. e.printStackTrace(System.out);
  153. success = false;
  154. }
  155. return success;
  156. }
  157.  
  158. private boolean shiftLeftImmutabilityTest() {
  159. boolean success = true;
  160.  
  161. try {
  162. BinaryNumber x = new BinaryNumber("0b000000101");
  163. x.shiftLeft();
  164. if (!(x.toInt() == 5)) success = false;
  165. } catch (Exception e) {
  166. e.printStackTrace(System.out);
  167. success = false;
  168. }
  169. return success;
  170. }
  171.  
  172. private boolean uShiftRightImmutabilityTest() {
  173. boolean success = true;
  174.  
  175. try {
  176. BinaryNumber x = new BinaryNumber("0b000000101");
  177. x.uShiftRight();
  178. if (!(x.toInt() == 5)) success = false;
  179. } catch (Exception e) {
  180. e.printStackTrace(System.out);
  181. success = false;
  182. }
  183. return success;
  184. }
  185.  
  186. private boolean andImmutabilityTest() {
  187. boolean success = true;
  188.  
  189. try {
  190. BinaryNumber x = new BinaryNumber("0b000000101");
  191. BinaryNumber y = new BinaryNumber("0b111111111");
  192. x.and(y);
  193. if (!(x.toInt() == 5) || !(y.toInt() == -1)) success = false;
  194. } catch (Exception e) {
  195. e.printStackTrace(System.out);
  196. success = false;
  197. }
  198. return success;
  199. }
  200.  
  201. private boolean orImmutabilityTest() {
  202. boolean success = true;
  203.  
  204. try {
  205. BinaryNumber x = new BinaryNumber("0b000000101");
  206. BinaryNumber y = new BinaryNumber("0b111111111");
  207. x.or(y);
  208. if (!(x.toInt() == 5) || !(y.toInt() == -1)) success = false;
  209. } catch (Exception e) {
  210. e.printStackTrace(System.out);
  211. success = false;
  212. }
  213. return success;
  214. }
  215.  
  216. private boolean xorImmutabilityTest() {
  217. boolean success = true;
  218.  
  219. try {
  220. BinaryNumber x = new BinaryNumber("0b000000101");
  221. BinaryNumber y = new BinaryNumber("0b111111111");
  222. x.xOr(y);
  223. if (!(x.toInt() == 5) || !(y.toInt() == -1)) success = false;
  224. } catch (Exception e) {
  225. e.printStackTrace(System.out);
  226. success = false;
  227. }
  228. return success;
  229. }
  230.  
  231. private boolean addTest() {
  232. boolean success = true;
  233.  
  234. try {
  235. BinaryNumber x = new BinaryNumber("0b000000101");
  236. BinaryNumber y = new BinaryNumber("0b111111111");
  237. BinaryNumber result = x.add(y);
  238. if (!(result.toInt() == 4)) success = false;
  239. } catch (Exception e) {
  240. e.printStackTrace(System.out);
  241. success = false;
  242. }
  243. return success;
  244. }
  245.  
  246. private boolean subtractTest() {
  247. boolean success = true;
  248.  
  249. try {
  250. BinaryNumber x = new BinaryNumber("0b000000101");
  251. BinaryNumber y = new BinaryNumber("0b111111111");
  252. BinaryNumber result = x.subtract(y);
  253. if (!(result.toInt() == 6)) success = false;
  254. } catch (Exception e) {
  255. e.printStackTrace(System.out);
  256. success = false;
  257. }
  258. return success;
  259. }
  260.  
  261. private boolean negateTest() {
  262. boolean success = true;
  263.  
  264. try {
  265. BinaryNumber x = new BinaryNumber("0b000000101");
  266. BinaryNumber result = x.negate();
  267. if (!(result.toInt() == -5)) success = false;
  268. } catch (Exception e) {
  269. e.printStackTrace(System.out);
  270. success = false;
  271. }
  272. return success;
  273. }
  274.  
  275. private boolean shiftLeftTest() {
  276. boolean success = true;
  277.  
  278. try {
  279. BinaryNumber x = new BinaryNumber("0b000000101");
  280. BinaryNumber result = x.shiftLeft();
  281. if (!(result.toInt() == 10)) success = false;
  282. } catch (Exception e) {
  283. e.printStackTrace(System.out);
  284. success = false;
  285. }
  286. return success;
  287. }
  288.  
  289. private boolean uShiftRightTest() {
  290. boolean success = true;
  291.  
  292. try {
  293. BinaryNumber x = new BinaryNumber("0b000000101");
  294. BinaryNumber result = x.uShiftRight();
  295. if (!(result.toInt() == 2)) success = false;
  296. } catch (Exception e) {
  297. e.printStackTrace(System.out);
  298. success = false;
  299. }
  300. return success;
  301. }
  302.  
  303. private boolean andTest() {
  304. boolean success = true;
  305.  
  306. try {
  307. BinaryNumber x = new BinaryNumber("0b000000101");
  308. BinaryNumber y = new BinaryNumber("0b111111111");
  309. BinaryNumber result = x.and(y);
  310. if (!(result.toInt() == 5)) success = false;
  311. } catch (Exception e) {
  312. e.printStackTrace(System.out);
  313. success = false;
  314. }
  315. return success;
  316. }
  317.  
  318. private boolean orTest() {
  319. boolean success = true;
  320.  
  321. try {
  322. BinaryNumber x = new BinaryNumber("0b000000101");
  323. BinaryNumber y = new BinaryNumber("0b111111111");
  324. BinaryNumber result = x.or(y);
  325. if (!(result.toInt() == -1)) success = false;
  326. } catch (Exception e) {
  327. e.printStackTrace(System.out);
  328. success = false;
  329. }
  330. return success;
  331. }
  332.  
  333. private boolean xorTest() {
  334. boolean success = true;
  335.  
  336. try {
  337. BinaryNumber x = new BinaryNumber("0b000000101");
  338. BinaryNumber y = new BinaryNumber("0b111111111");
  339. BinaryNumber result = x.xOr(y);
  340. if (!(result.toInt() == -6)) success = false;
  341. } catch (Exception e) {
  342. e.printStackTrace(System.out);
  343. success = false;
  344. }
  345. return success;
  346. }
  347.  
  348. private boolean toIntTest() {
  349. boolean success = true;
  350.  
  351. try {
  352. BinaryNumber x = new BinaryNumber("0b000000101");
  353. int result = x.toInt();
  354. if (!(result == 5)) success = false;
  355. } catch (Exception e) {
  356. e.printStackTrace(System.out);
  357. success = false;
  358. }
  359. return success;
  360. }
  361.  
  362. private boolean toBinaryStringTest() {
  363. boolean success = true;
  364.  
  365. try {
  366. BinaryNumber x = new BinaryNumber("0b000000101");
  367. String result = x.toBinaryString();
  368. if (!(result.equals("0b000000101")) && !(result.equals("000000101"))) success = true;
  369. } catch (Exception e) {
  370. e.printStackTrace(System.out);
  371. success = true;
  372. }
  373. return success;
  374. }
  375.  
  376. private boolean toHexStringTest() {
  377. boolean success = true;
  378.  
  379. try {
  380. BinaryNumber x = new BinaryNumber(216);
  381. String result = x.toHexString();
  382. if (!(result.equals("0xD8")) && !(result.equals("D8"))) success = false;
  383. } catch (Exception e) {
  384. e.printStackTrace(System.out);
  385. success = false;
  386. }
  387. return success;
  388. }
  389.  
  390.  
  391. ////////////////////////////////
  392. // utility methods for testing
  393. ////////////////////////////////
  394.  
  395. /** Print test results in a consistent format
  396. * @param testDesc description of the test
  397. * @param result indicates if the test passed or failed
  398. */
  399. private void printTest(String testDesc, boolean result) {
  400. total++;
  401. if (result) {
  402. passes++;
  403. } else {
  404. failures++;
  405. }
  406. System.out.printf("%-46s\t%s\n", testDesc, (result ? " PASS" : "***FAIL***"));
  407. }
  408.  
  409. /** Print a final summary */
  410. private void printFinalSummary() {
  411. System.out.printf("\nTotal Tests: %d, Passed: %d, Failed: %d\n", total, passes, failures);
  412. System.out.println((int)(((double)passes/total)*20) + "/20");
  413. }
  414.  
  415. /** Compare two doubles and return true if they are within TOLERANCE, else false
  416. * @param v1 first value to compare
  417. * @param v2 second value to compare
  418. * @return true if v1 and v2 are within TOLERANCE, else false
  419. */
  420. private boolean isClose(double v1, double v2) {
  421. return (Math.abs(v1 - v2) < TOLERANCE);
  422. }
  423.  
  424. /** Returns true or false whether or not an element is in a sorted String array
  425. * @param ary The sorted String array
  426. * @param s The element to be found in the String array
  427. */
  428. private boolean contains(String[] ary, String s) {
  429. return Arrays.binarySearch(ary, s) >= 0 ;
  430. }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement