-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL2023120245_7_Test.java
More file actions
221 lines (206 loc) · 7.93 KB
/
L2023120245_7_Test.java
File metadata and controls
221 lines (206 loc) · 7.93 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
// 文件名:L2023120245_7_Test.java
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.List;
/**
* 测试用例设计总体原则:
*
* 本测试类遵循以下测试用例设计原则:
* 1. 等价类划分原则:将输入字符串和索引对划分为有效和无效的等价类,通过代表性测试用例覆盖每个类。
* 2. 边界值分析:测试输入字符串的最小长度(如1),以及没有索引对的情况。
* 3. 错误推测法:考虑可能存在的特殊情况,如重复的索引对、自环索引对等。
*/
public class L2023120245_7_Test {
/**
* 测试目的:验证在只有两个索引对的情况下,函数能正确返回最小字典序字符串。
* 测试用例:
* 1. 输入:s = "dcab", pairs = [[0,3],[1,2]]
* 预期输出:"bacd"
*/
@Test
@DisplayName("测试用例1:两个索引对的正常情况")
public void testCase1() {
Solution7 solution = new Solution7();
String s = "dcab";
List<List<Integer>> pairs = Arrays.asList(
Arrays.asList(0, 3),
Arrays.asList(1, 2)
);
String expected = "bacd";
String actual = solution.smallestStringWithSwaps(s, pairs);
assertEquals(expected, actual, "应返回 'bacd'");
}
/**
* 测试目的:验证在多个索引对形成一个连通分量的情况下,函数能正确返回最小字典序字符串。
* 测试用例:
* 1. 输入:s = "dcab", pairs = [[0,3],[1,2],[0,2]]
* 预期输出:"abcd"
*/
@Test
@DisplayName("测试用例2:多个索引对形成一个连通分量")
public void testCase2() {
Solution7 solution = new Solution7();
String s = "dcab";
List<List<Integer>> pairs = Arrays.asList(
Arrays.asList(0, 3),
Arrays.asList(1, 2),
Arrays.asList(0, 2)
);
String expected = "abcd";
String actual = solution.smallestStringWithSwaps(s, pairs);
assertEquals(expected, actual, "应返回 'abcd'");
}
/**
* 测试目的:验证在没有索引对的情况下,函数应返回原始字符串。
* 测试用例:
* 1. 输入:s = "a", pairs = []
* 预期输出:"a"
* 2. 输入:s = "", pairs = []
* 预期输出:""
*/
@Test
@DisplayName("测试用例3:没有索引对的情况")
public void testCase3() {
Solution7 solution = new Solution7();
// 子用例1
String s1 = "a";
List<List<Integer>> pairs1 = Arrays.asList();
String expected1 = "a";
String actual1 = solution.smallestStringWithSwaps(s1, pairs1);
assertEquals(expected1, actual1, "应返回 'a'");
// 子用例2
String s2 = "";
List<List<Integer>> pairs2 = Arrays.asList();
String expected2 = "";
String actual2 = solution.smallestStringWithSwaps(s2, pairs2);
assertEquals(expected2, actual2, "应返回空字符串");
}
/**
* 测试目的:验证在存在重复索引对和自环索引对的情况下,函数能正确返回最小字典序字符串。
* 测试用例:
* 1. 输入:s = "cba", pairs = [[0,1],[1,2],[0,1]]
* 预期输出:"abc"
* 2. 输入:s = "aab", pairs = [[0,0],[1,2]]
* 预期输出:"aab"
*/
@Test
@DisplayName("测试用例4:存在重复索引对和自环索引对")
public void testCase4() {
Solution7 solution = new Solution7();
// 子用例1
String s1 = "cba";
List<List<Integer>> pairs1 = Arrays.asList(
Arrays.asList(0, 1),
Arrays.asList(1, 2),
Arrays.asList(0, 1) // 重复索引对
);
String expected1 = "abc";
String actual1 = solution.smallestStringWithSwaps(s1, pairs1);
assertEquals(expected1, actual1, "应返回 'abc'");
// 子用例2
String s2 = "aab";
List<List<Integer>> pairs2 = Arrays.asList(
Arrays.asList(0, 0), // 自环索引对
Arrays.asList(1, 2)
);
String expected2 = "aab";
String actual2 = solution.smallestStringWithSwaps(s2, pairs2);
assertEquals(expected2, actual2, "应返回 'aab'");
}
/**
* 测试目的:验证在最大输入范围下,函数的性能和正确性。
* 测试用例:
* 1. 输入:s = "zzz...z"(长度为10^5),pairs = []
* 预期输出:"zzz...z"
* (由于长度过长,此处仅描述,实际测试可选用较大的输入)
*/
@Test
@DisplayName("测试用例5:大规模输入")
public void testCase5() {
Solution7 solution = new Solution7();
// 这里为了避免资源消耗,使用较大的字符串而非10^5长度
StringBuilder sb = new StringBuilder();
int largeSize = 10000; // 可以根据实际情况调整
for (int i = 0; i < largeSize; i++) {
sb.append('z');
}
String s = sb.toString();
List<List<Integer>> pairs = Arrays.asList(); // 无索引对
String expected = s;
String actual = solution.smallestStringWithSwaps(s, pairs);
assertEquals(expected, actual, "应返回原始的大规模字符串");
}
/**
* 测试目的:验证函数在索引对覆盖整个字符串时,能否正确排序字符串。
* 测试用例:
* 1. 输入:s = "bca", pairs = [[0,1],[1,2],[0,2]]
* 预期输出:"abc"
*/
@Test
@DisplayName("测试用例6:索引对覆盖整个字符串")
public void testCase6() {
Solution7 solution = new Solution7();
String s = "bca";
List<List<Integer>> pairs = Arrays.asList(
Arrays.asList(0, 1),
Arrays.asList(1, 2),
Arrays.asList(0, 2)
);
String expected = "abc";
String actual = solution.smallestStringWithSwaps(s, pairs);
assertEquals(expected, actual, "应返回 'abc'");
}
/**
* 测试目的:验证函数在存在多个独立连通分量时,能否分别排序各个分量。
* 测试用例:
* 1. 输入:s = "dcab", pairs = [[0,3],[1,2]]
* 预期输出:"bacd"
* 2. 输入:s = "dcabef", pairs = [[0,3],[1,2],[4,5]]
* 预期输出:"bacdef"
*/
@Test
@DisplayName("测试用例7:存在多个独立连通分量")
public void testCase7() {
Solution7 solution = new Solution7();
// 子用例1
String s1 = "dcab";
List<List<Integer>> pairs1 = Arrays.asList(
Arrays.asList(0, 3),
Arrays.asList(1, 2)
);
String expected1 = "bacd";
String actual1 = solution.smallestStringWithSwaps(s1, pairs1);
assertEquals(expected1, actual1, "应返回 'bacd'");
// 子用例2
String s2 = "dcabef";
List<List<Integer>> pairs2 = Arrays.asList(
Arrays.asList(0, 3),
Arrays.asList(1, 2),
Arrays.asList(4, 5)
);
String expected2 = "bacdef";
String actual2 = solution.smallestStringWithSwaps(s2, pairs2);
assertEquals(expected2, actual2, "应返回 'bacdef'");
}
/**
* 测试目的:验证函数在所有字符已经按字典序排列时,保持不变。
* 测试用例:
* 1. 输入:s = "abc", pairs = [[0,1],[1,2]]
* 预期输出:"abc"
*/
@Test
@DisplayName("测试用例8:字符串已按字典序排列")
public void testCase8() {
Solution7 solution = new Solution7();
String s = "abc";
List<List<Integer>> pairs = Arrays.asList(
Arrays.asList(0, 1),
Arrays.asList(1, 2)
);
String expected = "abc";
String actual = solution.smallestStringWithSwaps(s, pairs);
assertEquals(expected, actual, "应保持 'abc' 不变");
}
}