| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
Tests of CNode formatting. |
| 4 |
""" |
| 5 |
from ffc.uflacs.language.cnodes import * |
| 6 |
|
| 7 |
|
| 8 |
def test_cnode_expression_precedence(): |
| 9 |
assert str(Add(1, 2)) == "1 + 2" |
| 10 |
assert str(Add(Add(1, 2), 3)) == "1 + 2 + 3" |
| 11 |
assert str(Add(1, Add(2, 3))) == "1 + (2 + 3)" |
| 12 |
assert str(Add(Add(1, 2), Add(3, 4))) == "1 + 2 + (3 + 4)" |
| 13 |
assert str(Add(Add(1, Add(2, 3)), 4)) == "1 + (2 + 3) + 4" |
| 14 |
|
| 15 |
assert str(Div(Mul(1, 2), 3)) == "1 * 2 / 3" |
| 16 |
assert str(Mul(Div(1, 2), 3)) == "1 / 2 * 3" |
| 17 |
assert str(Mul(1, Div(2, 3))) == "1 * (2 / 3)" |
| 18 |
assert str(Mul(1, Mul(2, Mul(3, 4)))) == "1 * (2 * (3 * 4))" |
| 19 |
assert str(Mul(Mul(Mul(1, 2), 3), 4)) == "1 * 2 * 3 * 4" |
| 20 |
assert str(Mul(Mul(1, Mul(2, 3)), 4)) == "1 * (2 * 3) * 4" |
| 21 |
|
| 22 |
assert str(Mul(Add(1, 2), Add(3, 4))) == "(1 + 2) * (3 + 4)" |
| 23 |
|
| 24 |
|
| 25 |
def test_cnode_expressions(): |
| 26 |
A = Symbol("A") |
| 27 |
B = Symbol("B") |
| 28 |
i = Symbol("i") |
| 29 |
j = Symbol("j") |
| 30 |
x = Symbol("x") |
| 31 |
y = Symbol("y") |
| 32 |
|
| 33 |
# Literals |
| 34 |
assert str(LiteralInt(123)) == "123" |
| 35 |
assert str(LiteralFloat(0.0)) == "0.0" |
| 36 |
assert str(LiteralFloat(1.0)) == "1.0" |
| 37 |
assert str(LiteralFloat(12.3)) == "12.3" # 1.23e+01" |
| 38 |
|
| 39 |
# Variables |
| 40 |
# TODO: VariableAccess |
| 41 |
|
| 42 |
# Arrays |
| 43 |
assert str(ArrayAccess("A", (1,))) == "A[1]" |
| 44 |
assert str(ArrayAccess(A, (1, 2))) == "A[1][2]" |
| 45 |
assert str(A[1, 2, 3]) == "A[1][2][3]" |
| 46 |
assert str(ArrayAccess(ArrayDecl("double", "A", (2,)), 1)) == "A[1]" |
| 47 |
assert str(ArrayDecl("double", A, (2, 3))[1, 2]) == "A[1][2]" |
| 48 |
|
| 49 |
# FlattenedArray |
| 50 |
n = Symbol("n") |
| 51 |
decl = ArrayDecl("double", A, (4,)) |
| 52 |
assert str(FlattenedArray(decl, strides=(2,), offset=3)[0]) == "A[3]" # "A[3 + 2 * 0]" |
| 53 |
assert str(FlattenedArray(decl, strides=(2,))[0]) == "A[0]" # "A[2 * 0]" |
| 54 |
decl = ArrayDecl("double", A, (2, 3, 4)) |
| 55 |
flattened = FlattenedArray(decl, strides=(7, 8 * n, n - 1)) |
| 56 |
#assert str(flattened[0, n, n * 7]) == "A[7 * 0 + 8 * n * n + (n - 1) * (n * 7)]" |
| 57 |
#assert str(flattened[0, n][n * 7]) == "A[7 * 0 + 8 * n * n + (n - 1) * (n * 7)]" |
| 58 |
#assert str(flattened[0][n][n * 7]) == "A[7 * 0 + 8 * n * n + (n - 1) * (n * 7)]" |
| 59 |
assert str(flattened[0, n, n * 7]) == "A[8 * n * n + (n - 1) * (n * 7)]" |
| 60 |
assert str(flattened[0, n][n * 7]) == "A[8 * n * n + (n - 1) * (n * 7)]" |
| 61 |
assert str(flattened[0][n][n * 7]) == "A[8 * n * n + (n - 1) * (n * 7)]" |
| 62 |
|
| 63 |
# Unary operators |
| 64 |
assert str(Pos(1)) == "+1" |
| 65 |
assert str(Neg(1)) == "-1" |
| 66 |
assert str(Not(1)) == "!1" |
| 67 |
assert str(BitNot(1)) == "~1" |
| 68 |
assert str(PreIncrement(i)) == "++i" |
| 69 |
assert str(PostIncrement(i)) == "i++" |
| 70 |
assert str(PreDecrement(i)) == "--i" |
| 71 |
assert str(PostDecrement(i)) == "i--" |
| 72 |
assert str(Call(Symbol("f"), [])) == "f()" |
| 73 |
assert str(Call("f", [x, 3, Mul(y, 8.0)])) == "f(x, 3, y * 8.0)" |
| 74 |
assert str(Call("sin", Mul(B, 3.0))) == "sin(B * 3.0)" |
| 75 |
|
| 76 |
# Binary operators |
| 77 |
assert str(Add(1, 2)) == "1 + 2" |
| 78 |
assert str(Mul(1, 2)) == "1 * 2" |
| 79 |
assert str(Div(1, 2)) == "1 / 2" |
| 80 |
assert str(Sub(1, 2)) == "1 - 2" |
| 81 |
assert str(Mod(1, 2)) == "1 % 2" |
| 82 |
assert str(EQ(1, 2)) == "1 == 2" |
| 83 |
assert str(NE(1, 2)) == "1 != 2" |
| 84 |
assert str(LT(1, 2)) == "1 < 2" |
| 85 |
assert str(LE(1, 2)) == "1 <= 2" |
| 86 |
assert str(GT(1, 2)) == "1 > 2" |
| 87 |
assert str(GE(1, 2)) == "1 >= 2" |
| 88 |
assert str(And(1, 2)) == "1 && 2" |
| 89 |
assert str(Or(1, 2)) == "1 || 2" |
| 90 |
assert str(BitAnd(1, 2)) == "1 & 2" |
| 91 |
assert str(BitXor(1, 2)) == "1 ^ 2" |
| 92 |
assert str(BitOr(1, 2)) == "1 | 2" |
| 93 |
|
| 94 |
# Binary operators translated from python |
| 95 |
assert str(A + B) == "A + B" |
| 96 |
assert str(A * B) == "A * B" |
| 97 |
assert str(A / B) == "A / B" |
| 98 |
assert str(A - B) == "A - B" |
| 99 |
|
| 100 |
# Ternary operator |
| 101 |
assert str(Conditional(1, 2, 3)) == "1 ? 2 : 3" |
| 102 |
|
| 103 |
# N-ary "operators" simplify code generation |
| 104 |
assert str(Sum([1, 2, 3, 4])) == "1 + 2 + 3 + 4" |
| 105 |
assert str(Product([1, 2, 3, 4])) == "1 * 2 * 3 * 4" |
| 106 |
assert str(Product([Sum([1, 2, 3]), Sub(4, 5)])) == "(1 + 2 + 3) * (4 - 5)" |
| 107 |
|
| 108 |
# Custom expression |
| 109 |
assert str(Mul(VerbatimExpr("1 + std::foo(3)"), Add(5, 6))) == "(1 + std::foo(3)) * (5 + 6)" |
| 110 |
|
| 111 |
|
| 112 |
def test_cnode_assignments(): |
| 113 |
x = Symbol("x") |
| 114 |
y = Symbol("y") |
| 115 |
assert str(Assign(x, y)) == "x = y" |
| 116 |
assert str(AssignAdd(x, y)) == "x += y" |
| 117 |
assert str(AssignSub(x, y)) == "x -= y" |
| 118 |
assert str(AssignMul(x, y)) == "x *= y" |
| 119 |
assert str(AssignDiv(x, y)) == "x /= y" |
| 120 |
assert str(AssignMod(x, y)) == "x %= y" |
| 121 |
assert str(AssignLShift(x, y)) == "x <<= y" |
| 122 |
assert str(AssignRShift(x, y)) == "x >>= y" |
| 123 |
assert str(AssignAnd(x, y)) == "x &&= y" |
| 124 |
assert str(AssignOr(x, y)) == "x ||= y" |
| 125 |
assert str(AssignBitAnd(x, y)) == "x &= y" |
| 126 |
assert str(AssignBitXor(x, y)) == "x ^= y" |
| 127 |
assert str(AssignBitOr(x, y)) == "x |= y" |
| 128 |
|
| 129 |
|
| 130 |
def test_cnode_variable_declarations(): |
| 131 |
y = Symbol("y") |
| 132 |
assert str(VariableDecl("foo", "x")) == "foo x;" |
| 133 |
assert str(VariableDecl("int", "n", 1)) == "int n = 1;" |
| 134 |
assert str(VariableDecl("double", "x", Mul(y, 3.0))) == "double x = y * 3.0;" |
| 135 |
|
| 136 |
|
| 137 |
def test_1d_initializer_list(): |
| 138 |
fmt = lambda v, s: format_indented_lines(build_initializer_lists(v, s, 0, str)) |
| 139 |
assert fmt([], (0,)) == "{ }" |
| 140 |
assert fmt([1], (1,)) == "{ 1 }" |
| 141 |
assert fmt([1, 2], (2,)) == "{ 1, 2 }" |
| 142 |
assert fmt([1, 2, 3], (3,)) == "{ 1, 2, 3 }" |
| 143 |
|
| 144 |
|
| 145 |
def test_nd_initializer_list_oneitem(): |
| 146 |
fmt = lambda v, s: format_indented_lines(build_initializer_lists(v, s, 0, str)) |
| 147 |
assert fmt([1], (1,)) == "{ 1 }" |
| 148 |
assert fmt([[1]], (1, 1)) == "{ { 1 } }" |
| 149 |
assert fmt([[[1]]], (1, 1, 1)) == "{ { { 1 } } }" |
| 150 |
assert fmt([[[[1]]]], (1, 1, 1, 1)) == "{ { { { 1 } } } }" |
| 151 |
|
| 152 |
|
| 153 |
def test_nd_initializer_list_twoitems(): |
| 154 |
fmt = lambda v, s: format_indented_lines(build_initializer_lists(v, s, 0, str)) |
| 155 |
assert fmt([1, 2], (2,)) == "{ 1, 2 }" |
| 156 |
assert fmt([[1, 2]], (1, 2)) == "{ { 1, 2 } }" |
| 157 |
assert fmt([[[1, 2]]], (1, 1, 2)) == "{ { { 1, 2 } } }" |
| 158 |
assert fmt([[[[1, 2]]]], (1, 1, 1, 2)) == "{ { { { 1, 2 } } } }" |
| 159 |
# transpose it: |
| 160 |
assert fmt([[1], [2]], (2, 1)) == "{ { 1 },\n { 2 } }" |
| 161 |
assert fmt([[[1], [2]]], (1, 2, 1)) == "{ { { 1 },\n { 2 } } }" |
| 162 |
|
| 163 |
|
| 164 |
def test_nd_initializer_list_twobytwoitems(): |
| 165 |
fmt = lambda v, s: format_indented_lines(build_initializer_lists(v, s, 0, str)) |
| 166 |
assert fmt([[1, 2], [3, 4]], (2, 2)) == "{ { 1, 2 },\n { 3, 4 } }" |
| 167 |
assert fmt([[[1, 2], [3, 4]]], (1, 2, 2)) == "{ { { 1, 2 },\n { 3, 4 } } }" |
| 168 |
assert fmt([[[[1, 2], [3, 4]]]], (1, 1, 2, 2)) == "{ { { { 1, 2 },\n { 3, 4 } } } }" |
| 169 |
|
| 170 |
|
| 171 |
def test_2d_initializer_list(): |
| 172 |
assert format_indented_lines(build_initializer_lists([[1, 2, 3], [4, 5, 6]], (2, 3), 0, str)) == "{ { 1, 2, 3 },\n { 4, 5, 6 } }" |
| 173 |
|
| 174 |
values = [[[1], [2]], [[3], [4]], [[5], [6]]] |
| 175 |
reference = """\ |
| 176 |
{ { { 1 }, |
| 177 |
{ 2 } }, |
| 178 |
{ { 3 }, |
| 179 |
{ 4 } }, |
| 180 |
{ { 5 }, |
| 181 |
{ 6 } } }""" |
| 182 |
assert format_indented_lines(build_initializer_lists(values, (3, 2, 1), 0, str)) == reference |
| 183 |
|
| 184 |
|
| 185 |
def test_2d_numpy_initializer_list(): |
| 186 |
import numpy |
| 187 |
values = [[1, 2, 3], [4, 5, 6]] |
| 188 |
array = numpy.asarray(values) |
| 189 |
sh = (2, 3) |
| 190 |
assert array.shape == sh |
| 191 |
fmt = lambda v, s: format_indented_lines(build_initializer_lists(v, s, 0, str)) |
| 192 |
assert fmt(values, sh) == "{ { 1, 2, 3 },\n { 4, 5, 6 } }" |
| 193 |
assert fmt(array, sh) == "{ { 1, 2, 3 },\n { 4, 5, 6 } }" |
| 194 |
|
| 195 |
values = [[[1], [2]], [[3], [4]], [[5], [6]]] |
| 196 |
array = numpy.asarray(values) |
| 197 |
sh = (3, 2, 1) |
| 198 |
assert sh == array.shape |
| 199 |
reference = """\ |
| 200 |
{ { { 1 }, |
| 201 |
{ 2 } }, |
| 202 |
{ { 3 }, |
| 203 |
{ 4 } }, |
| 204 |
{ { 5 }, |
| 205 |
{ 6 } } }""" |
| 206 |
assert fmt(values, sh) == reference |
| 207 |
assert fmt(array, sh) == reference |
| 208 |
|
| 209 |
|
| 210 |
def test_cnode_array_declarations(): |
| 211 |
assert str(ArrayDecl("double", "x", 3)) == "double x[3];" |
| 212 |
assert str(ArrayDecl("double", "x", (3,))) == "double x[3];" |
| 213 |
assert str(ArrayDecl("double", "x", (3, 4))) == "double x[3][4];" |
| 214 |
|
| 215 |
assert str(ArrayDecl("double", "x", 3, [1., 2., 3.])) == "double x[3] = { 1.0, 2.0, 3.0 };" |
| 216 |
assert str(ArrayDecl("double", "x", (3,), [1., 2., 3.])) == "double x[3] = { 1.0, 2.0, 3.0 };" |
| 217 |
reference = """\ |
| 218 |
{ |
| 219 |
double x[2][3] = |
| 220 |
{ { 1.0, 2.0, 3.0 }, |
| 221 |
{ 4.0, 5.0, 6.0 } }; |
| 222 |
}""" |
| 223 |
assert str(Scope(ArrayDecl("double", "x", (2, 3), [[1., 2., 3.], [4., 5., 6.]]))) == reference |
| 224 |
|
| 225 |
|
| 226 |
def test_cnode_comments(): |
| 227 |
assert str(Comment("hello world")) == "// hello world" |
| 228 |
assert str(Comment(" hello\n world ")) == "// hello\n// world" |
| 229 |
assert format_indented_lines(Indented(Comment(" hello\n world ").cs_format())) == " // hello\n // world" |
| 230 |
|
| 231 |
|
| 232 |
def test_cnode_statements(): |
| 233 |
assert str(Break()) == "break;" |
| 234 |
assert str(Continue()) == "continue;" |
| 235 |
assert str(Return(Add(1, 2))) == "return 1 + 2;" |
| 236 |
assert str(Case(3)) == "case 3:" |
| 237 |
assert str(Default()) == "default:" |
| 238 |
|
| 239 |
code = "for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)\n{ /* foobar */\n}" |
| 240 |
assert str(VerbatimStatement(code)) == code |
| 241 |
|
| 242 |
|
| 243 |
def test_cnode_loop_statements(): |
| 244 |
i, j, x, y, A = [Symbol(ch) for ch in "ijxyA"] |
| 245 |
|
| 246 |
body = [Assign("x", 3), AssignAdd(x, 5)] |
| 247 |
body_fmt = "{\n x = 3;\n x += 5;\n}" |
| 248 |
assert str(Scope(body)) == body_fmt |
| 249 |
assert str(Namespace("foo", body)) == "namespace foo\n" + body_fmt |
| 250 |
assert str(If(LT(x, 4.0), body)) == "if (x < 4.0)\n" + body_fmt |
| 251 |
assert str(ElseIf(LT(x, 4.0), body)) == "else if (x < 4.0)\n" + body_fmt |
| 252 |
assert str(Else(body)) == "else\n" + body_fmt |
| 253 |
assert str(While(LT(x, 4.0), body)) == "while (x < 4.0)\n" + body_fmt |
| 254 |
assert str(Do(LT(x, 4.0), body)) == "do\n" + body_fmt + " while (x < 4.0);" |
| 255 |
assert str(For(VariableDecl("int", "i", 0), LT(i, 4), PreIncrement(i), body)) == "for (int i = 0; i < 4; ++i)\n" + body_fmt |
| 256 |
|
| 257 |
assert str(ForRange("i", 3, 7, Comment("body"))) == "for (int i = 3; i < 7; ++i)\n{\n // body\n}" |
| 258 |
|
| 259 |
# Using assigns as both statements and expressions |
| 260 |
assert str(While(LT(AssignAdd("x", 4.0), 17.0), AssignAdd("A", y))) == "while ((x += 4.0) < 17.0)\n{\n A += y;\n}" |
| 261 |
assert str(ForRange("i", 3, 7, AssignAdd("A", i))) == "for (int i = 3; i < 7; ++i)\n A += i;" |
| 262 |
|
| 263 |
|
| 264 |
def test_cnode_loop_helpers(): |
| 265 |
i = Symbol("i") |
| 266 |
j = Symbol("j") |
| 267 |
A = Symbol("A") |
| 268 |
B = Symbol("B") |
| 269 |
C = Symbol("C") |
| 270 |
dst = A[i + 4 * j] |
| 271 |
src = 2.0 * B[j] * C[i] |
| 272 |
ranges = [(i, 0, 2), (j, 1, 3)] |
| 273 |
assert str(assign_loop(dst, src, ranges)) == """\ |
| 274 |
for (int i = 0; i < 2; ++i) |
| 275 |
for (int j = 1; j < 3; ++j) |
| 276 |
A[i + 4 * j] = 2.0 * B[j] * C[i];""" |
| 277 |
assert str(scale_loop(dst, src, ranges)) == """\ |
| 278 |
for (int i = 0; i < 2; ++i) |
| 279 |
for (int j = 1; j < 3; ++j) |
| 280 |
A[i + 4 * j] *= 2.0 * B[j] * C[i];""" |
| 281 |
assert str(accumulate_loop(dst, src, ranges)) == """\ |
| 282 |
for (int i = 0; i < 2; ++i) |
| 283 |
for (int j = 1; j < 3; ++j) |
| 284 |
A[i + 4 * j] += 2.0 * B[j] * C[i];""" |
| 285 |
|
| 286 |
|
| 287 |
def test_cnode_switch_statements(): |
| 288 |
i, j, x, y, A = [Symbol(ch) for ch in "ijxyA"] |
| 289 |
assert str(Switch("x", [])) == "switch (x)\n{\n}" |
| 290 |
assert str(Switch("x", [], default=Assign("i", 3))) == "switch (x)\n{\ndefault:\n {\n i = 3;\n }\n}" |
| 291 |
|
| 292 |
reference_switch = """switch (x) |
| 293 |
{ |
| 294 |
case 1: |
| 295 |
{ |
| 296 |
y = 3; |
| 297 |
} |
| 298 |
break; |
| 299 |
case 2: |
| 300 |
{ |
| 301 |
y = 4; |
| 302 |
} |
| 303 |
break; |
| 304 |
default: |
| 305 |
{ |
| 306 |
y = 5; |
| 307 |
} |
| 308 |
}""" |
| 309 |
cnode_switch = str(Switch("x", |
| 310 |
[(1, Assign("y", 3)), (2, Assign("y", 4)), ], |
| 311 |
default=Assign("y", 5))) |
| 312 |
assert cnode_switch == reference_switch |
| 313 |
|
| 314 |
reference_switch = """switch (x) |
| 315 |
{ |
| 316 |
case 1: |
| 317 |
y = 3; |
| 318 |
case 2: |
| 319 |
y = 4; |
| 320 |
default: |
| 321 |
y = 5; |
| 322 |
}""" |
| 323 |
cnode_switch = str(Switch("x", |
| 324 |
[(1, Assign("y", 3)), (2, Assign("y", 4)), ], |
| 325 |
default=Assign("y", 5), |
| 326 |
autobreak=False, autoscope=False)) |
| 327 |
assert cnode_switch == reference_switch |
| 328 |
|
| 329 |
|
| 330 |
def test_conceptual_tabulate_tensor(): |
| 331 |
A = ArrayDecl("double", "A", (4, 6), values=0.0) |
| 332 |
code = StatementList([ |
| 333 |
A, |
| 334 |
ForRange("q", 0, 2, [ |
| 335 |
VariableDecl("double", "x", ArrayAccess("quadpoints", "q")), |
| 336 |
ForRange("i", 0, 4, [ |
| 337 |
ForRange("j", 0, 6, [ |
| 338 |
AssignAdd(ArrayAccess(A, ("i", "j")), |
| 339 |
Mul(ArrayAccess("FE0", ("q", "i")), |
| 340 |
ArrayAccess("FE1", ("q", "j")))) |
| 341 |
]) |
| 342 |
]) |
| 343 |
]) |
| 344 |
]) |
| 345 |
print(str(code)) |