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 "istreamreader.h"
21 #include "endian_tools.h"
22 
23 #include "gtest/gtest.h"
24 
25 namespace
26 {
27 
28 using namespace zim;
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 // IDataStream
32 ////////////////////////////////////////////////////////////////////////////////
33 
34 // Implement the IStreamReader interface in the simplest way
35 class InfiniteZeroStream : public IStreamReader
36 {
readImpl(char * buf,zim::zsize_t nbytes)37   void readImpl(char* buf, zim::zsize_t nbytes) { memset(buf, 0, nbytes.v); }
38 };
39 
40 // ... and test that it compiles and works as intended
41 
TEST(IStreamReader,read)42 TEST(IStreamReader, read)
43 {
44   InfiniteZeroStream izs;
45   IStreamReader& ids = izs;
46   EXPECT_EQ(0, ids.read<int>());
47   EXPECT_EQ(0L, ids.read<long>());
48 
49   // zim::fromLittleEndian() handles only integer types
50   // EXPECT_EQ(0.0, ids.read<double>());
51 }
52 
TEST(IStreamReader,sub_reader)53 TEST(IStreamReader, sub_reader)
54 {
55   const size_t N = 16;
56   const char zerobuf[N] = {0};
57   InfiniteZeroStream izs;
58   IStreamReader& ids = izs;
59   auto subReader = ids.sub_reader(zim::zsize_t(N));
60   EXPECT_EQ(subReader->size().v, N);
61   auto buffer = subReader->get_buffer(zim::offset_t(0), zim::zsize_t(N));
62   EXPECT_EQ(buffer.size().v, N);
63   EXPECT_EQ(0, memcmp(buffer.data(), zerobuf, N));
64 }
65 
66 } // unnamed namespace
67