blob: eb8d024e89ae0f8b4f3aff64727d97a6a3eb90a5 [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:
Kun Yi8ca234e2019-03-04 10:14:45 -080026 FakeSysFile()
27 {
28 }
29 FakeSysFile(const std::string& s) : data_(s)
30 {
31 }
32
Kun Yi913ba972019-01-16 11:08:08 -080033 size_t readToBuf(size_t pos, size_t count, char* buf) const override
34 {
35 auto result = readAsStr(pos, count);
36 std::copy(result.begin(), result.end(), buf);
37 return result.size();
38 }
39
40 std::string readAsStr(size_t pos, size_t count) const override
41 {
42 if (pos >= data_.size())
43 {
44 return "";
45 }
46
47 return data_.substr(pos, count);
48 }
49
50 std::string readRemainingAsStr(size_t pos) const override
51 {
52 return readAsStr(pos, data_.size());
53 }
54
55 void writeStr(const std::string& data, size_t pos) override
56 {
57 if (pos >= data.size())
58 {
59 return;
60 }
61
62 data_.resize(pos);
63 data_.insert(data_.begin() + pos, data.begin(), data.end());
64 }
65
66 protected:
67 std::string data_ = ""s;
68};
69
70} // namespace binstore