]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added a regression test.
authorBart Van Assche <bvanassche@acm.org>
Mon, 9 Nov 2009 15:44:53 +0000 (15:44 +0000)
committerBart Van Assche <bvanassche@acm.org>
Mon, 9 Nov 2009 15:44:53 +0000 (15:44 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10932

drd/tests/Makefile.am
drd/tests/qt4_atomic.cpp [new file with mode: 0644]

index 1918ede9df13bc6beb1a47999dea1e4f3eb29b7c..ccf80c81d3612a686fba1d0cb7de813df2c2f3be 100644 (file)
@@ -147,6 +147,8 @@ EXTRA_DIST =                                        \
        pth_process_shared_mutex.vgtest             \
        pth_spinlock.stderr.exp                     \
        pth_spinlock.vgtest                         \
+       qt4_atomic.stderr.exp                       \
+       qt4_atomic.vgtest                           \
        qt4_mutex.stderr.exp                        \
        qt4_mutex.vgtest                            \
        qt4_rwlock.stderr.exp                       \
@@ -300,7 +302,7 @@ check_PROGRAMS += pth_spinlock
 endif
 
 if HAVE_QTCORE
-check_PROGRAMS += qt4_mutex qt4_rwlock qt4_semaphore
+check_PROGRAMS += qt4_atomic qt4_mutex qt4_rwlock qt4_semaphore
 endif
 
 
@@ -352,6 +354,10 @@ matinv_LDADD                = $(LDADD) -lm
 endif
 
 if HAVE_QTCORE
+qt4_atomic_SOURCES          = qt4_atomic.cpp
+qt4_atomic_CXXFLAGS         = $(AM_CXXFLAGS) $(QTCORE_CFLAGS)
+qt4_atomic_LDADD            = $(LDADD) $(QTCORE_LIBS)
+
 qt4_mutex_SOURCES           = qt4_mutex.cpp
 qt4_mutex_CXXFLAGS          = $(AM_CXXFLAGS) $(QTCORE_CFLAGS)
 qt4_mutex_LDADD             = $(LDADD) $(QTCORE_LIBS)
diff --git a/drd/tests/qt4_atomic.cpp b/drd/tests/qt4_atomic.cpp
new file mode 100644 (file)
index 0000000..903a89e
--- /dev/null
@@ -0,0 +1,66 @@
+/// Test program that uses the QAtomicInt class.
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include "config.h"
+#include <QMutex>         // class QMutex
+#include <QAtomicInt>     // class QAtomicInt
+#include <cassert>
+#include <cstdio>         // fprintf()
+#include <cstdlib>        // atoi()
+#include <new>
+#include <pthread.h>      // pthread_barrier_t
+#include <vector>
+
+
+static pthread_barrier_t s_barrier;
+static QAtomicInt* s_pAtomicInt;
+
+
+void* thread_func(void* pArg)
+{
+  const int iArg = *reinterpret_cast<int*>(pArg);
+
+  pthread_barrier_wait(&s_barrier);
+
+  while (! s_pAtomicInt->testAndSetOrdered(iArg, iArg + 1))
+    ;
+
+  return NULL;
+}
+
+int main(int argc, char** argv)
+{
+  int i;
+  const int n_threads = 10;
+  std::vector<int>       thread_arg(n_threads);
+  std::vector<pthread_t> tid(n_threads);
+
+  fprintf(stderr, "Start of test.\n");
+
+  pthread_barrier_init(&s_barrier, 0, n_threads);
+  s_pAtomicInt = new QAtomicInt();
+  for (i = 0; i < n_threads; i++)
+  {
+    thread_arg[i] = i;
+    pthread_create(&tid[i], 0, thread_func, &thread_arg[i]);
+  }
+  for (i = 0; i < n_threads; i++)
+  {
+    pthread_join(tid[i], NULL);
+  }
+  pthread_barrier_destroy(&s_barrier);
+
+  if (*s_pAtomicInt == n_threads)
+    fprintf(stderr, "Test successful.\n");
+  else
+    fprintf(stderr, "Test failed: counter = %d, should be %d\n",
+            static_cast<int>(*s_pAtomicInt), n_threads);
+
+  delete s_pAtomicInt;
+  s_pAtomicInt = 0;
+
+  return 0;
+}