1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Eliminate code duplication in the two JNI tester classes.

FossilOrigin-Name: 63e7bbe3d5fcfb531f9d7fa88398c1191570e69b5d11adcb9c5e64b8345b4e6c
This commit is contained in:
stephan
2023-08-04 13:27:45 +00:00
parent 5e208f712f
commit c7795cfd47
4 changed files with 44 additions and 103 deletions

View File

@ -23,16 +23,16 @@ public class Tester1 {
static final Metrics metrics = new Metrics();
private static <T> void out(T val){
public static <T> void out(T val){
System.out.print(val);
}
private static <T> void outln(T val){
public static <T> void outln(T val){
System.out.println(val);
}
private static int affirmCount = 0;
private static void affirm(Boolean v){
static int affirmCount = 0;
public static void affirm(Boolean v){
++affirmCount;
if( !v ) throw new RuntimeException("Assertion failed.");
}
@ -50,21 +50,23 @@ public class Tester1 {
affirm(SQLITE_MAX_TRIGGER_DEPTH>0);
}
private static void testCompileOption(){
int i = 0;
String optName;
outln("compile options:");
for( ; null != (optName = sqlite3_compileoption_get(i)); ++i){
outln("\t"+optName+"\t (used="+
sqlite3_compileoption_used(optName)+")");
}
public static sqlite3 createNewDb(){
sqlite3 db = new sqlite3();
affirm(0 == db.getNativePointer());
int rc = sqlite3_open(":memory:", db);
++metrics.dbOpen;
affirm(0 == rc);
affirm(0 != db.getNativePointer());
rc = sqlite3_busy_timeout(db, 2000);
affirm( 0 == rc );
return db;
}
private static void execSql(sqlite3 db, String[] sql){
public static void execSql(sqlite3 db, String[] sql){
execSql(db, String.join("", sql));
}
private static int execSql(sqlite3 db, boolean throwOnError, String sql){
public static int execSql(sqlite3 db, boolean throwOnError, String sql){
OutputPointer.Int32 oTail = new OutputPointer.Int32();
final byte[] sqlUtf8 = sql.getBytes(StandardCharsets.UTF_8);
int pos = 0, n = 1;
@ -95,9 +97,11 @@ public class Tester1 {
if(SQLITE_ROW==rc || SQLITE_DONE==rc) rc = 0;
return rc;
}
private static void execSql(sqlite3 db, String sql){
public static void execSql(sqlite3 db, String sql){
execSql(db, true, sql);
}
private static void testOpenDb1(){
sqlite3 db = new sqlite3();
affirm(0 == db.getNativePointer());
@ -109,6 +113,17 @@ public class Tester1 {
affirm(0 == db.getNativePointer());
}
private static void testCompileOption(){
int i = 0;
String optName;
outln("compile options:");
for( ; null != (optName = sqlite3_compileoption_get(i)); ++i){
outln("\t"+optName+"\t (used="+
sqlite3_compileoption_used(optName)+")");
}
}
private static void testOpenDb2(){
sqlite3 db = new sqlite3();
affirm(0 == db.getNativePointer());
@ -122,18 +137,6 @@ public class Tester1 {
affirm(0 == db.getNativePointer());
}
private static sqlite3 createNewDb(){
sqlite3 db = new sqlite3();
affirm(0 == db.getNativePointer());
int rc = sqlite3_open(":memory:", db);
++metrics.dbOpen;
affirm(0 == rc);
affirm(0 != db.getNativePointer());
rc = sqlite3_busy_timeout(db, 2000);
affirm( 0 == rc );
return db;
}
private static void testPrepare123(){
sqlite3 db = createNewDb();
int rc;

View File

@ -13,75 +13,10 @@
*/
package org.sqlite.jni;
import static org.sqlite.jni.SQLite3Jni.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static org.sqlite.jni.Tester1.*;
public class TesterFts5 {
private static <T> void out(T val){
System.out.print(val);
}
private static <T> void outln(T val){
System.out.println(val);
}
private static int affirmCount = 0;
private static void affirm(Boolean v){
++affirmCount;
if( !v ) throw new RuntimeException("Assertion failed.");
}
private static void execSql(sqlite3 db, String[] sql){
execSql(db, String.join("", sql));
}
private static int execSql(sqlite3 db, boolean throwOnError, String sql){
OutputPointer.Int32 oTail = new OutputPointer.Int32();
final byte[] sqlUtf8 = sql.getBytes(StandardCharsets.UTF_8);
int pos = 0, n = 1;
byte[] sqlChunk = sqlUtf8;
sqlite3_stmt stmt = new sqlite3_stmt();
int rc = 0;
while(pos < sqlChunk.length){
if(pos > 0){
sqlChunk = Arrays.copyOfRange(sqlChunk, pos,
sqlChunk.length);
}
if( 0==sqlChunk.length ) break;
rc = sqlite3_prepare_v2(db, sqlChunk, stmt, oTail);
affirm(0 == rc);
pos = oTail.getValue();
affirm(0 != stmt.getNativePointer());
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
affirm(0 == stmt.getNativePointer());
if(0!=rc && SQLITE_ROW!=rc && SQLITE_DONE!=rc){
if(throwOnError){
throw new RuntimeException("db op failed with rc="+rc);
}else{
break;
}
}
}
if(SQLITE_ROW==rc || SQLITE_DONE==rc) rc = 0;
return rc;
}
private static void execSql(sqlite3 db, String sql){
execSql(db, true, sql);
}
private static sqlite3 createNewDb(){
sqlite3 db = new sqlite3();
affirm(0 == db.getNativePointer());
int rc = sqlite3_open(":memory:", db);
affirm(0 == rc);
affirm(0 != db.getNativePointer());
rc = sqlite3_busy_timeout(db, 2000);
affirm( 0 == rc );
return db;
}
private static void test1(){
Fts5ExtensionApi fea = Fts5ExtensionApi.getInstance();
affirm( null != fea );
@ -90,12 +25,15 @@ public class TesterFts5 {
}
public TesterFts5(){
int oldAffirmCount = Tester1.affirmCount;
Tester1.affirmCount = 0;
final long timeStart = System.nanoTime();
test1();
final long timeEnd = System.nanoTime();
outln("FTS5 Tests done. Metrics:");
outln("\tAssertions checked: "+affirmCount);
outln("\tAssertions checked: "+Tester1.affirmCount);
outln("\tTotal time = "
+((timeEnd - timeStart)/1000000.0)+"ms");
Tester1.affirmCount = oldAffirmCount;
}
}