Commit 4c88198a85932b69f779b2078f22b0231a18857e

Edward Thomson 2016-03-16T10:17:20

iterator: test that we're at the end of iteration Ensure that we have hit the end of iteration; previously we tested that we saw all the values that we expected to see. We did not then ensure that we were at the end of the iteration (and that there were subsequently values in the iteration that we did *not* expect.)

diff --git a/src/iterator.c b/src/iterator.c
index ce0fb0e..88b7ed2 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -437,8 +437,10 @@ GIT_INLINE(bool) iterator_has_started(git_iterator *iter, const char *path)
 
 GIT_INLINE(bool) iterator_has_ended(git_iterator *iter, const char *path)
 {
-	if (iter->end == NULL || iter->ended == true)
+	if (iter->end == NULL)
 		return false;
+	else if (iter->ended)
+		return true;
 
 	iter->ended = (iter->prefixcomp(path, iter->end) > 0);
 	return iter->ended;
diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c
index ea2b37d..e668caf 100644
--- a/tests/repo/iterator.c
+++ b/tests/repo/iterator.c
@@ -16,6 +16,17 @@ void test_repo_iterator__cleanup(void)
 	g_repo = NULL;
 }
 
+static void assert_at_end(git_iterator *i, bool verbose)
+{
+	const git_index_entry *end;
+	int error = git_iterator_advance(&end, i);
+
+	if (verbose && error != GIT_ITEROVER)
+		fprintf(stderr, "Expected end of iterator, got '%s'\n", end->path);
+
+	cl_git_fail_with(GIT_ITEROVER, error);
+}
+
 static void expect_iterator_items(
 	git_iterator *i,
 	int expected_flat,
@@ -57,6 +68,7 @@ static void expect_iterator_items(
 			break;
 	}
 
+	assert_at_end(i, v);
 	cl_assert_equal_i(expected_flat, count);
 
 	cl_git_pass(git_iterator_reset(i));
@@ -103,6 +115,7 @@ static void expect_iterator_items(
 			break;
 	}
 
+	assert_at_end(i, v);
 	cl_assert_equal_i(expected_total, count);
 }