| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include <Kokkos_Core.hpp> |
| 36 |
#include <gtest/gtest.h> |
| 37 |
#include <stk_ngp_test/ngp_test.hpp> |
| 38 |
#include <stk_expreval/Evaluator.hpp> |
| 39 |
#include <fstream> |
| 40 |
#include <iostream> |
| 41 |
#include <limits> |
| 42 |
#include <iomanip> |
| 43 |
#include <cmath> |
| 44 |
#include <memory> |
| 45 |
|
| 46 |
namespace { |
| 47 |
|
| 48 |
//-BEGIN |
| 49 |
TEST(Variable, scalar_vs_array) |
| 50 |
{ |
| 51 |
stk::expreval::Eval expr("x[1]", stk::expreval::Variable::ArrayOffset::ZERO_BASED_INDEX); |
| 52 |
expr.parse(); |
| 53 |
EXPECT_TRUE(expr.is_scalar("x")); |
| 54 |
EXPECT_EQ(expr.getValue("x"), 0.0); |
| 55 |
EXPECT_ANY_THROW(expr.evaluate()); |
| 56 |
|
| 57 |
double x[2] = {3.0, 4.0}; |
| 58 |
expr.bindVariable("x", *x, 2); |
| 59 |
EXPECT_FALSE(expr.is_scalar("x")); |
| 60 |
EXPECT_EQ(expr.evaluate(), 4.0); |
| 61 |
|
| 62 |
stk::expreval::Eval expr2("y[1]", stk::expreval::Variable::ArrayOffset::ONE_BASED_INDEX); |
| 63 |
expr2.parse(); |
| 64 |
double y[2] = {3.0, 4.0}; |
| 65 |
expr2.bindVariable("y", *y, 2); |
| 66 |
EXPECT_FALSE(expr2.is_scalar("y")); |
| 67 |
EXPECT_EQ(expr2.evaluate(), 3.0); |
| 68 |
} |
| 69 |
|
| 70 |
TEST(Variable, demonstrate_states) |
| 71 |
{ |
| 72 |
stk::expreval::Eval expr("x"); |
| 73 |
expr.parse(); |
| 74 |
EXPECT_EQ(expr.evaluate(), 0.0); |
| 75 |
|
| 76 |
double x = 2.0; |
| 77 |
expr.bindVariable("x", x, 1); |
| 78 |
EXPECT_EQ(expr.evaluate(), 2.0); |
| 79 |
|
| 80 |
expr.unbindVariable("x"); |
| 81 |
EXPECT_EQ(expr.evaluate(), 0.0); |
| 82 |
|
| 83 |
expr.deactivateVariable("x"); |
| 84 |
EXPECT_ANY_THROW(expr.evaluate()); |
| 85 |
} |
| 86 |
//-END |
| 87 |
} // namespace <unnamed> |