xref: /aosp_15_r20/external/libbrillo/brillo/glib/object_test.cc (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #include "brillo/glib/object.h"
6*1a96fba6SXin Li 
7*1a96fba6SXin Li #include <gtest/gtest.h>
8*1a96fba6SXin Li 
9*1a96fba6SXin Li #include <algorithm>
10*1a96fba6SXin Li #include <cstring>
11*1a96fba6SXin Li #include <iterator>
12*1a96fba6SXin Li #include <string>
13*1a96fba6SXin Li 
14*1a96fba6SXin Li using brillo::glib::ScopedPtrArray;
15*1a96fba6SXin Li using brillo::glib::ScopedError;
16*1a96fba6SXin Li using brillo::glib::Retrieve;
17*1a96fba6SXin Li using brillo::glib::Value;
18*1a96fba6SXin Li using brillo::Resetter;
19*1a96fba6SXin Li 
20*1a96fba6SXin Li namespace {  // NOLINT
21*1a96fba6SXin Li 
22*1a96fba6SXin Li template <typename T>
SetRetrieveTest(const T & x)23*1a96fba6SXin Li void SetRetrieveTest(const T& x) {
24*1a96fba6SXin Li   Value tmp(x);
25*1a96fba6SXin Li   T result;
26*1a96fba6SXin Li   EXPECT_TRUE(Retrieve(tmp, &result));
27*1a96fba6SXin Li   EXPECT_EQ(result, x);
28*1a96fba6SXin Li }
29*1a96fba6SXin Li 
ModifyValue(Value * x)30*1a96fba6SXin Li void ModifyValue(Value* x) {
31*1a96fba6SXin Li   *x = 1.0 / 1231415926.0;   // An unlikely value
32*1a96fba6SXin Li }
33*1a96fba6SXin Li 
34*1a96fba6SXin Li template <typename T, typename O>
MutableRegularTestValue(const T & x,O modify)35*1a96fba6SXin Li void MutableRegularTestValue(const T& x, O modify) {
36*1a96fba6SXin Li   Value tmp(x);
37*1a96fba6SXin Li   Value y = tmp;  // copy-construction
38*1a96fba6SXin Li   T result;
39*1a96fba6SXin Li   EXPECT_TRUE(Retrieve(y, &result));
40*1a96fba6SXin Li   EXPECT_EQ(result, x);
41*1a96fba6SXin Li   modify(&y);
42*1a96fba6SXin Li   LOG(INFO) << "Warning Expected.";
43*1a96fba6SXin Li   EXPECT_TRUE(!(Retrieve(y, &result) && result == x));
44*1a96fba6SXin Li   y = tmp;  // assignment
45*1a96fba6SXin Li   EXPECT_TRUE(Retrieve(y, &result));
46*1a96fba6SXin Li   EXPECT_EQ(result, x);
47*1a96fba6SXin Li   modify(&y);
48*1a96fba6SXin Li   LOG(INFO) << "Warning Expected.";
49*1a96fba6SXin Li   EXPECT_TRUE(!(Retrieve(y, &result) && result == x));
50*1a96fba6SXin Li }
51*1a96fba6SXin Li 
OutArgument(int ** x)52*1a96fba6SXin Li void OutArgument(int** x) {
53*1a96fba6SXin Li   *x = new int(10);  // NOLINT
54*1a96fba6SXin Li }
55*1a96fba6SXin Li 
56*1a96fba6SXin Li }  // namespace
57*1a96fba6SXin Li 
TEST(ResetterTest,All)58*1a96fba6SXin Li TEST(ResetterTest, All) {
59*1a96fba6SXin Li   std::unique_ptr<int> x;
60*1a96fba6SXin Li   OutArgument(&Resetter(&x).lvalue());
61*1a96fba6SXin Li   EXPECT_EQ(*x, 10);
62*1a96fba6SXin Li }
63*1a96fba6SXin Li 
TEST(RetrieveTest,Types)64*1a96fba6SXin Li TEST(RetrieveTest, Types) {
65*1a96fba6SXin Li   SetRetrieveTest(std::string("Hello!"));
66*1a96fba6SXin Li   SetRetrieveTest(static_cast<uint32_t>(10));
67*1a96fba6SXin Li   SetRetrieveTest(10.5);
68*1a96fba6SXin Li   SetRetrieveTest(true);
69*1a96fba6SXin Li }
70*1a96fba6SXin Li 
TEST(ValueTest,All)71*1a96fba6SXin Li TEST(ValueTest, All) {
72*1a96fba6SXin Li   Value x;  // default construction
73*1a96fba6SXin Li   Value y = x;  // copy with default value
74*1a96fba6SXin Li   x = y;  // assignment with default value
75*1a96fba6SXin Li   Value z(1.5);
76*1a96fba6SXin Li   x = z;  // assignment to default value
77*1a96fba6SXin Li   MutableRegularTestValue(std::string("Hello!"), &ModifyValue);
78*1a96fba6SXin Li }
79*1a96fba6SXin Li 
TEST(ScopedErrorTest,All)80*1a96fba6SXin Li TEST(ScopedErrorTest, All) {
81*1a96fba6SXin Li   ScopedError a;  // default construction
82*1a96fba6SXin Li   ScopedError b(::g_error_new(::g_quark_from_static_string("error"), -1,
83*1a96fba6SXin Li                               ""));  // constructor
84*1a96fba6SXin Li   ::GError* c = ::g_error_new(::g_quark_from_static_string("error"), -1,
85*1a96fba6SXin Li                               "");
86*1a96fba6SXin Li   ::GError* d = ::g_error_new(::g_quark_from_static_string("error"), -1,
87*1a96fba6SXin Li                               "");
88*1a96fba6SXin Li   a.reset(c);  // reset form 1
89*1a96fba6SXin Li   (void)d;
90*1a96fba6SXin Li }
91*1a96fba6SXin Li 
TEST(ScopedPtrArrayTest,Construction)92*1a96fba6SXin Li TEST(ScopedPtrArrayTest, Construction) {
93*1a96fba6SXin Li   const char item[] = "a string";
94*1a96fba6SXin Li   char* a = static_cast<char*>(::g_malloc(sizeof(item)));
95*1a96fba6SXin Li   std::strcpy(a, &item[0]);  // NOLINT
96*1a96fba6SXin Li 
97*1a96fba6SXin Li   ::GPtrArray* array = ::g_ptr_array_new();
98*1a96fba6SXin Li   ::g_ptr_array_add(array, ::gpointer(a));
99*1a96fba6SXin Li 
100*1a96fba6SXin Li   ScopedPtrArray<const char*> x(array);
101*1a96fba6SXin Li   EXPECT_EQ(x.size(), 1);
102*1a96fba6SXin Li   EXPECT_EQ(x[0], a);  // indexing
103*1a96fba6SXin Li }
104*1a96fba6SXin Li 
TEST(ScopedPtrArrayTest,Reset)105*1a96fba6SXin Li TEST(ScopedPtrArrayTest, Reset) {
106*1a96fba6SXin Li   const char item[] = "a string";
107*1a96fba6SXin Li   char* a = static_cast<char*>(::g_malloc(sizeof(item)));
108*1a96fba6SXin Li   std::strcpy(a, &item[0]);  // NOLINT
109*1a96fba6SXin Li 
110*1a96fba6SXin Li   ScopedPtrArray<const char*> x;  // default construction
111*1a96fba6SXin Li   x.push_back(a);
112*1a96fba6SXin Li   EXPECT_EQ(x.size(), 1);
113*1a96fba6SXin Li   x.reset();
114*1a96fba6SXin Li   EXPECT_EQ(x.size(), 0);
115*1a96fba6SXin Li 
116*1a96fba6SXin Li   char* b = static_cast<char*>(::g_malloc(sizeof(item)));
117*1a96fba6SXin Li   std::strcpy(b, &item[0]);  // NOLINT
118*1a96fba6SXin Li 
119*1a96fba6SXin Li   ::GPtrArray* array = ::g_ptr_array_new();
120*1a96fba6SXin Li   ::g_ptr_array_add(array, ::gpointer(b));
121*1a96fba6SXin Li 
122*1a96fba6SXin Li   x.reset(array);
123*1a96fba6SXin Li   EXPECT_EQ(x.size(), 1);
124*1a96fba6SXin Li }
125*1a96fba6SXin Li 
TEST(ScopedPtrArrayTest,Iteration)126*1a96fba6SXin Li TEST(ScopedPtrArrayTest, Iteration) {
127*1a96fba6SXin Li   char* a[] = { static_cast<char*>(::g_malloc(1)),
128*1a96fba6SXin Li       static_cast<char*>(::g_malloc(1)), static_cast<char*>(::g_malloc(1)) };
129*1a96fba6SXin Li 
130*1a96fba6SXin Li   ScopedPtrArray<const char*> x;
131*1a96fba6SXin Li   std::copy(&a[0], &a[3], std::back_inserter(x));
132*1a96fba6SXin Li   EXPECT_TRUE(std::equal(x.begin(), x.end(), &a[0]));
133*1a96fba6SXin Li }
134*1a96fba6SXin Li 
135