From 482d0dccdf7ad8f6018e44782b363ee0ae4358c7 Mon Sep 17 00:00:00 2001 From: Mihail Mihov Date: Sat, 19 Oct 2024 15:57:15 +0300 Subject: [PATCH] Add tests for constexpr and consteval --- test/ForwardMode/ConstevalTest.C | 53 +++++++++++++++++++++++++++ test/ForwardMode/ConstexprTest.C | 63 ++++++++++++++++++++++++++++++++ test/ForwardMode/constexprTest.C | 43 ---------------------- 3 files changed, 116 insertions(+), 43 deletions(-) create mode 100644 test/ForwardMode/ConstevalTest.C create mode 100644 test/ForwardMode/ConstexprTest.C delete mode 100644 test/ForwardMode/constexprTest.C diff --git a/test/ForwardMode/ConstevalTest.C b/test/ForwardMode/ConstevalTest.C new file mode 100644 index 000000000..0ae3829af --- /dev/null +++ b/test/ForwardMode/ConstevalTest.C @@ -0,0 +1,53 @@ +// RUN: %cladclang %s -I%S/../../include -std=c++23 -oConstevalTest.out | %filecheck %s +// RUN: ./ConstevalTest.out | %filecheck_exec %s +// UNSUPPORTED: clang-8, clang-9, clang-10, clang-11, clang-12, clang-13, clang-14, clang-15 + +#include "clad/Differentiator/Differentiator.h" + +consteval double fn(double x, double y) { + return (x+y)/2; +} + +//CHECK: consteval double fn_darg0(double x, double y) { +//CHECK-NEXT: double _d_x = 1; +//CHECK-NEXT: double _d_y = 0; +//CHECK-NEXT: double _t0 = (x + y); +//CHECK-NEXT: return ((_d_x + _d_y) * 2 - _t0 * 0) / (2 * 2); +//CHECK-NEXT:} + +consteval double mul(double a, double b, double c) { + double val = 99.00; + double result = val * a + 100 - b + c; + return result; +} + +//CHECK: consteval double mul_darg0(double a, double b, double c) { +//CHECK-NEXT: double _d_a = 1; +//CHECK-NEXT: double _d_b = 0; +//CHECK-NEXT: double _d_c = 0; +//CHECK-NEXT: double _d_val = 0.; +//CHECK-NEXT: double val = 99.; +//CHECK-NEXT: double _d_result = _d_val * a + val * _d_a + 0 - _d_b + _d_c; +//CHECK-NEXT: double result = val * a + 100 - b + c; +//CHECK-NEXT: return _d_result; +//CHECK-NEXT:} + +consteval double fn_test() { + auto dx = clad::differentiate(fn, "x"); + + return dx.execute(4, 7); +} + +consteval double mul_test() { + auto dx = clad::differentiate(mul, "a"); + + return dx.execute(5, 6, 10); +} + +int main() { + constexpr double fn_result = fn_test(); + printf("%.2f\n", fn_result); // CHECK-EXEC: 0.50 + + constexpr double mul_result = mul_test(); + printf("%.2f\n", mul_result); // CHECK-EXEC: 99.00 +} diff --git a/test/ForwardMode/ConstexprTest.C b/test/ForwardMode/ConstexprTest.C new file mode 100644 index 000000000..0c7e195ae --- /dev/null +++ b/test/ForwardMode/ConstexprTest.C @@ -0,0 +1,63 @@ +// RUN: %cladclang %s -I%S/../../include -std=c++23 -oConstexprTest.out | %filecheck %s +// RUN: ./ConstexprTest.out | %filecheck_exec %s +// UNSUPPORTED: clang-8, clang-9, clang-10, clang-11, clang-12, clang-13, clang-14, clang-15 + +#include "clad/Differentiator/Differentiator.h" + +constexpr double fn(double x, double y) { + return (x + y) / 2; +} + +//CHECK: constexpr double fn_darg0(double x, double y) { +//CHECK-NEXT: double _d_x = 1; +//CHECK-NEXT: double _d_y = 0; +//CHECK-NEXT: double _t0 = (x + y); +//CHECK-NEXT: return ((_d_x + _d_y) * 2 - _t0 * 0) / (2 * 2); +//CHECK-NEXT:} + +constexpr double mul(double a, double b, double c) { + double val = 99.00; + double result = val * a + 100 - b + c; + return result; +} + +//CHECK: constexpr double mul_darg0(double a, double b, double c) { +//CHECK-NEXT: double _d_a = 1; +//CHECK-NEXT: double _d_b = 0; +//CHECK-NEXT: double _d_c = 0; +//CHECK-NEXT: double _d_val = 0.; +//CHECK-NEXT: double val = 99.; +//CHECK-NEXT: double _d_result = _d_val * a + val * _d_a + 0 - _d_b + _d_c; +//CHECK-NEXT: double result = val * a + 100 - b + c; +//CHECK-NEXT: return _d_result; +//CHECK-NEXT:} + +constexpr double fn_test() { + if consteval { + auto dx = clad::differentiate(fn, "x"); + + return dx.execute(4, 7); + } else { + assert(false && "fn non-immediate context"); + return -1.; + } +} + +constexpr double mul_test() { + if consteval { + auto dx = clad::differentiate(mul, "a"); + + return dx.execute(5, 6, 10); + } else { + assert(false && "mul non-immediate context"); + return -1.; + } +} + +int main() { + constexpr double fn_result = fn_test(); + printf("%.2f\n", fn_result); // CHECK-EXEC: 0.50 + + constexpr double mul_result = mul_test(); + printf("%.2f\n", mul_result); // CHECK-EXEC: 99.0 +} diff --git a/test/ForwardMode/constexprTest.C b/test/ForwardMode/constexprTest.C deleted file mode 100644 index 5ead5b1b0..000000000 --- a/test/ForwardMode/constexprTest.C +++ /dev/null @@ -1,43 +0,0 @@ -// RUN: %cladclang %s -I%S/../../include -oconstexprTest.out | %filecheck %s -// RUN: ./constexprTest.out | %filecheck_exec %s - -#include "clad/Differentiator/Differentiator.h" - -#include "../TestUtils.h" - - -constexpr double fn(double a, double b) { - return (a+b)/2; -} - -//CHECK: constexpr double fn_darg0(double a, double b) { -//CHECK-NEXT: double _d_a = 1; -//CHECK-NEXT: double _d_b = 0; -//CHECK-NEXT: double _t0 = (a + b); -//CHECK-NEXT: return ((_d_a + _d_b) * 2 - _t0 * 0) / (2 * 2); -//CHECK-NEXT:} - -constexpr double mul(double a, double b, double c) { - double val = 99.00; - double result = val * a + 100 - b + c; - return result; -} - -//CHECK: constexpr double mul_darg0(double a, double b, double c) { -//CHECK-NEXT: double _d_a = 1; -//CHECK-NEXT: double _d_b = 0; -//CHECK-NEXT: double _d_c = 0; -//CHECK-NEXT: double _d_val = 0.; -//CHECK-NEXT: double val = 99.; -//CHECK-NEXT: double _d_result = _d_val * a + val * _d_a + 0 - _d_b + _d_c; -//CHECK-NEXT: double result = val * a + 100 - b + c; -//CHECK-NEXT: return _d_result; -//CHECK-NEXT:} - -int main() { - INIT_DIFFERENTIATE(fn,"a"); - INIT_DIFFERENTIATE(mul, "a"); - - TEST_DIFFERENTIATE(fn, 4, 7); // CHECK-EXEC: {0.50} - TEST_DIFFERENTIATE(mul, 5, 6, 10); // CHECK-EXEC: {99.00} -}