Blame glib/tests/asyncqueue.c

Packit ae235b
/* Unit tests for GAsyncQueue
Packit ae235b
 * Copyright (C) 2011 Red Hat, Inc
Packit ae235b
 * Author: Matthias Clasen
Packit ae235b
 *
Packit ae235b
 * This work is provided "as is"; redistribution and modification
Packit ae235b
 * in whole or in part, in any medium, physical or electronic is
Packit ae235b
 * permitted without restriction.
Packit ae235b
 *
Packit ae235b
 * This work is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit ae235b
 *
Packit ae235b
 * In no event shall the authors or contributors be liable for any
Packit ae235b
 * direct, indirect, incidental, special, exemplary, or consequential
Packit ae235b
 * damages (including, but not limited to, procurement of substitute
Packit ae235b
 * goods or services; loss of use, data, or profits; or business
Packit ae235b
 * interruption) however caused and on any theory of liability, whether
Packit ae235b
 * in contract, strict liability, or tort (including negligence or
Packit ae235b
 * otherwise) arising in any way out of the use of this software, even
Packit ae235b
 * if advised of the possibility of such damage.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* We are testing some deprecated APIs here */
Packit ae235b
#define GLIB_DISABLE_DEPRECATION_WARNINGS
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
static gint
Packit ae235b
compare_func (gconstpointer d1, gconstpointer d2, gpointer data)
Packit ae235b
{
Packit ae235b
  gint i1, i2;
Packit ae235b
Packit ae235b
  i1 = GPOINTER_TO_INT (d1);
Packit ae235b
  i2 = GPOINTER_TO_INT (d2);
Packit ae235b
Packit ae235b
  return i1 - i2;
Packit ae235b
}
Packit ae235b
Packit ae235b
static
Packit ae235b
void test_async_queue_sort (void)
Packit ae235b
{
Packit ae235b
  GAsyncQueue *q;
Packit ae235b
Packit ae235b
  q = g_async_queue_new ();
Packit ae235b
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (10));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (2));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (7));
Packit ae235b
Packit ae235b
  g_async_queue_sort (q, compare_func, NULL);
Packit ae235b
Packit ae235b
  g_async_queue_push_sorted (q, GINT_TO_POINTER (1), compare_func, NULL);
Packit ae235b
  g_async_queue_push_sorted (q, GINT_TO_POINTER (8), compare_func, NULL);
Packit ae235b
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 1);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 2);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 7);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 8);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 10);
Packit ae235b
Packit ae235b
  g_assert (g_async_queue_try_pop (q) == NULL);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint destroy_count;
Packit ae235b
Packit ae235b
static void
Packit ae235b
destroy_notify (gpointer item)
Packit ae235b
{
Packit ae235b
  destroy_count++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_queue_destroy (void)
Packit ae235b
{
Packit ae235b
  GAsyncQueue *q;
Packit ae235b
Packit ae235b
  q = g_async_queue_new_full (destroy_notify);
Packit ae235b
Packit ae235b
  g_assert (destroy_count == 0);
Packit ae235b
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (1));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (1));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (1));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (1));
Packit ae235b
Packit ae235b
  g_assert (g_async_queue_length (q) == 4);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
Packit ae235b
  g_assert (destroy_count == 4);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GAsyncQueue *q;
Packit ae235b
Packit ae235b
static GThread *threads[10];
Packit ae235b
static gint counts[10];
Packit ae235b
static gint sums[10];
Packit ae235b
static gint total;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
thread_func (gpointer data)
Packit ae235b
{
Packit ae235b
  gint pos = GPOINTER_TO_INT (data);
Packit ae235b
  gint value;
Packit ae235b
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      value = GPOINTER_TO_INT (g_async_queue_pop (q));
Packit ae235b
Packit ae235b
      if (value == -1)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      counts[pos]++;
Packit ae235b
      sums[pos] += value;
Packit ae235b
Packit ae235b
      g_usleep (1000);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_queue_threads (void)
Packit ae235b
{
Packit ae235b
  gint i, j;
Packit ae235b
  gint s, c;
Packit ae235b
  gint value;
Packit ae235b
Packit ae235b
  q = g_async_queue_new ();
Packit ae235b
Packit ae235b
  for (i = 0; i < 10; i++)
Packit ae235b
    threads[i] = g_thread_new ("test", thread_func, GINT_TO_POINTER (i));
Packit ae235b
Packit ae235b
  for (i = 0; i < 100; i++)
Packit ae235b
    {
Packit ae235b
      g_async_queue_lock (q);
Packit ae235b
      for (j = 0; j < 10; j++)
Packit ae235b
        {
Packit ae235b
          value = g_random_int_range (1, 100);
Packit ae235b
          total += value;
Packit ae235b
          g_async_queue_push_unlocked (q, GINT_TO_POINTER (value));
Packit ae235b
        }
Packit ae235b
      g_async_queue_unlock (q);
Packit ae235b
Packit ae235b
      g_usleep (1000);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 0; i < 10; i++)
Packit ae235b
    g_async_queue_push (q, GINT_TO_POINTER(-1));
Packit ae235b
Packit ae235b
  for (i = 0; i < 10; i++)
Packit ae235b
    g_thread_join (threads[i]);
Packit ae235b
Packit ae235b
  g_assert_cmpint (g_async_queue_length (q), ==, 0);
Packit ae235b
Packit ae235b
  s = c = 0;
Packit ae235b
Packit ae235b
  for (i = 0; i < 10; i++)
Packit ae235b
    {
Packit ae235b
      g_assert_cmpint (sums[i], >, 0);
Packit ae235b
      g_assert_cmpint (counts[i], >, 0);
Packit ae235b
      s += sums[i];
Packit ae235b
      c += counts[i];
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert_cmpint (s, ==, total);
Packit ae235b
  g_assert_cmpint (c, ==, 1000);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_queue_timed (void)
Packit ae235b
{
Packit ae235b
  GAsyncQueue *q;
Packit ae235b
  GTimeVal tv;
Packit ae235b
  gint64 start, end, diff;
Packit ae235b
  gpointer val;
Packit ae235b
Packit ae235b
  q = g_async_queue_new ();
Packit ae235b
Packit ae235b
  start = g_get_monotonic_time ();
Packit ae235b
  val = g_async_queue_timeout_pop (q, G_USEC_PER_SEC / 10);
Packit ae235b
  g_assert (val == NULL);
Packit ae235b
Packit ae235b
  end = g_get_monotonic_time ();
Packit ae235b
  diff = end - start;
Packit ae235b
  g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10);
Packit ae235b
  /* diff should be only a little bit more than G_USEC_PER_SEC/10, but
Packit ae235b
   * we have to leave some wiggle room for heavily-loaded machines...
Packit ae235b
   */
Packit ae235b
  g_assert_cmpint (diff, <, G_USEC_PER_SEC);
Packit ae235b
Packit ae235b
  start = end;
Packit ae235b
  g_get_current_time (&tv;;
Packit ae235b
  g_time_val_add (&tv, G_USEC_PER_SEC / 10);
Packit ae235b
  val = g_async_queue_timed_pop (q, &tv;;
Packit ae235b
  g_assert (val == NULL);
Packit ae235b
Packit ae235b
  end = g_get_monotonic_time ();
Packit ae235b
  diff = end - start;
Packit ae235b
  g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10);
Packit ae235b
  g_assert_cmpint (diff, <, G_USEC_PER_SEC);
Packit ae235b
Packit ae235b
  start = end;
Packit ae235b
  g_get_current_time (&tv;;
Packit ae235b
  g_time_val_add (&tv, G_USEC_PER_SEC / 10);
Packit ae235b
  g_async_queue_lock (q);
Packit ae235b
  val = g_async_queue_timed_pop_unlocked (q, &tv;;
Packit ae235b
  g_async_queue_unlock (q);
Packit ae235b
  g_assert (val == NULL);
Packit ae235b
Packit ae235b
  end = g_get_monotonic_time ();
Packit ae235b
  diff = end - start;
Packit ae235b
  g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10);
Packit ae235b
  g_assert_cmpint (diff, <, G_USEC_PER_SEC);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_queue_remove (void)
Packit ae235b
{
Packit ae235b
  GAsyncQueue *q;
Packit ae235b
Packit ae235b
  q = g_async_queue_new ();
Packit ae235b
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (10));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (2));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (7));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (1));
Packit ae235b
Packit ae235b
  g_async_queue_remove (q, GINT_TO_POINTER (7));
Packit ae235b
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 10);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 2);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 1);
Packit ae235b
Packit ae235b
  g_assert (g_async_queue_try_pop (q) == NULL);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async_queue_push_front (void)
Packit ae235b
{
Packit ae235b
  GAsyncQueue *q;
Packit ae235b
Packit ae235b
  q = g_async_queue_new ();
Packit ae235b
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (10));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (2));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (7));
Packit ae235b
Packit ae235b
  g_async_queue_push_front (q, GINT_TO_POINTER (1));
Packit ae235b
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 1);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 10);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 2);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 7);
Packit ae235b
Packit ae235b
  g_assert (g_async_queue_try_pop (q) == NULL);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/asyncqueue/sort", test_async_queue_sort);
Packit ae235b
  g_test_add_func ("/asyncqueue/destroy", test_async_queue_destroy);
Packit ae235b
  g_test_add_func ("/asyncqueue/threads", test_async_queue_threads);
Packit ae235b
  g_test_add_func ("/asyncqueue/timed", test_async_queue_timed);
Packit ae235b
  g_test_add_func ("/asyncqueue/remove", test_async_queue_remove);
Packit ae235b
  g_test_add_func ("/asyncqueue/push_front", test_async_queue_push_front);
Packit ae235b
Packit ae235b
  return g_test_run ();
Packit ae235b
}