1 import warnings

2 import pytest

3 from muutils.statcounter import StatCounter

4 import pstats

5

6# Assuming the timeit_fancy function and FancyTimeitResult are imported from the correct module

7 from muutils.timeit_fancy import timeit_fancy, FancyTimeitResult

8

9

10 def test_timeit_fancy_basic():

11 def simple_function():

12 return sum(range(1000))

13

14 result = timeit_fancy(simple_function)

15

16 assert isinstance(result, FancyTimeitResult)

17 assert isinstance(result.timings, StatCounter)

18 assert result.profile is None

19 assert all(t > 0 for t in result.timings.values())

20

21

22 def test_timeit_fancy_with_return():

23 def simple_function():

24 return sum(range(1000))

25

26 result = timeit_fancy(simple_function, get_return=True)

27

28 assert isinstance(result, FancyTimeitResult)

29 assert isinstance(result.timings, StatCounter)

30 assert result.return_value == 499500

31 assert result.profile is None

32 assert all(t > 0 for t in result.timings.values())

33

34

35 def test_timeit_fancy_with_profiling():

36 def simple_function():

37 return sum(range(1000))

38

39 result = timeit_fancy(simple_function, do_profiling=True)

40

41 assert isinstance(result, FancyTimeitResult)

42 assert isinstance(result.timings, StatCounter)

43 assert isinstance(result.profile, pstats.Stats)

44 assert all(t > 0 for t in result.timings.values())

45

46

47 def test_timeit_fancy_no_return():

48 def simple_function_returns_data():

49 return "helloworld"

50

51 result = timeit_fancy(simple_function_returns_data, get_return=False)

52 assert isinstance(result, FancyTimeitResult)

53 assert result.return_value is None

54 assert isinstance(result.timings, StatCounter)

55 assert result.profile is None

56 assert all(t > 0 for t in result.timings.values())

57

58

59 def test_timeit_fancy_with_repeats():

60 def simple_function():

61 return sum(range(100))

62

63 result = timeit_fancy(simple_function, repeats=10)

64

65 assert isinstance(result, FancyTimeitResult)

66 assert isinstance(result.timings, StatCounter)

67 assert result.timings.total() == 10

68 assert result.profile is None

69 assert all(t > 0 for t in result.timings.values())

70

71

72 def test_timeit_fancy_cmd_lambda():

73 result = timeit_fancy(lambda: sum(range(100)))

74

75 assert isinstance(result, FancyTimeitResult)

76 assert isinstance(result.timings, StatCounter)

77 assert result.profile is None

78 assert all(t > 0 for t in result.timings.values())

79

80

81 def test_timeit_fancy_cmd_string_warns():

82 # expect a warning

83 with pytest.warns(UserWarning):

84 result = timeit_fancy("sum(range(100))")

85

86 assert isinstance(result, FancyTimeitResult)

87 assert isinstance(result.timings, StatCounter)

88 assert result.profile is None

89 assert all(t > 0 for t in result.timings.values())

90

91

92 def test_timeit_fancy_cmd_string_nowarn():

93 # expect no warnings

94 with warnings.catch_warnings():

95 warnings.simplefilter("error")

96 result = timeit_fancy("sum(range(100))", get_return=False, do_profiling=False)

97

98 assert isinstance(result, FancyTimeitResult)

99 assert isinstance(result.timings, StatCounter)

100 assert result.profile is None

101 assert all(t > 0 for t in result.timings.values())