From bf2f174988f6c82ed4a207f34f4881cf08590c40 Mon Sep 17 00:00:00 2001 From: AlexB Date: Thu, 23 Dec 2021 16:15:34 +0200 Subject: [PATCH 1/2] Linereader Candidate: tests for sorting and potential integer overflow fix added --- .../main/java/org/jline/reader/Candidate.java | 2 +- .../java/org/jline/reader/CandidateTest.java | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 reader/src/test/java/org/jline/reader/CandidateTest.java diff --git a/reader/src/main/java/org/jline/reader/Candidate.java b/reader/src/main/java/org/jline/reader/Candidate.java index 3a204c85b..07da89725 100644 --- a/reader/src/main/java/org/jline/reader/Candidate.java +++ b/reader/src/main/java/org/jline/reader/Candidate.java @@ -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()); } } diff --git a/reader/src/test/java/org/jline/reader/CandidateTest.java b/reader/src/test/java/org/jline/reader/CandidateTest.java new file mode 100644 index 000000000..525443a6d --- /dev/null +++ b/reader/src/test/java/org/jline/reader/CandidateTest.java @@ -0,0 +1,68 @@ +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 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 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 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 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()); + } +} From 4628ff68276798a095daaa845febbd43d530b5be Mon Sep 17 00:00:00 2001 From: AlexB Date: Thu, 23 Dec 2021 17:42:04 +0200 Subject: [PATCH 2/2] test file copyright added --- reader/src/test/java/org/jline/reader/CandidateTest.java | 8 ++++++++ .../test/java/org/jline/reader/LineReaderBuilderTest.java | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/reader/src/test/java/org/jline/reader/CandidateTest.java b/reader/src/test/java/org/jline/reader/CandidateTest.java index 525443a6d..96715a8c9 100644 --- a/reader/src/test/java/org/jline/reader/CandidateTest.java +++ b/reader/src/test/java/org/jline/reader/CandidateTest.java @@ -1,3 +1,11 @@ +/* + * 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; diff --git a/reader/src/test/java/org/jline/reader/LineReaderBuilderTest.java b/reader/src/test/java/org/jline/reader/LineReaderBuilderTest.java index f400b5501..341cdb038 100644 --- a/reader/src/test/java/org/jline/reader/LineReaderBuilderTest.java +++ b/reader/src/test/java/org/jline/reader/LineReaderBuilderTest.java @@ -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.