Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed Nov 12, 2022
1 parent 6791f19 commit 3e8f876
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 103 deletions.
71 changes: 71 additions & 0 deletions hashers/test/__snapshots__/blake2b.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
export const snapshot = {};

snapshot[`blake2b 512 > empty > reference 1`] = `"786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"`;

snapshot[`blake2b 512 > [0; 1024] > reference 1`] = `"b4b72b45c308e963f4c927827426228c0ed898403411ad108fbd0611e146ccd494bda4b9a593c33d7cf49931748e8bc29a829d50e904305cb38dfb1443532ef7"`;

snapshot[`blake2b 512 > 0..256 > reference 1`] = `"1ecc896f34d3f9cac484c73f75f6a5fb58ee6784be41b35f46067b9c65c63a6794d3d744112c653f73dd7deb6666204c5a9bfa5b46081fc10fdbe7884fa5cbf8"`;

snapshot[`blake2b 512 > lorem > reference 1`] = `"bf6a2cf4132a6436f90190ebbf436dd622a73294d44348046c32504c224aaa3d1c80ca83107eb9548ccba36e141183b3bfb0f38779c0d9ab76b5971590e79d5e"`;

snapshot[`blake2b 512 > license > reference 1`] = `"992fa23952ba982b26a74a176c9602c4e4e9e1dc60dacf04df6f33bf631964b70c676c98a73270a4356abdad2a38709ac8a930460e84f101dc5cdfd3e7cccf1a"`;

snapshot[`blake2b 8 > empty > reference 1`] = `"2e"`;

snapshot[`blake2b 8 > [0; 1024] > reference 1`] = `"ed"`;

snapshot[`blake2b 8 > 0..256 > reference 1`] = `"31"`;

snapshot[`blake2b 8 > lorem > reference 1`] = `"f7"`;

snapshot[`blake2b 8 > license > reference 1`] = `"71"`;

snapshot[`blake2b 16 > empty > reference 1`] = `"b1fe"`;

snapshot[`blake2b 16 > [0; 1024] > reference 1`] = `"2765"`;

snapshot[`blake2b 16 > 0..256 > reference 1`] = `"8c4d"`;

snapshot[`blake2b 16 > lorem > reference 1`] = `"b3cf"`;

snapshot[`blake2b 16 > license > reference 1`] = `"7687"`;

snapshot[`blake2b 32 > empty > reference 1`] = `"1271cf25"`;

snapshot[`blake2b 32 > [0; 1024] > reference 1`] = `"0cd38b2c"`;

snapshot[`blake2b 32 > 0..256 > reference 1`] = `"0f2e5c3b"`;

snapshot[`blake2b 32 > lorem > reference 1`] = `"1ac49e34"`;

snapshot[`blake2b 32 > license > reference 1`] = `"2d5fdc03"`;

snapshot[`blake2b 64 > empty > reference 1`] = `"e4a6a0577479b2b4"`;

snapshot[`blake2b 64 > [0; 1024] > reference 1`] = `"cc6d171260b47f4c"`;

snapshot[`blake2b 64 > 0..256 > reference 1`] = `"2b2cedfed655ad3f"`;

snapshot[`blake2b 64 > lorem > reference 1`] = `"4c88d05abcf28cc9"`;

snapshot[`blake2b 64 > license > reference 1`] = `"a3fa5fbc7ae44daa"`;

snapshot[`blake2b 128 > empty > reference 1`] = `"cae66941d9efbd404e4d88758ea67670"`;

snapshot[`blake2b 128 > [0; 1024] > reference 1`] = `"b369bf5cbc592281f47f17906c077fe2"`;

snapshot[`blake2b 128 > 0..256 > reference 1`] = `"c2472c0ac37a8dbdb25f05ada0d82643"`;

snapshot[`blake2b 128 > lorem > reference 1`] = `"2f4ed80c9a9bd02ea97ff9bde5a1c89f"`;

snapshot[`blake2b 128 > license > reference 1`] = `"be52669a304127adc627508500703483"`;

snapshot[`blake2b 256 > empty > reference 1`] = `"0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8"`;

snapshot[`blake2b 256 > [0; 1024] > reference 1`] = `"347ebd71659fe9f2bc7c182fb475b03112785953498185042565590a1bfb89a2"`;

snapshot[`blake2b 256 > 0..256 > reference 1`] = `"39a7eb9fedc19aabc83425c6755dd90e6f9d0c804964a1f4aaeea3b9fb599835"`;

snapshot[`blake2b 256 > lorem > reference 1`] = `"d6d9034f61e2f7ada6e58c252e15684c8df7f0b197a95d80f42ca0a3685de26e"`;

snapshot[`blake2b 256 > license > reference 1`] = `"3ee00dfbab41f09838f0444605dbef691486244f4ca29f90957a894b44153663"`;
61 changes: 61 additions & 0 deletions hashers/test/__snapshots__/xxhash.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export const snapshot = {};

snapshot[`xxhash 512 > empty > reference 1`] = `"99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203adb47db9f0e7b8df13bfd1c9806d4e14b99c769b2d80a6fdf09440a6f6f62f095"`;

snapshot[`xxhash 512 > [0; 1024] > reference 1`] = `"cdac85f0882874275d7ee38a70da85e98ed188fdabc8466b923b4e0a88c21e18411f08ee10a208b4dd712c68cfeecf68fabd60b05467e45268807a47a3359f41"`;

snapshot[`xxhash 512 > 0..256 > reference 1`] = `"4b90cd0684beac1ff7566a6db01eecbe796a6681a553fa6f4ca7f8a1da4c7241822eb628b1ece95206ada4385fb1f2d08e2d2f953b2bcc4aa86e0ec82ef633d2"`;

snapshot[`xxhash 512 > lorem > reference 1`] = `"3056764314b1a8c5331511d478ff3f1203586c1a9308a54594234d9956fa6173dde73c6f4d92a266b80f4ea640fc58880baa7ba549315bf42b06d60439cdd4cc"`;

snapshot[`xxhash 512 > license > reference 1`] = `"f8221dfc889a7a5c51aa83f994a21dfcd50ec17435b60d9835bce3c6dfd4f39aa7f95091df2c93cb5f1a5e6a4aa6db95afbf6ee35bcbb915bbfe02625a834561"`;

snapshot[`xxhash 64 > empty > reference 1`] = `"99e9d85137db46ef"`;

snapshot[`xxhash 64 > [0; 1024] > reference 1`] = `"cdac85f088287427"`;

snapshot[`xxhash 64 > 0..256 > reference 1`] = `"4b90cd0684beac1f"`;

snapshot[`xxhash 64 > lorem > reference 1`] = `"3056764314b1a8c5"`;

snapshot[`xxhash 64 > license > reference 1`] = `"f8221dfc889a7a5c"`;

snapshot[`xxhash 128 > empty > reference 1`] = `"99e9d85137db46ef4bbea33613baafd5"`;

snapshot[`xxhash 128 > [0; 1024] > reference 1`] = `"cdac85f0882874275d7ee38a70da85e9"`;

snapshot[`xxhash 128 > 0..256 > reference 1`] = `"4b90cd0684beac1ff7566a6db01eecbe"`;

snapshot[`xxhash 128 > lorem > reference 1`] = `"3056764314b1a8c5331511d478ff3f12"`;

snapshot[`xxhash 128 > license > reference 1`] = `"f8221dfc889a7a5c51aa83f994a21dfc"`;

snapshot[`xxhash 256 > empty > reference 1`] = `"99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a"`;

snapshot[`xxhash 256 > [0; 1024] > reference 1`] = `"cdac85f0882874275d7ee38a70da85e98ed188fdabc8466b923b4e0a88c21e18"`;

snapshot[`xxhash 256 > 0..256 > reference 1`] = `"4b90cd0684beac1ff7566a6db01eecbe796a6681a553fa6f4ca7f8a1da4c7241"`;

snapshot[`xxhash 256 > lorem > reference 1`] = `"3056764314b1a8c5331511d478ff3f1203586c1a9308a54594234d9956fa6173"`;

snapshot[`xxhash 256 > license > reference 1`] = `"f8221dfc889a7a5c51aa83f994a21dfcd50ec17435b60d9835bce3c6dfd4f39a"`;

snapshot[`xxhash 384 > empty > reference 1`] = `"99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203adb47db9f0e7b8df13bfd1c9806d4e14b"`;

snapshot[`xxhash 384 > [0; 1024] > reference 1`] = `"cdac85f0882874275d7ee38a70da85e98ed188fdabc8466b923b4e0a88c21e18411f08ee10a208b4dd712c68cfeecf68"`;

snapshot[`xxhash 384 > 0..256 > reference 1`] = `"4b90cd0684beac1ff7566a6db01eecbe796a6681a553fa6f4ca7f8a1da4c7241822eb628b1ece95206ada4385fb1f2d0"`;

snapshot[`xxhash 384 > lorem > reference 1`] = `"3056764314b1a8c5331511d478ff3f1203586c1a9308a54594234d9956fa6173dde73c6f4d92a266b80f4ea640fc5888"`;

snapshot[`xxhash 384 > license > reference 1`] = `"f8221dfc889a7a5c51aa83f994a21dfcd50ec17435b60d9835bce3c6dfd4f39aa7f95091df2c93cb5f1a5e6a4aa6db95"`;

snapshot[`xxhash 512 > empty > reference 2`] = `"99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203adb47db9f0e7b8df13bfd1c9806d4e14b99c769b2d80a6fdf09440a6f6f62f095"`;

snapshot[`xxhash 512 > [0; 1024] > reference 2`] = `"cdac85f0882874275d7ee38a70da85e98ed188fdabc8466b923b4e0a88c21e18411f08ee10a208b4dd712c68cfeecf68fabd60b05467e45268807a47a3359f41"`;

snapshot[`xxhash 512 > 0..256 > reference 2`] = `"4b90cd0684beac1ff7566a6db01eecbe796a6681a553fa6f4ca7f8a1da4c7241822eb628b1ece95206ada4385fb1f2d08e2d2f953b2bcc4aa86e0ec82ef633d2"`;

snapshot[`xxhash 512 > lorem > reference 2`] = `"3056764314b1a8c5331511d478ff3f1203586c1a9308a54594234d9956fa6173dde73c6f4d92a266b80f4ea640fc58880baa7ba549315bf42b06d60439cdd4cc"`;

snapshot[`xxhash 512 > license > reference 2`] = `"f8221dfc889a7a5c51aa83f994a21dfcd50ec17435b60d9835bce3c6dfd4f39aa7f95091df2c93cb5f1a5e6a4aa6db95afbf6ee35bcbb915bbfe02625a834561"`;
54 changes: 6 additions & 48 deletions hashers/test/blake2b.test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,11 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"
import * as refImpl from "https://esm.sh/@noble/[email protected]/blake2b"
import { encodeHex } from "../../util.ts"
import { Blake2b } from "../blake2b.ts"
import { testHasher } from "./test_util.ts"

const lorem =
// cspell:disable-next-line
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

const hashes: [name: string, data: Uint8Array, hash: string][] = [
[
"empty",
new Uint8Array(),
"786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce",
],
[
"[0; 1024]",
new Uint8Array(1024),
"b4b72b45c308e963f4c927827426228c0ed898403411ad108fbd0611e146ccd494bda4b9a593c33d7cf49931748e8bc29a829d50e904305cb38dfb1443532ef7",
],
[
"0..256",
new Uint8Array(Array.from({ length: 256 }, (_, i) => i)),
"1ecc896f34d3f9cac484c73f75f6a5fb58ee6784be41b35f46067b9c65c63a6794d3d744112c653f73dd7deb6666204c5a9bfa5b46081fc10fdbe7884fa5cbf8",
],
[
"lorem",
new TextEncoder().encode(lorem),
"bf6a2cf4132a6436f90190ebbf436dd622a73294d44348046c32504c224aaa3d1c80ca83107eb9548ccba36e141183b3bfb0f38779c0d9ab76b5971590e79d5e",
],
]

for (const [name, data, hash] of hashes) {
Deno.test(`${name} reference`, () => {
assertEquals(encodeHex(refImpl.blake2b(data)), hash)
})
Deno.test(`${name} straight`, () => {
const hasher = new Blake2b()
hasher.update(data)
assertEquals(encodeHex(hasher.digest()), hash)
hasher.dispose()
for (const size of [512, 8, 16, 32, 64, 128, 256] as const) {
testHasher({
name: `blake2b ${size}`,
reference: (data) => refImpl.blake2b(data, { dkLen: size / 8 }),
create: () => new Blake2b(size / 8),
})
for (const chunkSize of [1, 13, 31, 32, 33, 49, 64, 65, 113]) {
Deno.test(`${name} chunked ${chunkSize}`, () => {
const hasher = new Blake2b()
for (let i = 0; i < data.length; i += chunkSize) {
hasher.update(data.slice(i, i + chunkSize))
}
assertEquals(encodeHex(hasher.digest()), hash)
hasher.dispose()
})
}
}
53 changes: 53 additions & 0 deletions hashers/test/test_util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"
import { assertSnapshot } from "https://deno.land/[email protected]/testing/snapshot.ts"
import { encodeHex } from "../../util.ts"
import { Hasher } from "../common.ts"

interface TestHasherProps {
name: string
reference: (data: Uint8Array) => Uint8Array
create: () => Hasher
}

const lorem =
// cspell:disable-next-line
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

const license = await fetch(new URL("../../LICENSE", import.meta.url)).then((r) => r.arrayBuffer())

const testCases: [name: string, data: Uint8Array][] = [
["empty", new Uint8Array()],
["[0; 1024]", new Uint8Array(1024)],
["0..256", new Uint8Array(Array.from({ length: 256 }, (_, i) => i))],
["lorem", new TextEncoder().encode(lorem)],
["license", new Uint8Array(license)],
]

export function testHasher(props: TestHasherProps) {
Deno.test(props.name, async (t) => {
for (const [name, data] of testCases) {
await t.step(name, async (t) => {
const hash = encodeHex(props.reference(data))
await t.step(`reference`, async (t) => {
await assertSnapshot(t, hash)
})
await t.step(`straight`, () => {
const hasher = props.create()
hasher.update(data)
assertEquals(encodeHex(hasher.digest()), hash)
hasher.dispose()
})
for (const chunkSize of [1, 13, 31, 32, 33, 49, 64, 65, 113]) {
await t.step(`chunked ${chunkSize}`, () => {
const hasher = props.create()
for (let i = 0; i < data.length; i += chunkSize) {
hasher.update(data.subarray(i, i + chunkSize))
}
assertEquals(encodeHex(hasher.digest()), hash)
hasher.dispose()
})
}
})
}
})
}
62 changes: 7 additions & 55 deletions hashers/test/xxhash.test.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,11 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"
import * as refImpl from "https://esm.sh/@polkadot/[email protected]/xxhash/index.js"
import { encodeHex } from "../../util.ts"
import { Xxhash } from "../xxhash.ts"
import { testHasher } from "./test_util.ts"

const lorem =
// cspell:disable-next-line
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

const hashes: [name: string, data: Uint8Array, hash: string][] = [
[
"empty",
new Uint8Array(),
"99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203adb47db9f0e7b8df13bfd1c9806d4e14b99c769b2d80a6fdf09440a6f6f62f095",
],
[
"[0; 1024]",
new Uint8Array(1024),
"cdac85f0882874275d7ee38a70da85e98ed188fdabc8466b923b4e0a88c21e18411f08ee10a208b4dd712c68cfeecf68fabd60b05467e45268807a47a3359f41",
],
[
"0..256",
new Uint8Array(Array.from({ length: 256 }, (_, i) => i)),
"4b90cd0684beac1ff7566a6db01eecbe796a6681a553fa6f4ca7f8a1da4c7241822eb628b1ece95206ada4385fb1f2d08e2d2f953b2bcc4aa86e0ec82ef633d2",
],
[
"lorem",
new TextEncoder().encode(lorem),
"3056764314b1a8c5331511d478ff3f1203586c1a9308a54594234d9956fa6173dde73c6f4d92a266b80f4ea640fc58880baa7ba549315bf42b06d60439cdd4cc",
],
]

for (const [name, data, fullHash] of hashes) {
for (const size of [64, 128, 256, 512] as const) {
const hash = fullHash.slice(0, size / 4)
const rounds = size / 64
Deno.test(`${name} ${size} reference`, () => {
assertEquals(refImpl.xxhashAsHex(data, size).slice(2), hash)
})
Deno.test(`${name} ${size} straight`, () => {
const hasher = new Xxhash(rounds)
hasher.update(data)
assertEquals(encodeHex(hasher.digest()), hash)
hasher.dispose()
})
if (size === 512) {
for (const chunkSize of [1, 13, 31, 32, 33, 49, 64, 65, 113]) {
Deno.test(`${name} ${size} chunked ${chunkSize}`, () => {
const hasher = new Xxhash(rounds)
for (let i = 0; i < data.length; i += chunkSize) {
hasher.update(data.slice(i, i + chunkSize))
}
assertEquals(encodeHex(hasher.digest()), hash)
hasher.dispose()
})
}
}
}
for (const size of [512, 64, 128, 256, 384, 512] as const) {
testHasher({
name: `xxhash ${size}`,
reference: (data) => refImpl.xxhashAsU8a(data, size),
create: () => new Xxhash(size / 64),
})
}

0 comments on commit 3e8f876

Please sign in to comment.