Commits

dbacchet committed f067436

added thread/mutex tests

Comments (0)

Files changed (2)

testing/foundation/test_memory.cpp

             TempAllocator128 ta(a); // use malloc as backing allocator
             int *p = (int*)ta.allocate(100);
             // printf("pointer: %x\n",p);
-            CHECK_EQUAL(ta.allocated_size(p),Allocator::SIZE_NOT_TRACKED);
+            //CHECK_EQUAL(ta.allocated_size(p),Allocator::SIZE_NOT_TRACKED);
+            CHECK_EQUAL(ta.allocated_size(p),0xffffffffu);
             CHECK(a.total_allocated()>=1024);
             CHECK(a.total_allocated()<1024+100);
 
-            int *q = (int*)ta.allocate(24);
+            int *q = (int*)ta.allocate(20);
             // printf("pointer: %x\n",q);
             // printf("temp allocator alloc size = %d\n",ta.allocated_size(q));
             // printf("def allocator total alloc = %d\n",a.total_allocated());

testing/foundation/test_threads.cpp

 {
     using namespace foundation;
 
+    // test class: update a counter and store its value to a given location (testdata)
+    class TestThread : public Thread
+    {
+	public:
+		int counter;
+		int *ref;
+
+		TestThread(int *refdata)
+		: counter(0), ref(NULL)
+		{
+			if (refdata)
+			{
+				counter = *refdata;
+				ref = refdata;
+			}
+		}
+
+		// actual code run
+		void run()
+		{
+			for (unsigned int i=0; i<100; i++)
+			{
+				this->counter++;
+				//printf("[%llu] %d\n",Thread::get_current_thread_id(),counter);
+				Thread::sleep(10); // wait 10 ms
+			}
+			if (ref)
+				*ref = counter;
+		}
+	};
+
+	TEST (TestThreads)
+	{
+		int data[5] = {1,2,3,4,5};
+		TestThread t1(data+0);
+		TestThread t2(data+1);
+		TestThread t3(data+2);
+		TestThread t4(data+3);
+		TestThread t5(data+4);
+		// run concurrently
+		t1.start();
+		t2.start();
+		t3.start();
+		t4.start();
+		t5.start();
+		// join
+		t1.join();
+		t2.join();
+		t3.join();
+		t4.join();
+		t5.join();
+		
+        CHECK_EQUAL(data[0],101);
+        CHECK_EQUAL(data[1],102);
+        CHECK_EQUAL(data[2],103);
+        CHECK_EQUAL(data[3],104);
+        CHECK_EQUAL(data[4],105);
+	}
+	
+
+
+	Mutex mutex;
+	int sharedCounter = 0;
+
+    class CounterThread : public Thread
+    {
+	public:
+		// actual code run
+		void run()
+		{
+			for (unsigned int i=0; i<100; i++)
+			{
+				mutex.lock();
+
+				int tmp = sharedCounter;
+				tmp++;
+				// wait, to produce an unsafe area
+				Thread::sleep(10);
+				sharedCounter = tmp;
+				
+				mutex.unlock();
+			}
+		}
+	};
+
 	TEST (TestMutex)
 	{
-        CHECK(true);
+		CounterThread t1;
+		CounterThread t2;
+		// run concurrently
+		t1.start();
+		t2.start();
+		// join
+		t1.join();
+		t2.join();
+		
+        CHECK_EQUAL(200,sharedCounter);
 	}
 	
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.