aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-01-01 17:57:12 -0800
committerGarrett D'Amore <garrett@damore.org>2020-01-01 17:57:12 -0800
commit11985f8c59cccc0364bde7dd314e246ea53cff90 (patch)
treec75c15db75b297817c10c9f2f6188aa767cbfa82 /tests
parentec7de57627a2bba8fadfb34d118ac478fbc351aa (diff)
downloadnng-11985f8c59cccc0364bde7dd314e246ea53cff90.tar.gz
nng-11985f8c59cccc0364bde7dd314e246ea53cff90.tar.bz2
nng-11985f8c59cccc0364bde7dd314e246ea53cff90.zip
fixes #1083 Random number improvements
Diffstat (limited to 'tests')
-rw-r--r--tests/platform.c77
1 files changed, 51 insertions, 26 deletions
diff --git a/tests/platform.c b/tests/platform.c
index 7546fa7f..fc9eb210 100644
--- a/tests/platform.c
+++ b/tests/platform.c
@@ -66,10 +66,10 @@ test_clock(void)
TEST_CHECK(usdelta >= 200);
TEST_CHECK(usdelta < 500); // increased tolerance for CIs
if (msdelta > usdelta) {
- TEST_CHECK((msdelta - usdelta) < 50);
+ TEST_CHECK((msdelta - usdelta) < 50);
} else {
- TEST_CHECK((usdelta - msdelta) < 50);
- }
+ TEST_CHECK((usdelta - msdelta) < 50);
+ }
}
void
@@ -108,40 +108,65 @@ test_thread(void)
nng_thread_destroy(thr);
TEST_CHECK(aa.cnt == 1);
- nng_cv_free(aa.cv);
+ nng_cv_free(aa.cv);
nng_mtx_free(aa.mx);
}
void
test_condvar(void)
{
- nng_thread * thr;
- int rv;
- struct addarg aa;
-
- TEST_CHECK(nng_mtx_alloc(&aa.mx) == 0);
- TEST_CHECK(nng_cv_alloc(&aa.cv, aa.mx) == 0);
- aa.cnt = 0;
-
- TEST_CHECK((rv = nng_thread_create(&thr, add, &aa)) == 0);
-
- nng_mtx_lock(aa.mx);
- while (aa.cnt == 0) {
- nng_cv_wait(aa.cv);
- }
- nng_mtx_unlock(aa.mx);
- nng_thread_destroy(thr);
- TEST_CHECK(aa.cnt == 1);
-
- nng_cv_free(aa.cv);
- nng_mtx_free(aa.mx);
+ nng_thread * thr;
+ int rv;
+ struct addarg aa;
+
+ TEST_CHECK(nng_mtx_alloc(&aa.mx) == 0);
+ TEST_CHECK(nng_cv_alloc(&aa.cv, aa.mx) == 0);
+ aa.cnt = 0;
+
+ TEST_CHECK((rv = nng_thread_create(&thr, add, &aa)) == 0);
+
+ nng_mtx_lock(aa.mx);
+ while (aa.cnt == 0) {
+ nng_cv_wait(aa.cv);
+ }
+ nng_mtx_unlock(aa.mx);
+ nng_thread_destroy(thr);
+ TEST_CHECK(aa.cnt == 1);
+
+ nng_cv_free(aa.cv);
+ nng_mtx_free(aa.mx);
+}
+
+void
+test_random(void)
+{
+ int same = 0;
+ uint32_t values[1000];
+
+ for (int i = 0; i < 1000; i++) {
+ values[i] = nng_random();
+ }
+ for (int i = 0; i < 1000; i++) {
+ for (int j = 0; j < i; j++) {
+ if (values[j] == values[i]) {
+ same++;
+ }
+ }
+ }
+
+ // 1% reproduction is *highly* unlikely.
+ // There are 4 billion possible options, we are only looking at
+ // 1000 of them. In general, it would be an extreme outlier
+ // to see more than 2 repeats, unless you RNG is biased.
+ TEST_CHECK_(same < 5, "fewer than 5 in 1000 repeats: %d", same);
}
TEST_LIST = {
{ "sleep", test_sleep },
{ "clock", test_clock },
{ "mutex", test_mutex },
- { "thread", test_thread },
- { "condvar", test_condvar },
+ { "thread", test_thread },
+ { "condvar", test_condvar },
+ { "random", test_random },
{ NULL, NULL },
};