Skip to content

Add new script test #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class StringCompression {
* @param str input string containing only a-z characters, both cases
* @return which ever is the shorter string
*/
private static String compressString(String str) {
static String compressString(String str) {
StringBuilder compressedSb = new StringBuilder();
int countConsecutive = 0;
for (int i = 0; i < str.length(); i++) {
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/ctci/arraysandstrings/StringCompressionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.ctci.arraysandstrings;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class StringCompressionTest {
@Test
public void testEmptyString() {
String str = "";
assertEquals(StringCompression.compressString(str), "");
}

@Test
public void testSingleCharacter() {
String str = "a";
assertEquals(StringCompression.compressString(str), "a");
}

@Test
public void testNoConsecutiveCharacter() {
String str = "abcdef";
assertEquals(StringCompression.compressString(str), "abcdef");
}

@Test
public void testCompressedNotShorterThanOriginal() {
String str = "aabbccddee";
assertEquals(StringCompression.compressString(str), "aabbccddee");
}

@Test
public void testLowercaseNexttoUppercase() {
String str = "aAbbBBBCCCCcccc";
assertEquals(StringCompression.compressString(str), "a1A1b2B3C4c4");
}

@Test
public void testRandomString() {
String str = "aabcccccccccccccccccccccccccaaa";
assertEquals(StringCompression.compressString(str), "a2b1c25a3");
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/leetcode/strings/StrStrTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.leetcode.strings;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class StrStrTest {
@Test
public void testEdgeCase1() {
String haystack = "";
String needle = "";
assertEquals(StrStr.strStr(haystack, needle), 0);
}

@Test
public void testEdgeCase2() {
String haystack = "";
String needle = "abc";
assertEquals(StrStr.strStr(haystack, needle), -1);
}

@Test
public void testEdgeCase3() {
String haystack = "aaaa";
String needle = "";
assertEquals(StrStr.strStr(haystack, needle), 0);
}

@Test
public void testEdgeCase4() {
String haystack = "aaa";
String needle = "aaaa";
assertEquals(StrStr.strStr(haystack, needle), -1);
}

@Test
public void testRandomCase1NotFound() {
String haystack = "aaaa";
String needle = "bba";
assertEquals(StrStr.strStr(haystack, needle), -1);
}

@Test
public void testRandomCase2Found() {
String haystack = "mississippi";
String needle = "issip";
assertEquals(StrStr.strStr(haystack, needle), 4);
}

@Test
public void testRandomCase3Found() {
String haystack = "duuuuuuy";
String needle = "u";
assertEquals(StrStr.strStr(haystack, needle), 1);
}
}