summaryrefslogtreecommitdiffstats
path: root/test/test_data.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-07-02 13:54:45 -0400
committerDavid Robillard <d@drobilla.net>2021-07-17 19:58:17 -0400
commit5942e985c6ac9b18090ec92b11aa8a586b6365c5 (patch)
treece66d68e863df9ecba01c16dfe1a5bc7100068f0 /test/test_data.h
parentbc264ab6f58177124d49a72b4a808eb97fa2cb25 (diff)
downloadzix-5942e985c6ac9b18090ec92b11aa8a586b6365c5.tar.gz
zix-5942e985c6ac9b18090ec92b11aa8a586b6365c5.tar.bz2
zix-5942e985c6ac9b18090ec92b11aa8a586b6365c5.zip
Avoid use of rand()
Diffstat (limited to 'test/test_data.h')
-rw-r--r--test/test_data.h59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/test_data.h b/test/test_data.h
new file mode 100644
index 0000000..9c9a51d
--- /dev/null
+++ b/test/test_data.h
@@ -0,0 +1,59 @@
+/*
+ Copyright 2011-2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef ZIX_TEST_DATA_H
+#define ZIX_TEST_DATA_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+/// Linear Congruential Generator for making random 32-bit integers
+static inline uint32_t
+lcg32(const uint32_t i)
+{
+ static const uint32_t a = 134775813u;
+ static const uint32_t c = 1u;
+
+ return (a * i) + c;
+}
+
+/// Linear Congruential Generator for making random 64-bit integers
+static inline uint64_t
+lcg64(const uint64_t i)
+{
+ static const uint64_t a = 6364136223846793005ull;
+ static const uint64_t c = 1ull;
+
+ return (a * i) + c;
+}
+
+/// Return a pseudo-pseudo-pseudo-random-ish integer with no duplicates
+static inline size_t
+unique_rand(size_t i)
+{
+ i ^= 0x5CA1AB1Eu; // Juggle bits to avoid linear clumps
+
+ // Largest prime < 2^32 which satisfies (2^32 = 3 mod 4)
+ static const size_t prime = 4294967291u;
+ if (i >= prime) {
+ return i; // Values >= prime are mapped to themselves
+ }
+
+ const size_t residue = ((uint64_t)i * i) % prime;
+ return (i <= prime / 2) ? residue : prime - residue;
+}
+
+#endif // ZIX_TEST_DATA_H