Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_idbstore.c
4150 views
1
/*
2
* Copyright 2015 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#include <stdbool.h>
9
#include <stdio.h>
10
#include <string.h>
11
#include <assert.h>
12
#include <stdlib.h>
13
#include <stdio.h>
14
15
#include <emscripten/emscripten.h>
16
#include <emscripten/html5.h>
17
18
#define DB "THE_DB"
19
20
long expected;
21
int result;
22
23
void ok(void* arg) {
24
assert(expected == (long)arg);
25
}
26
27
void onerror(void* arg) {
28
assert(expected == (long)arg);
29
assert(false);
30
}
31
32
void onload(void* arg, void* ptr, int num) {
33
assert(expected == (long)arg);
34
printf("loaded %s\n", (char*)ptr);
35
assert(num == strlen(SECRET)+1);
36
assert(strcmp(ptr, SECRET) == 0);
37
}
38
39
void onbadload(void* arg, void* ptr, int num) {
40
printf("load failed, surprising\n");
41
assert(false);
42
}
43
44
void oncheck(void* arg, int exists) {
45
assert(expected == (long)arg);
46
printf("exists? %d\n", exists);
47
assert(exists);
48
}
49
50
void onchecknope(void* arg, int exists) {
51
assert(expected == (long)arg);
52
printf("exists (hopefully not)? %d\n", exists);
53
assert(!exists);
54
}
55
56
int main() {
57
result = STAGE;
58
#if STAGE == 0
59
expected = 12;
60
printf("storing %s\n", SECRET);
61
emscripten_idb_async_store(DB, "the_secret", SECRET, strlen(SECRET)+1, (void*)expected, ok, onerror);
62
#elif STAGE == 1
63
expected = 31;
64
printf("loading the_secret\n");
65
emscripten_idb_async_load(DB, "the_secret", (void*)expected, onload, onerror);
66
#elif STAGE == 2
67
expected = 44;
68
printf("deleting the_secret\n");
69
emscripten_idb_async_delete(DB, "the_secret", (void*)expected, ok, onerror);
70
#elif STAGE == 3
71
expected = 55;
72
printf("loading, should fail as we deleted\n");
73
emscripten_idb_async_load(DB, "the_secret", (void*)expected, onbadload, ok);
74
#elif STAGE == 4
75
expected = 66;
76
emscripten_idb_async_exists(DB, "the_secret", (void*)expected, oncheck, onerror);
77
#elif STAGE == 5
78
expected = 77;
79
emscripten_idb_async_exists(DB, "the_secret", (void*)expected, onchecknope, onerror);
80
#elif STAGE == 6
81
expected = 88;
82
printf("clearing\n");
83
emscripten_idb_async_clear(DB, (void*)expected, ok, onerror);
84
#else
85
assert(0);
86
#endif
87
}
88
89
90