summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/test/blob.cpp
blob: ca6ce834c254f67df7dfdbc678ed7ba80388c7b9 (plain)
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
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//


#include <boost/sqlite/blob.hpp>
#include <boost/sqlite/connection.hpp>

#include <random>

#include "test.hpp"

using namespace boost;

BOOST_AUTO_TEST_CASE(blob)
{
  sqlite::connection conn{":memory:"};
  // language=sqlite
  conn.execute("create table blobs(id integer primary key autoincrement, bb blob);");

  std::vector<unsigned char> blobby;
  blobby.resize(4096*4096);
  std::random_device dev;
  std::mt19937 rng(dev());
  std::uniform_int_distribution<std::mt19937::result_type> dist(0,255); // distribution in range [1, 6]

  std::generate(blobby.begin(), blobby.end(),
                [&]{return static_cast<unsigned char>(dist(rng));});


  conn.prepare("insert into blobs(bb) values ($1);").execute(std::make_tuple(sqlite::zero_blob(4096 * 4096 )));

  auto bh = open_blob(conn, "main", "blobs", "bb", 1);

  BOOST_CHECK(bh.size() == 4096 * 4096);


  unsigned char buf[4096];
  std::generate(std::begin(buf), std::end(buf), [&]{return static_cast<unsigned char>(dist(rng));});
  bh.read_at(buf, 4096, 4096);
  BOOST_CHECK(std::all_of(std::begin(buf), std::end(buf), [](unsigned char c) {return c == 0u;}));

  bh.write_at(blobby.data(), blobby.size(), 0u);
  bh.read_at(buf, 4096, 4096);
  BOOST_CHECK(std::memcmp(buf, blobby.data() + 4096, 4096) == 0);

  BOOST_CHECK_THROW(open_blob(conn, "main", "doesnt-exit", "blobber", 2), boost::system::system_error);

  sqlite::blob_handle bb;
  BOOST_CHECK_THROW(bb.read_at(blobby.data(), blobby.size(), 0), boost::system::system_error);
  BOOST_CHECK_THROW(bb.write_at(blobby.data(), blobby.size(), 0), boost::system::system_error);

}