GithubHelp home page GithubHelp logo

Support `no_std` Usage about proptest HOT 18 OPEN

ZackPierce avatar ZackPierce commented on July 30, 2024
Support `no_std` Usage

from proptest.

Comments (18)

AltSysrq avatar AltSysrq commented on July 30, 2024 2

no_std + alloc support is available in 0.7.0 via #48

from proptest.

AltSysrq avatar AltSysrq commented on July 30, 2024

Possibly a dumb question (as I have no no_std experience), but is it not possible to set a project up so that the main build is no_std but tests have the stdlib?

I'm not sure about the possibility of supporting running without heap allocation. There's certainly a subset of the API that could work there, but I'm not sure how useful it'd be.

As for core + alloc, off hand it does seem fairly viable. There's three main things I can think of that would be excluded:

  • Obviously anything in arbitrary/_std that doesn't have a core/alloc component.
  • Failure case persistence.
  • Handling panics.

The last one seems like a possibly big deal, since it means that proptest can't really do its job on a test that can fail in any way other than returning a failure code normally. But I guess it could still be useful.

In any case, thanks for being willing to assist with this!

from proptest.

ZackPierce avatar ZackPierce commented on July 30, 2024

Thank you for the encouragement, @AltSysrq .

Yep, it is often possible to arrange a project such that the tests are run with access to the standard library (e.g. whilst running on a local linux/windows/osx dev machine), even though the main project is geared for an embedded use case.

However, this is not always the case -- sometimes the capabilities that need to be exercised are only truly available when being run in a context without access to the standard library at all. For example, when running on an esoteric device with custom (sometimes black-box) features and no prayer of a full stdlib in sight.

That sort of "in-vivo" testing is what I'm hoping to cover with this effort.

I think persistence can be handled conventionally enough by being made optional -- perhaps factoring it into a trait with a few implementations (noop, filesystem, manual piping, etc), the present filesystem-based variant of which could be made std-only.

As for panics, you're right that we'd be pushed into a returned-failure-signifying style. There is an alternative available, which utest uses, that overrides panic! in order to flip some global static bits that the test runner probes. However, I'm not keen on spooky action at a distance or fragile single-package-level macro overloads. I'd rather give the return-value based approach a shot first, since it seems more Rustic how.

from proptest.

memoryruins avatar memoryruins commented on July 30, 2024

Related issue rust-embedded/wg#47

from proptest.

jparris avatar jparris commented on July 30, 2024

@ZackPierce can you add an example or more documentation on setting up proptest in a #![no_std] environment. I don't want to disrail this bug for support but addingproptest = {version="0.7", features = ["alloc", "nightly"]} to my Cargo.toml doesn't work because the dependencies are still wanting to use std.

-Jon

from proptest.

AltSysrq avatar AltSysrq commented on July 30, 2024

@jparris A couple pointers to get you started:

  • You additionally need default-features=false to opt out of the std default feature.

  • Only version 0.8.4 (the latest as of writing) is compatible with the latest nightly rust build.

I think there may be a bit more fiddling necessary for actually writing the tests, and unfortunately I don't have any examples handy.

from proptest.

Lokathor avatar Lokathor commented on July 30, 2024

Definitely an example is needed for how to no_std the crate.

I also attempted to use it:

proptest = { version = "0.8", defatult-features = false, features = [] }

but then it pulled in many std using things. There should just be an example of every dependency you have to list out with default-features off

from proptest.

AltSysrq avatar AltSysrq commented on July 30, 2024
[dev-dependencies.proptest]
version = "0.8"
default-features = false
features = ["alloc", "unstable"]

This works for me, assuming I'm testing it correctly. I will add documenting this to my to do list for the next release.

from proptest.

Lokathor avatar Lokathor commented on July 30, 2024

When using that configuration, I get a problem with the transitive lazy_static dependency:

   Compiling num-traits v0.2.6
   Compiling rand_core v0.3.0
   Compiling lazy_static v1.2.0
   Compiling gba-proc-macro v0.5.0
error[E0463]: can't find crate for `std`
 --> C:\Users\Daniel\.cargo\registry\src\github.com-1ecc6299db9ec823\lazy_static-1.2.0\src\inline_lazy.rs:9:1
  |
9 | extern crate std;
  | ^^^^^^^^^^^^^^^^^ can't find crate
  |
  = note: the `thumbv4-none-agb-15848137922264977313` target may not be installed

error: aborting due to previous error

from proptest.

AltSysrq avatar AltSysrq commented on July 30, 2024

Oh, we have a separate nightly feature you also need for lazy_static to work until #112 is released.

Do you know of a way to ensure nothing brings std in short of compiling for a target that doesn't have std? Clearly the way I was testing that (a #![no_std] bin crate) doesn't work.

from proptest.

Lokathor avatar Lokathor commented on July 30, 2024

I don't.

In my particularly strange use case, I don't need proptest to run on the device, I just need it for tests that run on the local machine. However, [dev-dependences] is for both tests and examples, so if I want my examples to target the device I can't have proptest as a dev-dependency :/

from proptest.

nivkner avatar nivkner commented on July 30, 2024

@AltSysrq to test that no_std works you can use the fact that Cortex-M targets ship only with core and use cargo check --target thumbv7em-none-eabihf since checking only requires getting the target with rustup, and not an actual ARM machine

from proptest.

goral09 avatar goral09 commented on July 30, 2024

@AltSysrq It fails to compile in my case as well. This is my configuration:

[dev-dependencies.proptest]
version = "0.8"
default-features = false
features = ["alloc", "unstable"]  

and the error:

error[E0277]: the trait bound `str: proptest::strategy::Strategy` is not satisfied
  --> tests/bytesrepr_test.rs:3:1
   |
3  | / proptest! {
4  | |   #[test]
5  | |   fn test_addition(a in 0..10, b in 0..10) {
6  | |     prop_assert!(a + b <= 18);
...  |
13 | |   }
14 | | }
   | |_^ the trait `proptest::strategy::Strategy` is not implemented for `str`
   |
   = note: required because of the requirements on the impl of `proptest::strategy::Strategy` for `&str`
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

from proptest.

nivkner avatar nivkner commented on July 30, 2024

@goral09 as mentioned previously you also need the nightly feature for now, so it should be:

[dev-dependencies.proptest]
version = "0.8"
default-features = false
features = ["alloc", "unstable", "nightly"]

from proptest.

goral09 avatar goral09 commented on July 30, 2024

Thanks @nivkner but this doesn't solve my problem. Still getting the same errors.

from proptest.

AltSysrq avatar AltSysrq commented on July 30, 2024

@nivkner Thanks for that cargo test suggestion. I believe 0.9.0 should work for you.

from proptest.

AltSysrq avatar AltSysrq commented on July 30, 2024

@goral09 I'm not entirely sure what's going on in your case. The error message itself suggests you're trying to use a regex-based String strategy, which is only supported on std. Is there such a usage between lines 6 and 13?

from proptest.

AltSysrq avatar AltSysrq commented on July 30, 2024

Also, there's now documentation on using no_std with proptest.

from proptest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.