Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. package org.autotest;
  2.  
  3. import org.junit.Test;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. import static org.junit.Assert.*;
  8.  
  9. public class TestStackAr {
  10.  
  11. @Test
  12. public void aNewStackShouldBeEmpty() {
  13. StackAr stack = new StackAr();
  14.  
  15. boolean isEmpty = stack.isEmpty();
  16.  
  17. assertTrue(isEmpty);
  18. }
  19.  
  20. @Test
  21. public void aNewStackShouldntBeFull() {
  22. StackAr stack = new StackAr();
  23.  
  24. boolean isFull = stack.isFull();
  25.  
  26. assertFalse(isFull);
  27. }
  28.  
  29. @Test
  30. public void anEmptyStackShouldHaveSizeZero() {
  31. StackAr stack = new StackAr();
  32.  
  33. assertEquals(0, stack.size());
  34. }
  35.  
  36. @Test
  37. public void stackSizeShouldIncreaseWhenItemsAreAdded() {
  38. StackAr stack = new StackAr();
  39.  
  40. stack.push(new Object());
  41.  
  42. assertEquals(1, stack.size());
  43. }
  44.  
  45. @Test
  46. public void stackCapacityShouldBeTheOnePassedToTheConstructor() {
  47. StackAr stack = new StackAr(1);
  48.  
  49. stack.push(new Object());
  50.  
  51. assertTrue(stack.isFull());
  52. }
  53.  
  54. @Test(expected = IllegalArgumentException.class)
  55. public void aStackCantHaveNegativeCapacity() {
  56. new StackAr(-1);
  57. }
  58.  
  59. @Test
  60. public void stackShouldntBeFullWhenThereIsStillSpace() {
  61. StackAr stack = new StackAr(4);
  62.  
  63. stack.push(new Object());
  64.  
  65. assertFalse(stack.isFull());
  66. }
  67.  
  68. @Test(expected = IllegalStateException.class)
  69. public void exceptionShouldBeThrownWhenCapacityIsExceeded() {
  70. StackAr stack = new StackAr(1);
  71.  
  72. stack.push(new Object());
  73. stack.push(new Object());
  74. }
  75.  
  76.  
  77. @Test(expected = IllegalStateException.class)
  78. public void exceptionShouldBeThrownWhenCheckingTheTopOfAnEmptyStack() {
  79. StackAr stack = new StackAr();
  80.  
  81. stack.top();
  82. }
  83.  
  84. @Test
  85. public void stackSizeShouldStayTheSameWhenAskedForTheTop(){
  86. StackAr stack = new StackAr();
  87. stack.push(new Object());
  88.  
  89. stack.top();
  90.  
  91. assertEquals(1, stack.size());
  92. }
  93.  
  94. @Test
  95. public void theStackTopShouldBeTheLastItemPushed(){
  96. StackAr stack = new StackAr();
  97. Object item = new Object();
  98. stack.push(item);
  99.  
  100. Object top = stack.top();
  101.  
  102. assertSame(item, top);
  103. }
  104.  
  105. @Test
  106. public void stackSizeShouldDecreaseWhenItemsAreRemoved() {
  107. StackAr stack = new StackAr();
  108. stack.push(new Object());
  109.  
  110. stack.pop();
  111.  
  112. assertEquals(0, stack.size());
  113. }
  114.  
  115. @Test(expected = IllegalStateException.class)
  116. public void exceptionShouldBeThrownWhenAnEmptyStackIsPopped() {
  117. StackAr stack = new StackAr();
  118.  
  119. stack.pop();
  120. }
  121.  
  122. @Test
  123. public void aPoppedItemShouldBeTheSameThatWasPushed(){
  124. StackAr stack = new StackAr();
  125. Object item = new Object();
  126. stack.push(item);
  127.  
  128. Object popped = stack.pop();
  129.  
  130. assertSame(item, popped);
  131. }
  132.  
  133. @Test
  134. public void anItemShouldRemainUnchangedWhenANewItemIsPushedOnTopOfIt(){
  135. StackAr stack = new StackAr();
  136. Object item = new Object();
  137. stack.push(item);
  138.  
  139. stack.push(new Object());
  140. stack.pop();
  141.  
  142. assertSame(item, stack.top());
  143. }
  144.  
  145. @Test
  146. public void twoDifferentStacksShouldHaveDifferentHashCodes(){
  147. StackAr stackOne = new StackAr();
  148. StackAr stackTwo = new StackAr();
  149.  
  150. stackOne.push(new Object());
  151. stackTwo.push(new Object());
  152.  
  153. assertNotEquals(stackOne.hashCode(), stackTwo.hashCode());
  154. }
  155.  
  156. @Test
  157. public void twoEqualStacksShouldHaveEqualHashCodes(){
  158. StackAr stackOne = new StackAr();
  159. StackAr stackTwo = new StackAr();
  160.  
  161. stackOne.push(new Object());
  162. stackTwo.push(stackOne.top());
  163.  
  164. assertEquals(stackOne.hashCode(), stackTwo.hashCode());
  165. }
  166.  
  167. @Test
  168. public void aStackShouldBeEqualToItself(){
  169. StackAr stack = new StackAr();
  170.  
  171. stack.push(new Object());
  172.  
  173. assertEquals(stack, stack);
  174. }
  175.  
  176. @Test
  177. public void emptyStacksShouldBeEqual(){
  178. StackAr stackOne = new StackAr();
  179. StackAr stackTwo = new StackAr();
  180.  
  181. assertEquals(stackOne, stackTwo);
  182. }
  183.  
  184. @Test
  185. public void aStackShouldntBeEqualToNull(){
  186. StackAr stack = new StackAr();
  187.  
  188. assertNotEquals(stack, null);
  189. }
  190.  
  191. @Test
  192. public void aStackShouldntBeEqualToSomethingOtherThanAStack(){
  193. StackAr stack = new StackAr();
  194.  
  195. assertNotEquals(stack, new ArrayList<>());
  196. }
  197.  
  198. @Test
  199. public void stacksWithDifferentSizeShouldntBeEqual(){
  200. StackAr stackOne = new StackAr();
  201. StackAr stackTwo = new StackAr();
  202.  
  203. stackOne.push("foo");
  204. stackOne.push("bar");
  205. stackTwo.push("foo");
  206.  
  207. assertNotEquals(stackOne, stackTwo);
  208. }
  209.  
  210.  
  211. @Test
  212. public void stacksWithDifferentCapacityShouldntBeEqual(){
  213. StackAr stackOne = new StackAr(10);
  214. StackAr stackTwo = new StackAr(20);
  215.  
  216. assertNotEquals(stackOne, stackTwo);
  217. }
  218.  
  219. @Test
  220. public void stacksWithDifferentItemsShouldntBeEqual(){
  221. StackAr stackOne = new StackAr();
  222. StackAr stackTwo = new StackAr();
  223.  
  224. stackOne.push(new Object());
  225. stackTwo.push(new Object());
  226.  
  227. assertNotEquals(stackOne, stackTwo);
  228. }
  229.  
  230. @Test
  231. public void stacksWithTheSameItemsShouldBeEqual(){
  232. StackAr stackOne = new StackAr();
  233. StackAr stackTwo = new StackAr();
  234.  
  235. stackOne.push(new Object());
  236. stackTwo.push(stackOne.top());
  237.  
  238. assertEquals(stackOne, stackTwo);
  239. }
  240.  
  241. @Test
  242. public void aStackWithAPoppedItemShouldntBeEqualToAstackWithIt(){
  243. StackAr stackOne = new StackAr();
  244. StackAr stackTwo = new StackAr();
  245.  
  246. stackOne.push(new Object());
  247. stackTwo.push(stackOne.top());
  248. stackTwo.pop();
  249.  
  250. assertNotEquals(stackOne, stackTwo);
  251. }
  252.  
  253. @Test
  254. public void anEmptyStackToStringShouldBeProperlyFormatted(){
  255. StackAr stack = new StackAr();
  256.  
  257. String print = stack.toString();
  258.  
  259. assertEquals("[]", print);
  260. }
  261.  
  262. @Test
  263. public void aStackWithOneItemToStringShouldBeProperlyFormatted(){
  264. StackAr stack = new StackAr();
  265. stack.push("foo");
  266.  
  267. String print = stack.toString();
  268.  
  269. assertEquals("[foo]", print);
  270. }
  271.  
  272. @Test
  273. public void aStackWithMoreThanOneItemToStringShouldBeProperlyFormatted(){
  274. StackAr stack = new StackAr();
  275. stack.push("foo");
  276. stack.push("bar");
  277.  
  278. String print = stack.toString();
  279.  
  280. assertEquals("[foo,bar]", print);
  281. }
  282.  
  283. @Test
  284. public void poppedItemsShouldntAppearInAStacksToString(){
  285. StackAr stack = new StackAr();
  286. stack.push("foo");
  287. stack.push("bar");
  288.  
  289. stack.pop();
  290. String print = stack.toString();
  291.  
  292. assertEquals("[foo]", print);
  293. }
  294.  
  295. //MUTATION
  296.  
  297. @Test
  298. public void capacityZeroShouldBeValid(){
  299. try {
  300. new StackAr(0);
  301.  
  302. }catch (Exception e){
  303. fail("No exception expected");
  304. }
  305. }
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement