2021-08-02 02:37:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 - 2016, Roland Bock
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2024-07-29 13:00:12 +08:00
|
|
|
#include <sqlpp11/sqlite3/database/connection.h>
|
2021-08-02 02:37:36 +08:00
|
|
|
#include <sqlpp11/sqlpp11.h>
|
|
|
|
|
2024-06-17 00:45:26 +08:00
|
|
|
#include "Tables.h"
|
2021-08-02 02:37:36 +08:00
|
|
|
#ifdef SQLPP_USE_SQLCIPHER
|
|
|
|
#include <sqlcipher/sqlite3.h>
|
|
|
|
#else
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#endif
|
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace sql = sqlpp::sqlite3;
|
|
|
|
|
2024-06-17 00:45:26 +08:00
|
|
|
const auto fp = test::FpSample{};
|
2021-08-02 02:37:36 +08:00
|
|
|
|
2024-04-13 18:04:41 +08:00
|
|
|
template <typename T>
|
2024-07-30 02:49:45 +08:00
|
|
|
std::ostream& operator<<(std::ostream& os, const ::sqlpp::optional<T>& t) {
|
2024-04-13 18:04:41 +08:00
|
|
|
if (not t)
|
|
|
|
return os << "NULL";
|
|
|
|
return os << t.value();
|
|
|
|
}
|
|
|
|
|
2021-08-02 02:37:36 +08:00
|
|
|
template <typename L, typename R>
|
|
|
|
auto require_equal(int line, const L& l, const R& r) -> void
|
|
|
|
{
|
|
|
|
if (l != r)
|
|
|
|
{
|
|
|
|
std::cerr << line << ": ";
|
2024-04-13 18:04:41 +08:00
|
|
|
std::cerr << l;
|
2021-08-02 02:37:36 +08:00
|
|
|
std::cerr << " != ";
|
2024-04-13 18:04:41 +08:00
|
|
|
std::cerr << r;
|
2021-08-02 02:37:36 +08:00
|
|
|
throw std::runtime_error("Unexpected result");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto require(int line, bool condition) -> void
|
|
|
|
{
|
|
|
|
if (!condition)
|
|
|
|
{
|
|
|
|
std::cerr << line << " condition violated";
|
|
|
|
throw std::runtime_error("Unexpected result");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 14:23:36 +08:00
|
|
|
int FloatingPoint(int, char*[])
|
2021-08-02 02:37:36 +08:00
|
|
|
{
|
|
|
|
sql::connection_config config;
|
|
|
|
config.path_to_database = ":memory:";
|
|
|
|
config.flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
|
|
|
config.debug = true;
|
|
|
|
|
|
|
|
sql::connection db(config);
|
2024-06-17 00:45:26 +08:00
|
|
|
test::createFpSample(db);
|
2021-08-02 02:37:36 +08:00
|
|
|
|
|
|
|
db.execute("INSERT into fp_sample (id, fp) values(NULL, 1.0)");
|
|
|
|
db.execute("INSERT into fp_sample (id, fp) values(NULL, 'Inf')");
|
|
|
|
db.execute("INSERT into fp_sample (id, fp) values(NULL, 'Nan')");
|
|
|
|
db.execute("INSERT into fp_sample (id, fp) values(NULL, 'SomeString')");
|
|
|
|
db(insert_into(fp).set(fp.fp = std::numeric_limits<double>::quiet_NaN()));
|
|
|
|
db(insert_into(fp).set(fp.fp = std::numeric_limits<double>::infinity()));
|
|
|
|
db(insert_into(fp).set(fp.fp = -std::numeric_limits<double>::infinity()));
|
|
|
|
|
|
|
|
auto prepared_insert = db.prepare(insert_into(fp).set(fp.fp = parameter(fp.fp)));
|
|
|
|
prepared_insert.params.fp = std::numeric_limits<double>::quiet_NaN();
|
|
|
|
db(prepared_insert);
|
|
|
|
prepared_insert.params.fp = std::numeric_limits<double>::infinity();
|
|
|
|
db(prepared_insert);
|
|
|
|
prepared_insert.params.fp = -std::numeric_limits<double>::infinity();
|
|
|
|
db(prepared_insert);
|
|
|
|
|
|
|
|
auto q = select(fp.fp).from(fp).unconditionally();
|
|
|
|
auto rows = db(q);
|
|
|
|
|
|
|
|
// raw string inserts
|
|
|
|
require_equal(__LINE__, rows.front().fp, 1.0);
|
|
|
|
rows.pop_front();
|
|
|
|
require(__LINE__, std::isinf(rows.front().fp.value()));
|
|
|
|
rows.pop_front();
|
|
|
|
require(__LINE__, std::isnan(rows.front().fp.value()));
|
|
|
|
rows.pop_front();
|
|
|
|
require_equal(__LINE__, rows.front().fp, 0.0);
|
|
|
|
rows.pop_front();
|
|
|
|
|
|
|
|
// dsl inserts
|
|
|
|
require(__LINE__, std::isnan(rows.front().fp.value()));
|
|
|
|
rows.pop_front();
|
|
|
|
require(__LINE__, std::isinf(rows.front().fp.value()));
|
|
|
|
require(__LINE__, rows.front().fp.value() > std::numeric_limits<double>::max());
|
|
|
|
rows.pop_front();
|
|
|
|
require(__LINE__, std::isinf(rows.front().fp.value()));
|
|
|
|
require(__LINE__, rows.front().fp.value() < std::numeric_limits<double>::lowest());
|
|
|
|
|
|
|
|
// prepared dsl inserts
|
|
|
|
rows.pop_front();
|
|
|
|
require(__LINE__, std::isnan(rows.front().fp.value()));
|
|
|
|
rows.pop_front();
|
|
|
|
require(__LINE__, std::isinf(rows.front().fp.value()));
|
|
|
|
require(__LINE__, rows.front().fp.value() > std::numeric_limits<double>::max());
|
|
|
|
rows.pop_front();
|
|
|
|
require(__LINE__, std::isinf(rows.front().fp.value()));
|
|
|
|
require(__LINE__, rows.front().fp.value() < std::numeric_limits<double>::lowest());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|