Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -3,12 +3,11 @@ package com.thealgorithms.datastructures.bloomfilter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class BloomFilterTest {
@Test
public void test1(){
BloomFilter<Integer> bloomFilter = new BloomFilter<>(3,10);
public void test1() {
BloomFilter<Integer> bloomFilter = new BloomFilter<>(3, 10);
bloomFilter.insert(3);
bloomFilter.insert(17);
@ -17,8 +16,8 @@ public class BloomFilterTest {
}
@Test
public void test2(){
BloomFilter<String> bloomFilter = new BloomFilter<>(4,20);
public void test2() {
BloomFilter<String> bloomFilter = new BloomFilter<>(4, 20);
bloomFilter.insert("omar");
bloomFilter.insert("mahamid");

View File

@ -6,62 +6,59 @@ import org.junit.jupiter.api.Test;
class LFUCacheTest {
@Test
void testLFUCacheWithIntegerValueShouldPass() {
@Test
void testLFUCacheWithIntegerValueShouldPass() {
LFUCache<Integer, Integer> lfuCache = new LFUCache<>(5);
lfuCache.put(1, 10);
lfuCache.put(2, 20);
lfuCache.put(3, 30);
lfuCache.put(4, 40);
lfuCache.put(5, 50);
LFUCache<Integer, Integer> lfuCache = new LFUCache<>(5);
lfuCache.put(1, 10);
lfuCache.put(2, 20);
lfuCache.put(3, 30);
lfuCache.put(4, 40);
lfuCache.put(5, 50);
//get method call will increase frequency of key 1 by 1
assertEquals(10, lfuCache.get(1));
//get method call will increase frequency of key 1 by 1
assertEquals(10, lfuCache.get(1));
//this operation will remove value with key as 2
lfuCache.put(6, 60);
//this operation will remove value with key as 2
lfuCache.put(6, 60);
//will return null as value with key 2 is now evicted
assertEquals(null, lfuCache.get(2));
//will return null as value with key 2 is now evicted
assertEquals(null, lfuCache.get(2));
//should return 60
assertEquals(60, lfuCache.get(6));
//should return 60
assertEquals(60, lfuCache.get(6));
//this operation will remove value with key as 3
lfuCache.put(7, 70);
//this operation will remove value with key as 3
lfuCache.put(7, 70);
assertEquals(null, lfuCache.get(2));
assertEquals(70, lfuCache.get(7));
}
assertEquals(null, lfuCache.get(2));
assertEquals(70, lfuCache.get(7));
}
@Test
void testLFUCacheWithStringValueShouldPass() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(5);
lfuCache.put(1, "Alpha");
lfuCache.put(2, "Beta");
lfuCache.put(3, "Gamma");
lfuCache.put(4, "Delta");
lfuCache.put(5, "Eplison");
@Test
void testLFUCacheWithStringValueShouldPass() {
//get method call will increase frequency of key 1 by 1
assertEquals("Alpha", lfuCache.get(1));
LFUCache<Integer, String> lfuCache = new LFUCache<>(5);
lfuCache.put(1, "Alpha");
lfuCache.put(2, "Beta");
lfuCache.put(3, "Gamma");
lfuCache.put(4, "Delta");
lfuCache.put(5, "Eplison");
//this operation will remove value with key as 2
lfuCache.put(6, "Digamma");
//get method call will increase frequency of key 1 by 1
assertEquals("Alpha", lfuCache.get(1));
//will return null as value with key 2 is now evicted
assertEquals(null, lfuCache.get(2));
//this operation will remove value with key as 2
lfuCache.put(6, "Digamma");
//should return string Digamma
assertEquals("Digamma", lfuCache.get(6));
//will return null as value with key 2 is now evicted
assertEquals(null, lfuCache.get(2));
//should return string Digamma
assertEquals("Digamma", lfuCache.get(6));
//this operation will remove value with key as 3
lfuCache.put(7, "Zeta");
assertEquals(null, lfuCache.get(2));
assertEquals("Zeta", lfuCache.get(7));
}
//this operation will remove value with key as 3
lfuCache.put(7, "Zeta");
assertEquals(null, lfuCache.get(2));
assertEquals("Zeta", lfuCache.get(7));
}
}

View File

@ -13,11 +13,11 @@ public class LRUCacheTest {
public void putAndGetIntegerValues() {
LRUCache<Integer, Integer> lruCache = new LRUCache<>(SIZE);
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
lruCache.put(i, i);
}
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
assertEquals(i, lruCache.get(i));
}
}
@ -26,11 +26,11 @@ public class LRUCacheTest {
public void putAndGetStringValues() {
LRUCache<String, String> lruCache = new LRUCache<>(SIZE);
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
lruCache.put("key" + i, "value" + i);
}
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
assertEquals("value" + i, lruCache.get("key" + i));
}
}
@ -49,7 +49,7 @@ public class LRUCacheTest {
public void overCapacity() {
LRUCache<Integer, Integer> mruCache = new LRUCache<>(SIZE);
for(int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++) {
mruCache.put(i, i);
}

View File

@ -1,10 +1,10 @@
package com.thealgorithms.datastructures.caches;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
public class MRUCacheTest {
private static final int SIZE = 5;
@ -13,11 +13,11 @@ public class MRUCacheTest {
public void putAndGetIntegerValues() {
MRUCache<Integer, Integer> lruCache = new MRUCache<>(SIZE);
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
lruCache.put(i, i);
}
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
assertEquals(i, lruCache.get(i));
}
}
@ -26,11 +26,11 @@ public class MRUCacheTest {
public void putAndGetStringValues() {
MRUCache<String, String> lruCache = new MRUCache<>(SIZE);
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
lruCache.put("key" + i, "value" + i);
}
for(int i = 0; i < SIZE; i++) {
for (int i = 0; i < SIZE; i++) {
assertEquals("value" + i, lruCache.get("key" + i));
}
}
@ -49,7 +49,7 @@ public class MRUCacheTest {
public void overCapacity() {
MRUCache<Integer, Integer> mruCache = new MRUCache<>(SIZE);
for(int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++) {
mruCache.put(i, i);
}

View File

@ -6,34 +6,40 @@ import org.junit.jupiter.api.Test;
class HamiltonianCycleTest {
private HamiltonianCycle hamiltonianCycle = new HamiltonianCycle();
private HamiltonianCycle hamiltonianCycle = new HamiltonianCycle();
@Test
void testFindHamiltonianCycleShouldReturnHamiltonianCycle() {
int[] expectedArray = {0,1,2,4,3,0};
int[][] inputArray = {
{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0}
};
@Test
void testFindHamiltonianCycleShouldReturnHamiltonianCycle() {
int[] expectedArray = { 0, 1, 2, 4, 3, 0 };
int[][] inputArray = {
{ 0, 1, 0, 1, 0 },
{ 1, 0, 1, 1, 1 },
{ 0, 1, 0, 0, 1 },
{ 1, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 0 },
};
assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray));
}
assertArrayEquals(
expectedArray,
hamiltonianCycle.findHamiltonianCycle(inputArray)
);
}
@Test
void testFindHamiltonianCycleShouldReturnInfinityArray() {
int[] expectedArray = {-1,-1,-1,-1,-1,-1};
@Test
void testFindHamiltonianCycleShouldReturnInfinityArray() {
int[] expectedArray = { -1, -1, -1, -1, -1, -1 };
int[][] inputArray = {
{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0}
};
int[][] inputArray = {
{ 0, 1, 0, 1, 0 },
{ 1, 0, 1, 1, 1 },
{ 0, 1, 0, 0, 1 },
{ 1, 1, 0, 0, 0 },
{ 0, 1, 1, 0, 0 },
};
assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray));
}
assertArrayEquals(
expectedArray,
hamiltonianCycle.findHamiltonianCycle(inputArray)
);
}
}

View File

@ -1,12 +1,11 @@
package com.thealgorithms.datastructures.hashmap;
import com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing;
import java.util.*;
import org.junit.jupiter.api.Test;
class HashMapCuckooHashingTest {
@Test
@ -55,8 +54,7 @@ class HashMapCuckooHashingTest {
int initialSize = hashTable.getNumberOfKeysInTable();
try {
hashTable.deleteKeyFromHashTable(3);
}
catch (Exception e){
} catch (Exception e) {
assertTrue(true);
return;
}
@ -93,12 +91,10 @@ class HashMapCuckooHashingTest {
assertTrue(hashTable.checkTableContainsKey(100));
}
private HashMapCuckooHashing createHashMapCuckooHashing() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
int[] values = {11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222};
int[] values = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222 };
Arrays.stream(values).forEach(hashTable::insertKey2HashTable);
return hashTable;
}
}
}

View File

@ -1,10 +1,11 @@
package com.thealgorithms.datastructures.hashmap.hashing;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class GenericHashMapUsingArrayListTest {
@Test
void testGenericHashmapWhichUsesArrayAndBothKeyAndValueAreStrings() {
GenericHashMapUsingArrayList<String, String> map = new GenericHashMapUsingArrayList<>();
@ -46,4 +47,4 @@ class GenericHashMapUsingArrayListTest {
assertEquals("Washington DC", map.get(101));
assertTrue(map.containsKey(46));
}
}
}

View File

@ -1,10 +1,11 @@
package com.thealgorithms.datastructures.hashmap.hashing;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class GenericHashMapUsingArrayTest {
@Test
void testGenericHashmapWhichUsesArrayAndBothKeyAndValueAreStrings() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
@ -46,4 +47,4 @@ class GenericHashMapUsingArrayTest {
assertEquals("Washington DC", map.get(101));
assertTrue(map.containsKey(46));
}
}
}

View File

@ -3,10 +3,10 @@ package com.thealgorithms.datastructures.heaps;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class FibonacciHeapTest{
public class FibonacciHeapTest {
@Test
void testHeap(){
void testHeap() {
FibonacciHeap fibonacciHeap = new FibonacciHeap();
fibonacciHeap.insert(5);
fibonacciHeap.insert(3);

View File

@ -1,11 +1,10 @@
package com.thealgorithms.datastructures.lists;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class SkipListTest {
@ -70,20 +69,33 @@ class SkipListTest {
@Test
void checkSortedOnLowestLayer() {
SkipList<String> skipList = new SkipList<>();
String[] values = {"d", "b", "a", "c"};
String[] values = { "d", "b", "a", "c" };
Arrays.stream(values).forEach(skipList::add);
print(skipList);
String[] actualOrder = IntStream.range(0, values.length)
.mapToObj(skipList::get)
.toArray(String[]::new);
String[] actualOrder = IntStream
.range(0, values.length)
.mapToObj(skipList::get)
.toArray(String[]::new);
assertArrayEquals(new String[]{"a", "b", "c", "d"}, actualOrder);
assertArrayEquals(new String[] { "a", "b", "c", "d" }, actualOrder);
}
private SkipList<String> createSkipList() {
SkipList<String> skipList = new SkipList<>();
String[] values = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};
String[] values = {
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
};
Arrays.stream(values).forEach(skipList::add);
return skipList;
}

View File

@ -1,24 +1,24 @@
package com.thealgorithms.datastructures.trees;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class KDTreeTest {
KDTree.Point pointOf(int x, int y) {
return new KDTree.Point(new int[]{x, y});
return new KDTree.Point(new int[] { x, y });
}
@Test
void findMin() {
int[][] coordinates = {
{30, 40},
{5, 25},
{70, 70},
{10, 12},
{50, 30},
{35, 45}
{ 30, 40 },
{ 5, 25 },
{ 70, 70 },
{ 10, 12 },
{ 50, 30 },
{ 35, 45 },
};
KDTree kdTree = new KDTree(coordinates);
@ -29,12 +29,12 @@ public class KDTreeTest {
@Test
void delete() {
int[][] coordinates = {
{30, 40},
{5, 25},
{70, 70},
{10, 12},
{50, 30},
{35, 45}
{ 30, 40 },
{ 5, 25 },
{ 70, 70 },
{ 10, 12 },
{ 50, 30 },
{ 35, 45 },
};
KDTree kdTree = new KDTree(coordinates);
@ -46,12 +46,12 @@ public class KDTreeTest {
@Test
void findNearest() {
int[][] coordinates = {
{2, 3},
{5, 4},
{9, 6},
{4, 7},
{8, 1},
{7, 2}
{ 2, 3 },
{ 5, 4 },
{ 9, 6 },
{ 4, 7 },
{ 8, 1 },
{ 7, 2 },
};
KDTree kdTree = new KDTree(coordinates);
@ -60,5 +60,4 @@ public class KDTreeTest {
assertEquals(pointOf(2, 3), kdTree.findNearest(pointOf(1, 1)));
assertEquals(pointOf(5, 4), kdTree.findNearest(pointOf(5, 5)));
}
}

View File

@ -1,14 +1,14 @@
package com.thealgorithms.datastructures.trees;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class LazySegmentTreeTest {
@Test
void build() {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
assertEquals(55, lazySegmentTree.getRoot().getValue());
assertEquals(15, lazySegmentTree.getRoot().getLeft().getValue());
@ -17,7 +17,7 @@ public class LazySegmentTreeTest {
@Test
void update() {
int[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int[] arr = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
assertEquals(10, lazySegmentTree.getRoot().getValue());
@ -36,7 +36,7 @@ public class LazySegmentTreeTest {
@Test
void get() {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
assertEquals(55, lazySegmentTree.getRange(0, 10));
assertEquals(3, lazySegmentTree.getRange(0, 2));
@ -46,7 +46,7 @@ public class LazySegmentTreeTest {
@Test
void updateAndGet() {
int[] arr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int[] arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
for (int i = 0; i < 10; i++) for (int j = i + 1; j < 10; j++) {
@ -56,5 +56,4 @@ public class LazySegmentTreeTest {
assertEquals(0, lazySegmentTree.getRange(i, j));
}
}
}