mark_test.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: mark_test.cc /main/5 1996/07/18 16:03:09 drk $ */
  24. #ifdef REGRESSION_TEST
  25. #include "mark_test.h"
  26. void print_mark_list(mark_base* bptr, char* locator)
  27. {
  28. oid_list_handler* x = bptr -> get_mark_list(locator);
  29. if ( x == 0 ) {
  30. MESSAGE(cerr, "empty oid_list");
  31. return ;
  32. }
  33. int ind = (*x) -> first();
  34. while (ind) {
  35. oid_t id = (*x) -> operator()(ind);
  36. if ( id.eq(ground) == false ) {
  37. mark_smart_ptr y(bptr, id);
  38. debug(cerr, y);
  39. }
  40. (*x) -> next(ind);
  41. }
  42. }
  43. mark_base* get_mark_base(char* path, char* name)
  44. {
  45. mark_base* ub = 0;
  46. try {
  47. ub = new mark_base(
  48. path,
  49. name,
  50. "this marks",
  51. user_base::WRITE
  52. );
  53. }
  54. catch (MMDBeXCEPtion &,e)
  55. {
  56. return 0;
  57. } end_try;
  58. if ( ub -> open_status() != user_base::SUCC ) {
  59. return 0;
  60. }
  61. return ub;
  62. }
  63. void _get_id(istream& in, oid_t& x)
  64. {
  65. static char buf[LBUFSIZ];
  66. in.getline(buf, LBUFSIZ);
  67. if ( buf[0] != 'k' || buf[1] != '\t' ) {
  68. debug(cerr, buf);
  69. throw(stringException("_get_id(): missing k\t"));
  70. }
  71. char* key = buf+2;
  72. oid_t id(key, true, false);
  73. x = id;
  74. }
  75. void _get_key(istream& in, char*& key)
  76. {
  77. static char buf[LBUFSIZ];
  78. in.getline(buf, LBUFSIZ);
  79. if ( buf[0] != 'k' || buf[1] != '\t' ) {
  80. debug(cerr, buf);
  81. throw(stringException("_get_key(): missing k\t"));
  82. }
  83. key = buf + 2;
  84. }
  85. void _get_data(istream& in, char*& data)
  86. {
  87. static char buf[LBUFSIZ];
  88. in.getline(buf, LBUFSIZ);
  89. if ( buf[0] != 'd' || buf[1] != '\t' ) {
  90. debug(cerr, buf);
  91. throw(stringException("_get_data(): missing d\t"));
  92. }
  93. data = buf + 2;
  94. }
  95. void _get_key_pos(istream& in, char*& key, int& n)
  96. {
  97. static char buf[LBUFSIZ];
  98. in.getline(buf, LBUFSIZ);
  99. if ( buf[0] != 'k' || buf[1] != '\t' ) {
  100. debug(cerr, buf);
  101. throw(stringException("_get_key_pos(): missing k\t"));
  102. }
  103. key = buf + 2;
  104. char* nextToken = strchr(key, '\t');
  105. if ( nextToken == 0 )
  106. throw(stringException("can't find position (integer)."));
  107. *nextToken = 0;
  108. n = atoi(nextToken+1);
  109. }
  110. oid_t get_nth_mark_id(mark_base* ub, char* key, int n)
  111. {
  112. oid_list_handler* handle = ub -> get_mark_list(key);
  113. if ( handle == 0 ) {
  114. throw(stringException("empty mark list"));
  115. }
  116. int ind = (*handle) -> first();
  117. int i = 0;
  118. while (ind) {
  119. if ( n == i ) {
  120. oid_t id = (*handle) -> operator()(ind);
  121. delete handle;
  122. return id;
  123. }
  124. (*handle) -> next(ind);
  125. i++;
  126. }
  127. int count = (*handle)->count();
  128. delete handle;
  129. throw(boundaryException(0, count, i));
  130. }
  131. //////////////
  132. // create
  133. // k \t locator
  134. // d \t data
  135. //////////////
  136. int _create_subtest(char* buf, istream& in, mark_base* ub)
  137. {
  138. char* key, *data;
  139. mark_smart_ptr* x = 0;
  140. if ( strcmp(buf, "create") == 0 ) {
  141. cout << ">>> create:" << endl;
  142. _get_key(in, key);
  143. _get_data(in, data);
  144. cout << "---- new mark smtart_ptr ..." << endl;
  145. x = new mark_smart_ptr(ub, key);
  146. cout << "---- updating ..." << endl;
  147. x -> update_usermark(data, strlen(data));
  148. delete x;
  149. cout << "<<< create:" << endl;
  150. } else
  151. return 1;
  152. }
  153. //////////////
  154. // get_by_key
  155. // k \t locator \t n
  156. //////////////
  157. _get_by_key_subtest(char* buf, istream& in, mark_base* ub)
  158. {
  159. char* key;
  160. int n;
  161. mark_smart_ptr* x = 0;
  162. if ( strcmp(buf, "get_by_key") == 0 ) {
  163. cout << ">>> get_by_key:" << endl;
  164. _get_key_pos(in, key, n);
  165. oid_t id = get_nth_mark_id(ub, key, n);
  166. x = new mark_smart_ptr(ub, id);
  167. cout << x->node_locator();
  168. cout << "\n";
  169. cout << *(x->mark_value());
  170. cout << "\n";
  171. delete x;
  172. cout << "<<< get_by_key:" << endl;
  173. } else return 1;
  174. }
  175. //////////////
  176. // get_by_oid
  177. // k \t oidInAscii
  178. //////////////
  179. _get_by_oid_subtest(char* buf, istream& in, mark_base* ub)
  180. {
  181. char* key;
  182. mark_smart_ptr* x = 0;
  183. if ( strcmp(buf, "get_by_oid") == 0 ) {
  184. _get_key(in, key);
  185. oid_t id(key, true, false);
  186. x = new mark_smart_ptr(ub, id);
  187. cout << x->node_locator();
  188. cout << "\n";
  189. cout << *(x->mark_value());
  190. cout << "\n";
  191. delete x;
  192. } else
  193. return 1;
  194. }
  195. //////////////
  196. // show_for_a_locator
  197. // k \t locator
  198. //////////////
  199. _show_for_a_locator_subtest(char* buf, istream& in, mark_base* ub)
  200. {
  201. char* key;
  202. if ( strcmp(buf, "show_for_a_locator") == 0 ) {
  203. cout << ">>> show_for_a_locator:" << endl;
  204. _get_key(in, key);
  205. oid_list_handler* z = ub-> get_mark_list(key);
  206. if ( z == 0 )
  207. throw(stringException("empty oid_list"));
  208. int ind = (*z) -> first();
  209. while (ind) {
  210. oid_t id = (*z) -> operator()(ind);
  211. if ( id.eq(ground) == false ) {
  212. mark_smart_ptr y(ub, id);
  213. cout << y.its_oid();
  214. cout << "\n";
  215. cout << y.node_locator();
  216. cout << "\n";
  217. cout << *(y.mark_value());
  218. cout << "\n";
  219. }
  220. (*z) -> next(ind);
  221. }
  222. delete z;
  223. cout << "<<< show_for_a_locator" << endl;
  224. } else return 1;
  225. }
  226. //////////////
  227. //show_all_marks_subtest
  228. //////////////
  229. _show_all_marks_subtest(char* buf, istream& in, mark_base* ub)
  230. {
  231. if ( strcmp(buf, "show_all_marks") == 0 ) {
  232. cout << ">>> show_all_marks" << endl;
  233. mmdb_pos_t ind = ub -> first();
  234. while (ind) {
  235. oid_t id = ub -> get_mark_oid(ind);
  236. if ( id.eq(ground) == false ) {
  237. mark_smart_ptr y(ub, id);
  238. cout << y.its_oid();
  239. cout << "\n";
  240. cout << y.node_locator();
  241. cout << "\n";
  242. cout << *(y.mark_value());
  243. cout << "\n";
  244. }
  245. ub -> next(ind);
  246. }
  247. cout << "<<< show_all_marks." << endl;
  248. } else
  249. return 1;
  250. }
  251. //////////////////////////////////////
  252. //////////////////////////////////////
  253. int _update_by_id_subtest(istream& in, oid_t id, mark_base* ub)
  254. {
  255. char* data;
  256. mark_smart_ptr* x = 0;
  257. _get_data(in, data);
  258. x = new mark_smart_ptr(ub, id);
  259. cout << x -> its_oid() << "\n";
  260. x -> update_usermark(data, strlen(data));
  261. delete x;
  262. x = new mark_smart_ptr(ub, id);
  263. pstring* y = x->mark_value();
  264. if ( y -> size() != strlen(data) || strcmp(y -> get(), data) != 0 )
  265. {
  266. debug(cerr, y->size());
  267. debug(cerr, (long)strlen(data));
  268. debug(cerr, y -> get());
  269. debug(cerr, data);
  270. throw(stringException("improperly updated mark"));
  271. }
  272. delete x;
  273. return 0;
  274. }
  275. //////////////
  276. // update_by_key
  277. // k \t locator \t n
  278. // d \t data
  279. //////////////
  280. _update_by_key_subtest(char* buf, istream& in, mark_base* ub)
  281. {
  282. char* key;
  283. int n;
  284. if ( strcmp(buf, "update_by_key") == 0 ) {
  285. cout << ">>> update_by_key" << endl;
  286. _get_key_pos(in, key, n);
  287. oid_t id = get_nth_mark_id(ub, key, n);
  288. _update_by_id_subtest(in, id, ub);
  289. cout << "<<< update_by_key" << endl;
  290. return 0;
  291. } else
  292. return 1;
  293. }
  294. ////////////////////
  295. // update_by_id
  296. // k \t oid_tInAscii
  297. // d \t data
  298. ////////////////////
  299. _update_by_id_subtest(char* buf, istream& in, mark_base* ub)
  300. {
  301. if ( strcmp(buf, "update_by_id") == 0 ) {
  302. cout << ">>> _update_by_id_subtest" << endl;
  303. oid_t id;
  304. _get_id(in, id);
  305. _update_by_id_subtest(in, id, ub);
  306. cout << "<<< _update_by_id_subtest" << endl;
  307. return 0;
  308. } else
  309. return 1;
  310. }
  311. //////////////
  312. // delete_by_key
  313. // k \t locator \t n
  314. //////////////
  315. _delete_by_key_subtest(char* buf, istream& in, mark_base* ub)
  316. {
  317. mark_smart_ptr* x = 0;
  318. char* key; int n;
  319. if ( strcmp(buf, "delete_by_key") == 0 ) {
  320. cout << ">>> _delete_by_key_subtest" << endl;
  321. _get_key_pos(in, key, n);
  322. oid_t id = get_nth_mark_id(ub, key, n);
  323. x = new mark_smart_ptr(ub, id);
  324. x -> remove_from_db();
  325. delete x;
  326. cout << "<<< _delete_by_key_subtest" << endl;
  327. } else return 1;
  328. }
  329. //////////////
  330. // delete_by_oid
  331. // k \t oidInAscii
  332. //////////////
  333. _delete_by_oid_subtest(char* buf, istream& in, mark_base* ub)
  334. {
  335. mark_smart_ptr* x = 0;
  336. if ( strcmp(buf, "delete_by_oid") == 0 ) {
  337. oid_t id;
  338. _get_id(in, id);
  339. x = new mark_smart_ptr(ub, id);
  340. x -> remove_from_db();
  341. delete x;
  342. } else return 1;
  343. }
  344. int mark_test(int argc, char** argv)
  345. {
  346. int ok = 2;
  347. if ( strcmp(argv[1], "create_mark_base") == 0 ) {
  348. if ( argc != 4 ) {
  349. MESSAGE(cerr, "args: create_mark_base path name");
  350. ok = -1;
  351. } else {
  352. mark_base* ub = get_mark_base(argv[2], argv[3]);
  353. if ( ub == 0 || ub -> open_status() != user_base::SUCC ) {
  354. ok = -1;
  355. } else {
  356. delete ub;
  357. ok = 0;
  358. }
  359. }
  360. } else
  361. if ( strcmp(argv[1], "mark_comprehensive") == 0 ) {
  362. if ( argc != 5 ) {
  363. MESSAGE(cerr,
  364. "mark_comprehensive args: mark_comprehensive test_file path name");
  365. ok = -1;
  366. } else {
  367. mark_base* ub = get_mark_base(argv[3], argv[4]);
  368. if ( ub == 0 || ub -> open_status() != user_base::SUCC ) {
  369. ok = -1;
  370. } else {
  371. fstream in(argv[2], ios::in);
  372. int i=1;
  373. char buf[LBUFSIZ];
  374. while ( in.getline(buf, LBUFSIZ) ) {
  375. if ( buf[0] == '#' )
  376. continue;
  377. if ( _create_subtest(buf, in, ub) != 1 )
  378. continue;
  379. if ( _get_by_key_subtest(buf, in, ub) != 1 )
  380. continue;
  381. if ( _get_by_oid_subtest(buf, in, ub) != 1 )
  382. continue;
  383. if ( _show_for_a_locator_subtest(buf, in, ub) != 1 )
  384. continue;
  385. if ( _show_all_marks_subtest(buf, in, ub) != 1 )
  386. continue;
  387. if ( _update_by_key_subtest(buf, in, ub) != 1 )
  388. continue;
  389. if ( _update_by_id_subtest(buf, in, ub) != 1 )
  390. continue;
  391. if ( _delete_by_key_subtest(buf, in, ub) != 1 )
  392. continue;
  393. if ( _delete_by_oid_subtest(buf, in, ub) != 1 ) {
  394. debug(cerr, buf);
  395. throw(stringException("bad subtest command"));
  396. }
  397. if ( i % 50 == 0 )
  398. MESSAGE(cerr, form("%d processed", i));
  399. i++;
  400. }
  401. }
  402. delete ub;
  403. ok = 0;
  404. }
  405. }
  406. return ok;
  407. }
  408. #endif