From 516b44a4afcf16a87e0c3dbca873b86f1089ebbf Mon Sep 17 00:00:00 2001 From: kunitoki Date: Mon, 16 Dec 2024 00:08:20 +0100 Subject: [PATCH] More tests for random --- tests/juce_core/juce_Random.cpp | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/juce_core/juce_Random.cpp b/tests/juce_core/juce_Random.cpp index 1105df1e..99477f7f 100644 --- a/tests/juce_core/juce_Random.cpp +++ b/tests/juce_core/juce_Random.cpp @@ -61,3 +61,64 @@ TEST (RandomTests, RandomNumbers) EXPECT_TRUE (r.nextInt (n) >= 0 && r.nextInt (n) < n); } } + +TEST (RandomTests, Concurrent) +{ + class FastWaitableEvent + { + public: + void notify() { notified = true; } + void wait() const { while (! notified){} } + + private: + std::atomic notified = false; + }; + + class InvokerThread final : private Thread + { + public: + InvokerThread (std::function fn, FastWaitableEvent& notificationEvent, int numInvocationsToTrigger) + : Thread ("InvokerThread"), + invokable (fn), + notified (¬ificationEvent), + numInvocations (numInvocationsToTrigger) + { + startThread(); + } + + ~InvokerThread() { stopThread (-1); } + + void waitUntilReady() const { ready.wait(); } + + private: + void run() final + { + ready.notify(); + notified->wait(); + + for (int i = numInvocations; --i >= 0;) + invokable(); + } + + std::function invokable; + FastWaitableEvent* notified; + FastWaitableEvent ready; + int numInvocations; + }; + + constexpr int numberOfInvocationsPerThread = 10000; + constexpr int numberOfThreads = 100; + + std::vector> threads; + threads.reserve ((size_t) numberOfThreads); + FastWaitableEvent start; + + for (int i = numberOfThreads; --i >= 0;) + threads.push_back (std::make_unique ([] { Random::getSystemRandom().nextInt(); }, start, numberOfInvocationsPerThread)); + + for (auto& thread : threads) + thread->waitUntilReady(); + + Thread::sleep (1); + start.notify(); +}