Snapshot of upstream SQLite 3.40.1
[sqlcipher.git] / ext / wasm / scratchpad-wasmfs-main.js
blob56f9325de5b9fa60f029bb56ec0d16911b19c1a5
1 /*
2 2022-05-22
4 The author disclaims copyright to this source code. In place of a
5 legal notice, here is a blessing:
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.
11 ***********************************************************************
13 A basic test script for sqlite3-api.js. This file must be run in
14 main JS thread and sqlite3.js must have been loaded before it.
16 'use strict';
17 (function(){
18 const toss = function(...args){throw new Error(args.join(' '))};
19 const log = console.log.bind(console),
20 warn = console.warn.bind(console),
21 error = console.error.bind(console);
23 const stdout = log;
24 const stderr = error;
26 const test1 = function(db){
27 db.exec("create table if not exists t(a);")
28 .transaction(function(db){
29 db.prepare("insert into t(a) values(?)")
30 .bind(new Date().getTime())
31 .stepFinalize();
32 stdout("Number of values in table t:",
33 db.selectValue("select count(*) from t"));
34 });
37 const runTests = function(sqlite3){
38 const capi = sqlite3.capi,
39 oo = sqlite3.oo1,
40 wasm = sqlite3.wasm;
41 stdout("Loaded sqlite3:",capi.sqlite3_libversion(), capi.sqlite3_sourceid());
42 const persistentDir = capi.sqlite3_wasmfs_opfs_dir();
43 if(persistentDir){
44 stdout("Persistent storage dir:",persistentDir);
45 }else{
46 stderr("No persistent storage available.");
48 const startTime = performance.now();
49 let db;
50 try {
51 db = new oo.DB(persistentDir+'/foo.db');
52 stdout("DB filename:",db.filename);
53 const banner1 = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>',
54 banner2 = '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<';
56 test1
57 ].forEach((f)=>{
58 const n = performance.now();
59 stdout(banner1,"Running",f.name+"()...");
60 f(db, sqlite3);
61 stdout(banner2,f.name+"() took ",(performance.now() - n),"ms");
62 });
63 }finally{
64 if(db) db.close();
66 stdout("Total test time:",(performance.now() - startTime),"ms");
69 sqlite3InitModule(self.sqlite3TestModule).then(runTests);
70 })();