1 /*
2  * Copyright (C) 2009 Miguel Rocha
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 <zim/zim.h>
21 #include <zim/file.h>
22 #include <zim/error.h>
23 #include <zim/fileiterator.h>
24 
25 #include "gtest/gtest.h"
26 
27 namespace
28 {
29 
30 
TEST(ClusterIteratorTest,getArticleByClusterOrder)31 TEST(ClusterIteratorTest, getArticleByClusterOrder)
32 {
33     std::vector<zim::article_index_type> expected = {
34 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
35 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
36 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
37 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 109, 110, 111, 112, 113, 114, 115, 116,
38 117, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
39 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108 };
40 
41     zim::File file ("./data/wikibooks_be_all_nopic_2017-02.zim");
42 
43     auto nbArticles = file.getCountArticles();
44 
45     ASSERT_EQ(nbArticles, expected.size());
46 
47     for (auto i = 0u; i < nbArticles; i++)
48     {
49         EXPECT_EQ(file.getArticleByClusterOrder(i).getIndex(), expected[i]);
50     }
51 }
52 
TEST(getArticle,indexOutOfRange)53 TEST(getArticle, indexOutOfRange)
54 {
55     zim::File file ("./data/wikibooks_be_all_nopic_2017-02.zim");
56 
57     auto nbArticles = file.getCountArticles();
58 
59     try {
60         file.getArticle(nbArticles);
61         FAIL() << "Should throw exception\n";
62     }  catch (zim::ZimFileFormatError &e) {
63         ASSERT_EQ(e.what(), std::string("article index out of range"));
64     }  catch(...) {
65         FAIL() << "Should throw exception\n";
66     }
67 }
68 
69 // ByTitle
TEST(IteratorTests,begin)70 TEST(IteratorTests, begin)
71 {
72     std::vector<zim::article_index_type> expected = {
73 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
74 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
75 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
76 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 109, 110, 111, 112, 113, 114, 115, 116,
77 117, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
78 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108 };
79 
80     zim::File file ("./data/wikibooks_be_all_nopic_2017-02.zim");
81 
82     auto it = file.begin();
83     int i = 0;
84     while (it != file.end())
85     {
86         EXPECT_EQ(it->getIndex(), expected[i]);
87         it++; i++;
88     }
89 }
90 
91 
92 // ByTitle
TEST(IteratorTests,beginByTitle)93 TEST(IteratorTests, beginByTitle)
94 {
95     std::vector<zim::article_index_type> expected = { 0, 1, 2, 3, 4, 5, 7, 8, 9, 10};
96     zim::File file ("./data/wikibooks_be_all_nopic_2017-02.zim");
97 
98     auto it = file.beginByTitle();
99 
100     int i = 0;
101     while (i < 10)
102     {
103         EXPECT_EQ(it->getIndex(), expected[i]);
104         it++; i++;
105     }
106     std::cout << "\n";
107 }
108 
109 
110 // ByUrl
TEST(IteratorTests,beginByUrl)111 TEST(IteratorTests, beginByUrl)
112 {
113     std::vector<zim::article_index_type> expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
114     zim::File file ("./data/wikibooks_be_all_nopic_2017-02.zim");
115 
116     auto it = file.beginByUrl();
117     int i = 0;
118     while (i < 10)
119     {
120         EXPECT_EQ(it->getIndex(), expected[i]);
121         it++; i++;
122     }
123 }
124 
125 } // namespace
126