-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #951 from richarda23/crc64checksumtest
CRC64Checksum test #944
- Loading branch information
Showing
3 changed files
with
130 additions
and
1 deletion.
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
109 changes: 109 additions & 0 deletions
109
biojava-core/src/test/java/org/biojava/nbio/core/util/CRC64ChecksumTest.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,109 @@ | ||
package org.biojava.nbio.core.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.RepeatedTest; | ||
import org.junit.jupiter.api.Test; | ||
/** | ||
* | ||
* @author Richard Adams | ||
*/ | ||
class CRC64ChecksumTest { | ||
CRC64Checksum crc64 = null; | ||
private final String helloInCrc64Hex = "53C1111D27800000"; | ||
private final Long helloInCrc64Decimal = 6035123792567599104L; | ||
|
||
@BeforeEach | ||
void before (){ | ||
crc64 = new CRC64Checksum(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Default value is 0") | ||
void initialBehaviour() { | ||
assertEquals(0, crc64.getValue()); | ||
assertEquals("0000000000000000", crc64.toString()); | ||
} | ||
|
||
@RepeatedTest(10) | ||
void sameInputRepeatedlyGeneratesSameOutput(){ | ||
crc64.update("hello"); | ||
assertEquals(helloInCrc64Decimal, crc64.getValue()); | ||
assertEquals(helloInCrc64Hex, crc64.toString()); | ||
} | ||
|
||
@Test | ||
void afterResettingCrcIsZero(){ | ||
crc64.update("hello"); | ||
crc64.reset(); | ||
assertEquals(0, crc64.getValue()); | ||
} | ||
|
||
@Test | ||
void addingIncrementallyIsSameAsAllAtOnce(){ | ||
crc64.update("h"); | ||
crc64.update("e"); | ||
crc64.update("l"); | ||
crc64.update("l"); | ||
crc64.update("o"); | ||
assertEquals(helloInCrc64Hex, crc64.toString()); | ||
} | ||
|
||
@Test | ||
void allbyteRange (){ | ||
byte [] testBytes = new byte [] {1,2,3,4,5}; | ||
crc64.update(testBytes, 0, testBytes.length); | ||
String allBytesHex = crc64.toString(); | ||
crc64.reset(); | ||
for (byte b: testBytes) { | ||
crc64.update(b); | ||
} | ||
assertEquals(allBytesHex, crc64.toString()); | ||
} | ||
|
||
@Test | ||
void allRangeIsSameAsAllArray (){ | ||
byte [] testBytes = new byte [] {1,2,3,4,5}; | ||
crc64.update(testBytes, 0, testBytes.length); | ||
Long valueFromAllRange = crc64.getValue(); | ||
crc64.reset(); | ||
crc64.update(testBytes); | ||
assertEquals(valueFromAllRange, crc64.getValue()); | ||
} | ||
|
||
@Test | ||
void partialByteRange (){ | ||
byte [] testBytes = new byte [] {1,2,3,4,5}; | ||
crc64.update(testBytes, 2, 1); | ||
String partialBytesHex = crc64.toString(); | ||
crc64.reset(); | ||
crc64.update(testBytes[2]); | ||
assertEquals(partialBytesHex, crc64.toString()); | ||
} | ||
|
||
@Test | ||
void partialByteRangeRejectsInvalidInput (){ | ||
byte [] testBytes = new byte [] {1,2,3,4,5}; | ||
assertAll( | ||
()->assertThrows(IllegalArgumentException.class, | ||
()->crc64.update(testBytes, -1, 0)), | ||
()->assertThrows(IllegalArgumentException.class, | ||
()->crc64.update(testBytes, 0, -1)), | ||
()->assertThrows(IllegalArgumentException.class, | ||
()->crc64.update(testBytes, 0, testBytes.length+1)), | ||
()->assertThrows(IllegalArgumentException.class, | ||
()->crc64.update(testBytes, testBytes.length, 1)) | ||
); | ||
} | ||
|
||
|
||
@Test | ||
void hexStringIsEqualToValue(){ | ||
Long value = Long.parseLong(helloInCrc64Hex, 16); | ||
assertEquals(helloInCrc64Decimal, value); | ||
} | ||
} |