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