blob: 1c6a45d8d720cf04a3a93f62c60699a4c90e9bd5 [file] [log] [blame]
Kun Yi913ba972019-01-16 11:08:08 -08001#pragma once
2
3#include "sys_file.hpp"
4
5#include <stdint.h>
6
7#include <cstring>
8#include <string>
9#include <vector>
10
11using std::size_t;
12using std::uint16_t;
13using std::uint32_t;
14using std::uint8_t;
15
16using namespace std::string_literals;
17
18namespace binstore
19{
20
21/* An in-memory file implementation to test read/write operations, which works
22 * around the problem that Docker image cannot create file in tmpdir. */
23class FakeSysFile : public SysFile
24{
25 public:
26 size_t readToBuf(size_t pos, size_t count, char* buf) const override
27 {
28 auto result = readAsStr(pos, count);
29 std::copy(result.begin(), result.end(), buf);
30 return result.size();
31 }
32
33 std::string readAsStr(size_t pos, size_t count) const override
34 {
35 if (pos >= data_.size())
36 {
37 return "";
38 }
39
40 return data_.substr(pos, count);
41 }
42
43 std::string readRemainingAsStr(size_t pos) const override
44 {
45 return readAsStr(pos, data_.size());
46 }
47
48 void writeStr(const std::string& data, size_t pos) override
49 {
50 if (pos >= data.size())
51 {
52 return;
53 }
54
55 data_.resize(pos);
56 data_.insert(data_.begin() + pos, data.begin(), data.end());
57 }
58
59 protected:
60 std::string data_ = ""s;
61};
62
63} // namespace binstore