Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / tests / zypp / GZStream_test.cc
1 // Boost.Test
2 #include <boost/test/unit_test.hpp>
3
4 #include <zypp/base/GzStream.h>
5 #include <zypp/Pathname.h>
6 #include <zypp/base/InputStream.h>
7
8 BOOST_AUTO_TEST_CASE(gz_simple_read_write)
9 {
10   const zypp::Pathname file = zypp::Pathname(TESTS_BUILD_DIR) / "test.gz";
11   const std::string testString("HelloWorld");
12
13   {
14     zypp::ofgzstream strOut( file.c_str() );
15     BOOST_REQUIRE ( strOut.is_open() );
16     BOOST_REQUIRE ( !strOut.fail() );
17     BOOST_REQUIRE ( strOut.getbuf().canWrite() );
18     BOOST_REQUIRE ( !strOut.getbuf().canRead() );
19     strOut << testString;
20     BOOST_REQUIRE ( !strOut.fail() );
21   }
22
23   {
24     std::string test;
25     zypp::ifgzstream str( file.c_str() );
26     BOOST_REQUIRE ( str.is_open() );
27     BOOST_REQUIRE ( !str.fail() );
28     BOOST_REQUIRE ( !str.getbuf().canWrite() );
29     BOOST_REQUIRE ( str.getbuf().canRead() );
30     str >> test;
31     BOOST_REQUIRE ( !str.fail() );
32     BOOST_REQUIRE_EQUAL( test, testString );
33   }
34
35   {
36     zypp::InputStream iStr( file );
37     BOOST_REQUIRE( typeid( iStr.stream() ) == typeid( zypp::ifgzstream& ) );
38   }
39 }
40
41 BOOST_AUTO_TEST_CASE(gz_seek)
42 {
43   const zypp::Pathname file = zypp::Pathname(TESTS_BUILD_DIR) / "testseek.gz";
44   const std::string testString("Hello World!\nLet's see if seeking works!");
45
46   {
47     zypp::ofgzstream strOut( file.c_str() );
48     BOOST_REQUIRE( strOut.is_open() );
49     strOut << testString;
50   }
51
52   //gzseek only supports SEEK_SET (beg) and SEEK_CUR (cur), SEEK_END (end) is not supported
53   {
54     std::string test;
55     zypp::ifgzstream str( file.c_str() );
56     str.seekg( 6, std::ios_base::beg );
57     BOOST_REQUIRE ( !str.fail() );
58     BOOST_REQUIRE_EQUAL( str.tellg(), 6 );
59     str >> test;
60     BOOST_REQUIRE_EQUAL( test, "World!" );
61     BOOST_REQUIRE ( !str.fail() );
62     BOOST_REQUIRE_EQUAL( str.tellg(), 12 );
63
64     str.seekg( 7, std::ios_base::cur );
65     BOOST_REQUIRE ( !str.fail() );
66     BOOST_REQUIRE_EQUAL( str.tellg(), 19 );
67     str >> test;
68     BOOST_REQUIRE_EQUAL( test, "see" );
69     BOOST_REQUIRE ( !str.fail() );
70     BOOST_REQUIRE_EQUAL( str.tellg(), 22 );
71
72     str.seekg( 0, std::ios_base::beg );
73     BOOST_REQUIRE ( !str.fail() );
74     BOOST_REQUIRE_EQUAL( str.tellg(), 0 );
75     str >> test;
76     BOOST_REQUIRE ( !str.fail() );
77     BOOST_REQUIRE_EQUAL( str.tellg(), 5 );
78     BOOST_REQUIRE_EQUAL( test, "Hello" );
79   }
80 }