StringArrayUtilsTest.java 11KB

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