2
* Copyright (C) 2017 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Pete Woods <pete.woods@canonical.com>
19
#include <unity/util/GlibMemory.h>
21
#include <gtest/gtest.h>
27
using namespace unity;
28
using namespace unity::util;
33
static set<string> keys;
35
static set<string> values;
37
class GlibMemoryTest: public testing::Test
46
static void deleteKey(gpointer data)
48
// Note that we don't g_free the data, because it's a reference to a static string
49
keys.emplace(string((gchar*) data));
52
static void deleteValue(gpointer data)
54
// Note that we don't g_free the data, because it's a reference to a static string
55
values.emplace(string((gchar*) data));
59
TEST_F(GlibMemoryTest, Unique)
62
auto gkf = unique_glib(g_key_file_new());
63
g_key_file_set_boolean(gkf.get(), "group", "key", TRUE);
64
EXPECT_EQ(TRUE, g_key_file_get_boolean(gkf.get(), "group", "key", NULL));
68
auto variant = unique_glib(g_variant_new_string("hello"));
69
EXPECT_STREQ("hello", g_variant_get_string(variant.get(), NULL));
74
auto hash = unique_glib(g_hash_table_new_full(g_str_hash, g_str_equal, deleteKey, deleteValue));
75
g_hash_table_insert(hash.get(), (gpointer) "hello", (gpointer) "there");
76
g_hash_table_insert(hash.get(), (gpointer) "hash", (gpointer) "world");
78
EXPECT_EQ(set<string>({"hello", "hash"}), keys);
79
EXPECT_EQ(set<string>({"there", "world"}), values);
83
TEST_F(GlibMemoryTest, Share)
86
auto gkf = share_glib(g_key_file_new());
87
g_key_file_set_boolean(gkf.get(), "group", "key", TRUE);
88
EXPECT_EQ(TRUE, g_key_file_get_boolean(gkf.get(), "group", "key", NULL));
92
auto variant = share_glib(g_variant_new_string("hello"));
93
EXPECT_STREQ("hello", g_variant_get_string(variant.get(), NULL));
98
auto hash = share_glib(g_hash_table_new_full(g_str_hash, g_str_equal, deleteKey, deleteValue));
99
g_hash_table_insert(hash.get(), (gpointer) "hello", (gpointer) "there");
100
g_hash_table_insert(hash.get(), (gpointer) "hash", (gpointer) "world");
102
EXPECT_EQ(set<string>({"hello", "hash"}), keys);
103
EXPECT_EQ(set<string>({"there", "world"}), values);