Patchwork [v3,10/12] test-iov: add iov_get_ptr() test case

login
register
mail settings
Submitter Stefan Hajnoczi
Date Nov. 21, 2012, 6:32 p.m.
Message ID <1353522781-12721-11-git-send-email-stefanha@redhat.com>
Download mbox | patch
Permalink /patch/200833/
State New
Headers show

Comments

Stefan Hajnoczi - Nov. 21, 2012, 6:32 p.m.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 tests/test-iov.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

Patch

diff --git a/tests/test-iov.c b/tests/test-iov.c
index 7997fb5..477aad8 100644
--- a/tests/test-iov.c
+++ b/tests/test-iov.c
@@ -378,6 +378,43 @@  static void test_discard(void)
     iov_free(iov, iov_cnt);
 }
 
+static void test_get_ptr(void)
+{
+    struct iovec *iov;
+    unsigned int iov_cnt;
+    ssize_t offset;
+    size_t size;
+    void *ret;
+
+    /* Offset beyond end of vector */
+    iov_random(&iov, &iov_cnt);
+    size = iov_size(iov, iov_cnt);
+    ret = iov_get_ptr(iov, iov_cnt, size + 1, 1);
+    g_assert(ret == NULL);
+    iov_free(iov, iov_cnt);
+
+    /* Offset before beginning of vector */
+    iov_random(&iov, &iov_cnt);
+    size = iov_size(iov, iov_cnt);
+    ret = iov_get_ptr(iov, iov_cnt, -(size + 1), 1);
+    g_assert(ret == NULL);
+    iov_free(iov, iov_cnt);
+
+    /* Range spans elements */
+    iov_random(&iov, &iov_cnt);
+    ret = iov_get_ptr(iov, iov_cnt, iov->iov_len - 1, 2);
+    g_assert(ret == NULL);
+    iov_free(iov, iov_cnt);
+
+    /* Range within element */
+    iov_random(&iov, &iov_cnt);
+    offset = g_test_rand_int_range(1, iov->iov_len);
+    size = g_test_rand_int_range(1, iov->iov_len - offset + 1);
+    ret = iov_get_ptr(iov, iov_cnt, offset, size);
+    g_assert(ret == iov->iov_base + offset);
+    iov_free(iov, iov_cnt);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -385,5 +422,6 @@  int main(int argc, char **argv)
     g_test_add_func("/basic/iov/from-to-buf", test_to_from_buf);
     g_test_add_func("/basic/iov/io", test_io);
     g_test_add_func("/basic/iov/discard", test_discard);
+    g_test_add_func("/basic/iov/get-ptr", test_get_ptr);
     return g_test_run();
 }