StringArrayUtilsTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 testReverse1() {
  101. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  102. String[] expected = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
  103. String[] actual = StringArrayUtils.reverse(array);
  104. Assert.assertEquals(expected, actual);
  105. }
  106. @Test
  107. public void testReverse2() {
  108. String[] array = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
  109. String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  110. String[] actual = StringArrayUtils.reverse(array);
  111. Assert.assertEquals(expected, actual);
  112. }
  113. @Test
  114. public void testReverse3() {
  115. String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  116. String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
  117. Assert.assertEquals(expected, actual);
  118. }
  119. @Test
  120. public void testIsPalindromic1() {
  121. String[] array = {"a", "b", "c", "b", "a"};
  122. boolean outcome = StringArrayUtils.isPalindromic(array);
  123. Assert.assertTrue(outcome);
  124. }
  125. @Test
  126. public void testIsPalindromic2() {
  127. String[] array = {"Is this a palindrome?", "This is a palindrome", "Is this a palindrome?"};
  128. boolean outcome = StringArrayUtils.isPalindromic(array);
  129. Assert.assertTrue(outcome);
  130. }
  131. @Test
  132. public void testIsPalindromic3() {
  133. String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
  134. boolean outcome = StringArrayUtils.isPalindromic(array);
  135. Assert.assertFalse(outcome);
  136. }
  137. @Test
  138. public void testIsPangramic1() {
  139. String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
  140. boolean outcome = StringArrayUtils.isPangramic(array);
  141. Assert.assertTrue(outcome);
  142. }
  143. @Test
  144. public void testIsPangramic2() {
  145. String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"};
  146. boolean outcome = StringArrayUtils.isPangramic(array);
  147. Assert.assertTrue(outcome);
  148. }
  149. @Test
  150. public void testIsPangramic3() {
  151. String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"};
  152. boolean outcome = StringArrayUtils.isPangramic(array);
  153. Assert.assertTrue(outcome);
  154. }
  155. @Test
  156. public void testIsPangramic4() {
  157. String[] array = {"a", "b", "c", "d"};
  158. boolean outcome = StringArrayUtils.isPangramic(array);
  159. Assert.assertFalse(outcome);
  160. }
  161. @Test
  162. public void testGetNumberOfOccurrences1() {
  163. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  164. int expected = 4;
  165. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  166. Assert.assertEquals(actual, expected);
  167. }
  168. @Test
  169. public void testGetNumberOfOccurrences2() {
  170. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  171. int expected = 2;
  172. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
  173. Assert.assertEquals(actual, expected);
  174. }
  175. @Test
  176. public void testGetNumberOfOccurrences3() {
  177. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  178. int expected = 4;
  179. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  180. Assert.assertEquals(actual, expected);
  181. }
  182. @Test
  183. public void testRemoveConsecutiveDuplicates1() {
  184. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  185. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  186. String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
  187. Assert.assertEquals(actual, expected);
  188. }
  189. @Test
  190. public void testRemoveConsecutiveDuplicates2() {
  191. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
  192. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  193. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"};
  194. Assert.assertEquals(actual, expected);
  195. }
  196. @Test
  197. public void testRemoveConsecutiveDuplicates3() {
  198. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "aba", "bbb"};
  199. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  200. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
  201. Assert.assertEquals(actual, expected);
  202. }
  203. @Test
  204. public void testRemovePackDuplicates1() {
  205. String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
  206. String[] expected = {"aaa", "b", "cc", "aa", "d"};
  207. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  208. Assert.assertEquals(expected, actual);
  209. }
  210. @Test
  211. public void testRemovePackDuplicates2() {
  212. String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
  213. String[] expected = {"tt", "q", "aaa", "b", "cc", "aa", "d", "eee"};
  214. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  215. Assert.assertEquals(expected, actual);
  216. }
  217. @Test
  218. public void testRemovePackDuplicates3() {
  219. String[] array = {"m", "o", "o", "n", "m", "a", "n"};
  220. String[] expected = {"m", "oo", "n", "m", "a", "n"};
  221. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  222. Assert.assertEquals(expected, actual);
  223. }
  224. @Test
  225. public void testRemoveValue() {
  226. String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  227. String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  228. String[] actual = StringArrayUtils.removeValue(array, "The");
  229. Assert.assertEquals(expected, actual);
  230. }
  231. @Test
  232. public void testRemoveValue1() {
  233. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  234. String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  235. String[] actual = StringArrayUtils.removeValue(array, "quick");
  236. Assert.assertEquals(expected, actual);
  237. }
  238. @Test
  239. public void testRemoveValue2() {
  240. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  241. String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
  242. String[] actual = StringArrayUtils.removeValue(array, "brown");
  243. Assert.assertEquals(expected, actual);
  244. }
  245. }