@@ -14,6 +14,7 @@ import org.junit.Test
1414import software.amazon.awssdk.regions.Region
1515import software.amazon.awssdk.services.codewhispererruntime.model.OptOutPreference
1616import software.amazon.awssdk.services.ssooidc.SsoOidcClient
17+ import software.aws.toolkits.core.utils.test.aStringWithLineCount
1718import software.aws.toolkits.jetbrains.core.MockClientManagerRule
1819import software.aws.toolkits.jetbrains.core.credentials.LegacyManagedBearerSsoConnection
1920import software.aws.toolkits.jetbrains.core.credentials.sono.Q_SCOPES
@@ -119,83 +120,45 @@ class CodeWhispererUtilTest {
119120
120121 @Test
121122 fun `toCodeChunk case_2` () {
122- val psiFile = fixture.configureByText(" Sample.java" , codeSample33Lines)
123+ val fakeCodeWith210Lines = aStringWithLineCount(210 )
124+ val psiFile = fixture.configureByText(" Sample.java" , fakeCodeWith210Lines)
123125
124126 val result = runBlocking {
125127 psiFile.virtualFile.toCodeChunk(" fake/path" )
126128 }.toList()
127129
128- assertThat(result).hasSize(5 )
130+ // 210 / 50 + 2
131+ assertThat(result).hasSize(6 )
129132
130133 // 0th
131- assertThat(result[0 ].content).isEqualTo(
132- """ public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
133- | int middle = low + ((high - low) / 2);
134- """ .trimMargin()
135- )
134+ assertThat(result[0 ].content).isEqualTo(aStringWithLineCount(3 ))
136135 assertThat(result[0 ].path).isEqualTo(" fake/path" )
137- assertThat(result[0 ].nextChunk).isEqualTo(result[ 1 ].content )
136+ assertThat(result[0 ].nextChunk).isEqualTo(aStringWithLineCount( 50 , start = 0 ) )
138137
139138 // 1st
140- assertThat(result[1 ].content).isEqualTo(
141- """ |public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
142- | int middle = low + ((high - low) / 2);
143- |
144- | if (high < low) {
145- | return -1;
146- | }
147- |
148- | if (key == sortedArray[middle]) {
149- | return middle;
150- | } else if (key < sortedArray[middle]) {
151- """ .trimMargin()
152- )
139+ assertThat(result[1 ].content).isEqualTo(aStringWithLineCount(50 , start = 0 ))
153140 assertThat(result[1 ].path).isEqualTo(" fake/path" )
154- assertThat(result[1 ].nextChunk).isEqualTo(result[ 2 ].content )
141+ assertThat(result[1 ].nextChunk).isEqualTo(aStringWithLineCount( 50 , start = 50 ) )
155142
156143 // 2nd
157- assertThat(result[2 ].content).isEqualTo(
158- """ | return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
159- | } else {
160- | return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
161- | }
162- |}
163- |
164- |public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
165- | int index = Integer.MAX_VALUE;
166- |
167- | while (low <= high) {
168- """ .trimMargin()
169- )
144+ assertThat(result[2 ].content).isEqualTo(aStringWithLineCount(50 , start = 50 ))
170145 assertThat(result[2 ].path).isEqualTo(" fake/path" )
171- assertThat(result[2 ].nextChunk).isEqualTo(result[ 3 ].content )
146+ assertThat(result[2 ].nextChunk).isEqualTo(aStringWithLineCount( 50 , start = 100 ) )
172147
173148 // 3rd
174- assertThat(result[3 ].content).isEqualTo(
175- """ | int mid = low + ((high - low) / 2);
176- | if (sortedArray[mid] < key) {
177- | low = mid + 1;
178- | } else if (sortedArray[mid] > key) {
179- | high = mid - 1;
180- | } else if (sortedArray[mid] == key) {
181- | index = mid;
182- | break;
183- | }
184- | }
185- """ .trimMargin()
186- )
149+ assertThat(result[3 ].content).isEqualTo(aStringWithLineCount(50 , start = 100 ))
187150 assertThat(result[3 ].path).isEqualTo(" fake/path" )
188- assertThat(result[3 ].nextChunk).isEqualTo(result[ 4 ].content )
151+ assertThat(result[3 ].nextChunk).isEqualTo(aStringWithLineCount( 50 , start = 150 ) )
189152
190153 // 4th
191- assertThat(result[4 ].content).isEqualTo(
192- """ |
193- | return index;
194- |}
195- """ .trimMargin()
196- )
154+ assertThat(result[4 ].content).isEqualTo(aStringWithLineCount(50 , start = 150 ))
197155 assertThat(result[4 ].path).isEqualTo(" fake/path" )
198- assertThat(result[4 ].nextChunk).isEqualTo(result[4 ].content)
156+ assertThat(result[4 ].nextChunk).isEqualTo(aStringWithLineCount(10 , start = 200 ))
157+
158+ // 5th
159+ assertThat(result[5 ].content).isEqualTo(aStringWithLineCount(10 , start = 200 ))
160+ assertThat(result[5 ].path).isEqualTo(" fake/path" )
161+ assertThat(result[5 ].nextChunk).isEqualTo(aStringWithLineCount(10 , start = 200 ))
199162 }
200163
201164 @Test
@@ -231,7 +194,7 @@ class CodeWhispererUtilTest {
231194 }
232195}
233196
234- private val codeSample33Lines =
197+ private val codeSample110Lines =
235198 """ public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
236199 | int middle = low + ((high - low) / 2);
237200 |
@@ -266,4 +229,80 @@ private val codeSample33Lines =
266229 | return index;
267230 |}
268231 |
232+ |public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
233+ | int index = Integer.MAX_VALUE;
234+ |
235+ | while (low <= high) {
236+ | int mid = low + ((high - low) / 2);
237+ | if (sortedArray[mid] < key) {
238+ | low = mid + 1;
239+ | } else if (sortedArray[mid] > key) {
240+ | high = mid - 1;
241+ | } else if (sortedArray[mid] == key) {
242+ | index = mid;
243+ | break;
244+ | }
245+ | }
246+ |
247+ | return index;
248+ |}
249+ |
250+ |public void mergeSort(int[] array) {
251+ | if (array.length > 1) {
252+ | int[] left = Arrays.copyOfRange(array, 0, array.length / 2);
253+ | int[] right = Arrays.copyOfRange(array, array.length / 2, array.length);
254+ |
255+ | mergeSort(left);
256+ | mergeSort(right);
257+ |
258+ | int i = 0;
259+ | int j = 0;
260+ | int k = 0;
261+ |
262+ | while (i < left.length && j < right.length) {
263+ | if (left[i] < right[j]) {
264+ | array[k++] = left[i++];
265+ | } else {
266+ | array[k++] = right[j++];
267+ | }
268+ | }
269+ |
270+ | while (i < left.length) {
271+ | array[k++] = left[i++];
272+ | }
273+ |
274+ | while (j < right.length) {
275+ | array[k++] = right[j++];
276+ | }
277+ | }
278+ |}
279+ |
280+ |public void bubbleSort(int[] array) {
281+ | boolean isSorted = false;
282+ | int lastUnsorted = array.length - 1;
283+ | while (!isSorted) {
284+ | isSorted = true;
285+ | for (int i = 0; i < lastUnsorted; i++) {
286+ | if (array[i] > array[i + 1]) {
287+ | int temp = array[i];
288+ | array[i] = array[i + 1];
289+ | array[i + 1] = temp;
290+ | isSorted = false;
291+ | }
292+ | }
293+ | lastUnsorted--;
294+ | }
295+ |}
296+ |
297+ |public void insertionSort(int[] array) {
298+ | for (int i = 1; i < array.length; i++) {
299+ | int current = array[i];
300+ | int j = i - 1;
301+ | while (j >= 0 && array[j] > current) {
302+ | array[j + 1] = array[j];
303+ | j--;
304+ | }
305+ | array[j + 1] = current;
306+ | }
307+ |}
269308 """ .trimMargin()
0 commit comments