Skip to content

Commit

Permalink
[tests] Add a test to demonstrate env.spec bug.
Browse files Browse the repository at this point in the history
Issue #587.
  • Loading branch information
ChrisCummins committed Mar 1, 2022
1 parent b5a295c commit 891c287
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/llvm/gym_interface_compatability_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# LICENSE file in the root directory of this source tree.
"""Test that LlvmEnv is compatible with OpenAI gym interface."""
import gym
import pytest

from compiler_gym.envs.llvm import LlvmEnv
from tests.test_main import main
Expand All @@ -12,6 +13,10 @@


def test_type_classes(env: LlvmEnv):
env.observation_space = "Autophase"
env.reward_space = "IrInstructionCount"
env.reset()

assert isinstance(env, gym.Env)
assert isinstance(env, LlvmEnv)
assert isinstance(env.unwrapped, LlvmEnv)
Expand Down Expand Up @@ -68,5 +73,28 @@ def reward(self, reward):
assert reward == 1


@pytest.mark.xfail(
reason="github.com/facebookresearch/CompilerGym/issues/587", strict=True
)
def test_env_spec_make(env: LlvmEnv):
"""Test that demonstrates a failure in gym compatibility: env.spec does
not encode mutable state like benchmark, reward space, and observation
space.
"""
env.reset(benchmark="cbench-v1/bitcount")
with env.spec.make() as new_env:
assert new_env.benchmark == env.benchmark


def test_env_spec_make_workaround(env: LlvmEnv):
"""Demonstrate how #587 would be fixed, by updating the 'kwargs' dict."""
env.reset(benchmark="cbench-v1/bitcount")
env.spec._kwargs[ # pylint: disable=protected-access
"benchmark"
] = "cbench-v1/bitcount"
with env.spec.make() as new_env:
assert new_env.benchmark == env.benchmark


if __name__ == "__main__":
main()

0 comments on commit 891c287

Please sign in to comment.