| 1 |
#!/usr/bin/env py.test |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
import ufl_legacy as ufl |
| 5 |
from ufl_legacy import product |
| 6 |
|
| 7 |
from ffc.uflacs.backends.toy.toy_compiler import compile_expression |
| 8 |
|
| 9 |
""" |
| 10 |
Unit tests of generated geometry snippet code. |
| 11 |
""" |
| 12 |
|
| 13 |
def compile_expression0(expr): |
| 14 |
code = "" |
| 15 |
return code |
| 16 |
|
| 17 |
def compile_expression1(expr): |
| 18 |
code = "double values[%d];" % (product(expr.ufl_shape),) |
| 19 |
return code |
| 20 |
|
| 21 |
def compile_expression2(expr): |
| 22 |
code = compile_expression(expr, "") |
| 23 |
return code |
| 24 |
|
| 25 |
def test_compilation_of_x(gtest): |
| 26 |
"""Test that compilation of x results in the value of x placed in values.""" |
| 27 |
|
| 28 |
pre = """ |
| 29 |
double x[3] = { 0.0, 0.1, 0.2 }; |
| 30 |
double A[3]; |
| 31 |
""" |
| 32 |
|
| 33 |
post = """ |
| 34 |
ASSERT_EQ(A[0], 0.0); |
| 35 |
ASSERT_EQ(A[1], 0.1); |
| 36 |
ASSERT_EQ(A[2], 0.2); |
| 37 |
""" |
| 38 |
|
| 39 |
expr = ufl.SpatialCoordinate(ufl.tetrahedron) |
| 40 |
code = compile_expression2(expr) |
| 41 |
|
| 42 |
gtest.add(pre + code + post) |
| 43 |
|
| 44 |
def test_compilation_of_sums(gtest): |
| 45 |
"""Test that sums are compiled correctly.""" |
| 46 |
|
| 47 |
pre = """ |
| 48 |
double x[3] = { 0.1, 0.2, 0.3 }; |
| 49 |
double A[1]; |
| 50 |
""" |
| 51 |
|
| 52 |
post = """ |
| 53 |
ASSERT_EQ(A[0], x[0] + x[1] + x[2]); |
| 54 |
""" |
| 55 |
|
| 56 |
x = ufl.SpatialCoordinate(ufl.tetrahedron) |
| 57 |
expr = x[0] + x[1] + x[2] |
| 58 |
code = compile_expression2(expr) |
| 59 |
print(code) |
| 60 |
|
| 61 |
gtest.add(pre + code + post) |
| 62 |
|
| 63 |
def test_compilation_of_products(gtest): |
| 64 |
"""Test that products are compiled correctly.""" |
| 65 |
|
| 66 |
pre = """ |
| 67 |
double x[3] = { 0.1, 0.2, 0.3 }; |
| 68 |
double A[1]; |
| 69 |
""" |
| 70 |
|
| 71 |
post = """ |
| 72 |
ASSERT_EQ(A[0], x[0] * x[1] * x[2]); |
| 73 |
""" |
| 74 |
|
| 75 |
x = ufl.SpatialCoordinate(ufl.tetrahedron) |
| 76 |
expr = x[0] * x[1] * x[2] |
| 77 |
code = compile_expression2(expr) |
| 78 |
print(code) |
| 79 |
|
| 80 |
gtest.add(pre + code + post) |
| 81 |
|
| 82 |
def test_compilation_of_sums_and_products_with_precedence(gtest): |
| 83 |
"""Test that combinations of sums and products are |
| 84 |
compiled correctly with precedence rules in mind. |
| 85 |
""" |
| 86 |
|
| 87 |
pre = """ |
| 88 |
double x[3] = { 0.1, 0.2, 0.3 }; |
| 89 |
double A[1]; |
| 90 |
""" |
| 91 |
|
| 92 |
post = """ |
| 93 |
ASSERT_EQ(A[0], (x[0] + x[1]) * x[2] - (x[0] + (x[1] * x[2]))); |
| 94 |
""" |
| 95 |
|
| 96 |
x = ufl.SpatialCoordinate(ufl.tetrahedron) |
| 97 |
expr = (x[0] + x[1]) * x[2] - (x[0] + (x[1] * x[2])) |
| 98 |
code = compile_expression2(expr) |
| 99 |
print(code) |
| 100 |
|
| 101 |
gtest.add(pre + code + post) |
| 102 |
|