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