1 /*
2 * Copyright (C) 2020 Veloman Yunkan
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12 * NON-INFRINGEMENT. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19
20 #include "buffer.h"
21 #include "bufferstreamer.h"
22 #include "endian_tools.h"
23
24 #include "gtest/gtest.h"
25
26 namespace
27 {
28
29 using namespace zim;
30
31 ////////////////////////////////////////////////////////////////////////////////
32 // BufferStreamer
33 ////////////////////////////////////////////////////////////////////////////////
34
TEST(BufferStreamer,shouldJustWork)35 TEST(BufferStreamer, shouldJustWork)
36 {
37 char data[] = "abcdefghijklmnopqrstuvwxyz";
38 zim::toLittleEndian(uint32_t(1234), data);
39 zim::toLittleEndian(int64_t(-987654321), data+18);
40
41 auto buffer = Buffer::makeBuffer(data, zsize_t(sizeof(data)));
42 zim::BufferStreamer bds(buffer, zsize_t(sizeof(data)));
43
44 ASSERT_EQ(1234, bds.read<uint32_t>());
45
46 ASSERT_EQ(data + 4, bds.current());
47 const auto blob1 = std::string(bds.current(), 4);
48 bds.skip(zsize_t(4));
49 ASSERT_EQ("efgh", blob1);
50
51 ASSERT_EQ(data + 8, bds.current());
52 const auto blob2 = std::string(bds.current(), 10);
53 bds.skip(zsize_t(10));
54 ASSERT_EQ("ijklmnopqr", blob2);
55
56 ASSERT_EQ(-987654321, bds.read<int64_t>());
57 }
58
59 } // unnamed namespace
60