Skip to content

Commit f220c06

Browse files
authored
feat: add arbitraries (#24)
Add arbitraries and a simple roundtrip test
1 parent f3ce026 commit f220c06

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ pre-release-commit-message = "Release {{version}} 🎉🎉"
1414
no-dev-version = true
1515

1616
[dependencies]
17-
multihash = "0.10"
17+
multihash = "0.10.1"
1818
multibase = "0.8.0"
1919
unsigned-varint = "0.3"
20+
21+
quickcheck = { version = "0.9.2", optional = true }
22+
rand = { version = "0.7.3", optional = true }
23+
24+
[dev-dependencies]
25+
quickcheck = "0.9.2"
26+
rand = "0.7.3"
27+
multihash = { version = "0.10.1", features = ["test"] }
28+
29+
[features]
30+
test = ["quickcheck", "rand"]

src/arb.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use crate::{Cid, Codec, Prefix, Version};
2+
use multihash::Multihash;
3+
use quickcheck::{Arbitrary, Gen};
4+
use rand::seq::SliceRandom;
5+
use rand::Rng;
6+
7+
const CODECS: [Codec; 18] = [
8+
Codec::Raw,
9+
Codec::DagProtobuf,
10+
Codec::DagCBOR,
11+
Codec::GitRaw,
12+
Codec::EthereumBlock,
13+
Codec::EthereumBlockList,
14+
Codec::EthereumTxTrie,
15+
Codec::EthereumTx,
16+
Codec::EthereumTxReceiptTrie,
17+
Codec::EthereumTxReceipt,
18+
Codec::EthereumStateTrie,
19+
Codec::EthereumAccountSnapshot,
20+
Codec::EthereumStorageTrie,
21+
Codec::BitcoinBlock,
22+
Codec::BitcoinTx,
23+
Codec::ZcashBlock,
24+
Codec::ZcashTx,
25+
Codec::DagJSON,
26+
];
27+
28+
const POPULAR: [Codec; 4] = [
29+
Codec::Raw,
30+
Codec::DagProtobuf,
31+
Codec::DagCBOR,
32+
Codec::DagJSON,
33+
];
34+
35+
impl Arbitrary for Codec {
36+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
37+
// chose the most frequently used codecs more often
38+
if g.gen_bool(0.7) {
39+
*POPULAR.choose(g).unwrap()
40+
} else {
41+
*CODECS.choose(g).unwrap()
42+
}
43+
}
44+
}
45+
46+
impl Arbitrary for Version {
47+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
48+
let version = if g.gen_bool(0.7) { 1 } else { 0 };
49+
Version::from(version).unwrap()
50+
}
51+
}
52+
53+
impl Arbitrary for Cid {
54+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
55+
let version: Version = Arbitrary::arbitrary(g);
56+
let v0 = version == Version::V0;
57+
if v0 {
58+
let data: Vec<u8> = Arbitrary::arbitrary(g);
59+
let hash = multihash::Sha2_256::digest(&data);
60+
Cid::new_v0(hash).expect("sha2_256 is a valid hash for cid v0")
61+
} else {
62+
let codec: Codec = Arbitrary::arbitrary(g);
63+
let hash: Multihash = Arbitrary::arbitrary(g);
64+
Cid::new_v1(codec, hash)
65+
}
66+
}
67+
}
68+
69+
impl Arbitrary for Prefix {
70+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
71+
let cid: Cid = Arbitrary::arbitrary(g);
72+
Prefix {
73+
version: cid.version,
74+
codec: cid.codec,
75+
mh_type: cid.hash.algorithm(),
76+
mh_len: cid.hash.digest().len(),
77+
}
78+
}
79+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ mod error;
1010
mod prefix;
1111
mod version;
1212

13+
#[cfg(any(test, feature = "test"))]
14+
mod arb;
15+
1316
pub use self::cid::Cid;
1417
pub use self::codec::Codec;
1518
pub use self::error::{Error, Result};

0 commit comments

Comments
 (0)