test9.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. ** 2007 March 29
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. **
  13. ** This file contains obscure tests of the C-interface required
  14. ** for completeness. Test code is written in C for these cases
  15. ** as there is not much point in binding to Tcl.
  16. */
  17. #include "sqliteInt.h"
  18. #include "tcl.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. /*
  22. ** c_collation_test
  23. */
  24. static int c_collation_test(
  25. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  26. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  27. int objc, /* Number of arguments */
  28. Tcl_Obj *CONST objv[] /* Command arguments */
  29. ){
  30. const char *zErrFunction = "N/A";
  31. sqlite3 *db;
  32. int rc;
  33. if( objc!=1 ){
  34. Tcl_WrongNumArgs(interp, 1, objv, "");
  35. return TCL_ERROR;
  36. }
  37. /* Open a database. */
  38. rc = sqlite3_open(":memory:", &db);
  39. if( rc!=SQLITE_OK ){
  40. zErrFunction = "sqlite3_open";
  41. goto error_out;
  42. }
  43. rc = sqlite3_create_collation(db, "collate", 456, 0, 0);
  44. if( rc!=SQLITE_MISUSE ){
  45. sqlite3_close(db);
  46. zErrFunction = "sqlite3_create_collation";
  47. goto error_out;
  48. }
  49. sqlite3_close(db);
  50. return TCL_OK;
  51. error_out:
  52. Tcl_ResetResult(interp);
  53. Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
  54. return TCL_ERROR;
  55. }
  56. /*
  57. ** c_realloc_test
  58. */
  59. static int c_realloc_test(
  60. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  61. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  62. int objc, /* Number of arguments */
  63. Tcl_Obj *CONST objv[] /* Command arguments */
  64. ){
  65. void *p;
  66. const char *zErrFunction = "N/A";
  67. if( objc!=1 ){
  68. Tcl_WrongNumArgs(interp, 1, objv, "");
  69. return TCL_ERROR;
  70. }
  71. p = sqlite3_malloc(5);
  72. if( !p ){
  73. zErrFunction = "sqlite3_malloc";
  74. goto error_out;
  75. }
  76. /* Test that realloc()ing a block of memory to a negative size is
  77. ** the same as free()ing that memory.
  78. */
  79. p = sqlite3_realloc(p, -1);
  80. if( p ){
  81. zErrFunction = "sqlite3_realloc";
  82. goto error_out;
  83. }
  84. return TCL_OK;
  85. error_out:
  86. Tcl_ResetResult(interp);
  87. Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
  88. return TCL_ERROR;
  89. }
  90. /*
  91. ** c_misuse_test
  92. */
  93. static int c_misuse_test(
  94. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  95. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  96. int objc, /* Number of arguments */
  97. Tcl_Obj *CONST objv[] /* Command arguments */
  98. ){
  99. const char *zErrFunction = "N/A";
  100. sqlite3 *db = 0;
  101. sqlite3_stmt *pStmt;
  102. int rc;
  103. if( objc!=1 ){
  104. Tcl_WrongNumArgs(interp, 1, objv, "");
  105. return TCL_ERROR;
  106. }
  107. /* Open a database. Then close it again. We need to do this so that
  108. ** we have a "closed database handle" to pass to various API functions.
  109. */
  110. rc = sqlite3_open(":memory:", &db);
  111. if( rc!=SQLITE_OK ){
  112. zErrFunction = "sqlite3_open";
  113. goto error_out;
  114. }
  115. sqlite3_close(db);
  116. rc = sqlite3_errcode(db);
  117. if( rc!=SQLITE_MISUSE ){
  118. zErrFunction = "sqlite3_errcode";
  119. goto error_out;
  120. }
  121. pStmt = (sqlite3_stmt*)1234;
  122. rc = sqlite3_prepare(db, 0, 0, &pStmt, 0);
  123. if( rc!=SQLITE_MISUSE ){
  124. zErrFunction = "sqlite3_prepare";
  125. goto error_out;
  126. }
  127. assert( pStmt==0 ); /* Verify that pStmt is zeroed even on a MISUSE error */
  128. pStmt = (sqlite3_stmt*)1234;
  129. rc = sqlite3_prepare_v2(db, 0, 0, &pStmt, 0);
  130. if( rc!=SQLITE_MISUSE ){
  131. zErrFunction = "sqlite3_prepare_v2";
  132. goto error_out;
  133. }
  134. assert( pStmt==0 );
  135. #ifndef SQLITE_OMIT_UTF16
  136. pStmt = (sqlite3_stmt*)1234;
  137. rc = sqlite3_prepare16(db, 0, 0, &pStmt, 0);
  138. if( rc!=SQLITE_MISUSE ){
  139. zErrFunction = "sqlite3_prepare16";
  140. goto error_out;
  141. }
  142. assert( pStmt==0 );
  143. pStmt = (sqlite3_stmt*)1234;
  144. rc = sqlite3_prepare16_v2(db, 0, 0, &pStmt, 0);
  145. if( rc!=SQLITE_MISUSE ){
  146. zErrFunction = "sqlite3_prepare16_v2";
  147. goto error_out;
  148. }
  149. assert( pStmt==0 );
  150. #endif
  151. return TCL_OK;
  152. error_out:
  153. Tcl_ResetResult(interp);
  154. Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0);
  155. return TCL_ERROR;
  156. }
  157. /*
  158. ** Register commands with the TCL interpreter.
  159. */
  160. int Sqlitetest9_Init(Tcl_Interp *interp){
  161. static struct {
  162. char *zName;
  163. Tcl_ObjCmdProc *xProc;
  164. void *clientData;
  165. } aObjCmd[] = {
  166. { "c_misuse_test", c_misuse_test, 0 },
  167. { "c_realloc_test", c_realloc_test, 0 },
  168. { "c_collation_test", c_collation_test, 0 },
  169. };
  170. int i;
  171. for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
  172. Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
  173. aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
  174. }
  175. return TCL_OK;
  176. }