~dobey/unity-api/add-simple-logger

« back to all changes in this revision

Viewing changes to test/gtest/unity/util/GlibMemory/GlibMemory_test.cpp

  • Committer: Pete Woods
  • Date: 2017-01-19 13:52:32 UTC
  • mto: This revision was merged to the branch mainline in revision 266.
  • Revision ID: pete.woods@canonical.com-20170119135232-oiyrjg6kbpw38gxw
unity::util - add Glib memory management utility functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2017 Canonical Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Pete Woods <pete.woods@canonical.com>
 
17
 */
 
18
 
 
19
#include <unity/util/GlibMemory.h>
 
20
 
 
21
#include <gtest/gtest.h>
 
22
#include <iostream>
 
23
#include <set>
 
24
#include <string>
 
25
 
 
26
using namespace std;
 
27
using namespace unity;
 
28
using namespace unity::util;
 
29
 
 
30
namespace
 
31
{
 
32
 
 
33
static set<string> keys;
 
34
 
 
35
static set<string> values;
 
36
 
 
37
class GlibMemoryTest: public testing::Test
 
38
{
 
39
protected:
 
40
    void SetUp() override
 
41
    {
 
42
        keys.clear();
 
43
        values.clear();
 
44
    }
 
45
 
 
46
    static void deleteKey(gpointer data)
 
47
    {
 
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));
 
50
    }
 
51
 
 
52
    static void deleteValue(gpointer data)
 
53
    {
 
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));
 
56
    }
 
57
};
 
58
 
 
59
TEST_F(GlibMemoryTest, Unique)
 
60
{
 
61
    {
 
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));
 
65
    }
 
66
 
 
67
    {
 
68
        auto variant = unique_glib(g_variant_new_string("hello"));
 
69
        EXPECT_STREQ("hello", g_variant_get_string(variant.get(), NULL));
 
70
    }
 
71
 
 
72
    {
 
73
        {
 
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");
 
77
        }
 
78
        EXPECT_EQ(set<string>({"hello", "hash"}), keys);
 
79
        EXPECT_EQ(set<string>({"there", "world"}), values);
 
80
    }
 
81
}
 
82
 
 
83
TEST_F(GlibMemoryTest, Share)
 
84
{
 
85
    {
 
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));
 
89
    }
 
90
 
 
91
    {
 
92
        auto variant = share_glib(g_variant_new_string("hello"));
 
93
        EXPECT_STREQ("hello", g_variant_get_string(variant.get(), NULL));
 
94
    }
 
95
 
 
96
    {
 
97
        {
 
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");
 
101
        }
 
102
        EXPECT_EQ(set<string>({"hello", "hash"}), keys);
 
103
        EXPECT_EQ(set<string>({"there", "world"}), values);
 
104
    }
 
105
}
 
106
 
 
107
}