diff options
author | Thomas Haller <thaller@redhat.com> | 2015-10-22 16:14:45 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-11-01 17:28:07 +0100 |
commit | d5ef08e01e0d613f3cc9f4d66d066e579f2ac6f5 (patch) | |
tree | 1db519898f25c4c6b6da7cb8ee3371c6fd5a49c9 /include | |
parent | 09983442bd8be13392876cb8cf4e1564907b07e4 (diff) |
tests: add nmtst_rand_perm() utility
Diffstat (limited to 'include')
-rw-r--r-- | include/nm-test-utils.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h index 22d5fb1bf3..54a326904c 100644 --- a/include/nm-test-utils.h +++ b/include/nm-test-utils.h @@ -583,6 +583,47 @@ nmtst_get_rand_int (void) return g_rand_int (nmtst_get_rand ()); } +inline static void * +nmtst_rand_perm (GRand *rand, void *dst, const void *src, gsize elmt_size, gsize n_elmt) +{ + gsize i, j; + char *p_, *pj; + char *bu; + + g_assert (dst); + g_assert (elmt_size > 0); + g_assert (n_elmt < G_MAXINT32); + + if (n_elmt == 0) + return dst; + + if (src && dst != src) + memcpy (dst, src, elmt_size * n_elmt); + + if (!rand) + rand = nmtst_get_rand (); + + bu = g_slice_alloc (elmt_size); + + p_ = dst; + for (i = n_elmt; i > 1; i--) { + j = g_rand_int_range (rand, 0, i); + + if (j != 0) { + pj = &p_[j * elmt_size]; + + /* swap */ + memcpy (bu, p_, elmt_size); + memcpy (p_, pj, elmt_size); + memcpy (pj, bu, elmt_size); + } + p_ += elmt_size; + } + + g_slice_free1 (elmt_size, bu); + return dst; +} + inline static const char * nmtst_get_sudo_cmd (void) { |