Debian Codegraph @

packages/sacado/test/tradoptest/testgen.as.in

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