Coverage for tests/test_code_gen.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-01 05:55 +0000

1import math 

2import os 

3 

4from pytest import approx 

5 

6from starlord import CodeGenerator 

7from starlord._config import config, _load_config 

8 

9 

10def test_expressions(): 

11 g = CodeGenerator() 

12 g.expression("l.foo = np.sin(p.stuff)") 

13 assert len(g._like_components) == 1 

14 comp = g._like_components[0] 

15 # Check variable processing 

16 assert comp.requires == {"p.stuff"} 

17 assert comp.provides == {"l.foo"} 

18 assert comp.code.count("np.") == 1 

19 assert comp.code.count("l_foo") == 0 

20 assert comp.code.count("l.foo") == 1 

21 assert comp.code.count("p_stuff") == 0 

22 assert comp.code.count("p.stuff") == 1 

23 # Check variable aggregation 

24 assert g.variables == {"l.foo", "p.stuff"} 

25 assert g.params == ("p.stuff",) 

26 assert g.locals == ("l.foo",) 

27 assert g.constants == () 

28 assert g.arrays == () 

29 # Check summary function 

30 s = g.summary().splitlines() 

31 assert s[1].startswith("Params:") 

32 assert "stuff" in s[1] 

33 assert s[2].startswith("Locals:") 

34 assert "foo" in s[2] 

35 # No prior was specified 

36 assert "=== Prior ===" == s[-1] 

37 

38 

39def test_compilation(): 

40 code = "from libc cimport math\n\n" 

41 code += "cpdef double testFunction(double x):\n" 

42 code += " return 3.5 * math.sin(x/2.)\n" 

43 hash = CodeGenerator._compile_to_module(code) 

44 mod = CodeGenerator._load_module(hash) 

45 assert mod.testFunction(12.) == approx(3.5 * math.sin(12. / 2.)) 

46 

47def test_config(): 

48 _load_config() 

49 assert config.system in ["Windows", "Linux", "Darwin"] 

50 assert os.path.exists(config.cache_dir) 

51 assert os.path.exists(config.grid_dir)