Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_idbstore_sync.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 <stdio.h>
9
#include <string.h>
10
#include <assert.h>
11
#include <stdlib.h>
12
13
#include <emscripten.h>
14
15
#define DB "THE_DB"
16
17
void test() {
18
void *buffer;
19
int num, error, exists;
20
int sum = 0;
21
22
printf("storing %s\n", SECRET);
23
emscripten_idb_store(DB, "the_secret", SECRET, strlen(SECRET)+1, &error);
24
assert(!error);
25
sum++;
26
27
printf("checking\n");
28
exists = 5555;
29
emscripten_idb_exists(DB, "the_secret", &exists, &error);
30
assert(exists != 5555);
31
assert(!error);
32
assert(exists);
33
sum++;
34
35
printf("loading\n");
36
emscripten_idb_load(DB, "the_secret", &buffer, &num, &error);
37
assert(!error);
38
char *ptr = buffer;
39
printf("loaded %s\n", ptr);
40
assert(num == strlen(SECRET)+1);
41
assert(strcmp(ptr, SECRET) == 0);
42
free(buffer);
43
sum++;
44
45
printf("deleting the_secret\n");
46
emscripten_idb_delete(DB, "the_secret", &error);
47
assert(!error);
48
sum++;
49
50
printf("loading, should fail as we deleted\n");
51
emscripten_idb_load(DB, "the_secret", &buffer, &num, &error);
52
assert(error); // expected error!
53
sum++;
54
55
printf("storing %s again\n", SECRET);
56
emscripten_idb_store(DB, "the_secret", SECRET, strlen(SECRET)+1, &error);
57
assert(!error);
58
sum++;
59
60
printf("clearing the store\n");
61
emscripten_idb_clear(DB, &error);
62
assert(!error);
63
sum++;
64
65
printf("last checking\n");
66
emscripten_idb_exists(DB, "the_secret", &exists, &error);
67
assert(!error);
68
assert(!exists);
69
sum++;
70
71
REPORT_RESULT(sum);
72
}
73
74
void never() {
75
EM_ASM({ alert('this should never be reached! runtime must not be shut down!') });
76
assert(0);
77
while (1) {}
78
}
79
80
int main() {
81
atexit(never);
82
test();
83
return 0;
84
}
85
86
87