StringArrayUtilsTest.java 12KB

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