-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathvariant.cc
More file actions
57 lines (45 loc) · 1.38 KB
/
variant.cc
File metadata and controls
57 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>
#include <sqlite_modern_cpp.h>
#include <catch2/catch.hpp>
using namespace sqlite;
using namespace std;
#ifdef MODERN_SQLITE_STD_VARIANT_SUPPORT
TEST_CASE("std::variant works", "[variant]") {
database db(":memory:");
db << "CREATE TABLE foo (a);";
std::variant<std::string, int, std::optional<float>> v;
v = 1;
db << "INSERT INTO foo VALUES (?)" << v;
v = "a";
db << "INSERT INTO foo VALUES (?)" << v;
db << "SELECT a FROM foo WHERE a=?;" << 1 >> v;
REQUIRE(v.index() == 1);
REQUIRE(std::get<1>(v) == 1);
db << "SELECT NULL" >> v;
REQUIRE(!std::get<2>(v));
db << "SELECT 0.0" >> v;
REQUIRE(std::get<2>(v));
}
TEST_CASE("std::monostate is a nullptr substitute", "[monostate]") {
database db(":memory:");
db << "CREATE TABLE foo (a);";
std::variant<std::monostate,std::string> v;
v=std::monostate();
db << "INSERT INTO foo VALUES (?)" << v;
db << "INSERT INTO foo VALUES (?)" << "This isn't a monostate!";
bool found_null = false,
found_string = false;
db << "SELECT * FROM foo"
>> [&](std::variant<std::monostate, std::string> z) {
if(z.index() == 0) {
found_null = true;
} else {
found_string = true;
}
};
REQUIRE((found_null && found_string));
db << "SELECT NULL" >> v;
REQUIRE(v.index() == 0);
}
#endif