aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2025-01-20 21:06:59 +0100
committerFlorian Weimer <fweimer@redhat.com>2025-01-20 21:31:59 +0100
commit1ac28b781882e3f14b41dcb06f3f945d53938948 (patch)
tree4e9ca32991e92bf33ce9e807de0950b12de6927d /stdlib
parent33b684e0194930ff072cf812b37c191637261dbe (diff)
downloadglibc-1ac28b781882e3f14b41dcb06f3f945d53938948.zip
glibc-1ac28b781882e3f14b41dcb06f3f945d53938948.tar.gz
glibc-1ac28b781882e3f14b41dcb06f3f945d53938948.tar.bz2
stdlib: Test for expected sequence of random numbers from rand
As the test comment explains, this test is not quite valid, but preserving the exact sequences helps distributions to port to newer glibc versions. We can remove this test if we ever switch to a different implementation. Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/Makefile1
-rw-r--r--stdlib/tst-rand-sequence.c57
2 files changed, 58 insertions, 0 deletions
diff --git a/stdlib/Makefile b/stdlib/Makefile
index 18a19f4..a5fbc1a 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -296,6 +296,7 @@ tests := \
tst-qsort3 \
tst-qsort6 \
tst-quick_exit \
+ tst-rand-sequence \
tst-rand48 \
tst-rand48-2 \
tst-random \
diff --git a/stdlib/tst-rand-sequence.c b/stdlib/tst-rand-sequence.c
new file mode 100644
index 0000000..4691dfd
--- /dev/null
+++ b/stdlib/tst-rand-sequence.c
@@ -0,0 +1,57 @@
+/* Test that rand () evaluates to a special expected sequence.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+/* This test is borderline invalid because the rand function is not
+ guaranteed to produce the same sequence of numbers on every
+ architecture or glibc build. However, some software packages have
+ test suites that depend on the specific sequence of numbers return
+ here, so this test verifies that they do not change unexpectedly. */
+
+#include <stdlib.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+ TEST_COMPARE (rand (), 1804289383);
+ TEST_COMPARE (rand (), 846930886);
+ TEST_COMPARE (rand (), 1681692777);
+ TEST_COMPARE (rand (), 1714636915);
+ TEST_COMPARE (rand (), 1957747793);
+ TEST_COMPARE (rand (), 424238335);
+ TEST_COMPARE (rand (), 719885386);
+ TEST_COMPARE (rand (), 1649760492);
+ TEST_COMPARE (rand (), 596516649);
+ TEST_COMPARE (rand (), 1189641421);
+
+ srand (1009);
+ TEST_COMPARE (rand (), 176208083);
+ TEST_COMPARE (rand (), 1650100842);
+ TEST_COMPARE (rand (), 1813188575);
+ TEST_COMPARE (rand (), 2064804480);
+ TEST_COMPARE (rand (), 1944264725);
+ TEST_COMPARE (rand (), 1673642853);
+ TEST_COMPARE (rand (), 1582759448);
+ TEST_COMPARE (rand (), 309901569);
+ TEST_COMPARE (rand (), 444031692);
+ TEST_COMPARE (rand (), 1926035991);
+
+ return 0;
+}
+
+#include <support/test-driver.c>