Skip to content

Add arbitraries #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ pre-release-commit-message = "Release {{version}} 🎉🎉"
no-dev-version = true

[dependencies]
multihash = "0.10"
multihash = "0.10.1"
multibase = "0.8.0"
unsigned-varint = "0.3"

quickcheck = { version = "0.9.2", optional = true }
rand = { version = "0.7.3", optional = true }

[dev-dependencies]
quickcheck = "0.9.2"
rand = "0.7.3"
multihash = { version = "0.10.1", features = ["test"] }

[features]
test = ["quickcheck", "rand"]
79 changes: 79 additions & 0 deletions src/arb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::{Cid, Codec, Prefix, Version};
use multihash::Multihash;
use quickcheck::{Arbitrary, Gen};
use rand::seq::SliceRandom;
use rand::Rng;

const CODECS: [Codec; 18] = [
Codec::Raw,
Codec::DagProtobuf,
Codec::DagCBOR,
Codec::GitRaw,
Codec::EthereumBlock,
Codec::EthereumBlockList,
Codec::EthereumTxTrie,
Codec::EthereumTx,
Codec::EthereumTxReceiptTrie,
Codec::EthereumTxReceipt,
Codec::EthereumStateTrie,
Codec::EthereumAccountSnapshot,
Codec::EthereumStorageTrie,
Codec::BitcoinBlock,
Codec::BitcoinTx,
Codec::ZcashBlock,
Codec::ZcashTx,
Codec::DagJSON,
];

const POPULAR: [Codec; 4] = [
Codec::Raw,
Codec::DagProtobuf,
Codec::DagCBOR,
Codec::DagJSON,
];

impl Arbitrary for Codec {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
// chose the most frequently used codecs more often
if g.gen_bool(0.7) {
*POPULAR.choose(g).unwrap()
} else {
*CODECS.choose(g).unwrap()
}
}
}

impl Arbitrary for Version {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let version = if g.gen_bool(0.7) { 1 } else { 0 };
Version::from(version).unwrap()
}
}

impl Arbitrary for Cid {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let version: Version = Arbitrary::arbitrary(g);
let v0 = version == Version::V0;
if v0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nitpick, no need to change it) Due to the API changes, this could be written as if version == Version::V0 {

let data: Vec<u8> = Arbitrary::arbitrary(g);
let hash = multihash::Sha2_256::digest(&data);
Cid::new_v0(hash).expect("sha2_256 is a valid hash for cid v0")
} else {
let codec: Codec = Arbitrary::arbitrary(g);
let hash: Multihash = Arbitrary::arbitrary(g);
Cid::new_v1(codec, hash)
}
}
}

impl Arbitrary for Prefix {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let cid: Cid = Arbitrary::arbitrary(g);
Prefix {
version: cid.version,
codec: cid.codec,
mh_type: cid.hash.algorithm(),
mh_len: cid.hash.digest().len(),
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ mod error;
mod prefix;
mod version;

#[cfg(any(test, feature = "test"))]
mod arb;

pub use self::cid::Cid;
pub use self::codec::Codec;
pub use self::error::{Error, Result};
Expand Down