[glib] GSequence: Make g_sequence_iter_move behave as documented
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] GSequence: Make g_sequence_iter_move behave as documented
- Date: Wed, 27 Apr 2011 02:09:24 +0000 (UTC)
commit 440bd2a97574e63c39f0c1e2dbf8a17c2635b0e9
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Apr 26 22:08:24 2011 -0400
GSequence: Make g_sequence_iter_move behave as documented
As pointed out in bug 658313, moving before the begin iter is
supposed to return the begin iter, not the end iter. Also add
a test for this behaviour.
glib/gsequence.c | 8 +++++++-
glib/tests/sequence.c | 32 ++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 1 deletions(-)
---
diff --git a/glib/gsequence.c b/glib/gsequence.c
index fa85347..2654f7b 100644
--- a/glib/gsequence.c
+++ b/glib/gsequence.c
@@ -1422,12 +1422,18 @@ g_sequence_iter_move (GSequenceIter *iter,
gint delta)
{
gint new_pos;
+ gint len;
g_return_val_if_fail (iter != NULL, NULL);
+ len = g_sequence_get_length (get_sequence (iter));
+
new_pos = node_get_pos (iter) + delta;
- new_pos = clamp_position (get_sequence (iter), new_pos);
+ if (new_pos < 0)
+ new_pos = 0;
+ else if (new_pos > len)
+ new_pos = len;
return node_get_by_pos (iter, new_pos);
}
diff --git a/glib/tests/sequence.c b/glib/tests/sequence.c
index e2a58f3..029f12b 100644
--- a/glib/tests/sequence.c
+++ b/glib/tests/sequence.c
@@ -1210,6 +1210,35 @@ test_out_of_range_jump (void)
g_assert (g_sequence_iter_is_begin (iter));
g_assert (g_sequence_iter_is_end (iter));
+
+ g_sequence_free (seq);
+}
+
+static void
+test_iter_move (void)
+{
+ GSequence *seq = g_sequence_new (NULL);
+ GSequenceIter *iter;
+ gint i;
+
+ for (i = 0; i < 10; ++i)
+ g_sequence_append (seq, GINT_TO_POINTER (i));
+
+ iter = g_sequence_get_begin_iter (seq);
+ iter = g_sequence_iter_move (iter, 5);
+ g_assert_cmpint (GPOINTER_TO_INT (g_sequence_get (iter)), ==, 5);
+
+ iter = g_sequence_iter_move (iter, -10);
+ g_assert (g_sequence_iter_is_begin (iter));
+
+ iter = g_sequence_get_end_iter (seq);
+ iter = g_sequence_iter_move (iter, -5);
+ g_assert_cmpint (GPOINTER_TO_INT (g_sequence_get (iter)), ==, 5);
+
+ iter = g_sequence_iter_move (iter, 10);
+ g_assert (g_sequence_iter_is_end (iter));
+
+ g_sequence_free (seq);
}
static int
@@ -1326,6 +1355,8 @@ test_stable_sort (void)
iter = g_sequence_iter_next (iter);
g_sequence_check (seq);
}
+
+ g_sequence_free (seq);
}
int
@@ -1340,6 +1371,7 @@ main (int argc,
/* Standalone tests */
g_test_add_func ("/sequence/out-of-range-jump", test_out_of_range_jump);
+ g_test_add_func ("/sequence/iter-move", test_iter_move);
g_test_add_func ("/sequence/insert-sorted-non-pointer", test_insert_sorted_non_pointer);
g_test_add_func ("/sequence/stable-sort", test_stable_sort);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]