summaryrefslogtreecommitdiff
path: root/tests/test-handle-set.c
blob: 39c5626aac64da5818f8e1987ca65d9c3b98d5a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <telepathy-glib/intset.h>
#include <telepathy-glib/handle-repo.h>
#include <telepathy-glib/handle-repo-dynamic.h>
#include <telepathy-glib/enums.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/errors.h>

int main (int argc, char **argv)
{

  TpHandleRepoIface *repo = NULL;
  TpHandleSet *set = NULL;
  TpIntSet *iset = NULL, *result = NULL;

  TpHandle h1, h2, h3, h4;

  g_type_init ();

  repo = (TpHandleRepoIface *)g_object_new (TP_TYPE_DYNAMIC_HANDLE_REPO,
      "handle-type", TP_HANDLE_TYPE_CONTACT, NULL);
  g_assert (repo != NULL);

  set = tp_handle_set_new (repo);
  g_assert (set != NULL);

  h1 = tp_handle_request (repo, "h1@foo", TRUE);
  h2 = tp_handle_request (repo, "h2@foo", TRUE);
  h3 = tp_handle_request (repo, "h3@foo", TRUE);
  h4 = tp_handle_request (repo, "h4@foo", TRUE);
  g_assert (h1 && h2 && h3 && h4);

  /* Add one handle, check that it's in, check the size */
  tp_handle_set_add (set, h1);
  g_assert (tp_handle_set_is_member (set, h1));
  g_assert (tp_handle_set_size (set) == 1);

  /* Adding it again should be no-op */
  tp_handle_set_add (set, h1);
  g_assert (tp_handle_set_size (set) == 1);

  /* Removing a non-member should fail */
  g_assert (tp_handle_set_remove (set, h2) == FALSE);

  /* Add some members via _update() */
  iset = tp_intset_new ();
  tp_intset_add (iset, h1);
  tp_intset_add (iset, h2);
  tp_intset_add (iset, h3);
  result = tp_handle_set_update (set, iset);
  tp_intset_destroy (iset);

  /* h2 and h3 should be added, and h1 not */
  g_assert (!tp_intset_is_member (result, h1));
  g_assert (tp_intset_is_member (result, h2));
  g_assert (tp_intset_is_member (result, h3));
  tp_intset_destroy (result);

  g_assert (tp_handle_set_is_member (set, h2));
  g_assert (tp_handle_set_is_member (set, h3));

  /* Remove some members via _update_difference() */
  iset = tp_intset_new ();
  tp_intset_add (iset, h1);
  tp_intset_add (iset, h4);
  result = tp_handle_set_difference_update (set, iset);
  tp_intset_destroy (iset);

  /* h1 should be removed, h4 not */
  g_assert (tp_intset_is_member (result, h1));
  g_assert (!tp_intset_is_member (result, h4));
  tp_intset_destroy (result);

  /* Removing a member should succeed */
  g_assert (tp_handle_set_remove (set, h2) == TRUE);

  /* Finally, only h3 should be in the set */
  g_assert (tp_handle_set_is_member (set, h3));
  g_assert (tp_handle_set_size (set) == 1);

  g_assert (tp_handle_set_remove (set, h3) == TRUE);

  tp_handle_set_destroy (set);
  g_object_unref (G_OBJECT (repo));

  return 0;
}