| 1 |
#!/bin/sh |
| 2 |
cat <<\! |
| 3 |
/* Try to test all combinations of types and operations with RAD (nontemplated) */ |
| 4 |
|
| 5 |
#include "Sacado_rad.hpp" |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
typedef IndepADvar AI; |
| 9 |
typedef ADvar A; |
| 10 |
typedef ConstADvar C; |
| 11 |
typedef ADvari Ai; |
| 12 |
typedef const IndepADvar cAI; |
| 13 |
typedef const ADvar cA; |
| 14 |
typedef const ConstADvar cC; |
| 15 |
typedef const ADvari cAi; |
| 16 |
static int rc; |
| 17 |
|
| 18 |
/* This is to be run through an awk program that changes lines */ |
| 19 |
/* with "BINTEST" or "UNOPTEST" at the beginning of the line into */ |
| 20 |
/* a the desired C++ (which we can then inspect). */ |
| 21 |
|
| 22 |
void |
| 23 |
botch(const char *what, double wanted, double got) |
| 24 |
{ |
| 25 |
printf("%s: expected %g, got %g, diff = %.2g\n", what, wanted, got, wanted-got); |
| 26 |
rc = 1; |
| 27 |
} |
| 28 |
|
| 29 |
const double tol = 5e-16; |
| 30 |
|
| 31 |
int |
| 32 |
differ(double a, double b) |
| 33 |
{ |
| 34 |
double d = a - b; |
| 35 |
if (d < 0.) |
| 36 |
d = -d; |
| 37 |
if (a < 0.) |
| 38 |
a = -a; |
| 39 |
if (b < 0.) |
| 40 |
b = -b; |
| 41 |
if (a < b) |
| 42 |
a = b; |
| 43 |
if (a > 0.) |
| 44 |
d /= a; |
| 45 |
return d > tol; |
| 46 |
} |
| 47 |
|
| 48 |
#ifndef RAD_EQ_ALIAS |
| 49 |
#define Plus_dx 1. |
| 50 |
#else |
| 51 |
#ifdef RAD_AUTO_AD_Const |
| 52 |
#define Plus_dx 1. |
| 53 |
#else |
| 54 |
#define Plus_dx 0. |
| 55 |
#endif |
| 56 |
#endif |
| 57 |
|
| 58 |
int |
| 59 |
main(void) |
| 60 |
{ |
| 61 |
AI xAI, yAI; |
| 62 |
A fA, xA, yA; |
| 63 |
C xC, yC; |
| 64 |
double dx, dy, f, xd, yd; |
| 65 |
long xL, yL; |
| 66 |
int xi, yi; |
| 67 |
|
| 68 |
rc = 0; |
| 69 |
! |
| 70 |
@AWK@ ' |
| 71 |
BEGIN { |
| 72 |
suf[0] = "AI" |
| 73 |
suf[1] = "A" |
| 74 |
suf[2] = "C" |
| 75 |
suf[3] = "Ai" |
| 76 |
suf[4] = "d" |
| 77 |
suf[5] = "L" |
| 78 |
suf[6] = "i" |
| 79 |
cast[0] = cast[1] = cast[2] = cast[3] = cast[4] = "" |
| 80 |
cast[5] = "(long)" |
| 81 |
cast[6] = "(int)" |
| 82 |
adj[0] = adj[1] = adj[2] = "adj()" |
| 83 |
adj[3] = "aval" |
| 84 |
} |
| 85 |
function crunch(op,i,j,cL,cR) |
| 86 |
{ |
| 87 |
L = suf[i] |
| 88 |
R = suf[j] |
| 89 |
newcontext = 0 |
| 90 |
if (cL + cR || i ==3 || j == 3) { |
| 91 |
newcontext = 1 |
| 92 |
printf "\t{\n" |
| 93 |
if (cL + cR) printf "\tA::aval_reset();\n" |
| 94 |
} |
| 95 |
if (cL) { |
| 96 |
x = "xc" L |
| 97 |
printf "\tc%s %s(xd);\n", L, x |
| 98 |
} |
| 99 |
else { |
| 100 |
x = "x" L |
| 101 |
if (i == 3) printf "\t%s %s(xd);\n", L, x |
| 102 |
else if (i != 4) printf "\t%s = %sxd;\n", x, cast[i] |
| 103 |
} |
| 104 |
if (cR) { |
| 105 |
y = "yc" R |
| 106 |
printf "\tc%s %s(yd);\n", R, y |
| 107 |
} |
| 108 |
else { |
| 109 |
y = "y" R |
| 110 |
if (j == 3) printf "\t%s %s(yd);\n", R, y |
| 111 |
else if (j != 4) printf "\t%s = %syd;\n", y, cast[j] |
| 112 |
} |
| 113 |
if (infix) opname = sprintf("%s %s %s", x, op, y) |
| 114 |
else opname = sprintf("%s(%s,%s)", op, x, y) |
| 115 |
printf "\tfA = %s;\n\tA::Gradcomp();\n", opname |
| 116 |
printf "\tif (differ(fA.val(), f)) botch(\"fA = %s\", f, fA.val());\n",opname |
| 117 |
if (i < 4) |
| 118 |
printf "\telse if (differ(%s.%s, dx)) botch(\"d %s/dx\", dx, %s.%s);\n",x,adj[i],opname,x,adj[i] |
| 119 |
if (j < 4) |
| 120 |
printf "\telse if (differ(%s.%s, dy)) botch(\"d %s/dy\", dy, %s.%s);\n",y,adj[j],opname,y,adj[j] |
| 121 |
if (newcontext) printf "\t}\n" |
| 122 |
printf "\n" |
| 123 |
} |
| 124 |
|
| 125 |
function crunch1(op, i, cL) |
| 126 |
{ |
| 127 |
L = suf[i] |
| 128 |
newcontext = 0 |
| 129 |
if (cL || i == 3) { |
| 130 |
newcontext = 1 |
| 131 |
printf "\t{\n" |
| 132 |
if (cL) printf "\tA::aval_reset();\n" |
| 133 |
x = "xc" L |
| 134 |
printf "\tc%s %s(xd);\n", L, x |
| 135 |
} |
| 136 |
else { |
| 137 |
x = "x" L |
| 138 |
if (i == 3) printf "\t%s %s(xd);\n", L, x |
| 139 |
else if (i != 4) printf "\t%s = %sxd;\n", x, cast[i] |
| 140 |
} |
| 141 |
xx = x |
| 142 |
if (copy) xx = sprintf("copy(%s)", x) |
| 143 |
if (infix) opname = sprintf("%s%s", op, xx) |
| 144 |
else opname = sprintf("%s(%s)", op, xx) |
| 145 |
printf "\tfA = %s;\n\tA::Gradcomp();\n", opname |
| 146 |
printf "\tif (differ(fA.val(), f)) botch(\"fA = %s\", f, fA.val());\n",opname |
| 147 |
printf "\telse if (differ(%s.%s, dx)) botch(\"d %s/dx\", dx, %s.%s);\n",x,adj[i],opname,x,adj[i] |
| 148 |
if (newcontext) printf "\t}\n" |
| 149 |
printf "\n" |
| 150 |
} |
| 151 |
|
| 152 |
/^BINTEST/ { |
| 153 |
op = $2 |
| 154 |
infix = $8 + 0 |
| 155 |
printf "\n\t/**** Test of %s ****/\n\n", op |
| 156 |
printf "\txd = %s; yd = %s; f = %s; dx = %s; dy = %s;\n",$3,$4,$5,$6,$7 |
| 157 |
for(i = 0; i < 7; i++) { |
| 158 |
jx = 7; if (i > 3) jx = 4 |
| 159 |
for(j = 0; j < jx; j++) { |
| 160 |
crunch(op,i,j,0,0) |
| 161 |
if (i < 4) crunch(op,i,j,1,0) |
| 162 |
if (j < 4) { |
| 163 |
crunch(op,i,j,0,1) |
| 164 |
if (i < 3) crunch(op,i,j,1,1) |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
next |
| 169 |
} |
| 170 |
/^UNOPTEST/ { |
| 171 |
op = $2 |
| 172 |
infix = $6 + 0 |
| 173 |
copy = $7 + 0 |
| 174 |
printf "\n\t/**** Test of %s ****/\n\n", op |
| 175 |
printf "\txd = %s; f = %s; dx = %s;\n",$3,$4,$5 |
| 176 |
for(i = 0; i < 4; i++) { |
| 177 |
crunch1(op,i,0) |
| 178 |
crunch1(op,i,1) |
| 179 |
} |
| 180 |
next |
| 181 |
} |
| 182 |
{print}' "$*" |
| 183 |
cat <<\! |
| 184 |
|
| 185 |
if (!rc) // chatter for cppunit test, which cannot tolerate silence |
| 186 |
printf("OK\n"); |
| 187 |
return rc; |
| 188 |
} |
| 189 |
! |