Blame libglnx/tests/test-libglnx-macros.c

rpm-build c487f7
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
rpm-build c487f7
 *
rpm-build c487f7
 * Copyright (C) 2017 Red Hat, Inc.
rpm-build c487f7
 *
rpm-build c487f7
 * This library is free software; you can redistribute it and/or
rpm-build c487f7
 * modify it under the terms of the GNU Lesser General Public
rpm-build c487f7
 * License as published by the Free Software Foundation; either
rpm-build c487f7
 * version 2 of the License, or (at your option) any later version.
rpm-build c487f7
 *
rpm-build c487f7
 * This library is distributed in the hope that it will be useful,
rpm-build c487f7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build c487f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build c487f7
 * Lesser General Public License for more details.
rpm-build c487f7
 *
rpm-build c487f7
 * You should have received a copy of the GNU Lesser General Public
rpm-build c487f7
 * License along with this library; if not, write to the
rpm-build c487f7
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build c487f7
 * Boston, MA 02111-1307, USA.
rpm-build c487f7
 */
rpm-build c487f7
rpm-build c487f7
#include "config.h"
rpm-build c487f7
#include "libglnx.h"
rpm-build c487f7
#include <glib.h>
rpm-build c487f7
#include <stdlib.h>
rpm-build c487f7
#include <gio/gio.h>
rpm-build c487f7
#include <string.h>
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_inset (void)
rpm-build c487f7
{
rpm-build c487f7
  g_assert (G_IN_SET (7, 7));
rpm-build c487f7
  g_assert (G_IN_SET (7, 42, 7));
rpm-build c487f7
  g_assert (G_IN_SET (7, 7,42,3,9));
rpm-build c487f7
  g_assert (G_IN_SET (42, 7,42,3,9));
rpm-build c487f7
  g_assert (G_IN_SET (3, 7,42,3,9));
rpm-build c487f7
  g_assert (G_IN_SET (9, 7,42,3,9));
rpm-build c487f7
  g_assert (!G_IN_SET (8, 7,42,3,9));
rpm-build c487f7
  g_assert (!G_IN_SET (-1, 7,42,3,9));
rpm-build c487f7
  g_assert (G_IN_SET ('x', 'a', 'x', 'c'));
rpm-build c487f7
  g_assert (!G_IN_SET ('y', 'a', 'x', 'c'));
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_hash_table_foreach (void)
rpm-build c487f7
{
rpm-build c487f7
  /* use var names all different from the macro metavars to ensure proper
rpm-build c487f7
   * substitution */
rpm-build c487f7
  g_autoptr(GHashTable) table = g_hash_table_new (g_str_hash, g_str_equal);
rpm-build c487f7
  const char *keys[] = {"key1", "key2"};
rpm-build c487f7
  const char *vals[] = {"val1", "val2"};
rpm-build c487f7
  g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
rpm-build c487f7
  g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
rpm-build c487f7
rpm-build c487f7
  guint i = 0;
rpm-build c487f7
  GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
rpm-build c487f7
    {
rpm-build c487f7
      g_assert_cmpstr (key, ==, keys[i]);
rpm-build c487f7
      g_assert_cmpstr (val, ==, vals[i]);
rpm-build c487f7
      i++;
rpm-build c487f7
    }
rpm-build c487f7
  g_assert_cmpuint (i, ==, 2);
rpm-build c487f7
rpm-build c487f7
  i = 0;
rpm-build c487f7
  GLNX_HASH_TABLE_FOREACH_IT (table, it, const char*, key, const char*, val)
rpm-build c487f7
    {
rpm-build c487f7
      g_hash_table_iter_remove (&it);
rpm-build c487f7
      break;
rpm-build c487f7
    }
rpm-build c487f7
  g_assert_cmpuint (g_hash_table_size (table), ==, 1);
rpm-build c487f7
rpm-build c487f7
  g_hash_table_insert (table, (gpointer)keys[1], (gpointer)vals[1]);
rpm-build c487f7
  g_assert_cmpuint (g_hash_table_size (table), ==, 1);
rpm-build c487f7
rpm-build c487f7
  g_hash_table_insert (table, (gpointer)keys[0], (gpointer)vals[0]);
rpm-build c487f7
  g_assert_cmpuint (g_hash_table_size (table), ==, 2);
rpm-build c487f7
rpm-build c487f7
  i = 0;
rpm-build c487f7
  GLNX_HASH_TABLE_FOREACH_KV (table, const char*, key, const char*, val)
rpm-build c487f7
    {
rpm-build c487f7
      g_assert_cmpstr (key, ==, keys[i]);
rpm-build c487f7
      g_assert_cmpstr (val, ==, vals[i]);
rpm-build c487f7
      i++;
rpm-build c487f7
    }
rpm-build c487f7
  g_assert_cmpuint (i, ==, 2);
rpm-build c487f7
rpm-build c487f7
  i = 0;
rpm-build c487f7
  GLNX_HASH_TABLE_FOREACH (table, const char*, key)
rpm-build c487f7
    {
rpm-build c487f7
      g_assert_cmpstr (key, ==, keys[i]);
rpm-build c487f7
      i++;
rpm-build c487f7
    }
rpm-build c487f7
  g_assert_cmpuint (i, ==, 2);
rpm-build c487f7
rpm-build c487f7
  i = 0;
rpm-build c487f7
  GLNX_HASH_TABLE_FOREACH_V (table, const char*, val)
rpm-build c487f7
    {
rpm-build c487f7
      g_assert_cmpstr (val, ==, vals[i]);
rpm-build c487f7
      i++;
rpm-build c487f7
    }
rpm-build c487f7
  g_assert_cmpuint (i, ==, 2);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
int main (int argc, char **argv)
rpm-build c487f7
{
rpm-build c487f7
  g_test_init (&argc, &argv, NULL);
rpm-build c487f7
  g_test_add_func ("/inset", test_inset);
rpm-build c487f7
  g_test_add_func ("/hash_table_foreach", test_hash_table_foreach);
rpm-build c487f7
  return g_test_run();
rpm-build c487f7
}