https://mooseframework.inl.gov
ExpressionBuilderTest.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "gtest/gtest.h"
11 
12 #include "ExpressionBuilder.h"
13 
14 class ExpressionBuilderTest : public ::testing::Test, public ExpressionBuilder
15 {
16 };
17 
19 {
20  EBTerm x;
21  x = "x";
22 
23  EBTerm y("y");
24  EBTerm z("z"), u("u"), v("v"), w("w");
25 
26  EBTerm a(1), b(4), c(8.9);
27 
28  EBTerm f = pow(x, 2) + pow(y, a) + 3 * y * (-a);
29 
30  EBFunction G, H;
31 
32  // Test using functions in terms
33  {
34  G((x, y)) = f;
35  H((x)) = -G((x, b)) + c % b;
36 
37  EXPECT_EQ(std::string(H), "-(x^2+4^1+3*4*-1)+8.9%4");
38  EXPECT_EQ(G.args(), "x,y");
39  }
40 
41  // Test substitution subtleties (this would return x^x if substitution were performed
42  // sequentially, which they are not.. ..because that would be dumb!)
43  {
44  G((x, y)) = pow(x, y);
45  H((x, y, z)) = x * y * z;
46  EXPECT_EQ(std::string(G), "x^y");
47  EXPECT_EQ(std::string(G((y, x))), "y^x");
48  EXPECT_EQ(std::string(H((z, y, x))), "z*y*x");
49  }
50 
51  // Test single bracket syntax
52  {
53  G(x, y, z) = x * y * z;
54  H(u, v, w, x, y, z) = u + v + w + x + y + z;
55 
56  EXPECT_EQ(std::string(G(a, b, c)), "1*4*8.9");
57  EXPECT_EQ(std::string(H(a, b, c, a, b, c)), "1+4+8.9+1+4+8.9");
58  EXPECT_EQ(std::string(G(a, b, c) + H(a, b, c, a, b, c)), "1*4*8.9+1+4+8.9+1+4+8.9");
59 
60  H(u, v, w, x, y, z) = u + v + w + G(y, x, x);
61  EXPECT_EQ(std::string(H(a, b, c, x, y, z)), "1+4+8.9+y*x*x");
62  }
63 
64  // Test associativity
65  {
66  EBTerm def = x - y - z;
67  EBTerm left = (x - y) - z;
68  EBTerm right = x - (y - z);
69 
70  EXPECT_EQ(std::string(def), "x-y-z");
71  EXPECT_EQ(std::string(left), "x-y-z");
72  EXPECT_EQ(std::string(right), "x-(y-z)");
73  }
74 
75  // test comparison operators
76  {
77  EBTerm comp1 = (x < y) + (x > y);
78  EBTerm comp2 = (x <= y) + (x >= y);
79  EBTerm comp3 = (x == y) + (x != y);
80 
81  EXPECT_EQ(std::string(comp1), "(x<y)+(x>y)");
82  EXPECT_EQ(std::string(comp2), "(x<=y)+(x>=y)");
83  EXPECT_EQ(std::string(comp3), "(x=y)+(x!=y)");
84  }
85 
86  // test binary functions
87  {
88  EBTerm comp4 = atan2(x, y);
89  EXPECT_EQ(std::string(comp4), "atan2(x,y)");
90  }
91 
92  // test ifexpr
93  {
94  EBTerm if1 = conditional(x < 2 * y, x * x + y, y * y + x);
95  EXPECT_EQ(std::string(if1),
96  "if"
97  "(x<2*y,x*x+y,y*y+x)");
98  }
99 
100  // test temp id node stringify
101  {
102  EBTerm temp1;
103  EBTerm temp2;
104  EXPECT_NE(std::string(temp1), std::string(temp2));
105  }
106 
107  // test substitution
108  {
109  // plog substitution
110  EBTerm u = log(x / a);
111  u.substitute(EBLogPlogSubstitution(b));
112  EXPECT_EQ(std::string(u), "plog(x/1,4)");
113 
114  // single substitution in a ternary
115  EBTerm v = conditional(x < y, x * y, x / y);
116  v.substitute(EBTermSubstitution(x, a));
117  EXPECT_EQ(std::string(v),
118  "if"
119  "(1<y,1*y,1/y)");
120 
121  // substitution list
122  EBTerm w = conditional(x < y, x * y, x / y);
123  EBSubstitutionRuleList s(2);
124  EBTermSubstitution s0(x, y), s1(y, a);
125  s[0] = &s0;
126  s[1] = &s1;
127  w.substitute(s);
128  EXPECT_EQ(std::string(w),
129  "if"
130  "(y<1,y*1,y/1)");
131  }
132 }
const std::vector< double > y
ExpressionBuilder adds an interface to derived classes that enables convenient construction of FParse...
static const std::string G
Definition: NS.h:166
const std::vector< double > x
Real f(Real x)
Test function for Brents method.
TEST_F(ExpressionBuilderTest, test)
auto conditional(const C &, const L &, const R &)
auto log(const T &)
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
static const std::string v
Definition: NS.h:84