Created
May 5, 2020 01:24
-
-
Save aungtuntun/f7b3ce9108a5ad7ec76874b94ca00041 to your computer and use it in GitHub Desktop.
CharCountUnitTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.itscreening.tests.faker; | |
| public class CharCount { | |
| public static void main(String[] args) { | |
| // String str1 = "aaabbccdaaee"; | |
| // String str2 = "a3b2c2da2e2"; | |
| // System.out.println(convert("").equals("")); | |
| } | |
| public String convert(String data) { | |
| StringBuilder builder = new StringBuilder(); | |
| if (data == null || data.isEmpty()) { | |
| return ""; | |
| } | |
| char start = data.charAt(0); | |
| int count = 0; | |
| for (int i = 0; i < data.length(); i++) { | |
| final char current = data.charAt(i); | |
| if (start == current) { | |
| count++; | |
| }else { | |
| if (count > 1) { | |
| builder.append(start).append(count); | |
| }else { | |
| builder.append(start); | |
| } | |
| start = current; | |
| count = 1; | |
| } | |
| } | |
| if (count > 1) { | |
| builder.append(start).append(count); | |
| }else { | |
| builder.append(start); | |
| } | |
| return builder.toString(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.itscreening.tests.faker; | |
| import org.testng.annotations.Test; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| @Test | |
| public class CharCountTest { | |
| @Test | |
| public void StringValidTest() { | |
| CharCount charCount = new CharCount(); | |
| String result = charCount.convert(""); | |
| assertThat(result).matches(""); | |
| System.out.println("RESULT :: " + result.equals("")); | |
| } | |
| @Test | |
| public void StringNullTest() { | |
| CharCount charCount = new CharCount(); | |
| String result = charCount.convert(null); | |
| assertThat(result).matches(""); | |
| System.out.println("RESULT :: " + result.equals("")); | |
| } | |
| @Test | |
| public void correctDataInput() { | |
| CharCount charCount = new CharCount(); | |
| String result = charCount.convert("aaabbccdaaee"); | |
| assertThat(result).matches("a3b2c2da2e2"); | |
| System.out.println("RESULT :: " + result.equals("a3b2c2da2e2")); | |
| } | |
| @Test | |
| public void singleValue() { | |
| CharCount charCount = new CharCount(); | |
| String result = charCount.convert("abcdae"); | |
| assertThat(result).matches("abcdae"); | |
| System.out.println("RESULT :: " + result.equals("abcdae")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment