Skip to content

Commit

Permalink
Add tests for constexpr and consteval
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailMihov committed Aug 6, 2024
1 parent e9c47b7 commit 8a9c9e2
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 9 deletions.
73 changes: 73 additions & 0 deletions test/ForwardMode/ConstevalTest.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// RUN: %cladclang %s -I%S/../../include -std=c++23 -oConstevalTest.out | %filecheck %s
// RUN: ./ConstevalTest.out | %filecheck_exec %s

#include <limits>

#include "clad/Differentiator/Differentiator.h"

#include "../TestUtils.h"


consteval double fn(double a, double b) {
return (a+b)/2;
}

//CHECK: consteval 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:}

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:}

constexpr double fn_test() {
if consteval {
auto dx = clad::differentiate(fn, "a");

return dx.execute(4, 7);
} else {
assert(false && "fn non-consteval context");
return std::numeric_limits<double>::infinity();
}
}

constexpr double mul_test() {
if consteval {
auto dx = clad::differentiate(mul, "a");

return dx.execute(5, 6, 10);
} else {
assert(false && "mul non-consteval context");
return std::numeric_limits<double>::infinity();
}
}

int main() {
constexpr double fn_result = fn_test();
assert(fn_result == 0.50 && "consteval fn test failed");

constexpr double mul_result = mul_test();
assert(mul_result == 99.50 && "consteval mul test failed");

/*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}*/
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// RUN: %cladclang %s -I%S/../../include -oconstexprTest.out | %filecheck %s
// RUN: ./constexprTest.out | %filecheck_exec %s
// RUN: %cladclang %s -I%S/../../include -std=c++23 -oConstexprTest.out | %filecheck %s
// RUN: ./ConstexprTest.out | %filecheck_exec %s

#include "clad/Differentiator/Differentiator.h"

#include "../TestUtils.h"
#include <limits>

#include "clad/Differentiator/Differentiator.h"

constexpr double fn(double a, double b) {
return (a+b)/2;
Expand Down Expand Up @@ -34,10 +33,34 @@ constexpr double mul(double a, double b, double c) {
//CHECK-NEXT: return _d_result;
//CHECK-NEXT:}

constexpr double fn_test() {
if consteval {
auto dx = clad::differentiate(fn, "a");

return dx.execute(4, 7);
} else {
assert(false && "fn non-consteval context");
return std::numeric_limits<double>::infinity();
}
}

constexpr double mul_test() {
if consteval {
auto dx = clad::differentiate(mul, "a");

return dx.execute(5, 6, 10);
} else {
assert(false && "mul non-consteval context");
return std::numeric_limits<double>::infinity();
}
}

int main() {
INIT_DIFFERENTIATE(fn,"a");
INIT_DIFFERENTIATE(mul, "a");
constexpr double fn_result = fn_test();
printf("%.2f\n", fn_result);
assert(fn_result == 0.50 && "consteval fn test failed");

TEST_DIFFERENTIATE(fn, 4, 7); // CHECK-EXEC: {0.50}
TEST_DIFFERENTIATE(mul, 5, 6, 10); // CHECK-EXEC: {99.00}
constexpr double mul_result = mul_test();
printf("%.2f\n", mul_result);
assert(mul_result == 99.50 && "consteval mul test failed");
}

0 comments on commit 8a9c9e2

Please sign in to comment.