Coverage for tests/tests_advertrain/test_dependencies/test_cleverhans/test_fast_gradient_method.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-09-10 08:11 +0000

1import pytest 

2import torch 

3 

4from robustML.advertrain.dependencies.cleverhans.fast_gradient_method import \ 

5 fast_gradient_method 

6 

7torch.manual_seed(0) 

8 

9 

10def mock_model_fn(x): 

11 return x 

12 

13 

14def test_adversarial_example_generation(): 

15 x = torch.randn(3, 3) 

16 eps = 0.1 

17 norm = 2 

18 

19 adv_x = fast_gradient_method(mock_model_fn, x, eps, norm) 

20 

21 assert adv_x.shape == x.shape 

22 assert torch.max(torch.abs(adv_x - x)) <= eps 

23 

24 

25def test_error_on_negative_eps(): 

26 x = torch.randn(3, 3) 

27 eps = -0.1 

28 norm = 2 

29 

30 with pytest.raises(ValueError): 

31 fast_gradient_method(mock_model_fn, x, eps, norm)