1/*
2 * Copyright (C) 2014 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include <gio/gio.h>
29
30inline std::ostringstream& log()
31{
32 static std::ostringstream log;
33 return log;
34}
35
36inline std::string takeLogStr()
37{
38 std::string string = log().str();
39 log().str("");
40 return string;
41}
42
43static void (* _g_free)(void*) = g_free;
44#define g_free(x) \
45 log() << "g_free(" << ptr << ");"; \
46 _g_free(x);
47
48static void (* _g_error_free)(GError*) = g_error_free;
49#define g_error_free(x) \
50 log() << "g_error_free(" << ptr << ");"; \
51 _g_error_free(x);
52
53static void (* _g_list_free)(GList*) = g_list_free;
54#define g_list_free(x) \
55 log() << "g_list_free(" << ptr << ");"; \
56 _g_list_free(x);
57
58static void (* _g_slist_free)(GSList*) = g_slist_free;
59#define g_slist_free(x) \
60 log() << "g_slist_free(" << ptr << ");"; \
61 _g_slist_free(x);
62
63static void (* _g_pattern_spec_free)(GPatternSpec*) = g_pattern_spec_free;
64#define g_pattern_spec_free(x) \
65 log() << "g_pattern_spec_free(" << ptr << ");"; \
66 _g_pattern_spec_free(x);
67
68static void (* _g_dir_close)(GDir*) = g_dir_close;
69#define g_dir_close(x) \
70 log() << "g_dir_close(" << ptr << ");"; \
71 _g_dir_close(x);
72
73static void (* _g_timer_destroy)(GTimer*) = g_timer_destroy;
74#define g_timer_destroy(x) \
75 log() << "g_timer_destroy(" << ptr << ");"; \
76 _g_timer_destroy(x);
77
78static void (* _g_key_file_free)(GKeyFile*) = g_key_file_free;
79#define g_key_file_free(x) \
80 log() << "g_key_file_free(" << ptr << ");"; \
81 _g_key_file_free(x);
82
83#include <wtf/glib/GUniquePtr.h>
84
85namespace TestWebKitAPI {
86
87TEST(WTF_GUniquePtr, Basic)
88{
89 std::ostringstream actual;
90
91 {
92 GUniquePtr<char> a(g_strdup("a"));
93 actual << "g_free(" << a.get() << ");";
94 }
95 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
96 actual.str("");
97
98 {
99 GUniquePtr<GError> a(g_error_new_literal(G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "a"));
100 actual << "g_error_free(" << a.get() << ");";
101 }
102 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
103 actual.str("");
104
105 {
106 GUniquePtr<GList> a(g_list_prepend(nullptr, g_strdup("a")));
107 actual << "g_list_free(" << a.get() << ");";
108 }
109 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
110 actual.str("");
111
112 {
113 GUniquePtr<GSList> a(g_slist_prepend(nullptr, g_strdup("a")));
114 actual << "g_slist_free(" << a.get() << ");";
115 }
116 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
117 actual.str("");
118
119 {
120 GUniquePtr<GPatternSpec> a(g_pattern_spec_new("a"));
121 actual << "g_pattern_spec_free(" << a.get() << ");";
122 }
123 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
124 actual.str("");
125
126 {
127 GUniquePtr<GDir> a(g_dir_open("/tmp", 0, nullptr));
128 actual << "g_dir_close(" << a.get() << ");";
129 }
130 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
131 actual.str("");
132
133 {
134 GUniquePtr<GTimer> a(g_timer_new());
135 actual << "g_timer_destroy(" << a.get() << ");";
136 }
137 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
138 actual.str("");
139
140 {
141 GUniquePtr<GKeyFile> a(g_key_file_new());
142 actual << "g_key_file_free(" << a.get() << ");";
143 }
144 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
145 actual.str("");
146}
147
148static void returnOutChar(char** outChar)
149{
150 *outChar = g_strdup("a");
151}
152
153TEST(WTF_GUniquePtr, OutPtr)
154{
155 std::ostringstream actual;
156
157 {
158 GUniqueOutPtr<char> a;
159 ASSERT_EQ(nullptr, a.get());
160 }
161 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
162 actual.str("");
163
164 {
165 GUniqueOutPtr<char> a;
166 returnOutChar(&a.outPtr());
167 actual << "g_free(" << a.get() << ");";
168 }
169 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
170 actual.str("");
171
172 {
173 GUniqueOutPtr<char> a;
174 returnOutChar(&a.outPtr());
175 actual << "g_free(" << a.get() << ");";
176 returnOutChar(&a.outPtr());
177 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
178 actual.str("");
179 actual << "g_free(" << a.get() << ");";
180 }
181 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
182 actual.str("");
183
184 {
185 GUniqueOutPtr<char> a;
186 returnOutChar(&a.outPtr());
187 GUniquePtr<char> b = a.release();
188 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
189 actual << "g_free(" << b.get() << ");";
190 }
191 ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
192 actual.str("");
193}
194
195} // namespace TestWebKitAPI
196