StringArrayUtilsTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package com.zipcodewilmington;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. /**
  5. * Created by leon on 1/29/18.
  6. */
  7. public class StringArrayUtilsTest {
  8. @Test
  9. public void testGetFirstElement1() {
  10. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  11. String expected = "the";
  12. String actual = StringArrayUtils.getFirstElement(array);
  13. Assert.assertEquals(expected, actual);
  14. }
  15. @Test
  16. public void testGetFirstElement2() {
  17. String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  18. String expected = "quick";
  19. String actual = StringArrayUtils.getFirstElement(array);
  20. Assert.assertEquals(expected, actual);
  21. }
  22. @Test
  23. public void testGetFirstElement3() {
  24. String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  25. String expected = "brown";
  26. String actual = StringArrayUtils.getFirstElement(array);
  27. Assert.assertEquals(expected, actual);
  28. }
  29. @Test
  30. public void testGetSecondElement1() {
  31. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  32. String expected = "quick";
  33. String actual = StringArrayUtils.getSecondElement(array);
  34. Assert.assertEquals(expected, actual);
  35. }
  36. @Test
  37. public void testGetSecondElement2() {
  38. String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  39. String expected = "brown";
  40. String actual = StringArrayUtils.getSecondElement(array);
  41. Assert.assertEquals(expected, actual);
  42. }
  43. @Test
  44. public void testGetSecondElement3() {
  45. String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  46. String expected = "fox";
  47. String actual = StringArrayUtils.getSecondElement(array);
  48. Assert.assertEquals(expected, actual);
  49. }
  50. @Test
  51. public void testGetLastElement1() {
  52. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  53. String expected = "dog";
  54. String actual = StringArrayUtils.getLastElement(array);
  55. Assert.assertEquals(expected, actual);
  56. }
  57. @Test
  58. public void testGetLastElement2() {
  59. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy"};
  60. String expected = "lazy";
  61. String actual = StringArrayUtils.getLastElement(array);
  62. Assert.assertEquals(expected, actual);
  63. }
  64. @Test
  65. public void testGetLastElement3() {
  66. String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
  67. String expected = "over";
  68. String actual = StringArrayUtils.getLastElement(array);
  69. Assert.assertEquals(expected, actual);
  70. }
  71. @Test
  72. public void testGetSecondToLastElement1() {
  73. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  74. String expected = "lazy";
  75. String actual = StringArrayUtils.getSecondToLastElement(array);
  76. Assert.assertEquals(expected, actual);
  77. }
  78. @Test
  79. public void testGetSecondToLastElement2() {
  80. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "lazy"};
  81. String expected = "over";
  82. String actual = StringArrayUtils.getSecondToLastElement(array);
  83. Assert.assertEquals(expected, actual);
  84. }
  85. @Test
  86. public void testGetSecondToLastElement3() {
  87. String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
  88. String expected = "jumps";
  89. String actual = StringArrayUtils.getSecondToLastElement(array);
  90. Assert.assertEquals(expected, actual);
  91. }
  92. @Test
  93. public void testContains() {
  94. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  95. for (String s : array) {
  96. boolean outcome = StringArrayUtils.contains(array, s);
  97. Assert.assertTrue(outcome);
  98. }
  99. }
  100. @Test
  101. public void testReverse1() {
  102. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  103. String[] expected = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
  104. String[] actual = StringArrayUtils.reverse(array);
  105. Assert.assertEquals(expected, actual);
  106. }
  107. @Test
  108. public void testReverse2() {
  109. String[] array = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
  110. String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  111. String[] actual = StringArrayUtils.reverse(array);
  112. Assert.assertEquals(expected, actual);
  113. }
  114. @Test
  115. public void testReverse3() {
  116. String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  117. String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
  118. Assert.assertEquals(expected, actual);
  119. }
  120. @Test
  121. public void testIsPalindromic1() {
  122. String[] array = {"a", "b", "c", "b", "a"};
  123. boolean outcome = StringArrayUtils.isPalindromic(array);
  124. Assert.assertTrue(outcome);
  125. }
  126. @Test
  127. public void testIsPalindromic2() {
  128. String[] array = {"Is this a palindrome?", "This is a palindrome", "Is this a palindrome?"};
  129. boolean outcome = StringArrayUtils.isPalindromic(array);
  130. Assert.assertTrue(outcome);
  131. }
  132. @Test
  133. public void testIsPalindromic3() {
  134. String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
  135. boolean outcome = StringArrayUtils.isPalindromic(array);
  136. Assert.assertFalse(outcome);
  137. }
  138. @Test
  139. public void testIsPangramic1() {
  140. String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
  141. boolean outcome = StringArrayUtils.isPangramic(array);
  142. Assert.assertTrue(outcome);
  143. }
  144. @Test
  145. public void testIsPangramic2() {
  146. String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"};
  147. boolean outcome = StringArrayUtils.isPangramic(array);
  148. Assert.assertTrue(outcome);
  149. }
  150. @Test
  151. public void testIsPangramic3() {
  152. String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"};
  153. boolean outcome = StringArrayUtils.isPangramic(array);
  154. Assert.assertTrue(outcome);
  155. }
  156. @Test
  157. public void testIsPangramic4() {
  158. String[] array = {"a", "b", "c", "d"};
  159. boolean outcome = StringArrayUtils.isPangramic(array);
  160. Assert.assertFalse(outcome);
  161. }
  162. @Test
  163. public void testGetNumberOfOccurrences1() {
  164. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  165. int expected = 4;
  166. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  167. Assert.assertEquals(actual, expected);
  168. }
  169. @Test
  170. public void testGetNumberOfOccurrences2() {
  171. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  172. int expected = 2;
  173. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
  174. Assert.assertEquals(actual, expected);
  175. }
  176. @Test
  177. public void testGetNumberOfOccurrences3() {
  178. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  179. int expected = 4;
  180. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  181. Assert.assertEquals(actual, expected);
  182. }
  183. @Test
  184. public void testRemoveConsecutiveDuplicates1() {
  185. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  186. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  187. String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
  188. Assert.assertEquals(actual, expected);
  189. }
  190. @Test
  191. public void testRemoveConsecutiveDuplicates2() {
  192. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
  193. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  194. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"};
  195. Assert.assertEquals(actual, expected);
  196. }
  197. @Test
  198. public void testRemoveConsecutiveDuplicates3() {
  199. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "aba", "bbb"};
  200. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  201. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
  202. Assert.assertEquals(actual, expected);
  203. }
  204. @Test
  205. public void testRemovePackDuplicates1() {
  206. String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
  207. String[] expected = {"aaa", "b", "cc", "aa", "d"};
  208. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  209. Assert.assertEquals(expected, actual);
  210. }
  211. @Test
  212. public void testRemovePackDuplicates2() {
  213. String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
  214. String[] expected = {"tt", "q", "aaa", "b", "cc", "aa", "d", "eee"};
  215. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  216. Assert.assertEquals(expected, actual);
  217. }
  218. @Test
  219. public void testRemovePackDuplicates3() {
  220. String[] array = {"m", "o", "o", "n", "m", "a", "n"};
  221. String[] expected = {"m", "oo", "n", "m", "a", "n"};
  222. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  223. Assert.assertEquals(expected, actual);
  224. }
  225. @Test
  226. public void testRemoveValue() {
  227. String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  228. String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  229. String[] actual = StringArrayUtils.removeValue(array, "The");
  230. Assert.assertEquals(expected, actual);
  231. }
  232. @Test
  233. public void testRemoveValue1() {
  234. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  235. String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  236. String[] actual = StringArrayUtils.removeValue(array, "quick");
  237. Assert.assertEquals(expected, actual);
  238. }
  239. @Test
  240. public void testRemoveValue2() {
  241. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  242. String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
  243. String[] actual = StringArrayUtils.removeValue(array, "brown");
  244. Assert.assertEquals(expected, actual);
  245. }
  246. }