foundry: Using enum as paramter in fuzz test fails

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.1.0 (39b6e39 2022-03-08T00:05:45.429338818+00:00)

What command(s) is the bug in?

No response

Operating System

Linux

Describe the bug

When using an enum as a parameter in a fuzz test the test will very likely fail because the provided value is invalid for the enum:

contract MyTest is DSTest {
	enum Enum { A, B }

	// Always fails
	function test_enum(Enum e) public {

	}
}

My understanding is that this happens because enums are represented as unit8 in the EVM, and when fuzzing we naturally want to sample values from the full range of uint8. However, in the case of enums this causes the test to immediately fail.

I don’t know if it is possible to somehow provide additional information to the test runner so that it knows that only a subset of values should be considered.

You can of course easily work around this issue, but being able to use enums would still be nice.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Ah, this seems hard to get around. The best way to do this would be:

function test_enum(uint8 _e) public {
    vm.assume(_e < 2);
    Enum e = Enum(_e);
}

I don’t think we can solve this in some other way, unless the compiler gives us information about how many enum variants there are? cc @mattsse

Yeah, I figured that this isn’t easy to do. Fortunately, one can easily work around this with some modulo operations or vm.assume(). (Thanks btw for mentioning vm.assume(), didn’t know about that one!)

I maintain documentation for the AST at https://solidity-ast.netlify.app, but dealing with different Solidity versions is difficult so to keep forge test AST agnostic is probably a good idea.

Ah, this seems hard to get around. The best way to do this would be:

function test_enum(uint8 _e) public {
    vm.assume(_e < 2);
    Enum e = Enum(_e);
}

I don’t think we can solve this in some other way, unless the compiler gives us information about how many enum variants there are? cc @mattsse

This is not included in the output and only limited to "internalType": "enum MyTest.Enum",

the module solution is pretty neat though.

vote close.