Skip to content

Commit 48757b5

Browse files
committed
Test for std::optional
1 parent 8342447 commit 48757b5

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tests/std_optional.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <unistd.h>
2+
#include <iostream>
3+
4+
#include <sqlite_modern_cpp.h>
5+
6+
using namespace sqlite;
7+
using namespace std;
8+
9+
void insert(database& db, bool is_null) {
10+
int id = 1;
11+
std::optional<int> val;
12+
if(!is_null) val = 5;
13+
14+
db << "delete from test where id = 1";
15+
db << "insert into test(id,val) values(?,?)" << id << val;
16+
}
17+
18+
void select(database& db, bool should_be_null) {
19+
db << "select id,val from test" >> [&](long long, std::optional<int> val) {
20+
if(should_be_null) {
21+
if(val) exit(EXIT_FAILURE);
22+
} else {
23+
if(!val) exit(EXIT_FAILURE);
24+
}
25+
};
26+
}
27+
28+
struct TmpFile {
29+
string fname;
30+
31+
TmpFile() {
32+
char f[] = "/tmp/sqlite_modern_cpp_test_XXXXXX";
33+
int fid = mkstemp(f);
34+
close(fid);
35+
36+
fname = f;
37+
}
38+
39+
~TmpFile() {
40+
unlink(fname.c_str());
41+
}
42+
};
43+
44+
int main() {
45+
try {
46+
// creates a database file 'dbfile.db' if it does not exists.
47+
TmpFile file;
48+
database db(file.fname);
49+
50+
db << "drop table if exists test";
51+
db <<
52+
"create table if not exists test ("
53+
" id integer primary key,"
54+
" val int"
55+
");";
56+
57+
insert(db, true);
58+
select(db, true);
59+
60+
insert(db, false);
61+
select(db, false);
62+
63+
} catch(exception& e) {
64+
cout << e.what() << endl;
65+
exit(EXIT_FAILURE);
66+
}
67+
exit(EXIT_SUCCESS);
68+
}

0 commit comments

Comments
 (0)