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 "rawstreamreader.h"
21 #include "buffer.h"
22 #include "buffer_reader.h"
23 
24 #include "gtest/gtest.h"
25 
26 namespace
27 {
28 
29 using namespace zim;
30 
toString(const Buffer & buffer)31 std::string toString(const Buffer& buffer)
32 {
33   return std::string(buffer.data(), buffer.size().v);
34 }
35 
TEST(ReaderDataStreamWrapper,shouldJustWork)36 TEST(ReaderDataStreamWrapper, shouldJustWork)
37 {
38   char data[] = "abcdefghijklmnopqrstuvwxyz";
39   toLittleEndian(uint32_t(1234), data);
40   toLittleEndian(int64_t(-987654321), data+18);
41 
42   auto  reader = std::make_shared<BufferReader>(Buffer::makeBuffer(data, zsize_t(sizeof(data))));
43 
44   RawStreamReader rdr(reader);
45 
46   ASSERT_EQ(1234,         rdr.read<uint32_t>());
47   auto subbuffer = rdr.sub_reader(zsize_t(4))->get_buffer(offset_t(0), zsize_t(4));
48   ASSERT_EQ("efgh",       toString(subbuffer));
49   subbuffer = rdr.sub_reader(zsize_t(10))->get_buffer(offset_t(0), zsize_t(10));
50   ASSERT_EQ("ijklmnopqr", toString(subbuffer));
51   ASSERT_EQ(-987654321,   rdr.read<int64_t>());
52 }
53 
54 } // unnamed namespace
55