Skip to content

Commit

Permalink
add replace test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
texadactyl committed Aug 8, 2024
1 parent c5aceec commit 5c0e09a
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This file is a version history of jacotest amendments. Entries appear in versio
| :------------: | :---: | :--- |
|<img width=90/>|<img width=60/>|<img width=600/>|
| 2024-08-07 | 3.4.6 | New test cases: stringbuffer-append, stringbuffer-delete, stringbuffer-dynamic, stringbuffer-insert, stringbuffer-misc, stringbuffer-perf. |
| | | New test cases: stringbuilder-replace, stringbuffer-replace. |
| 2024-08-07 | 3.4.5 | New test case: stringbuilder-misc. |
| | | Updated test case solitairgraphy. |
| 2024-08-06 | 3.4.4 | New test cases: stringbuilder-insert, stringbuilder-delete. |
Expand Down
70 changes: 70 additions & 0 deletions tests/stringbuffer-replace/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

public class main {

public static int checker(String label, String expected, String observed) {
System.out.printf("checker %s ", label);
if (expected.equals(observed)) {
System.out.printf(" ok, expected = %s = observed\n", expected);
return 0;
}
System.out.printf(" ********** ERROR, expected = %s, observed = %s\n", expected, observed);
return 1;
}

public static int checker(String label, int expected, int observed) {
System.out.printf("checker %s ", label);
if (expected == observed) {
System.out.printf(" ok, expected = %d = observed\n", expected);
return 0;
}
System.out.printf(" ********** ERROR, expected = %d, observed = %d\n", expected, observed);
return 1;
}

public static void main(String[] args) {
int errorCount = 0;

StringBuffer sb1 = new StringBuffer("ABCDEFGH");
String str = "123";

StringBuffer sb2 = sb1.replace(3, 6, str);
errorCount += checker("sb1.replace(3, 6, str)", "ABC123GH", sb2.toString());
errorCount += checker("sb1.capacity(24)", 24, sb1.capacity());
errorCount += checker("sb1.length(8)", 8, sb1.length());

sb2 = sb1.replace(0, 6, str);
errorCount += checker("sb1.replace(0, 6, str)", "123GH", sb2.toString());
errorCount += checker("sb1.capacity(24)", 24, sb1.capacity());
errorCount += checker("sb1.length(5)", 5, sb1.length());


sb1 = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZxyz");

try {
sb1.replace(-3, 6, str);
System.out.println("********** ERROR, failed to catch exception 1");
errorCount++;
} catch(StringIndexOutOfBoundsException ex) {
System.out.println("Successfully caught exception 1");
}

try {
sb1.replace(3, -6, str);
System.out.println("********** ERROR, failed to catch exception 2");
errorCount++;
} catch(StringIndexOutOfBoundsException ex) {
System.out.println("Successfully caught exception 2");
}

try {
sb1.replace(33, 6, str);
System.out.println("********** ERROR, failed to catch exception 3");
errorCount++;
} catch(StringIndexOutOfBoundsException ex) {
System.out.println("Successfully caught exception 3");
}

assert(errorCount == 0);
System.out.printf("No errors\n");
}
}
70 changes: 70 additions & 0 deletions tests/stringbuilder-replace/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

public class main {

public static int checker(String label, String expected, String observed) {
System.out.printf("checker %s ", label);
if (expected.equals(observed)) {
System.out.printf(" ok, expected = %s = observed\n", expected);
return 0;
}
System.out.printf(" ********** ERROR, expected = %s, observed = %s\n", expected, observed);
return 1;
}

public static int checker(String label, int expected, int observed) {
System.out.printf("checker %s ", label);
if (expected == observed) {
System.out.printf(" ok, expected = %d = observed\n", expected);
return 0;
}
System.out.printf(" ********** ERROR, expected = %d, observed = %d\n", expected, observed);
return 1;
}

public static void main(String[] args) {
int errorCount = 0;

StringBuilder sb1 = new StringBuilder("ABCDEFGH");
String str = "123";

StringBuilder sb2 = sb1.replace(3, 6, str);
errorCount += checker("sb1.replace(3, 6, str)", "ABC123GH", sb2.toString());
errorCount += checker("sb1.capacity(24)", 24, sb1.capacity());
errorCount += checker("sb1.length(8)", 8, sb1.length());

sb2 = sb1.replace(0, 6, str);
errorCount += checker("sb1.replace(0, 6, str)", "123GH", sb2.toString());
errorCount += checker("sb1.capacity(24)", 24, sb1.capacity());
errorCount += checker("sb1.length(5)", 5, sb1.length());


sb1 = new StringBuilder("ABCDEFGHIJKLMNOPQRSTUVWXYZxyz");

try {
sb1.replace(-3, 6, str);
System.out.println("********** ERROR, failed to catch exception 1");
errorCount++;
} catch(StringIndexOutOfBoundsException ex) {
System.out.println("Successfully caught exception 1");
}

try {
sb1.replace(3, -6, str);
System.out.println("********** ERROR, failed to catch exception 2");
errorCount++;
} catch(StringIndexOutOfBoundsException ex) {
System.out.println("Successfully caught exception 2");
}

try {
sb1.replace(33, 6, str);
System.out.println("********** ERROR, failed to catch exception 3");
errorCount++;
} catch(StringIndexOutOfBoundsException ex) {
System.out.println("Successfully caught exception 3");
}

assert(errorCount == 0);
System.out.printf("No errors\n");
}
}

0 comments on commit 5c0e09a

Please sign in to comment.