StringArrayUtilsTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 plaindrome?", "This is a plaindrome", "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.assertTrue(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 testGetNumberOfOccurrences1() {
  158. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  159. int expected = 4;
  160. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  161. Assert.assertEquals(actual, expected);
  162. }
  163. @Test
  164. public void testGetNumberOfOccurrences2() {
  165. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  166. int expected = 2;
  167. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
  168. Assert.assertEquals(actual, expected);
  169. }
  170. @Test
  171. public void testGetNumberOfOccurrences3() {
  172. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  173. int expected = 4;
  174. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  175. Assert.assertEquals(actual, expected);
  176. }
  177. @Test
  178. public void testRemoveConsecutiveDuplicates1() {
  179. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  180. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  181. String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
  182. Assert.assertEquals(actual, expected);
  183. }
  184. @Test
  185. public void testRemoveConsecutiveDuplicates2() {
  186. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
  187. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  188. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"};
  189. Assert.assertEquals(actual, expected);
  190. }
  191. @Test
  192. public void testRemoveConsecutiveDuplicates3() {
  193. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb", "aba", "bbb"};
  194. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  195. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
  196. Assert.assertEquals(actual, expected);
  197. }
  198. @Test
  199. public void testRemovePackDuplicates1() {
  200. String[] array = {"a", "a", "a", "a", "b", "c", "c", "a", "a", "d"};
  201. String[] expected = {"aaa", "b", "cc", "aa", "d", "eee"};
  202. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  203. Assert.assertEquals(expected, actual);
  204. }
  205. @Test
  206. public void testRemovePackDuplicates2() {
  207. String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
  208. String[] expected = {"tt", "q", "aaa", "cc", "aa", "d", "eee"};
  209. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  210. Assert.assertEquals(expected, actual);
  211. }
  212. @Test
  213. public void testRemovePackDuplicates3() {
  214. String[] array = {"m", "o", "o", "n", "m", "a", "n"};
  215. String[] expected = {"m", "oo", "n", "m", "a", "n"};
  216. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  217. Assert.assertEquals(expected, actual);
  218. }
  219. @Test
  220. public void testRemoveValue() {
  221. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  222. String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  223. String[] actual = StringArrayUtils.removeValue(array, "The");
  224. Assert.assertEquals(expected, actual);
  225. }
  226. @Test
  227. public void testRemoveValue1() {
  228. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  229. String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  230. String[] actual = StringArrayUtils.removeValue(array, "quick");
  231. Assert.assertEquals(expected, actual);
  232. }
  233. @Test
  234. public void testRemoveValue2() {
  235. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  236. String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
  237. String[] actual = StringArrayUtils.removeValue(array, "brown");
  238. Assert.assertEquals(expected, actual);
  239. }
  240. }