Skip to content

Commit 4fed04f

Browse files
authored
Merge pull request #61 from kiwix/fix_suggestion_iterator
2 parents f561034 + c94a04b commit 4fed04f

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

lib/src/main/cpp/libzim/entry_iterator.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,34 @@ METHOD0(jboolean, hasNext) {
8989
METHOD0(jobject, next) {
9090
switch (get_order(env, thisObj)) {
9191
case 0: {
92+
auto end = getPtr<PATH_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
93+
if ((*GET_PTR(PATH_NATIVE_TYPE)) == *end) {
94+
throwException(env, "java/util/NoSuchElementException", "");
95+
return nullptr;
96+
}
97+
9298
zim::Entry entry = **GET_PTR(PATH_NATIVE_TYPE);
9399
(*GET_PTR(PATH_NATIVE_TYPE))++;
94100
return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry);
95101
}
96102
case 1: {
103+
auto end = getPtr<TITLE_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
104+
if ((*GET_PTR(TITLE_NATIVE_TYPE)) == *end) {
105+
throwException(env, "java/util/NoSuchElementException", "");
106+
return nullptr;
107+
}
108+
97109
zim::Entry entry = **GET_PTR(TITLE_NATIVE_TYPE);
98110
(*GET_PTR(TITLE_NATIVE_TYPE))++;
99111
return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry);
100112
}
101113
case 2: {
114+
auto end = getPtr<EFFICIENT_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
115+
if ((*GET_PTR(EFFICIENT_NATIVE_TYPE)) == *end) {
116+
throwException(env, "java/util/NoSuchElementException", "");
117+
return nullptr;
118+
}
119+
102120
zim::Entry entry = **GET_PTR(EFFICIENT_NATIVE_TYPE);
103121
(*GET_PTR(EFFICIENT_NATIVE_TYPE))++;
104122
return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry);

lib/src/main/cpp/libzim/search_iterator.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,19 @@ METHOD0(jstring, getZimId) {
5757
} CATCH_EXCEPTION(0)
5858

5959
METHOD0(jboolean, hasNext) {
60+
// THIS is the next item to return. So we have to check it with end
6061
auto end = getPtr<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
6162
return *THIS != *end;
6263
} CATCH_EXCEPTION(false)
6364

6465
METHOD0(jobject, next) {
66+
// THIS is the next item to return. So we have to return it and advance for next round
67+
auto end = getPtr<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
68+
if (*THIS == *end) {
69+
throwException(env, "java/util/NoSuchElementException", "");
70+
return nullptr;
71+
}
72+
6573
zim::Entry entry = **THIS;
6674
(*THIS)++;
6775
return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry);

lib/src/main/cpp/libzim/suggestion_iterator.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@ METHOD0(void, dispose)
4242
} CATCH_EXCEPTION()
4343

4444
METHOD0(jboolean, hasNext) {
45-
NATIVE_TYPE next(*THIS);
46-
next++;
45+
// THIS is the next item to return. So we have to check it with end
4746
auto end = getPtr<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
48-
return next == *end;
47+
return *THIS != *end;
4948
} CATCH_EXCEPTION(false)
5049

5150
METHOD0(jobject, next) {
51+
// THIS is the next item to return. So we have to return it and advance for next round
52+
auto end = getPtr<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
53+
if (*THIS == *end) {
54+
throwException(env, "java/util/NoSuchElementException", "");
55+
return nullptr;
56+
}
57+
5258
zim::SuggestionItem item = **THIS;
5359
(*THIS)++;
5460
return BUILD_WRAPPER("org/kiwix/libzim/SuggestionItem", item);

lib/src/test/test.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ private void testArchive(TestArchive archive)
123123
assertEquals("favicon.png", iter.next().getPath());
124124
assertEquals("main.html", iter.next().getPath());
125125
assertFalse(iter.hasNext());
126+
try {
127+
iter.next();
128+
fail("ERROR: next() should raise a NoSuchElementException.");
129+
} catch (NoSuchElementException e) {
130+
// We are good
131+
} catch(Exception e) {
132+
fail("ERROR: Must be a NoSuchElementException.");
133+
}
126134
}
127135

128136
{
@@ -131,6 +139,14 @@ private void testArchive(TestArchive archive)
131139
assertEquals("main.html", iter.next().getPath());
132140
// No favicon, because favicon is not a main article (no title)
133141
assertFalse(iter.hasNext());
142+
try {
143+
iter.next();
144+
fail("ERROR: next() should raise a NoSuchElementException.");
145+
} catch (NoSuchElementException e) {
146+
// We are good
147+
} catch(Exception e) {
148+
fail("ERROR: Must be a NoSuchElementException.");
149+
}
134150
}
135151

136152
{
@@ -139,20 +155,44 @@ private void testArchive(TestArchive archive)
139155
assertEquals("main.html", iter.next().getPath());
140156
assertEquals("favicon.png", iter.next().getPath());
141157
assertFalse(iter.hasNext());
158+
try {
159+
iter.next();
160+
fail("ERROR: next() should raise a NoSuchElementException.");
161+
} catch (NoSuchElementException e) {
162+
// We are good
163+
} catch(Exception e) {
164+
fail("ERROR: Must be a NoSuchElementException.");
165+
}
142166
}
143167

144168
{
145169
TestEntryIterator iter = archive.findByPath("ma");
146170
assertTrue(iter.hasNext());
147171
assertEquals("main.html", iter.next().getPath());
148172
assertFalse(iter.hasNext());
173+
try {
174+
iter.next();
175+
fail("ERROR: next() should raise a NoSuchElementException.");
176+
} catch (NoSuchElementException e) {
177+
// We are good
178+
} catch(Exception e) {
179+
fail("ERROR: Must be a NoSuchElementException.");
180+
}
149181
}
150182

151183
{
152184
TestEntryIterator iter = archive.findByTitle("Test");
153185
assertTrue(iter.hasNext());
154186
assertEquals("main.html", iter.next().getPath());
155187
assertFalse(iter.hasNext());
188+
try {
189+
iter.next();
190+
fail("ERROR: next() should raise a NoSuchElementException.");
191+
} catch (NoSuchElementException e) {
192+
// We are good
193+
} catch(Exception e) {
194+
fail("ERROR: Must be a NoSuchElementException.");
195+
}
156196
}
157197

158198
// Test invalid path
@@ -210,7 +250,7 @@ public void testNotValid() {
210250
} catch (ZimFileFormatException e) {
211251
assertEquals("Invalid magic number", e.getMessage());
212252
} catch(Exception e) {
213-
fail("ERROR: Must be a ZimFileFormatException.");
253+
fail("ERROR: Must be a ZimFileFormatException.");
214254
}
215255
}
216256

@@ -459,6 +499,14 @@ public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixExc
459499
assertTrue(results.hasNext());
460500
TestSuggestionItem suggestionItem = results.next();
461501
assertFalse(results.hasNext());
502+
try {
503+
results.next();
504+
fail("ERROR: next() should raise a NoSuchElementException.");
505+
} catch (NoSuchElementException e) {
506+
// We are good
507+
} catch(Exception e) {
508+
fail("ERROR: Must be a NoSuchElementException.");
509+
}
462510
assertEquals("Test ZIM file", suggestionItem.getTitle());
463511
assertEquals("main.html", suggestionItem.getPath());
464512
assertTrue(suggestionItem.hasSnippet());

0 commit comments

Comments
 (0)