"Fossies" - the Fresh Open Source Software Archive

Member "muscle/test/testrefcount.cpp" (8 Jun 2019, 3846 Bytes) of package /linux/privat/muscle7.30.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "testrefcount.cpp": 7.12_vs_7.13.

    1 /* This file is Copyright 2000-2013 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */  
    2 
    3 #include <stdio.h>
    4 #include "system/SetupSystem.h"
    5 #include "system/Thread.h"
    6 #include "util/String.h"
    7 #include "util/RefCount.h"
    8 #include "util/Queue.h"
    9 #include "util/Hashtable.h"
   10 #include "util/MiscUtilityFunctions.h"
   11 
   12 using namespace muscle;
   13 
   14 class TestItem : public RefCountable
   15 {
   16 public:
   17    TestItem()  {/* empty */}
   18    explicit TestItem(const String & name) {SetName(name);}
   19    ~TestItem() {_name = "Dead";}  // Just to make dead-item-usage detection a bit easier
   20 
   21    const String & GetName() const {return _name;}
   22    void SetName(const String & name) {_name = name;}
   23 
   24 private:
   25    String _name;
   26 };
   27 DECLARE_REFTYPES(TestItem);
   28 static TestItemRef::ItemPool _pool;
   29 
   30 class TestThread : public Thread
   31 {
   32 public:
   33    TestThread() {/* empty */}
   34 
   35    virtual void InternalThreadEntry()
   36    {
   37       char buf[128]; muscleSprintf(buf, "TestThread-%p", this);
   38       const String prefix = buf;
   39       Queue<TestItemRef> q;
   40       bool keepGoing = 1;
   41       uint32 counter = 0;
   42       while(keepGoing)
   43       {
   44          const uint32 x = rand() % 10000;
   45          while(q.GetNumItems() < x) 
   46          {
   47             TestItemRef tRef(_pool.ObtainObject());
   48             if (tRef())
   49             {
   50                char buf[128]; muscleSprintf(buf, "-" UINT32_FORMAT_SPEC, ++counter);
   51                tRef()->SetName(prefix+buf);
   52                q.AddTail(tRef);
   53             }
   54             else WARN_OUT_OF_MEMORY; 
   55          }
   56          while(q.GetNumItems() > x) q.RemoveTail();
   57 
   58          // Check to make sure no other threads are overwriting our objects
   59          for (uint32 i=0; i<q.GetNumItems(); i++) 
   60          {
   61             if (q[i]()->GetName().StartsWith(prefix) == false)
   62             {
   63                printf("ERROR, thread %p expected prefix [%s], saw [%s] at position " INT32_FORMAT_SPEC "/" UINT32_FORMAT_SPEC "\n", this, prefix(), q[i]()->GetName()(), i, q.GetNumItems());
   64                ExitWithoutCleanup(10);
   65             }
   66          }
   67  
   68          // Check to see if the main thread wants us to exit yet
   69          MessageRef r;
   70          while(WaitForNextMessageFromOwner(r, 0) >= 0) if (r() == NULL) keepGoing = false;
   71       }
   72    }
   73 };
   74 
   75 // This program exercises the Ref class.
   76 int main(void) 
   77 {
   78    CompleteSetupSystem setupSystem;
   79 
   80    printf("sizeof(TestItemRef)=%i\n", (int)sizeof(TestItemRef));
   81 
   82    {
   83       printf("Checking queue...\n");
   84       Queue<TestItemRef> q;
   85       printf("Adding refs...\n");
   86       for (int i=0; i<10; i++) 
   87       {
   88          char temp[50]; muscleSprintf(temp, "%i", i);
   89          TestItemRef tr(new TestItem(temp));
   90          ConstTestItemRef ctr(tr);
   91          ConstTestItemRef t2(ctr);
   92          q.AddTail(tr);
   93       }
   94       printf("Removing refs...\n");
   95       while(q.HasItems()) q.RemoveHead();
   96       printf("Done with queue test!\n");
   97    }
   98 
   99    {
  100       printf("Checking hashtable\n");
  101       Hashtable<String, TestItemRef> ht;
  102       printf("Adding refs...\n");
  103       for (int i=0; i<10; i++) 
  104       {
  105          char temp[50]; muscleSprintf(temp, "%i", i);
  106          ht.Put(String(temp), TestItemRef(new TestItem(temp)));
  107       }
  108       printf("Removing refs...\n");
  109       for (int i=0; i<10; i++) 
  110       {
  111          char temp[50]; muscleSprintf(temp, "%i", i);
  112          ht.Remove(String(temp));
  113       }
  114       printf("Done with hash table test!\n");
  115    }
  116     
  117    printf("Beginning multithreaded object usage test...\n");
  118    {
  119       const uint32 NUM_THREADS = 50;
  120       TestThread threads[NUM_THREADS]; 
  121       for (uint32 i=0; i<NUM_THREADS; i++) threads[i].StartInternalThread();
  122       Snooze64(SecondsToMicros(10));
  123       for (uint32 i=0; i<NUM_THREADS; i++) threads[i].ShutdownInternalThread();
  124       printf("Multithreaded object usage test complete.\n");
  125    }
  126 
  127    printf("testrefcount complete, bye!\n");
  128    return 0;
  129 }