-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bug #1; feat: unit test optimize
- Loading branch information
Showing
7 changed files
with
114 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/test/java/me/ihxq/projects/pna/AlgorithmResultDifferTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package me.ihxq.projects.pna; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import me.ihxq.projects.pna.algorithm.LookupAlgorithm; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.commons.util.ReflectionUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* @author xq.h | ||
* on 2019/10/18 22:30 | ||
**/ | ||
@Slf4j | ||
public class AlgorithmResultDifferTest { | ||
|
||
private boolean isDiff(List<Optional<PhoneNumberInfo>> candidates) { | ||
boolean allPresent = false; | ||
boolean allAbsent = false; | ||
PhoneNumberInfo temp = null; | ||
for (Optional<PhoneNumberInfo> candidate : candidates) { | ||
if (candidate.isPresent()) { | ||
allPresent = true; | ||
if (allAbsent) { | ||
return false; | ||
} | ||
if (temp == null) { | ||
temp = candidate.get(); | ||
} else { | ||
if (!temp.equals(candidate.get())) { | ||
return false; | ||
} else { | ||
temp = candidate.get(); | ||
} | ||
} | ||
} else { | ||
allAbsent = true; | ||
if (allPresent) { | ||
return false; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* test all {@link LookupAlgorithm} implements result whether the same or not | ||
*/ | ||
@Test | ||
public void testDiff() { | ||
List<PhoneNumberLookup> lookups = ReflectionUtils.findAllClassesInPackage("me.ihxq.projects.pna", v -> Arrays.asList(v.getInterfaces()).contains(LookupAlgorithm.class), v -> true) | ||
.stream() | ||
.map(c -> { | ||
try { | ||
LookupAlgorithm algorithm = (LookupAlgorithm) c.newInstance(); | ||
return new PhoneNumberLookup(algorithm); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
.collect(Collectors.toList()); | ||
boolean noneMatch = Stream.generate(() -> { | ||
long phoneNumber = (long) (ThreadLocalRandom.current().nextDouble(1D, 2D) * 1000_000_000_0L); | ||
return String.valueOf(phoneNumber); | ||
}).limit(2_000) | ||
.parallel() | ||
.noneMatch(v -> { | ||
List<Optional<PhoneNumberInfo>> results = lookups.stream() | ||
.map(l -> l.lookup(v)) | ||
.collect(Collectors.toList()); | ||
return isDiff(results); | ||
}); | ||
assertTrue(noneMatch); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
src/test/java/me/ihxq/projects/pna/PhoneNumberLookupTest.java
This file was deleted.
Oops, something went wrong.