Guest User

Untitled

a guest
Feb 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.57 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import org.testng.annotations.*;
  8.  
  9. import static org.testng.Assert.*;
  10.  
  11. /**
  12. * @author Rich
  13. */
  14. public class boolArrayListNGTest
  15. {
  16.  
  17. /*
  18. * Set up a few Objects for use in test fixture
  19. */
  20. int nBits = 4;
  21.  
  22. /*
  23. * Initialize input sequences:
  24. */
  25. Boolean[] seq0;
  26. Boolean[] seq1;
  27. Boolean[] seq2;
  28. Boolean[] seq3;
  29.  
  30. public boolArrayListNGTest()
  31. {
  32. }
  33.  
  34. @BeforeClass
  35. public static void setUpClass()
  36. {
  37.  
  38. }
  39.  
  40. @AfterClass
  41. public static void tearDownClass()
  42. {
  43. }
  44.  
  45. @BeforeMethod
  46. public void setUpMethod()
  47. {
  48. /*
  49. * Set up test fixture...
  50. */
  51.  
  52. /*
  53. * Initialize input sequences:
  54. * seq0 = (0010)_2 = (2)_10
  55. * seq1 = (0110)_2 = (6)_10
  56. * seq2 = (1110)_2 = (-2)_10
  57. * seq3 = (1010)_2 = (-6)_10
  58. */
  59. seq0 = new Boolean[]{false, false, true, false};
  60. seq1 = new Boolean[]{false, true, true, false};
  61. seq2 = new Boolean[]{true, true, true, false};
  62. seq3 = new Boolean[]{true, false, true, false};
  63. }
  64.  
  65. @AfterMethod
  66. public void tearDownMethod()
  67. {
  68. }
  69.  
  70. /**
  71. * Test of size method, of class boolArrayList.
  72. */
  73. @Test
  74. public void testSize()
  75. {
  76. System.out.println("size");
  77. boolArrayList instance = new boolArrayList();
  78. //boolArrayList instance = new boolArrayList(10);
  79.  
  80. /*
  81. * Query empty object
  82. */
  83. int expResult = 0;
  84. int result = instance.size();
  85. assertEquals(result, expResult);
  86.  
  87. /*
  88. * Add some elements to instance
  89. */
  90. for(int i = 0; i < nBits; i++)
  91. {
  92. instance.insert(i, seq0[i]);
  93. }
  94.  
  95. /*
  96. * Query size
  97. */
  98. expResult = nBits;
  99. result = instance.size();
  100. assertEquals(result, expResult);
  101. }
  102.  
  103. /**
  104. * Test of insert method, of class boolArrayList.
  105. */
  106. @Test
  107. public void testInsert()
  108. {
  109. System.out.println("insert");
  110. boolArrayList instance0 = new boolArrayList();
  111. boolArrayList instance1 = new boolArrayList();
  112.  
  113. /*
  114. * Add some elements to instance
  115. */
  116. for(int i = 0; i < nBits; i++)
  117. {
  118. instance0.insert(i, seq0[i]);
  119. }
  120.  
  121. /*
  122. * Test encapsulation:
  123. * Next line shoule be illegal...
  124. * comment out as needed.
  125. * N.B. Didn't specify names of object fields so this is not nearly
  126. * general enough to use with everybody's code
  127. */
  128. //boolean temp = instance0.array[0];
  129.  
  130. /*
  131. * Query individual elements
  132. */
  133. boolean expResult;
  134. boolean result;
  135. for(int i = 0; i < nBits; i++)
  136. {
  137. result = instance0.lookup(i);
  138. expResult = seq0[i];
  139. assertEquals(result, expResult);
  140. }
  141.  
  142. /*
  143. * Check corner case - add element beyond last current element
  144. */
  145. int offset = 4;
  146. for(int i = 0; i < nBits; i++)
  147. {
  148. instance1.insert(i + offset, seq0[i]);
  149. }
  150.  
  151. /*
  152. * Query individual elements
  153. */
  154. for(int i = 0; i < nBits; i++)
  155. {
  156. result = instance1.lookup(i);
  157. expResult = seq0[i];
  158. assertEquals(result, expResult);
  159. }
  160. }
  161.  
  162. /**
  163. * Test of exception for insert method, of class boolArrayList.
  164. * N.B. For some reason, the "expected" parameter is not being
  165. * recognized. Receiving error:
  166. * cannot find symbol
  167. * symbol: method expected()
  168. * location: @interface Test
  169. * <p>
  170. * Commenting out use of @Test annotation and implementing try/catch
  171. * idiom.... In any case, may have different types of exceptions selected
  172. * (not specified in handout).
  173. * <p>
  174. * Note also, an exception not specified for the remove method in handout
  175. * (though I should have specified one...). This is just for style points.
  176. */
  177. //@Test(expected = IndexOutOfBoundsException.class)
  178. @Test
  179. public void testInsertException()
  180. {
  181. System.out.println("insert exception");
  182. boolArrayList instance = new boolArrayList();
  183. //boolArrayList instance = new boolArrayList(10);
  184.  
  185. /*
  186. * Try inserting elements before nonexistent
  187. */
  188. int target = -1;
  189. boolean toInsert = true;
  190.  
  191. boolean result;
  192. try
  193. {
  194. instance.insert(target, toInsert);
  195. result = false;
  196. }
  197. catch(IndexOutOfBoundsException e)
  198. {
  199. // Not going to test message or the like...
  200. result = true;
  201. }
  202. assertTrue(result);
  203.  
  204. /*
  205. * Try removing elements a nonexistent element from the list...
  206. */
  207. target = -1;
  208.  
  209. try
  210. {
  211. instance.insert(target, toInsert);
  212. result = false;
  213. }
  214. catch(IndexOutOfBoundsException e)
  215. {
  216. // Not going to test message or the like...
  217. result = true;
  218. }
  219. assertTrue(result);
  220. }
  221.  
  222. /**
  223. * Test of remove method, of class boolArrayList.
  224. */
  225. @Test
  226. public void testRemove()
  227. {
  228. System.out.println("remove");
  229. boolArrayList instance0 = new boolArrayList();
  230. boolArrayList instance1 = new boolArrayList();
  231. boolArrayList instance2 = new boolArrayList();
  232. boolArrayList instance3 = new boolArrayList();
  233.  
  234. /*
  235. * Add some elements to instances
  236. */
  237. for(int i = 0; i < nBits; i++)
  238. {
  239. instance0.insert(i, seq0[i]);
  240. instance1.insert(i, seq1[i]);
  241. instance2.insert(i, seq2[i]);
  242. instance3.insert(i, seq3[i]);
  243. }
  244.  
  245. /*
  246. * Remove leading (first) element
  247. */
  248. int toRemove = 0;
  249. instance0.remove(toRemove);
  250.  
  251. /*
  252. * Make sure bookeeping done properly...
  253. */
  254. int expIntResult;
  255. int intResult;
  256. intResult = instance0.size();
  257. expIntResult = nBits - 1;
  258. assertEquals(intResult, expIntResult);
  259.  
  260. /*
  261. * Query individual elements
  262. */
  263. boolean expResult;
  264. boolean result;
  265. for(int i = 0; i < nBits - 1; i++)
  266. {
  267. result = instance0.lookup(i);
  268. expResult = seq0[i + 1];
  269. assertEquals(result, expResult);
  270. }
  271.  
  272. /*
  273. * Remove trailing (last) element
  274. */
  275. toRemove = nBits - 1;
  276. instance1.remove(toRemove);
  277.  
  278. /*
  279. * Make sure bookeeping done properly...
  280. */
  281. intResult = instance1.size();
  282. expIntResult = nBits - 1;
  283. assertEquals(intResult, expIntResult);
  284.  
  285. /*
  286. * Query individual elements
  287. */
  288. for(int i = 0; i < nBits - 1; i++)
  289. {
  290. result = instance1.lookup(i);
  291. expResult = seq1[i];
  292. assertEquals(result, expResult);
  293. }
  294.  
  295. /*
  296. * Remove an arbitrary element
  297. */
  298. toRemove = 2;
  299. instance2.remove(toRemove);
  300.  
  301. /*
  302. * Make sure bookeeping done properly...
  303. */
  304. intResult = instance2.size();
  305. expIntResult = nBits - 1;
  306. assertEquals(intResult, expIntResult);
  307.  
  308. /*
  309. * Query individual elements
  310. */
  311.  
  312. // Elements preceeding removed item
  313. for(int i = 0; i < toRemove; i++)
  314. {
  315. result = instance2.lookup(i);
  316. expResult = seq2[i];
  317. assertEquals(result, expResult);
  318. }
  319.  
  320. // Elements following removed item
  321. for(int i = toRemove; i < nBits - 1; i++)
  322. {
  323. result = instance2.lookup(i);
  324. expResult = seq2[i + 1];
  325. assertEquals(result, expResult);
  326. }
  327.  
  328. /*
  329. * Remove non-existent element
  330. */
  331. toRemove = 6;
  332. instance3.remove(toRemove);
  333.  
  334.  
  335. /*
  336. * Make sure bookkeeping done properly...
  337. */
  338. intResult = instance3.size();
  339. expIntResult = nBits;
  340. assertEquals(intResult, expIntResult);
  341.  
  342. /*
  343. * Query individual elements
  344. */
  345. for(int i = 0; i < nBits; i++)
  346. {
  347. result = instance3.lookup(i);
  348. expResult = seq3[i];
  349. assertEquals(result, expResult);
  350. }
  351. }
  352.  
  353. /**
  354. * Test of lookup method, of class boolArrayList.
  355. */
  356. @Test
  357. public void testLookup()
  358. {
  359. System.out.println("lookup");
  360. boolArrayList instance = new boolArrayList();
  361. //boolArrayList instance = new boolArrayList(10);
  362.  
  363. /*
  364. * Add some elements to instances
  365. */
  366. for(int i = 0; i < nBits; i++)
  367. {
  368. instance.insert(i, seq0[i]);
  369. }
  370.  
  371. /*
  372. * Query individual elements
  373. */
  374. boolean expResult;
  375. boolean result;
  376. for(int i = 0; i < nBits; i++)
  377. {
  378. result = instance.lookup(i);
  379. expResult = seq0[i];
  380. assertEquals(result, expResult);
  381. }
  382. }
  383.  
  384. /**
  385. * Test of exception for lookup method, of class boolArrayList.
  386. * N.B. For some reason, the "expected" parameter is not being
  387. * recognized. See above comment for testRemoveException.
  388. * <p>
  389. * Commenting out use of @Test annotation and implementing try/catch
  390. * idiom.... In any case, may have different types of exceptions selected
  391. * (not specified in handout).
  392. */
  393. //@Test(expected = IndexOutOfBoundsException.class)
  394. @Test
  395. public void testLookupException()
  396. {
  397. System.out.println("lookup exception");
  398. boolArrayList instance = new boolArrayList();
  399. //boolArrayList instance = new boolArrayList(10);
  400.  
  401. /*
  402. * Try looking up elements from an empty list...
  403. */
  404. int toLookup = 0;
  405.  
  406. boolean result;
  407. try
  408. {
  409. instance.lookup(toLookup);
  410. result = false;
  411. }
  412. catch(IndexOutOfBoundsException e)
  413. {
  414. // Not going to test message or the like...
  415. result = true;
  416. }
  417. assertTrue(result);
  418.  
  419. /*
  420. * Add some elements to instance
  421. */
  422. for(int i = 0; i < nBits; i++)
  423. {
  424. instance.insert(i, seq0[i]);
  425. }
  426.  
  427. /*
  428. * Try looking up elements beyond existing list...
  429. */
  430. toLookup = -1;
  431. try
  432. {
  433. instance.lookup(toLookup);
  434. result = false;
  435. }
  436. catch(IndexOutOfBoundsException e)
  437. {
  438. // Not going to test message or the like...
  439. result = true;
  440. }
  441. assertTrue(result);
  442.  
  443. toLookup = nBits;
  444. try
  445. {
  446. instance.lookup(toLookup);
  447. result = false;
  448. }
  449. catch(IndexOutOfBoundsException e)
  450. {
  451. // Not going to test message or the like...
  452. result = true;
  453. }
  454. assertTrue(result);
  455. }
  456.  
  457. /**
  458. * Test of negateAll method, of class boolArrayList.
  459. */
  460. @Test
  461. public void testNegateAll()
  462. {
  463. System.out.println("negateAll");
  464. boolArrayList instance = new boolArrayList();
  465. //boolArrayList instance = new boolArrayList(10);
  466.  
  467. /*
  468. * Add some elements to instances
  469. */
  470. for(int i = 0; i < nBits; i++)
  471. {
  472. instance.insert(i, seq0[i]);
  473. }
  474.  
  475. /*
  476. * Negate elements
  477. */
  478. instance.negateAll();
  479.  
  480. /*
  481. * Query individual elements
  482. */
  483. boolean expResult;
  484. boolean result;
  485. for(int i = 0; i < nBits; i++)
  486. {
  487. result = instance.lookup(i);
  488. expResult = !seq0[i]; // Negation of stored result
  489. assertEquals(result, expResult);
  490. }
  491. }
  492.  
  493. /**
  494. * Test of exception for negateAll method, of class boolArrayList.
  495. * N.B. For some reason, the "expected" parameter is not being
  496. * recognized. See above comment for testRemoveException.
  497. * <p>
  498. * Commenting out use of @Test annotation and implementing try/catch
  499. * idiom.... In any case, may have different types of exceptions selected
  500. * (not specified in handout).
  501. * <p>
  502. * Also set up to return a flag for successful completion, not necessarily
  503. * going to have code that tosses an exception.
  504. */
  505. //@Test(expected = IllegalArgumentException.class)
  506. @Test
  507. public void testNegateAllException()
  508. {
  509. System.out.println("negateAll exception");
  510. boolArrayList instance = new boolArrayList();
  511.  
  512. /*
  513. * Try negating elements from an empty list...
  514. */
  515. boolean result;
  516. boolean flagResult;
  517. try
  518. {
  519. //flagResult = instance.negateAll();
  520. instance.negateAll();
  521. //result = !flagResult;
  522. result = false;
  523. }
  524. catch(IllegalArgumentException e)
  525. {
  526. // Not going to test message or the like...
  527. result = true;
  528. }
  529. assertTrue(result);
  530. }
  531. }
Add Comment
Please, Sign In to add comment