Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linereader Candidate: tests for sorting and potential int overflow fix #762

Merged
merged 2 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reader/src/main/java/org/jline/reader/Candidate.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public int compareTo(Candidate o) {
if( sort == o.sort() ) {
return value.compareTo(o.value);
} else {
return sort - o.sort();
return Integer.compare(sort, o.sort());
}
}

Expand Down
76 changes: 76 additions & 0 deletions reader/src/test/java/org/jline/reader/CandidateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2021, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.reader;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class CandidateTest {

@Test
public void testCandidatesSortedByValue() {
List<Candidate> candidates = Arrays.asList(
new Candidate("cand3", "cand3", null, null, null, null, true),
new Candidate("cand1", "cand1", null, null, null, null, true),
new Candidate("cand2", "cand2", null, null, null, null, true)
);
Collections.sort(candidates);

assertEquals("cand1", candidates.get(0).value());
assertEquals("cand2", candidates.get(1).value());
assertEquals("cand3", candidates.get(2).value());
}

@Test
public void testCandidatesSortedBySortProperty() {
List<Candidate> candidates = Arrays.asList(
new Candidate("cand3", "cand3", null, null, null, null, true, 3),
new Candidate("cand1", "cand1", null, null, null, null, true, 1),
new Candidate("cand2", "cand2", null, null, null, null, true, 2)
);
Collections.sort(candidates);

assertEquals("cand1", candidates.get(0).value());
assertEquals("cand2", candidates.get(1).value());
assertEquals("cand3", candidates.get(2).value());
}

@Test
public void testCandidatesSortedByValueAndSortProperty() {
List<Candidate> candidates = Arrays.asList(
new Candidate("cand3", "cand3", null, null, null, null, true, -3),
new Candidate("aaa", "cand1", null, null, null, null, true),
new Candidate("cand2", "cand2", null, null, null, null, true, 2)
);
Collections.sort(candidates);

assertEquals("cand3", candidates.get(0).value());
assertEquals("aaa", candidates.get(1).value());
assertEquals("cand2", candidates.get(2).value());
}

@Test
public void testCandidatesSortedByCornerSortProps() {
List<Candidate> candidates = Arrays.asList(
new Candidate("cand1", "cand1", null, null, null, null, true, 1),
new Candidate("cand2", "cand2", null, null, null, null, true, Integer.MAX_VALUE),
new Candidate("cand3", "cand3", null, null, null, null, true, Integer.MIN_VALUE)
);
Collections.sort(candidates);

assertEquals("cand3", candidates.get(0).value());
assertEquals("cand1", candidates.get(1).value());
assertEquals("cand2", candidates.get(2).value());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 20021, the original author or authors.
* Copyright (c) 2021, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down