Skip to content

Commit 2e235f5

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents 41d0cdb + 2643ab5 commit 2e235f5

File tree

10 files changed

+152
-58
lines changed

10 files changed

+152
-58
lines changed

.github/workflows/infer.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
name: Infer
3+
4+
'on':
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- master
9+
pull_request:
10+
11+
jobs:
12+
run_infer:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up JDK
18+
uses: actions/setup-java@v4
19+
with:
20+
java-version: 21
21+
distribution: 'adopt'
22+
23+
- name: Set up OCaml
24+
uses: ocaml/setup-ocaml@v3
25+
with:
26+
ocaml-compiler: 5
27+
28+
- name: Get current year/weak
29+
run: echo "year_week=$(date +'%Y_%U')" >> $GITHUB_ENV
30+
31+
- name: Cache infer build
32+
id: cache-infer
33+
uses: actions/cache@v4
34+
with:
35+
path: infer
36+
key: ${{ runner.os }}-infer-${{ env.year_week }}
37+
38+
- name: Build infer
39+
if: steps.cache-infer.outputs.cache-hit != 'true'
40+
run: |
41+
cd ..
42+
git clone https://github.com/facebook/infer.git
43+
cd infer
44+
./build-infer.sh java
45+
cp -r infer ../Java
46+
47+
- name: Add infer to PATH
48+
run: |
49+
echo "infer/bin" >> $GITHUB_PATH
50+
51+
- name: Display infer version
52+
run: |
53+
which infer
54+
infer --version
55+
56+
- name: Run infer
57+
run: |
58+
mvn clean
59+
infer --fail-on-issue --print-logs --no-progress-bar -- mvn test
60+
...

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ local.properties
4242
gradle.properties
4343
.vscode
4444
*.log
45+
46+
/infer-out/

.inferconfig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"report-block-list-path-regex": [
3+
"src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java",
4+
"src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java",
5+
"src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java",
6+
"src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java",
7+
"src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java",
8+
"src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java",
9+
"src/main/java/com/thealgorithms/datastructures/trees/CreateBinaryTreeFromInorderPreorder.java",
10+
"src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java",
11+
"src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java",
12+
"src/main/java/com/thealgorithms/maths/NthUglyNumber.java",
13+
"src/main/java/com/thealgorithms/maths/SimpsonIntegration.java",
14+
"src/main/java/com/thealgorithms/others/Dijkstra.java",
15+
"src/main/java/com/thealgorithms/sorts/TopologicalSort.java",
16+
"src/main/java/com/thealgorithms/strings/AhoCorasick.java",
17+
"src/test/java/com/thealgorithms/datastructures/caches/LRUCacheTest.java",
18+
"src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java",
19+
"src/test/java/com/thealgorithms/datastructures/trees/KDTreeTest.java",
20+
"src/test/java/com/thealgorithms/datastructures/trees/LazySegmentTreeTest.java",
21+
"src/test/java/com/thealgorithms/searches/QuickSelectTest.java",
22+
"src/test/java/com/thealgorithms/stacks/PostfixToInfixTest.java",
23+
"src/test/java/com/thealgorithms/strings/HorspoolSearchTest.java"
24+
]
25+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<plugin>
126126
<groupId>com.github.spotbugs</groupId>
127127
<artifactId>spotbugs-maven-plugin</artifactId>
128-
<version>4.8.6.3</version>
128+
<version>4.8.6.4</version>
129129
<configuration>
130130
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
131131
<includeTests>true</includeTests>

src/main/java/com/thealgorithms/conversions/RomanToInteger.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ private RomanToInteger() {
1919
}
2020
};
2121

22+
private static int romanSymbolToInt(final char symbol) {
23+
return ROMAN_TO_INT.computeIfAbsent(symbol, c -> { throw new IllegalArgumentException("Unknown Roman symbol: " + c); });
24+
}
25+
2226
// Roman Number = Roman Numerals
2327

2428
/**
@@ -39,10 +43,10 @@ public static int romanToInt(String a) {
3943

4044
if (prev != ' ') {
4145
// checking current Number greater than previous or not
42-
newPrev = ROMAN_TO_INT.get(prev) > newPrev ? ROMAN_TO_INT.get(prev) : newPrev;
46+
newPrev = romanSymbolToInt(prev) > newPrev ? romanSymbolToInt(prev) : newPrev;
4347
}
4448

45-
int currentNum = ROMAN_TO_INT.get(c);
49+
int currentNum = romanSymbolToInt(c);
4650

4751
// if current number greater than prev max previous then add
4852
if (currentNum >= newPrev) {
@@ -57,9 +61,4 @@ public static int romanToInt(String a) {
5761

5862
return sum;
5963
}
60-
61-
public static void main(String[] args) {
62-
int sum = romanToInt("MDCCCIV");
63-
System.out.println(sum);
64-
}
6564
}

src/main/java/com/thealgorithms/conversions/UnitsConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.HashMap;
44
import java.util.HashSet;
55
import java.util.Map;
6+
import java.util.NoSuchElementException;
67
import java.util.Set;
78
import org.apache.commons.lang3.tuple.Pair;
89

@@ -77,7 +78,7 @@ public double convert(final String inputUnit, final String outputUnit, final dou
7778
throw new IllegalArgumentException("inputUnit must be different from outputUnit.");
7879
}
7980
final var conversionKey = Pair.of(inputUnit, outputUnit);
80-
return conversions.get(conversionKey).convert(value);
81+
return conversions.computeIfAbsent(conversionKey, k -> { throw new NoSuchElementException("No converter for: " + k); }).convert(value);
8182
}
8283

8384
public Set<String> availableUnits() {

src/main/java/com/thealgorithms/misc/ColorContrastRatio.java

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
* calculate contrast ratio between colors on the web. This is used to calculate
88
* the readability of a foreground color on top of a background color.
99
* @since 2020-10-15
10-
* @see [Color Contrast
11-
* Ratio](https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure)
10+
* @see <a href="https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure">Color Contrast Ratio</a>
1211
* @author [Seth Falco](https://github.com/SethFalco)
1312
*/
1413
public class ColorContrastRatio {
@@ -34,8 +33,7 @@ public double getContrastRatio(Color a, Color b) {
3433
* @brief Calculates the relative luminance of a given color.
3534
* @param color Any color, used to get the red, green, and blue values.
3635
* @return The relative luminance of the color.
37-
* @see [More info on relative
38-
* luminance.](https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef)
36+
* @see <a href="https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef">More info on relative luminance.</a>
3937
*/
4038
public double getRelativeLuminance(Color color) {
4139
final double red = getColor(color.getRed());
@@ -46,63 +44,21 @@ public double getRelativeLuminance(Color color) {
4644
}
4745

4846
/**
49-
* @brief Calculates the final value for a color to be used in the relative
50-
* luminance formula as described in step 1.
47+
* @brief Calculates the final value for a color to be used in the relative luminance formula as described in step 1.
5148
* @param color8Bit 8-bit representation of a color component value.
52-
* @return Value for the provided color component to be used in the relative
53-
* luminance formula.
49+
* @return Value for the provided color component to be used in the relative luminance formula.
5450
*/
5551
public double getColor(int color8Bit) {
5652
final double sRgb = getColorSRgb(color8Bit);
5753
return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4);
5854
}
5955

6056
/**
61-
* @brief Calculates the Color sRGB value as denoted in step 1 of the
62-
* procedure document.
57+
* @brief Calculates the Color sRGB value as denoted in step 1 of the procedure document.
6358
* @param color8Bit 8-bit representation of a color component value.
6459
* @return A percentile value of the color component.
6560
*/
6661
private double getColorSRgb(double color8Bit) {
6762
return color8Bit / 255.0;
6863
}
69-
70-
/**
71-
* You can check this example against another open-source implementation
72-
* available on GitHub.
73-
*
74-
* @see [Online Contrast
75-
* Ratio](https://contrast-ratio.com/#rgb%28226%2C%20229%2C%20248-on-rgb%2823%2C%20103%2C%20154%29)
76-
* @see [GitHub Repository for Online Contrast
77-
* Ratio](https://github.com/LeaVerou/contrast-ratio)
78-
*/
79-
private static void test() {
80-
final ColorContrastRatio algImpl = new ColorContrastRatio();
81-
82-
final Color black = Color.BLACK;
83-
final double blackLuminance = algImpl.getRelativeLuminance(black);
84-
assert blackLuminance == 0 : "Test 1 Failed - Incorrect relative luminance.";
85-
86-
final Color white = Color.WHITE;
87-
final double whiteLuminance = algImpl.getRelativeLuminance(white);
88-
assert whiteLuminance == 1 : "Test 2 Failed - Incorrect relative luminance.";
89-
90-
final double highestColorRatio = algImpl.getContrastRatio(black, white);
91-
assert highestColorRatio == 21 : "Test 3 Failed - Incorrect contrast ratio.";
92-
93-
final Color foreground = new Color(23, 103, 154);
94-
final double foregroundLuminance = algImpl.getRelativeLuminance(foreground);
95-
assert foregroundLuminance == 0.12215748057375966 : "Test 4 Failed - Incorrect relative luminance.";
96-
97-
final Color background = new Color(226, 229, 248);
98-
final double backgroundLuminance = algImpl.getRelativeLuminance(background);
99-
assert backgroundLuminance == 0.7898468477881603 : "Test 5 Failed - Incorrect relative luminance.";
100-
101-
final double contrastRatio = algImpl.getContrastRatio(foreground, background);
102-
assert contrastRatio == 4.878363954846178 : "Test 6 Failed - Incorrect contrast ratio.";
103-
}
104-
105-
public static void main(String[] args) {
106-
test();
107-
}
10864
}

src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

@@ -10,5 +11,13 @@ public class RomanToIntegerTest {
1011
public void testRomanToInteger() {
1112
assertEquals(1994, RomanToInteger.romanToInt("MCMXCIV"));
1213
assertEquals(58, RomanToInteger.romanToInt("LVIII"));
14+
assertEquals(1804, RomanToInteger.romanToInt("MDCCCIV"));
15+
}
16+
17+
@Test
18+
void testRomanToIntegerThrows() {
19+
assertThrows(IllegalArgumentException.class, () -> RomanToInteger.romanToInt("Z"));
20+
assertThrows(IllegalArgumentException.class, () -> RomanToInteger.romanToInt("MZI"));
21+
assertThrows(IllegalArgumentException.class, () -> RomanToInteger.romanToInt("MMMO"));
1322
}
1423
}

src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

66
import java.util.Map;
7+
import java.util.NoSuchElementException;
78
import org.apache.commons.lang3.tuple.Pair;
89
import org.junit.jupiter.api.Test;
910

@@ -15,4 +16,12 @@ void testConvertThrowsForSameUnits() {
1516
assertThrows(IllegalArgumentException.class, () -> someConverter.convert("A", "A", 20.0));
1617
assertThrows(IllegalArgumentException.class, () -> someConverter.convert("B", "B", 20.0));
1718
}
19+
20+
@Test
21+
void testConvertThrowsForUnknownUnits() {
22+
final UnitsConverter someConverter = new UnitsConverter(Map.ofEntries(entry(Pair.of("A", "B"), new AffineConverter(10.0, -20.0))));
23+
assertThrows(NoSuchElementException.class, () -> someConverter.convert("A", "X", 20.0));
24+
assertThrows(NoSuchElementException.class, () -> someConverter.convert("X", "A", 20.0));
25+
assertThrows(NoSuchElementException.class, () -> someConverter.convert("X", "Y", 20.0));
26+
}
1827
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.awt.Color;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
class ColorContrastRatioTest {
12+
private final ColorContrastRatio colorContrastRationCalculator = new ColorContrastRatio();
13+
14+
static Stream<Arguments> relativeLuminanceProvider() {
15+
return Stream.of(Arguments.of(Color.BLACK, 0.0), Arguments.of(Color.WHITE, 1.0), Arguments.of(new Color(23, 103, 154), 0.12215748057375966), Arguments.of(new Color(226, 229, 248), 0.7898468477881603));
16+
}
17+
18+
static Stream<Arguments> contrastRatioProvider() {
19+
return Stream.of(Arguments.of(Color.BLACK, Color.WHITE, 21.0), Arguments.of(new Color(23, 103, 154), new Color(226, 229, 248), 4.878363954846178));
20+
}
21+
22+
@ParameterizedTest
23+
@MethodSource("relativeLuminanceProvider")
24+
void testGetRelativeLuminance(Color color, double expectedLuminance) {
25+
assertEquals(expectedLuminance, colorContrastRationCalculator.getRelativeLuminance(color), 1e-10);
26+
}
27+
28+
@ParameterizedTest
29+
@MethodSource("contrastRatioProvider")
30+
void testGetContrastRatio(Color a, Color b, double expectedRatio) {
31+
assertEquals(expectedRatio, colorContrastRationCalculator.getContrastRatio(a, b), 1e-10);
32+
}
33+
}

0 commit comments

Comments
 (0)