Debian Codegraph @

test/uflacs/unit/test_snippets.py

raw
1 # -*- coding: utf-8 -*-
2
3 from ffc.uflacs.language.format_value import format_float
4 from ffc.uflacs.language.format_lines import iter_indented_lines, Indented, format_indented_lines
5
6
7 def test_format_float():
8 # Ints handled
9 assert format_float(0, 3) == "0.0"
10 assert format_float(1, 3) == "1.0"
11 assert format_float(12, 15) == "12.0"
12
13 # Zeros simple
14 assert format_float(0.0, 0) == "0.0"
15 assert format_float(0.0, 3) == "0.0"
16 assert format_float(0.0, 15) == "0.0"
17
18 # Ones simple
19 assert format_float(1.0, 0) == "1.0"
20 assert format_float(1.0, 3) == "1.0"
21 assert format_float(1.0, 15) == "1.0"
22
23 # Small ints simple
24 assert format_float(12., 15) == "12.0"
25 assert format_float(12., 15) == "12.0"
26
27 # Precision truncates
28 assert format_float(1.2, 3) == "1.2"
29 assert format_float(1.23, 3) == "1.23"
30 assert format_float(1.2345, 3) == "1.23"
31 assert format_float(1.0 + 1e-5, 7) == "1.00001"
32 assert format_float(1.0 + 1e-5, 6) == "1.00001"
33 assert format_float(1.0 + 1e-5, 5) == "1.0"
34
35 # Cleanly formatted exponential numbers
36 # without superfluous +s and 0s
37 assert format_float(1234567.0, 3) == "1.23e6"
38 assert format_float(1.23e6, 3) == "1.23e6"
39 assert format_float(1.23e-6, 3) == "1.23e-6"
40 assert format_float(-1.23e-6, 3) == "-1.23e-6"
41 assert format_float(-1.23e6, 3) == "-1.23e6"
42
43
44
45 def test_iter_indented_lines():
46 assert list(iter_indented_lines("single line")) == ["single line"]
47 assert list(iter_indented_lines(["word"])) == ["word"]
48 assert list(iter_indented_lines(["line one", "line two"])) == ["line one", "line two"]
49 assert list(iter_indented_lines("line one\nline two")) == ["line one", "line two"]
50 assert list(iter_indented_lines(["line one", Indented("line two")])) == ["line one", " line two"]
51 assert list(iter_indented_lines([Indented("line one"), "line two"])) == [" line one", "line two"]
52 assert list(iter_indented_lines([[Indented("line one")], "line two"])) == [" line one", "line two"]
53 assert list(iter_indented_lines([Indented("line one"), ["line two"]])) == [" line one", "line two"]
54 assert list(iter_indented_lines([[Indented("line one"), "line two"]])) == [" line one", "line two"]
55
56
57 def test_format_indented_lines_example():
58 forheader = "for (int i=begin; i!=end; ++i)"
59 code = ["{",
60 Indented([forheader,
61 "{",
62 Indented("double x = y[i];"),
63 "}"]),
64 "}"]
65 reference_code = """{
66 for (int i=begin; i!=end; ++i)
67 {
68 double x = y[i];
69 }
70 }"""
71 assert format_indented_lines(code) == reference_code