PageRenderTime 104ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/stp-0.1/tests/generated_tests/random-tests/tree.cpp

#
C++ | 409 lines | 368 code | 35 blank | 6 comment | 31 complexity | 50ae40f56d9faf9f3d04cdd492eeeb32 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * g++ -DEXT_HASH_MAP tree.cpp -I ../../c_interface -L ../../lib -lstp -o cc
  3. */
  4. #include "/home/taking/stp/timking_expt/AST/AST.h"
  5. #include <stdio.h>
  6. #include "c_interface.h"
  7. #include <stdlib.h>
  8. #include <vector>
  9. #include <unistd.h>
  10. VC vc;
  11. vector<Expr> gen_forms;
  12. vector<Expr> gen_arrays;
  13. vector<Expr> gen_terms;
  14. //unsigned TERMS;
  15. //unsigned FORMULAS;
  16. bool o0,o1,o2,o3,o4,o5,o6,o7,o8;
  17. unsigned MIN;
  18. unsigned MAX;
  19. using namespace BEEV;
  20. unsigned T;
  21. int trand(){
  22. T++;
  23. return rand();
  24. }
  25. Expr randconst(){
  26. return vc_bvConstExprFromInt(vc, 32, rand());
  27. }
  28. #define BUF_LENGTH 63
  29. char buf[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
  30. void shufflebuf(){
  31. unsigned N = BUF_LENGTH;
  32. unsigned i;
  33. while(N--){
  34. i = trand()%BUF_LENGTH;
  35. char c = buf[i];
  36. buf[i] = buf[N];
  37. buf[N] = c;
  38. }
  39. }
  40. void nonsense(){
  41. gen_arrays.push_back(NULL);
  42. }
  43. char* rstr(unsigned len){
  44. char *ret = (char *)calloc(sizeof( char),len+1);
  45. shufflebuf();
  46. unsigned i = 0;
  47. while(len--){
  48. ret[i++] = buf[trand()%BUF_LENGTH];
  49. }
  50. return ret;
  51. }
  52. Expr randvar(){
  53. char *rname = rstr(trand()%11+6);
  54. Expr e = vc_varExpr1(vc,rname,0,32);
  55. free(rname);
  56. return e;
  57. }
  58. bool isTerm(Expr e){
  59. return BEEV::BITVECTOR_TYPE == ((ASTNode*)e)->GetType();
  60. }
  61. bool isFormula(Expr e){
  62. return BEEV::BOOLEAN_TYPE == ((ASTNode*)e)->GetType();
  63. }
  64. Expr selectTerm(){
  65. unsigned i = trand()%gen_terms.size();
  66. return gen_terms[i];
  67. }
  68. Expr selectForm(){
  69. unsigned i = trand()%gen_forms.size();
  70. return gen_forms[i];
  71. }
  72. Expr selectArray(){
  73. unsigned i = trand()%gen_arrays.size();
  74. return gen_arrays[i];
  75. }
  76. Expr randarrayvar(){
  77. char *rname = rstr(trand()%11+6);
  78. Expr e = vc_varExpr1(vc,rname,32,32);
  79. free(rname);
  80. return e;
  81. }
  82. Expr randarray(){
  83. unsigned r = trand()%11;
  84. if(0 == gen_arrays.size())
  85. r = 0;
  86. switch(r){
  87. case 0:
  88. return randarrayvar();
  89. default:
  90. return vc_writeExpr(vc,selectArray(),selectTerm(),selectTerm());
  91. }
  92. }
  93. Expr randbvterm(){
  94. unsigned r = trand()%11;
  95. Expr expr, left, right;
  96. left = selectTerm();
  97. if(1 == r){
  98. right = left;
  99. left = selectArray();
  100. }else if(r > 2 ){
  101. right = selectTerm();
  102. }
  103. switch(r){
  104. case 0:
  105. expr = vc_bvNotExpr(vc, left);
  106. break;
  107. case 1:
  108. expr = vc_bvUMinusExpr(vc, left);
  109. break;
  110. case 2:
  111. expr = vc_readExpr(vc, left, right);
  112. break;
  113. case 3:
  114. expr = vc_bvPlusExpr(vc, 32, left, right);
  115. break;
  116. case 4:
  117. expr = vc_bvMinusExpr(vc, 32, left, right);
  118. break;
  119. case 5:
  120. expr = vc_bvModExpr(vc, 32, left, right);
  121. break;
  122. case 6:
  123. expr = vc_sbvDivExpr(vc, 32, left, right);
  124. break;
  125. case 7:
  126. expr = vc_sbvModExpr(vc, 32, left, right);
  127. break;
  128. case 8:
  129. expr = vc_bvOrExpr(vc, left, right);
  130. break;
  131. case 9:
  132. expr = vc_bvAndExpr(vc, left, right);
  133. break;
  134. case 10:
  135. expr = vc_bvXorExpr(vc, left, right);
  136. break;
  137. default:
  138. expr = NULL;
  139. break;
  140. }
  141. return expr;
  142. }
  143. Expr randbvform(){
  144. unsigned r = trand()%10;
  145. Expr expr,left,right;
  146. left = selectTerm();
  147. if(r > 0 )
  148. right = selectTerm();
  149. switch(r){
  150. case 0:
  151. expr = vc_bvBoolExtract(vc, left, rand()%(vc_getBVLength(vc,left)));
  152. break;
  153. case 1:
  154. expr = vc_bvLeExpr(vc, left, right);
  155. break;
  156. case 2:
  157. expr = vc_bvGtExpr(vc, left, right);
  158. break;
  159. case 3:
  160. expr = vc_bvGeExpr(vc, left, right);
  161. break;
  162. case 4:
  163. expr = vc_eqExpr(vc, left, right);
  164. break;
  165. case 5:
  166. expr = vc_sbvLtExpr(vc, left, right);
  167. break;
  168. case 6:
  169. expr = vc_sbvLeExpr(vc, left, right);
  170. break;
  171. case 7:
  172. expr = vc_sbvGtExpr(vc, left, right);
  173. break;
  174. case 8:
  175. expr = vc_sbvGeExpr(vc, left, right);
  176. break;
  177. case 9:
  178. expr = vc_bvLtExpr(vc, left, right);
  179. break;
  180. default:
  181. expr = NULL;
  182. break;
  183. }
  184. return expr;
  185. }
  186. Expr randform(){
  187. unsigned r = rand()%6;
  188. Expr expr,left,right;
  189. left = selectForm();
  190. if(r != 0){
  191. right = selectForm();
  192. }
  193. switch(r){
  194. case 0:
  195. expr = vc_notExpr(vc, left);
  196. break;
  197. case 1:
  198. expr = vc_andExpr(vc, left, right);
  199. break;
  200. case 2:
  201. expr = vc_orExpr(vc, left, right);
  202. break;
  203. case 3:
  204. expr = vc_impliesExpr(vc, left, right);
  205. break;
  206. case 4:
  207. expr = vc_iffExpr(vc, left, right);
  208. break;
  209. case 5:
  210. expr = vc_iteExpr(vc,selectForm(), left, right);
  211. break;
  212. default:
  213. expr = NULL;
  214. break;
  215. }
  216. return expr;
  217. }
  218. void printExpr(Expr x){
  219. ASTNode q = *((ASTNode*)x);
  220. q.LispPrint(cout,0);
  221. }
  222. void printArray(vector<Expr>& v){
  223. for( unsigned i=0;i<v.size();i++)
  224. printExpr(v[i]);
  225. }
  226. void cleanUp(vector<Expr>& v){
  227. while(!v.empty()){
  228. Expr elem = v.back();
  229. v.pop_back();
  230. vc_DeleteExpr(elem);
  231. }
  232. }
  233. void setUpAST(){
  234. unsigned N;
  235. N = trand()%(MAX-MIN)+MIN;
  236. for( unsigned i=0;i<N;i++)
  237. gen_terms.push_back(randconst());
  238. N = trand()%((MAX-MIN)/10)+(MIN/10);
  239. for( unsigned i=0;i<N;i++)
  240. gen_terms.push_back(randvar());
  241. gen_arrays.push_back(randarray());
  242. N = trand()%(MAX-MIN)+MIN;
  243. for( unsigned i=0;i<N;i++){
  244. if(i%10 == 0){
  245. gen_arrays.push_back(randarray());
  246. }else{
  247. Expr e = randbvterm();
  248. gen_terms.push_back(e);
  249. if(vc_getBVLength(vc,e) > 32){
  250. gen_terms.push_back(vc_bvExtract(vc, e, 32, 0));
  251. }else if(32 > vc_getBVLength(vc,e)){
  252. gen_terms.push_back(vc_bv32LeftShiftExpr(vc, 0,e));
  253. }
  254. }
  255. }
  256. N = trand()%(MAX-MIN)+MIN;
  257. for( unsigned i=0;i<N;i++)
  258. gen_forms.push_back(randbvform());
  259. gen_forms.push_back(vc_falseExpr(vc));
  260. gen_forms.push_back(vc_trueExpr(vc));
  261. N = trand()%(MAX-MIN)+MIN;
  262. for( unsigned i=0;i<N;i++)
  263. gen_forms.push_back(randform());
  264. }
  265. void runVC(){
  266. printf("\n**********\n");
  267. vc = vc_createValidityChecker();
  268. vc_setFlags('n');
  269. vc_setFlags('d');
  270. vc_setFlags('p');
  271. vc_setFlags('v');
  272. setUpAST();
  273. if(o0){
  274. printArray(gen_terms);
  275. printArray(gen_arrays);
  276. printArray(gen_forms);
  277. }
  278. Expr head = gen_forms.back();
  279. gen_forms.pop_back();
  280. if(o2){
  281. printExpr(head);
  282. }
  283. if(o3){
  284. cout<<endl;
  285. cout<<vc_query(vc, head)<<endl;
  286. }
  287. if(o4){
  288. cleanUp(gen_terms);
  289. cleanUp(gen_arrays);
  290. cleanUp(gen_forms);
  291. }
  292. if(o5){
  293. printExpr(head);
  294. printf("\n");
  295. }
  296. if(o6){
  297. cout<<endl;
  298. cout<<vc_query(vc, head)<<endl;
  299. }
  300. if(o7)
  301. vc_DeleteExpr(head);
  302. if(o8)
  303. vc_Destroy(vc);
  304. }
  305. int main (int argc, char **argv) {
  306. int c;
  307. unsigned N;
  308. unsigned seed = 0;
  309. o0=o1=o2=o3=o4=o5=o6=o7=o8=false;
  310. MAX = 30;
  311. MIN = 10;
  312. T = 0;
  313. extern char *optarg;
  314. while ((c = getopt (argc, argv, "t:s:m:M:012345678::")) != -1){
  315. switch (c)
  316. {
  317. case '0':
  318. o0 = true;
  319. break;
  320. case '1':
  321. o1 = true;
  322. break;
  323. case '2':
  324. o2 = true;
  325. break;
  326. case '3':
  327. o3 = true;
  328. break;
  329. case '4':
  330. o4 = true;
  331. break;
  332. case '5':
  333. o5 = true;
  334. break;
  335. case '6':
  336. o6 = true;
  337. break;
  338. case '7':
  339. o7 = true;
  340. break;
  341. case '8':
  342. o8 = true;
  343. break;
  344. case 's':
  345. seed = atoi(optarg);
  346. break;
  347. case 'M':
  348. MAX = atoi(optarg);
  349. break;
  350. case 'm':
  351. MIN = atoi(optarg);
  352. break;
  353. case 't':
  354. T = atoi(optarg);
  355. break;
  356. default:
  357. abort();
  358. }
  359. }
  360. srand(seed);
  361. unsigned i = T;
  362. while(i--){
  363. rand();
  364. }
  365. N = trand()%999+1;
  366. N = trand()%10+1;
  367. // N = 2;
  368. while(N--){
  369. runVC();
  370. gen_terms.clear();
  371. gen_arrays.clear();
  372. gen_forms.clear();
  373. }
  374. }