|
| 1 | +package CustomUtil.StringBuilder.__test__; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import CustomUtil.StringBuilder.CustomStringBuilder; |
| 7 | + |
| 8 | +public class TestMain { |
| 9 | + private CustomStringBuilder sb; |
| 10 | + |
| 11 | + @BeforeEach |
| 12 | + void setup() { |
| 13 | + sb = new CustomStringBuilder(); |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + void testAppend() { |
| 18 | + sb.append("Hello"); |
| 19 | + assertEquals("Hello", sb.toString(), "Appending a string should store it correctly."); |
| 20 | + |
| 21 | + sb.append(" World"); |
| 22 | + assertEquals("Hello World", sb.toString(), "Appending another string should concatenate correctly."); |
| 23 | + |
| 24 | + sb.append('!'); |
| 25 | + assertEquals("Hello World!", sb.toString(), "Appending a char should work correctly."); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void testDelete() { |
| 30 | + sb.append("Hello World"); |
| 31 | + |
| 32 | + sb.delete(5, 11); |
| 33 | + assertEquals("Hello", sb.toString(), "Deleting a range should remove the specified characters."); |
| 34 | + |
| 35 | + sb.delete(0, 2); |
| 36 | + assertEquals("llo", sb.toString(), "Deleting from start should work correctly."); |
| 37 | + |
| 38 | + sb.delete(2, 3); |
| 39 | + assertEquals("ll", sb.toString(), "Deleting the last character should work correctly."); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testInsert() { |
| 44 | + sb.append("Helo"); |
| 45 | + |
| 46 | + sb.insert(2, 'l'); |
| 47 | + assertEquals("Hello", sb.toString(), "Inserting a character at index should work."); |
| 48 | + |
| 49 | + sb.insert(5, " World"); |
| 50 | + assertEquals("Hello World", sb.toString(), "Inserting a string should work."); |
| 51 | + |
| 52 | + sb.insert(0, "Start "); |
| 53 | + assertEquals("Start Hello World", sb.toString(), "Inserting at the beginning should work."); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testReverse() { |
| 58 | + sb.append("abcd"); |
| 59 | + sb.reverse(); |
| 60 | + assertEquals("dcba", sb.toString(), "Reversing a string should return the correct order."); |
| 61 | + |
| 62 | + sb.clear(); |
| 63 | + sb.append("racecar"); |
| 64 | + sb.reverse(); |
| 65 | + assertEquals("racecar", sb.toString(), "Reversing a palindrome should return the same string."); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testClear() { |
| 70 | + sb.append("Some text"); |
| 71 | + sb.clear(); |
| 72 | + assertEquals("", sb.toString(), "Clearing should remove all content."); |
| 73 | + assertTrue(sb.isEmpty(), "Builder should be empty after clearing."); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testLength() { |
| 78 | + assertEquals(0, sb.length(), "New builder should have length 0."); |
| 79 | + |
| 80 | + sb.append("Hello"); |
| 81 | + assertEquals(5, sb.length(), "Length should be correct after append."); |
| 82 | + |
| 83 | + sb.delete(1, 3); |
| 84 | + assertEquals(3, sb.length(), "Length should adjust correctly after deletion."); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testEdgeCases() { |
| 89 | + sb.append(""); |
| 90 | + assertEquals("", sb.toString(), "Appending an empty string should not change content."); |
| 91 | + |
| 92 | + sb.append("Test"); |
| 93 | + sb.delete(0, sb.length()); |
| 94 | + assertEquals("", sb.toString(), "Deleting entire content should result in empty builder."); |
| 95 | + |
| 96 | + sb.insert(0, ""); |
| 97 | + assertEquals("", sb.toString(), "Inserting an empty string should not change content."); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testReplaceSingleCharacter() { |
| 102 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 103 | + sb.append("Hello"); |
| 104 | + |
| 105 | + sb.replace(1, 2, "a"); // Replace "e" with "a" |
| 106 | + assertEquals("Hallo", sb.toString(), "Replacing single character failed."); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testReplaceSubstringWithShorterString() { |
| 111 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 112 | + sb.append("Hello World"); |
| 113 | + |
| 114 | + sb.replace(6, 11, "Mars"); // Replace "World" with "Mars" |
| 115 | + assertEquals("Hello Mars", sb.toString(), "Replacing a substring with a shorter string failed."); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void testReplaceSubstringWithLongerString() { |
| 120 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 121 | + sb.append("Hi there"); |
| 122 | + |
| 123 | + sb.replace(3, 8, "everyone"); // Replace "there" with "everyone" |
| 124 | + assertEquals("Hi everyone", sb.toString(), "Replacing a substring with a longer string failed."); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testReplaceFullString() { |
| 129 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 130 | + sb.append("OldString"); |
| 131 | + |
| 132 | + sb.replace(0, sb.length(), "NewString"); // Replace entire string |
| 133 | + assertEquals("NewString", sb.toString(), "Replacing the full string failed."); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testReplaceWithEmptyString() { |
| 138 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 139 | + sb.append("RemoveMe"); |
| 140 | + |
| 141 | + sb.replace(0, 8, ""); // Remove all characters |
| 142 | + assertEquals("", sb.toString(), "Replacing with an empty string should clear the range."); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void testReplaceSameStartAndEndIndex() { |
| 147 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 148 | + sb.append("Test"); |
| 149 | + |
| 150 | + sb.replace(2, 2, "Insert"); // Insert "Insert" at index 2 |
| 151 | + assertEquals("TeInsertst", sb.toString(), "Inserting a string with replace() failed."); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void testReplaceWithTrimmedEnd() { |
| 156 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 157 | + sb.append("Short"); // "Short" (length 5) |
| 158 | + |
| 159 | + sb.replace(2, 10, "New"); // Since 10 > length(5), end should be trimmed to 5 |
| 160 | + |
| 161 | + assertEquals("ShNew", sb.toString(), "End index should be trimmed to size"); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testLargeAppend() { |
| 166 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 167 | + int largeSize = 1_000_000; // 1 million characters |
| 168 | + for (int i = 0; i < largeSize; i++) { |
| 169 | + sb.append('a'); |
| 170 | + } |
| 171 | + assertEquals(largeSize, sb.length(), "Length should match the number of appended characters"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void testLargeInsert() { |
| 176 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 177 | + sb.append("A".repeat(500_000)); // Start with 500K 'A's |
| 178 | + sb.insert(250_000, "Hello"); // Insert in the middle |
| 179 | + assertEquals(500_005, sb.length(), "Length should account for inserted characters"); |
| 180 | + assertEquals('H', sb.charAt(250_000), "Inserted text should start with 'H'"); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + void testLargeDelete() { |
| 185 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 186 | + sb.append("B".repeat(1_000_000)); // 1 million 'B's |
| 187 | + sb.delete(100_000, 900_000); // Remove the middle 800K |
| 188 | + assertEquals(200_000, sb.length(), "Length should be 200K after deletion"); |
| 189 | + assertEquals('B', sb.charAt(99_999), "Remaining characters should be correct"); |
| 190 | + assertEquals('B', sb.charAt(100_000), "Deletion should start at correct index"); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + void testRepeatedReplace() { |
| 195 | + CustomStringBuilder sb = new CustomStringBuilder("XXXXX".repeat(200_000)); // 1M chars |
| 196 | + for (int i = 0; i < 10; i++) { |
| 197 | + sb.replace(0, 5, "YYYYY"); // Replace first 5 chars each iteration |
| 198 | + } |
| 199 | + assertEquals('Y', sb.charAt(0), "First character should be 'Y'"); |
| 200 | + assertEquals('Y', sb.charAt(4), "Fifth character should be 'Y'"); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + void testLargeReverse() { |
| 205 | + CustomStringBuilder sb = new CustomStringBuilder(); |
| 206 | + sb.append("1234567890".repeat(100_000)); // 1 million characters |
| 207 | + sb.reverse(); |
| 208 | + assertEquals('0', sb.charAt(0), "First character after reversal should be '0'"); |
| 209 | + assertEquals('1', sb.charAt(sb.length() - 1), "Last character after reversal should be '1'"); |
| 210 | + } |
| 211 | +} |
0 commit comments