Skip to content

Commit

Permalink
Add no_std + alloc support, refactoring persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackPierce committed Apr 25, 2018
1 parent 01ff1e4 commit 49b9069
Show file tree
Hide file tree
Showing 49 changed files with 1,386 additions and 601 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Cargo.lock
# VSCode:
.vscode/
persistence-test.txt

# IntelliJ
.idea
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ before_script:
script:
- |
travis-cargo build &&
travis-cargo test
travis-cargo test &&
travis-cargo --only nightly build -- --no-default-features --features "alloc nightly"
cache: cargo
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
## 0.7.0

### Potential Breaking Changes

- The persistence system has been refactored to allow for
non-file-system based persistence. `FailurePersistence`
is now a trait, and the prior file-based enum which fulfilled
that purpose is now called `FileFailurePersistence` and implements
the generic trait. The default behavior has not changed.
- Reflecting the change to persistence, `Config.failure_persistence`
is now of type `Option<Box<FailurePersistence>>`.
- The `source_file` used as an optional reference point to the location of the
calling test is now tracked on the `Config` struct rather than the `TestRunner`.

### New Additions

- Experimental support on nightly for working in `#![no_std]` environments has been added.
To use it, one must disable the default-features for proptest and use the
new "alloc" and "nightly" features. Currently access to a heap allocator is
still required.

## 0.6.0

### Potential Breaking Changes
Expand Down
45 changes: 38 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proptest"
version = "0.6.0"
version = "0.7.0"
authors = ["Jason Lingle"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand All @@ -18,18 +18,49 @@ travis-ci = { repository = "AltSysrq/proptest" }

[features]

default = []
default = ["std"]

# Enables unstable features of Rust.
unstable = ["rand/i128_support"]

# Required for no_std usage thanks to lazy_static's
# dependency on the spin crate, which is nightly-only
# for no_std.
nightly = ["lazy_static/spin_no_std"]

# Enables the use of standard-library dependent feature
std = ["rand/std", "bit-set/std", "quick-error", "regex-syntax"]

# For use in no_std environments with access to an allocator
alloc = ["hashmap_core"]

[dependencies]
bitflags = "1.0.1"
bit-set = "0.4.0"
quick-error = "1.2.1"
rand = "0.4.2"
regex-syntax = "0.4.2"
lazy_static = "1.0.0"

[dependencies.hashmap_core]
version = "0.1.4"
optional = true

[dependencies.lazy_static]
version = "1.0.0"
default-features = false

[dependencies.quick-error]
version = "1.2.1"
optional = true

[dependencies.regex-syntax]
version = "0.4.2"
optional = true

[dependencies.bit-set]
version = "0.5.0"
default-features = false

[dependencies.rand]
version = "0.4.2"
default-features = false
features = ["alloc"]

[dev-dependencies]
regex = "0.2.5"
21 changes: 14 additions & 7 deletions examples/dateparser_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use] extern crate proptest;
#[macro_use]
extern crate proptest;

fn parse_date(s: &str) -> Option<(u32, u32, u32)> {
if 10 != s.len() { return None; }
if 10 != s.len() {
return None;
}
// !
if "-" != &s[4..5] || "-" != &s[7..8] { return None; }
if "-" != &s[4..5] || "-" != &s[7..8] {
return None;
}

let year = &s[0..4];
let month = &s[6..7]; // !
let day = &s[8..10];

year.parse::<u32>().ok().and_then(
|y| month.parse::<u32>().ok().and_then(
|m| day.parse::<u32>().ok().map(
|d| (y, m, d))))
year.parse::<u32>().ok().and_then(|y| {
month
.parse::<u32>()
.ok()
.and_then(|m| day.parse::<u32>().ok().map(|d| (y, m, d)))
})
}

// NB We omit #[test] on these functions so that main() can call them.
Expand Down
27 changes: 18 additions & 9 deletions examples/dateparser_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,37 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use] extern crate proptest;
#[macro_use]
extern crate proptest;

// Needed for Rust 1.22.1 compatibility
#[allow(unused_imports,deprecated)]
#[allow(unused_imports, deprecated)]
use std::ascii::AsciiExt;

fn parse_date(s: &str) -> Option<(u32, u32, u32)> {
if 10 != s.len() { return None; }
if 10 != s.len() {
return None;
}

// NEW: Ignore non-ASCII strings so we don't need to deal with Unicode.
if !s.is_ascii() { return None; }
if !s.is_ascii() {
return None;
}

if "-" != &s[4..5] || "-" != &s[7..8] { return None; }
if "-" != &s[4..5] || "-" != &s[7..8] {
return None;
}

let year = &s[0..4];
let month = &s[6..7]; // !
let day = &s[8..10];

year.parse::<u32>().ok().and_then(
|y| month.parse::<u32>().ok().and_then(
|m| day.parse::<u32>().ok().map(
|d| (y, m, d))))
year.parse::<u32>().ok().and_then(|y| {
month
.parse::<u32>()
.ok()
.and_then(|m| day.parse::<u32>().ok().map(|d| (y, m, d)))
})
}

// NB We omit #[test] on these functions so that main() can call them.
Expand Down
5 changes: 3 additions & 2 deletions examples/tutorial-simplify-play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

extern crate proptest;

use proptest::test_runner::TestRunner;
use proptest::strategy::{Strategy, ValueTree};
use proptest::test_runner::TestRunner;

fn main() {
let mut runner = TestRunner::default();
let mut str_val = "[a-z]{1,4}\\p{Cyrillic}{1,4}\\p{Greek}{1,4}"
.new_value(&mut runner).unwrap();
.new_value(&mut runner)
.unwrap();
println!("str_val = {}", str_val.current());
while str_val.simplify() {
println!(" = {}", str_val.current());
Expand Down
12 changes: 8 additions & 4 deletions examples/tutorial-strategy-play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@

extern crate proptest;

use proptest::test_runner::TestRunner;
use proptest::strategy::{Strategy, ValueTree};
use proptest::test_runner::TestRunner;

fn main() {
let mut runner = TestRunner::default();
let int_val = (0..100i32).new_value(&mut runner).unwrap();
let str_val = "[a-z]{1,4}\\p{Cyrillic}{1,4}\\p{Greek}{1,4}"
.new_value(&mut runner).unwrap();
println!("int_val = {}, str_val = {}",
int_val.current(), str_val.current());
.new_value(&mut runner)
.unwrap();
println!(
"int_val = {}, str_val = {}",
int_val.current(),
str_val.current()
);
}
5 changes: 5 additions & 0 deletions src/arbitrary/_std/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

//! Arbitrary implementations for `std::boxed`.

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::boxed::Box;

wrap_from!(Box);

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions src/arbitrary/_std/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ use strategy::*;
use strategy::statics::static_map;
use arbitrary::*;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::vec::Vec;
#[allow(unused_imports)]
#[cfg(feature = "std")]
use std::vec::Vec;

macro_rules! impl_wrap_char {
($type: ty, $mapper: expr) => {
arbitrary!($type, SMapped<char, Self>;
Expand Down
20 changes: 19 additions & 1 deletion src/arbitrary/_std/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@

use std::fmt;
use std::hash::Hash;
use std::vec::Vec;
use std::rc::Rc;
use std::sync::Arc;
use std::ops::Range;
use std::collections::*;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::{vec_deque, linked_list, btree_set, binary_heap};
#[cfg(feature = "std")]
use std::collections::{vec_deque, linked_list, btree_set, binary_heap};

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::vec;
#[cfg(feature = "std")]
use std::vec;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::vec::Vec;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::boxed::Box;

use strategy::*;
use strategy::statics::static_map;
use collection::*;
Expand Down
15 changes: 15 additions & 0 deletions src/arbitrary/_std/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
use std::ffi::*;
use std::ops::Range;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::boxed::Box;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::string::String;
#[cfg(feature = "std")]
use std::string::String;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::vec::Vec;

use strategy::*;
use strategy::statics::static_map;
use collection::*;
Expand Down
15 changes: 14 additions & 1 deletion src/arbitrary/_std/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
use std::io::*;
use std::io::ErrorKind::*;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::string::String;
#[cfg(feature = "std")]
use std::string::String;

#[allow(unused_imports)]
#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::vec::Vec;
#[allow(unused_imports)]
#[cfg(feature = "std")]
use std::vec::Vec;

use strategy::*;
use strategy::statics::static_map;
use arbitrary::*;
Expand Down Expand Up @@ -149,6 +161,7 @@ arbitrary!(CharsError, SMapped<Option<Error>, Self>;

#[cfg(test)]
mod test {

no_panic_test!(
buf_reader => BufReader<Repeat>,
buf_writer => BufWriter<Sink>,
Expand All @@ -162,7 +175,7 @@ mod test {
stdout => Stdout,
lines => Lines<Empty>,
repeat => Repeat,
spit => Split<Cursor<Vec<u8>>>,
split => Split<Cursor<Vec<u8>>>,
take => Take<Repeat>,
error_kind => ErrorKind,
seek_from => SeekFrom,
Expand Down
5 changes: 5 additions & 0 deletions src/arbitrary/_std/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
use std::iter::repeat;
use std::str::{ParseBoolError, Utf8Error, from_utf8};

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::vec::Vec;

use strategy::*;
use strategy::statics::static_map;
use arbitrary::*;
Expand Down
16 changes: 15 additions & 1 deletion src/arbitrary/_std/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@

//! Arbitrary implementations for `std::string`.

use std::string::{String, FromUtf8Error, FromUtf16Error};
use std::iter;
use std::slice;
use std::rc::Rc;
use std::sync::Arc;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::boxed::Box;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::string::{String, FromUtf8Error, FromUtf16Error};
#[cfg(feature = "std")]
use std::string::{String, FromUtf8Error, FromUtf16Error};

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::vec::Vec;

use strategy::*;
use strategy::statics::static_map;
use collection;
Expand Down
5 changes: 5 additions & 0 deletions src/arbitrary/_std/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

use std::thread::*;

#[cfg(all(feature = "alloc", not(feature="std")))]
use alloc::string::String;
#[cfg(feature = "std")]
use std::string::String;

use strategy::statics::static_map;
use option::prob;
use arbitrary::*;
Expand Down
Loading

0 comments on commit 49b9069

Please sign in to comment.