Repository navigation
Expand file tree
/
Copy pathL2023111432_2_Test.java
More file actions
245 lines (224 loc) · 8.9 KB
/
Copy pathL2023111432_2_Test.java
File metadata and controls
245 lines (224 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* @description: Solution2类的单元测试
* 测试类命名:L2023111432_2_Test
* 学号:2023111432
* 题号:2
*
* 测试用例设计原则:
* 1. 等价类划分:将输入字符串按不同特征划分为多个等价类
* 2. 边界值分析:测试空字符串、单字符、全相同字符等边界情况
* 3. 典型场景测试:包含典型字典序调整需求的测试用例
* 4. 功能覆盖:确保覆盖所有主要功能路径
*/
public class L2023111432_2_Test {
private static int passed = 0;
private static int failed = 0;
/**
* 自定义断言方法
*/
private static void assertEquals(String message, String expected, String actual) {
if (expected.equals(actual)) {
passed++;
System.out.println(message + " 通过");
} else {
failed++;
System.out.println(message + " 失败");
System.out.println(" 期望: \"" + expected + "\"");
System.out.println(" 实际: \"" + actual + "\"");
}
}
private static void assertNotNull(String message, String actual) {
if (actual != null) {
passed++;
System.out.println(message + " 通过");
} else {
failed++;
System.out.println(message + " 失败");
}
}
private static void assertTrue(String message, boolean condition) {
if (condition) {
passed++;
System.out.println(message + " 通过");
} else {
failed++;
System.out.println(message + " 失败");
}
}
/**
* @test 测试目的:验证基本去重功能,确保重复字符被正确去除
* @test 测试用例:"bcabc" -> "abc"
* 等价类:包含重复字符的普通字符串
* 预期:去除重复字符,保留最小字典序
*/
public void testBasicDuplicateRemoval() {
Solution2 solution = new Solution2();
String input = "bcabc";
String expected = "abc";
String result = solution.removeDuplicateLetters(input);
assertEquals("基本去重功能测试", expected, result);
}
/**
* @test 测试目的:验证复杂情况下的字典序最小化
* @test 测试用例:"cbacdcbc" -> "acdb"
* 等价类:需要调整字符顺序以获得最小字典序的情况
* 预期:在保持相对位置的前提下得到最小字典序
*/
public void testComplexLexicographicalOrder() {
Solution2 solution = new Solution2();
String input = "cbacdcbc";
String expected = "acdb";
String result = solution.removeDuplicateLetters(input);
assertEquals("复杂字典序测试", expected, result);
}
/**
* @test 测试目的:验证无重复字符的字符串处理
* @test 测试用例:"abcdef" -> "abcdef"
* 等价类:无重复字符的字符串
* 预期:原样返回输入字符串
*/
public void testNoDuplicateCharacters() {
Solution2 solution = new Solution2();
String input = "abcdef";
String expected = "abcdef";
String result = solution.removeDuplicateLetters(input);
assertEquals("无重复字符测试", expected, result);
}
/**
* @test 测试目的:验证全相同字符的字符串处理
* @test 测试用例:"aaaaaa" -> "a"
* 等价类:所有字符都相同的字符串
* 预期:返回单个字符
*/
public void testAllSameCharacters() {
Solution2 solution = new Solution2();
String input = "aaaaaa";
String expected = "a";
String result = solution.removeDuplicateLetters(input);
assertEquals("全相同字符测试", expected, result);
}
/**
* @test 测试目的:验证单字符字符串处理
* @test 测试用例:"z" -> "z"
* 边界值:单字符字符串
* 预期:原样返回输入字符串
*/
public void testSingleCharacter() {
Solution2 solution = new Solution2();
String input = "z";
String expected = "z";
String result = solution.removeDuplicateLetters(input);
assertEquals("单字符测试", expected, result);
}
/**
* @test 测试目的:验证已是最小字典序的字符串处理
* @test 测试用例:"abcd" -> "abcd"
* 等价类:已经是最小字典序的字符串
* 预期:去重后保持原顺序
*/
public void testAlreadyLexicographical() {
Solution2 solution = new Solution2();
String input = "abcd";
String expected = "abcd";
String result = solution.removeDuplicateLetters(input);
assertEquals("已是最小字典序测试", expected, result);
}
/**
* @test 测试目的:验证需要复杂调整的字典序情况
* @test 测试用例:"leetcode" -> "letcod"
* 等价类:需要多轮调整的复杂情况
* 预期:正确得到最小字典序结果
*/
public void testComplexCase1() {
Solution2 solution = new Solution2();
String input = "leetcode";
String expected = "letcod";
String result = solution.removeDuplicateLetters(input);
assertEquals("复杂情况1测试", expected, result);
}
/**
* @test 测试目的:验证另一个复杂字典序调整情况
* @test 测试用例:"cdadabcc" -> "adbc"
* 等价类:需要精确判断字符弹出时机的场景
* 预期:正确得到最小字典序结果
*/
public void testComplexCase2() {
Solution2 solution = new Solution2();
String input = "cdadabcc";
String expected = "adbc";
String result = solution.removeDuplicateLetters(input);
assertEquals("复杂情况2测试", expected, result);
}
/**
* @test 测试目的:验证包含连续重复字符的情况
* @test 测试用例:"aabbcc" -> "abc"
* 等价类:连续重复字符的字符串
* 预期:正确去重并保持最小字典序
*/
public void testConsecutiveDuplicates() {
Solution2 solution = new Solution2();
String input = "aabbcc";
String expected = "abc";
String result = solution.removeDuplicateLetters(input);
assertEquals("连续重复字符测试", expected, result);
}
/**
* @test 测试目的:验证长字符串的处理性能
* @test 测试用例:长度为10000的字符串
* 边界值:最大长度边界测试
* 预期:正确处理不超时
*/
public void testLongString() {
Solution2 solution = new Solution2();
// 构造一个长字符串,包含重复字符
StringBuilder longInput = new StringBuilder();
for (int i = 0; i < 1000; i++) {
longInput.append("abcdefghij");
}
String input = longInput.toString();
// 执行测试,主要验证不抛出异常
String result = solution.removeDuplicateLetters(input);
assertNotNull("长字符串测试", result);
assertTrue("长字符串长度测试", result.length() > 0);
}
/**
* @test 测试目的:验证特殊顺序的字符串处理
* @test 测试用例:"zyxwvutsrqponmlkjihgfedcba" -> "zyxwvutsrqponmlkjihgfedcba"
* 等价类:逆序排列的字符串
* 预期:去重后保持逆序(因为已经是相对位置下的最小字典序)
*/
public void testReverseOrder() {
Solution2 solution = new Solution2();
String input = "zyxwvutsrqponmlkjihgfedcba";
String expected = "zyxwvutsrqponmlkjihgfedcba";
String result = solution.removeDuplicateLetters(input);
assertEquals("逆序字符串测试", expected, result);
}
/**
* 主方法 - 运行所有测试
*/
public static void main(String[] args) {
L2023111432_2_Test test = new L2023111432_2_Test();
System.out.println("开始运行 Solution2 单元测试...\n");
// 运行所有测试方法
test.testBasicDuplicateRemoval();
test.testComplexLexicographicalOrder();
test.testNoDuplicateCharacters();
test.testAllSameCharacters();
test.testSingleCharacter();
test.testAlreadyLexicographical();
test.testComplexCase1();
test.testComplexCase2();
test.testConsecutiveDuplicates();
test.testLongString();
test.testReverseOrder();
System.out.println("\n测试完成!");
System.out.println("通过: " + passed + ", 失败: " + failed);
System.out.println("成功率: " + (passed * 100 / (passed + failed)) + "%");
if (failed == 0) {
System.out.println("所有测试用例通过!");
} else {
System.out.println("有测试用例失败,请检查代码");
}
}
}