summaryrefslogtreecommitdiff
path: root/tests/intset.c
blob: c456e5dba262ca1a0683967c2351febc198727d7 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <glib.h>
#include <telepathy-glib/intset.h>
#include <telepathy-glib/util.h>

int main (int argc, char **argv)
{
  TpIntSet *set1 = tp_intset_new ();
  TpIntSet *a, *b, *copy;
  TpIntSet *ab_union, *ab_expected_union;
  TpIntSet *ab_inter, *ab_expected_inter;
  TpIntSet *a_diff_b, *a_expected_diff_b;
  TpIntSet *b_diff_a, *b_expected_diff_a;
  TpIntSet *ab_symmdiff, *ab_expected_symmdiff;
  GValue *value;

  g_type_init ();

  g_assert (tp_intset_is_empty (set1));
  g_assert_cmpuint (tp_intset_size (set1), ==, 0);

  tp_intset_add (set1, 0);

  tp_intset_add (set1, 2);
  tp_intset_add (set1, 3);
  tp_intset_add (set1, 5);
  tp_intset_add (set1, 8);

  tp_intset_add (set1, 1024);
  tp_intset_add (set1, 32);

  g_assert (!tp_intset_is_empty (set1));
  g_assert_cmpuint (tp_intset_size (set1), ==, 7);

  g_assert (tp_intset_is_member (set1, 2));
  g_assert (tp_intset_is_member (set1, 5));
  g_assert (tp_intset_is_member (set1, 1024));
  g_assert (!tp_intset_is_member (set1, 1023));
  g_assert (!tp_intset_is_member (set1, 1025));
  g_assert (tp_intset_is_member (set1, 0));
  g_assert (tp_intset_is_member (set1, 32));
  g_assert (!tp_intset_is_member (set1, 31));
  g_assert (!tp_intset_is_member (set1, 33));

  tp_intset_remove (set1, 8);
  tp_intset_remove (set1, 1024);
  g_assert_cmpuint (tp_intset_size (set1), ==, 5);

  tp_intset_destroy (set1);

#define NUM_A 11
#define NUM_B 823
#define NUM_C 367
#define NUM_D 4177
#define NUM_E 109
#define NUM_F 1861

  a = tp_intset_new ();
  tp_intset_add (a, NUM_A);
  tp_intset_add (a, NUM_B);
  tp_intset_add (a, NUM_C);
  tp_intset_add (a, NUM_D);

  g_assert (tp_intset_is_equal (a, a));

  b = tp_intset_new ();
  tp_intset_add (b, NUM_C);
  tp_intset_add (b, NUM_D);
  tp_intset_add (b, NUM_E);
  tp_intset_add (b, NUM_F);

  g_assert (tp_intset_is_equal (b, b));
  g_assert (!tp_intset_is_equal (a, b));

  ab_expected_union = tp_intset_new ();
  tp_intset_add (ab_expected_union, NUM_A);
  tp_intset_add (ab_expected_union, NUM_B);
  tp_intset_add (ab_expected_union, NUM_C);
  tp_intset_add (ab_expected_union, NUM_D);
  tp_intset_add (ab_expected_union, NUM_E);
  tp_intset_add (ab_expected_union, NUM_F);

  ab_union = tp_intset_union (a, b);
  g_assert (tp_intset_is_equal (ab_union, ab_expected_union));
  tp_intset_destroy (ab_union);
  tp_intset_destroy (ab_expected_union);
  ab_union = NULL;
  ab_expected_union = NULL;

  ab_expected_inter = tp_intset_new ();
  tp_intset_add (ab_expected_inter, NUM_C);
  tp_intset_add (ab_expected_inter, NUM_D);

  ab_inter = tp_intset_intersection (a, b);
  g_assert (tp_intset_is_equal (ab_inter, ab_expected_inter));
  tp_intset_destroy (ab_inter);
  tp_intset_destroy (ab_expected_inter);
  ab_inter = NULL;
  ab_expected_inter = NULL;

  a_expected_diff_b = tp_intset_new ();
  tp_intset_add (a_expected_diff_b, NUM_A);
  tp_intset_add (a_expected_diff_b, NUM_B);

  a_diff_b = tp_intset_difference (a, b);
  g_assert (tp_intset_is_equal (a_diff_b, a_expected_diff_b));
  tp_intset_destroy (a_diff_b);
  tp_intset_destroy (a_expected_diff_b);
  a_diff_b = NULL;
  a_expected_diff_b = NULL;

  b_expected_diff_a = tp_intset_new ();
  tp_intset_add (b_expected_diff_a, NUM_E);
  tp_intset_add (b_expected_diff_a, NUM_F);

  b_diff_a = tp_intset_difference (b, a);
  g_assert (tp_intset_is_equal (b_diff_a, b_expected_diff_a));
  tp_intset_destroy (b_diff_a);
  tp_intset_destroy (b_expected_diff_a);
  b_diff_a = NULL;
  b_expected_diff_a = NULL;

  ab_expected_symmdiff = tp_intset_new ();
  tp_intset_add (ab_expected_symmdiff, NUM_A);
  tp_intset_add (ab_expected_symmdiff, NUM_B);
  tp_intset_add (ab_expected_symmdiff, NUM_E);
  tp_intset_add (ab_expected_symmdiff, NUM_F);

  ab_symmdiff = tp_intset_symmetric_difference (a, b);
  g_assert (tp_intset_is_equal (ab_symmdiff, ab_expected_symmdiff));
  tp_intset_destroy (ab_symmdiff);
  tp_intset_destroy (ab_expected_symmdiff);
  ab_symmdiff = NULL;
  ab_expected_symmdiff = NULL;

  {
    GArray *arr;
    TpIntSet *tmp;

    arr = tp_intset_to_array (a);
    tmp = tp_intset_from_array (arr);
    g_assert (tp_intset_is_equal (a, tmp));
    g_array_free (arr, TRUE);
    tp_intset_destroy (tmp);
    arr = NULL;
    tmp = NULL;

    arr = tp_intset_to_array (b);
    tmp = tp_intset_from_array (arr);
    g_assert (tp_intset_is_equal (b, tmp));
    g_array_free (arr, TRUE);
    tp_intset_destroy (tmp);
    arr = NULL;
    tmp = NULL;
  }

  {
    TpIntSetFastIter iter;
    guint n = 0;
    guint i;

    tp_intset_fast_iter_init (&iter, a);

    while (tp_intset_fast_iter_next (&iter, &i))
      {
        g_assert (tp_intset_is_member (a, i));
        n++;
      }

    g_assert_cmpuint (n, ==, tp_intset_size (a));
  }

  value = tp_g_value_slice_new_take_boxed (TP_TYPE_INTSET, a);
  copy = g_value_dup_boxed (value);

  g_assert (copy != a);
  g_assert (tp_intset_is_equal (copy, a));
  g_boxed_free (TP_TYPE_INTSET, copy);

  /* a is owned by value now, so don't free it explicitly */
  tp_g_value_slice_free (value);
  tp_intset_destroy (b);
  a = NULL;
  value = NULL;
  b = NULL;

  return 0;
}