| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
Tests of generic code formatting utilities, |
| 4 |
which focus on the overall structure of the code, |
| 5 |
e.g. indentation, curly brace blocks, control flow |
| 6 |
structures like loops, and class and function definitions. |
| 7 |
Some of this is C++ specific, some is more generic. |
| 8 |
""" |
| 9 |
|
| 10 |
import pytest |
| 11 |
from ffc.uflacs.language.cnodes import * |
| 12 |
|
| 13 |
|
| 14 |
def test_format_basics(): |
| 15 |
# Reproduce a string |
| 16 |
assert format_indented_lines("string") == "string" |
| 17 |
|
| 18 |
# Basic strings with indentation |
| 19 |
assert format_indented_lines("string", 1) == " string" |
| 20 |
assert format_indented_lines("string", 2) == " string" |
| 21 |
|
| 22 |
# Multiline strings with indentation |
| 23 |
assert format_indented_lines("fee\nfie\nfoe", level=1) == " fee\n fie\n foe" |
| 24 |
|
| 25 |
# One item lists are the same as strings |
| 26 |
assert format_indented_lines(["hello"]) == "hello" |
| 27 |
assert format_indented_lines(["hello"], 1) == " hello" |
| 28 |
assert format_indented_lines(["hello"], 2) == " hello" |
| 29 |
|
| 30 |
# Lists are joined with newlines |
| 31 |
assert format_indented_lines(["hello", "world"]) == "hello\nworld" |
| 32 |
assert format_indented_lines(["hello", "world"], 1) == " hello\n world" |
| 33 |
assert format_indented_lines(["hello", "world"], 2) == " hello\n world" |
| 34 |
|
| 35 |
# Tuples are joined with newlines |
| 36 |
assert format_indented_lines(("hello", "world")) == "hello\nworld" |
| 37 |
assert format_indented_lines(("hello", "world"), 1) == " hello\n world" |
| 38 |
assert format_indented_lines(("hello", "world"), 2) == " hello\n world" |
| 39 |
code = format_indented_lines(("hello", "world"), 2) |
| 40 |
assert code == " hello\n world" |
| 41 |
|
| 42 |
|
| 43 |
def test_format_indented_lines(): |
| 44 |
# Strings and lists can be put in Indented containers |
| 45 |
assert format_indented_lines(Indented("hei")) == " hei" |
| 46 |
code = format_indented_lines(Indented(["hei", Indented("verden")])) |
| 47 |
assert code == " hei\n verden" |
| 48 |
code = format_indented_lines(["{", Indented("fee\nfie\nfoe"), "}"]) |
| 49 |
assert code == "{\n fee\n fie\n foe\n}" |
| 50 |
|
| 51 |
|
| 52 |
def xtest_format_blocks(): |
| 53 |
# A Scope is an indented body with brackets before and after |
| 54 |
code = format_code(Scope("fee\nfie\nfoe")) |
| 55 |
assert code == "{\n fee\n fie\n foe\n}" |
| 56 |
# A Namespace is a 'namespace foo' line before a block |
| 57 |
code = format_code(Namespace("bar", "fee\nfie\nfoe")) |
| 58 |
assert code == "namespace bar\n{\n fee\n fie\n foe\n}" |
| 59 |
|
| 60 |
# Making a for loop |
| 61 |
code = format_code(["for (iq...)", Scope("foo(iq);")]) |
| 62 |
assert code == "for (iq...)\n{\n foo(iq);\n}" |
| 63 |
# Making a do loop |
| 64 |
code = format_code(["iq = 0;", "do", (Scope("foo(iq);"), " while (iq < nq);")]) |
| 65 |
assert code == "iq = 0;\ndo\n{\n foo(iq);\n} while (iq < nq);" |
| 66 |
|
| 67 |
|
| 68 |
def xtest_format_class(): |
| 69 |
# Making a class declaration |
| 70 |
assert format_code(Class('Car')) == 'class Car\n{\n};' |
| 71 |
code = format_code(Class('Car', superclass='Vehicle')) |
| 72 |
assert code == 'class Car: public Vehicle\n{\n};' |
| 73 |
code = format_code(Class('Car', public_body='void eval()\n{\n}')) |
| 74 |
assert code == 'class Car\n{\npublic:\n void eval()\n {\n }\n};' |
| 75 |
code = format_code(Class('Car', protected_body='void eval()\n{\n}')) |
| 76 |
assert code == 'class Car\n{\nprotected:\n void eval()\n {\n }\n};' |
| 77 |
code = format_code(Class('Car', private_body='void eval()\n{\n}')) |
| 78 |
assert code == 'class Car\n{\nprivate:\n void eval()\n {\n }\n};' |
| 79 |
|
| 80 |
|
| 81 |
def xtest_format_template_argument_list(): |
| 82 |
def t(args, mlcode, slcode): |
| 83 |
code = format_code(TemplateArgumentList(args, False)) |
| 84 |
assert code == slcode |
| 85 |
code = format_code(TemplateArgumentList(args, True)) |
| 86 |
assert code == mlcode |
| 87 |
t(('A',), '<\n A\n>', '<A>') |
| 88 |
t(('A', 'B'), '<\n A,\n B\n>', '<A, B>') |
| 89 |
|
| 90 |
|
| 91 |
def xtest_format_templated_type(): |
| 92 |
code = format_code(Type('Foo')) |
| 93 |
assert code == 'Foo' |
| 94 |
code = format_code(Type('Foo', ('int', '123'))) |
| 95 |
assert code == 'Foo<int, 123>' |
| 96 |
code = format_code(Type('Foo', ('int', Type('Bar', ('123', 'float'))))) |
| 97 |
assert code == 'Foo<int, Bar<123, float> >' |
| 98 |
|
| 99 |
|
| 100 |
def xtest_format_typedef(): |
| 101 |
assert format_code(TypeDef('int', 'myint')) == 'typedef int myint;' |
| 102 |
code = format_code(TypeDef(Type('Foo', ('int', Type('Bar', ('123', 'float')))), 'Thing')) |
| 103 |
assert code == 'typedef Foo<int, Bar<123, float> > Thing;' |
| 104 |
|
| 105 |
|
| 106 |
def xtest_format_template_class(): |
| 107 |
expected = "template<typename T, typename R>\nclass MyClass\n{\npublic:\n void hello(int world) {}\n};" |
| 108 |
|
| 109 |
code = [('template', TemplateArgumentList(('typename T', 'typename R'), False)), |
| 110 |
Class('MyClass', public_body='void hello(int world) {}')] |
| 111 |
assert format_code(code) == expected |
| 112 |
|
| 113 |
code = Class('MyClass', public_body='void hello(int world) {}', |
| 114 |
template_arguments=('typename T', 'typename R')) |
| 115 |
assert format_code(code) == expected |
| 116 |
|
| 117 |
|
| 118 |
def test_format_variable_decl(): |
| 119 |
code = VariableDecl("double", "foo") |
| 120 |
expected = "double foo;" |
| 121 |
assert str(code) == expected |
| 122 |
|
| 123 |
|
| 124 |
def test_literal_cexpr_value_conversion(): |
| 125 |
assert bool(LiteralBool(True)) is True |
| 126 |
assert bool(LiteralBool(False)) is False |
| 127 |
assert int(LiteralInt(2)) == 2 |
| 128 |
assert float(LiteralFloat(2.0)) == 2.0 |
| 129 |
# assert complex(LiteralFloat(2.0+4.0j)) == 2.0+4.0j |
| 130 |
|
| 131 |
|
| 132 |
def test_format_array_decl(): |
| 133 |
expected = "double foo[3];" |
| 134 |
code = ArrayDecl("double", "foo", 3) |
| 135 |
assert str(code) == expected |
| 136 |
code = ArrayDecl("double", "foo", (3,)) |
| 137 |
assert str(code) == expected |
| 138 |
decl = code |
| 139 |
|
| 140 |
expected = "foo[1]" |
| 141 |
code = ArrayAccess(decl, 1) |
| 142 |
assert str(code) == expected |
| 143 |
code = ArrayAccess(decl, (1,)) |
| 144 |
assert str(code) == expected |
| 145 |
|
| 146 |
expected = "foo[0]" |
| 147 |
code = ArrayAccess(decl, 0) |
| 148 |
assert str(code) == expected |
| 149 |
with pytest.raises(ValueError): |
| 150 |
ArrayAccess(decl, 3) |
| 151 |
|
| 152 |
code = ArrayDecl("double", "foo", (3, 4)) |
| 153 |
expected = "double foo[3][4];" |
| 154 |
assert str(code) == expected |
| 155 |
decl = code |
| 156 |
|
| 157 |
expected = "foo[0][1]" |
| 158 |
code = ArrayAccess(decl, (0, 1)) |
| 159 |
assert str(code) == expected |
| 160 |
expected = "foo[2][3]" |
| 161 |
code = ArrayAccess(decl, (2, 3)) |
| 162 |
assert str(code) == expected |
| 163 |
|
| 164 |
with pytest.raises(ValueError): |
| 165 |
ArrayAccess(decl, 0) |
| 166 |
with pytest.raises(ValueError): |
| 167 |
ArrayAccess(decl, (0, 4)) |
| 168 |
with pytest.raises(ValueError): |
| 169 |
ArrayAccess(decl, (3, 0)) |
| 170 |
with pytest.raises(ValueError): |
| 171 |
ArrayAccess(decl, (3, -1)) |
| 172 |
|
| 173 |
|
| 174 |
def test_format_array_def(): |
| 175 |
expected = "double foo[3] = { 1.0, 2.0, 3.0 };" |
| 176 |
code = ArrayDecl("double", "foo", 3, [1.0, 2.0, 3.0]) |
| 177 |
assert str(code) == expected |
| 178 |
|
| 179 |
expected = """double foo[2][3] = |
| 180 |
{ { 1.0, 2.0, 3.0 }, |
| 181 |
{ 6.0, 5.0, 4.0 } };""" |
| 182 |
code = ArrayDecl("double", "foo", (2, 3), [[1.0, 2.0, 3.0], [6.0, 5.0, 4.0]]) |
| 183 |
assert str(code) == expected |
| 184 |
|
| 185 |
expected = """double foo[2][2][3] = |
| 186 |
{ { { 1.0, 2.0, 3.0 }, |
| 187 |
{ 6.0, 5.0, 4.0 } }, |
| 188 |
{ { 1.0, 2.0, 3.0 }, |
| 189 |
{ 6.0, 5.0, 4.0 } } };""" |
| 190 |
code = ArrayDecl("double", "foo", (2, 2, 3), [[[1.0, 2.0, 3.0], [6.0, 5.0, 4.0]], |
| 191 |
[[1.0, 2.0, 3.0], [6.0, 5.0, 4.0]]]) |
| 192 |
assert str(code) == expected |
| 193 |
|
| 194 |
|
| 195 |
def test_format_array_def_zero(): |
| 196 |
expected = "double foo[3] = {};" |
| 197 |
code = ArrayDecl("double", "foo", 3, [0.0, 0.0, 0.0]) |
| 198 |
assert str(code) == expected |
| 199 |
code = ArrayDecl("double", "foo", (3,), [0.0, 0.0, 0.0]) |
| 200 |
assert str(code) == expected |
| 201 |
|
| 202 |
expected = "double foo[2][3] = {};" |
| 203 |
code = ArrayDecl("double", "foo", (2, 3), [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) |
| 204 |
assert str(code) == expected |
| 205 |
|
| 206 |
|
| 207 |
def xtest_class_with_arrays(): |
| 208 |
adecl = ArrayDecl('double', 'phi', (3, 5)) |
| 209 |
aacc = ArrayAccess(adecl, (2, 1)) |
| 210 |
expected = "phi[2][1]" |
| 211 |
assert str(aacc) == expected |
| 212 |
|
| 213 |
decls = [VariableDecl('double', 'foo'), |
| 214 |
VariableDecl('int', 'bar'), |
| 215 |
adecl, |
| 216 |
] |
| 217 |
classcode = Class('Car', public_body=decls) |
| 218 |
|
| 219 |
expected_decls = [str(decl) for decl in decls] |
| 220 |
expected = 'class Car\n{\npublic:\n %s\n};' % '\n '.join(expected_decls) |
| 221 |
actual = str(classcode) |
| 222 |
assert actual == expected |
| 223 |
|
| 224 |
|
| 225 |
def test_class_array_access(): |
| 226 |
vname = 'phi' |
| 227 |
shape = (3, 4) |
| 228 |
component = ('i0', 2) |
| 229 |
decl = ArrayDecl('double', vname, shape) |
| 230 |
dcode = str(decl) |
| 231 |
acode = str(ArrayAccess(decl, component)) |
| 232 |
assert dcode == 'double phi[3][4];' |
| 233 |
assert acode == 'phi[i0][2]' |
| 234 |
|
| 235 |
|
| 236 |
def test_while_loop(): |
| 237 |
code = While(VerbatimExpr("--k < 3"), []) |
| 238 |
actual = str(code) |
| 239 |
expected = "while (--k < 3)\n{\n}" |
| 240 |
assert actual == expected |
| 241 |
|
| 242 |
code = While(VerbatimExpr("--k < 3"), body=["ting;", "tang;"]) |
| 243 |
actual = str(code) |
| 244 |
expected = "while (--k < 3)\n{\n ting;\n tang;\n}" |
| 245 |
assert actual == expected |
| 246 |
|
| 247 |
|
| 248 |
def test_for_loop(): |
| 249 |
code = For("int i = 0", "i < 3", "++i", []) |
| 250 |
actual = str(code) |
| 251 |
expected = "for (int i = 0; i < 3; ++i)\n{\n}" |
| 252 |
assert actual == expected |
| 253 |
|
| 254 |
code = For("int i = 0", "i < 3", "++i", body=["ting;", "tang;"]) |
| 255 |
actual = str(code) |
| 256 |
expected = "for (int i = 0; i < 3; ++i)\n{\n ting;\n tang;\n}" |
| 257 |
assert actual == expected |
| 258 |
|
| 259 |
|
| 260 |
def test_for_range(): |
| 261 |
code = ForRange("i", 0, 3, []) |
| 262 |
actual = str(code) |
| 263 |
expected = "for (int i = 0; i < 3; ++i)\n{\n}" |
| 264 |
assert actual == expected |
| 265 |
|
| 266 |
code = ForRange("i", 0, 3, body=["ting;", "tang;"]) |
| 267 |
actual = str(code) |
| 268 |
expected = "for (int i = 0; i < 3; ++i)\n{\n ting;\n tang;\n}" |
| 269 |
assert actual == expected |