Skip to content

Add new script test #34

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 1 commit
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
Next Next commit
add script test for StringCompression and StrStr
  • Loading branch information
quangduytr2403 committed Oct 12, 2022
commit 78b5492e19eeb1f8808bac8dac601b5c9515ea4d
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 stringCompression_EmptyString() {
String str = "";
assertEquals(StringCompression.compressString(""), "");
}

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

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

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

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

@Test
public void stringCompression_RandomString() {
String str = "aabcccccccccccccccccccccccccaaa";
assertEquals(StringCompression.compressString("aabcccccccccccccccccccccccccaaa"), "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 strStr_EdgeCase1() {
String haystack = "";
String needle = "";
assertEquals(StrStr.strStr(haystack, needle), 0);
}

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

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

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

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

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

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