123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import java.util.Arrays;
  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 = {"aa", "bb", "cc", "dd"};
  159. boolean outcome = StringArrayUtils.isPangramic(array);
  160. Assert.assertFalse(outcome);
  161. }
  162. @Test
  163. public void teststringContainsLetter(){
  164. String string = "A fox";
  165. boolean outcome = StringArrayUtils.stringContainsLetter(string,'e');
  166. Assert.assertFalse(outcome);
  167. }
  168. @Test
  169. public void teststringContainsLetter2(){
  170. String string = "A fox";
  171. boolean outcome = StringArrayUtils.stringContainsLetter(string,'o');
  172. Assert.assertTrue(outcome);
  173. }
  174. @Test
  175. public void arrayContainsLetter(){
  176. String[] string = {"A fox","bunny"};
  177. boolean outcome = StringArrayUtils.arrayContainsLetter(string,'e');
  178. Assert.assertFalse(outcome);
  179. }
  180. @Test
  181. public void arrayContainsLetter2(){
  182. String[] string = {"A fox","bunny"};
  183. boolean outcome = StringArrayUtils.arrayContainsLetter(string,'o');
  184. Assert.assertTrue(outcome);
  185. }
  186. @Test
  187. public void testGetNumberOfOccurrences1() {
  188. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  189. int expected = 4;
  190. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  191. Assert.assertEquals(actual, expected);
  192. }
  193. @Test
  194. public void testGetNumberOfOccurrences2() {
  195. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  196. int expected = 2;
  197. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
  198. Assert.assertEquals(actual, expected);
  199. }
  200. @Test
  201. public void testGetNumberOfOccurrences3() {
  202. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  203. int expected = 4;
  204. int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
  205. Assert.assertEquals(actual, expected);
  206. }
  207. @Test
  208. public void testRemoveConsecutiveDuplicates1() {
  209. String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
  210. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  211. String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
  212. Assert.assertEquals(actual, expected);
  213. }
  214. @Test
  215. public void testRemoveConsecutiveDuplicates2() {
  216. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
  217. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  218. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"};
  219. Assert.assertEquals(actual, expected);
  220. }
  221. @Test
  222. public void testRemoveConsecutiveDuplicates3() {
  223. String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "aba", "bbb"};
  224. String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
  225. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
  226. Assert.assertEquals(actual, expected);
  227. }
  228. @Test
  229. public void testRemovePackDuplicates1() {
  230. String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
  231. String[] expected = {"aaa", "b", "cc", "aa", "d"};
  232. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  233. System.out.print(Arrays.toString(actual));
  234. Assert.assertEquals(expected, actual);
  235. }
  236. @Test
  237. public void testRemovePackDuplicates2() {
  238. String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
  239. String[] expected = {"tt", "q", "aaa", "b", "cc", "aa", "d", "eee"};
  240. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  241. Assert.assertEquals(expected, actual);
  242. }
  243. @Test
  244. public void testRemovePackDuplicates3() {
  245. String[] array = {"m", "o", "o", "n", "m", "a", "n"};
  246. String[] expected = {"m", "oo", "n", "m", "a", "n"};
  247. String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
  248. Assert.assertEquals(expected, actual);
  249. }
  250. @Test
  251. public void testRemoveValue() {
  252. String[] array = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  253. String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  254. String[] actual = StringArrayUtils.removeValue(array, "The");
  255. Assert.assertEquals(expected, actual);
  256. }
  257. @Test
  258. public void testRemoveValue1() {
  259. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  260. String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  261. String[] actual = StringArrayUtils.removeValue(array, "quick");
  262. Assert.assertEquals(expected, actual);
  263. }
  264. @Test
  265. public void testRemoveValue2() {
  266. String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  267. String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
  268. String[] actual = StringArrayUtils.removeValue(array, "brown");
  269. Assert.assertEquals(expected, actual);
  270. }
  271. }