privatestaticvoid TestCRC32Update() { // public void update(int b) // // Tests for checksums of the byte 0x0 // Check that only the low eight bits of the argument are used.
assertEqual(0xD202EF8DL, CRC32Byte(0x0));
assertEqual(0xD202EF8DL, CRC32Byte(0x0100));
assertEqual(0xD202EF8DL, CRC32Byte(0x010000));
assertEqual(0xD202EF8DL, CRC32Byte(0x01000000));
assertEqual(0xD202EF8DL, CRC32Byte(0xff00));
assertEqual(0xD202EF8DL, CRC32Byte(0xffff00));
assertEqual(0xD202EF8DL, CRC32Byte(0xffffff00));
assertEqual(0xD202EF8DL, CRC32Byte(0x1200));
assertEqual(0xD202EF8DL, CRC32Byte(0x123400));
assertEqual(0xD202EF8DL, CRC32Byte(0x12345600));
assertEqual(0xD202EF8DL, CRC32Byte(Integer.MIN_VALUE));
// Tests for checksums of the byte 0x1 // Check that only the low eight bits of the argument are used.
assertEqual(0xA505DF1BL, CRC32Byte(0x1));
assertEqual(0xA505DF1BL, CRC32Byte(0x0101));
assertEqual(0xA505DF1BL, CRC32Byte(0x010001));
assertEqual(0xA505DF1BL, CRC32Byte(0x01000001));
assertEqual(0xA505DF1BL, CRC32Byte(0xff01));
assertEqual(0xA505DF1BL, CRC32Byte(0xffff01));
assertEqual(0xA505DF1BL, CRC32Byte(0xffffff01));
assertEqual(0xA505DF1BL, CRC32Byte(0x1201));
assertEqual(0xA505DF1BL, CRC32Byte(0x123401));
assertEqual(0xA505DF1BL, CRC32Byte(0x12345601));
// Tests for checksums of the byte 0x0f // Check that only the low eight bits of the argument are used.
assertEqual(0x42BDF21CL, CRC32Byte(0x0f));
assertEqual(0x42BDF21CL, CRC32Byte(0x010f));
assertEqual(0x42BDF21CL, CRC32Byte(0x01000f));
assertEqual(0x42BDF21CL, CRC32Byte(0x0100000f));
assertEqual(0x42BDF21CL, CRC32Byte(0xff0f));
assertEqual(0x42BDF21CL, CRC32Byte(0xffff0f));
assertEqual(0x42BDF21CL, CRC32Byte(0xffffff0f));
assertEqual(0x42BDF21CL, CRC32Byte(0x120f));
assertEqual(0x42BDF21CL, CRC32Byte(0x12340f));
assertEqual(0x42BDF21CL, CRC32Byte(0x1234560f));
// Tests for checksums of the byte 0xff // Check that only the low eight bits of the argument are used.
assertEqual(0xFF000000L, CRC32Byte(0x00ff));
assertEqual(0xFF000000L, CRC32Byte(0x01ff));
assertEqual(0xFF000000L, CRC32Byte(0x0100ff));
assertEqual(0xFF000000L, CRC32Byte(0x010000ff));
assertEqual(0xFF000000L, CRC32Byte(0x0000ffff));
assertEqual(0xFF000000L, CRC32Byte(0x00ffffff));
assertEqual(0xFF000000L, CRC32Byte(0xffffffff));
assertEqual(0xFF000000L, CRC32Byte(0x12ff));
assertEqual(0xFF000000L, CRC32Byte(0x1234ff));
assertEqual(0xFF000000L, CRC32Byte(0x123456ff));
assertEqual(0xFF000000L, CRC32Byte(Integer.MAX_VALUE));
// Tests for sequences // Check that only the low eight bits of the values are used.
assertEqual(0xFF41D912L, CRC32BytesUsingUpdateInt(0, 0, 0));
assertEqual(0xFF41D912L,
CRC32BytesUsingUpdateInt(0x0100, 0x010000, 0x01000000));
assertEqual(0xFF41D912L,
CRC32BytesUsingUpdateInt(0xff00, 0xffff00, 0xffffff00));
assertEqual(0xFF41D912L,
CRC32BytesUsingUpdateInt(0x1200, 0x123400, 0x12345600));
privatestaticlong CRC32ByteArray(byte[] bytes, int off, int len) {
CRC32 crc32 = new CRC32();
crc32.update(bytes, off, len); return crc32.getValue();
}
// This is used to test we generate correct code for constant offsets. // In this case the offset is 0. privatestaticlong CRC32ByteArray(byte[] bytes) {
CRC32 crc32 = new CRC32();
crc32.update(bytes); return crc32.getValue();
}
int off = rnd.nextInt(bytes.length / 2); for (int len = 0; len <= 16; ++len) {
assertEqual(CRC32BytesUsingUpdateInt(bytes, off, len),
CRC32ByteArray(bytes, off, len));
}
// Check there are no issues with unaligned accesses. for (int o = 1; o < 8; ++o) { for (int l = 0; l <= 16; ++l) {
assertEqual(CRC32BytesUsingUpdateInt(bytes, o, l),
CRC32ByteArray(bytes, o, l));
}
}
int len = bytes.length / 2;
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len - 1),
CRC32ByteArray(bytes, 0, len - 1));
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len),
CRC32ByteArray(bytes, 0, len));
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len + 1),
CRC32ByteArray(bytes, 0, len + 1));
len = rnd.nextInt(bytes.length + 1);
off = rnd.nextInt(bytes.length - len);
assertEqual(CRC32BytesUsingUpdateInt(bytes, off, len),
CRC32ByteArray(bytes, off, len));
}
privatestaticlong CRC32ByteBuffer(byte[] bytes, int off, int len) {
ByteBuffer buf = ByteBuffer.wrap(bytes, 0, off + len);
buf.position(off);
CRC32 crc32 = new CRC32();
crc32.update(buf); return crc32.getValue();
}
int off = rnd.nextInt(bytes.length / 2); for (int len = 0; len <= 16; ++len) {
assertEqual(CRC32BytesUsingUpdateInt(bytes, off, len),
CRC32ByteBuffer(bytes, off, len));
}
// Check there are no issues with unaligned accesses. for (int o = 1; o < 8; ++o) { for (int l = 0; l <= 16; ++l) {
assertEqual(CRC32BytesUsingUpdateInt(bytes, o, l),
CRC32ByteBuffer(bytes, o, l));
}
}
int len = bytes.length / 2;
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len - 1),
CRC32ByteBuffer(bytes, 0, len - 1));
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len),
CRC32ByteBuffer(bytes, 0, len));
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len + 1),
CRC32ByteBuffer(bytes, 0, len + 1));
len = rnd.nextInt(bytes.length + 1);
off = rnd.nextInt(bytes.length - len);
assertEqual(CRC32BytesUsingUpdateInt(bytes, off, len),
CRC32ByteBuffer(bytes, off, len));
}
privatestaticlong CRC32DirectByteBuffer(byte[] bytes, int off, int len) { finalint total_len = off + len;
ByteBuffer buf = ByteBuffer.allocateDirect(total_len).put(bytes, 0, total_len);
buf.position(off);
CRC32 crc32 = new CRC32();
crc32.update(buf); return crc32.getValue();
}
int off = rnd.nextInt(bytes.length / 2); for (int len = 0; len <= 16; ++len) {
assertEqual(CRC32BytesUsingUpdateInt(bytes, off, len),
CRC32DirectByteBuffer(bytes, off, len));
}
// Check there are no issues with unaligned accesses. for (int o = 1; o < 8; ++o) { for (int l = 0; l <= 16; ++l) {
assertEqual(CRC32BytesUsingUpdateInt(bytes, o, l),
CRC32DirectByteBuffer(bytes, o, l));
}
}
int len = bytes.length / 2;
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len - 1),
CRC32DirectByteBuffer(bytes, 0, len - 1));
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len),
CRC32DirectByteBuffer(bytes, 0, len));
assertEqual(CRC32BytesUsingUpdateInt(bytes, 0, len + 1),
CRC32DirectByteBuffer(bytes, 0, len + 1));
len = rnd.nextInt(bytes.length + 1);
off = rnd.nextInt(bytes.length - len);
assertEqual(CRC32BytesUsingUpdateInt(bytes, off, len),
CRC32DirectByteBuffer(bytes, off, len));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.