]> git.ozlabs.org Git - ccan/blobdiff - ccan/charset/test/common.h
charset: Rewrote utf8_validate, and added four new functions:
[ccan] / ccan / charset / test / common.h
diff --git a/ccan/charset/test/common.h b/ccan/charset/test/common.h
new file mode 100644 (file)
index 0000000..83b39c4
--- /dev/null
@@ -0,0 +1,27 @@
+#include <stdint.h>
+#include <stdlib.h>
+
+/*
+ * Finds a pseudorandom 32-bit number from 0 to 2^32-1 .
+ * Uses the BCPL linear congruential generator method.
+ *
+ * Used instead of system RNG to ensure tests are consistent.
+ */
+static uint32_t rand32(void)
+{
+#if 0
+       /*
+        * Tests should be run with a different random function
+        * from time to time.  I've found that the method below
+        * sometimes behaves poorly for testing purposes.
+        * For example, rand32() % N might only return even numbers.
+        */
+       assert(RAND_MAX == 2147483647);
+       return ((random() & 0xFFFF) << 16) | (random() & 0xFFFF);
+#else
+       static uint32_t rand32_state = 0;
+       rand32_state *= (uint32_t)0x7FF8A3ED;
+       rand32_state += (uint32_t)0x2AA01D31;
+       return rand32_state;
+#endif
+}