blob: 054313b7e7d52d6e99768c07f693ebfbd1cac0a7 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: vladl@google.com (Vlad Losev)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the internal cross-platform support utilities.
35
36#include <gmock/internal/gmock-port.h>
37#include <gtest/gtest.h>
38
vladlosev201ac162009-11-18 00:12:05 +000039// NOTE: if this file is left without tests for some reason, put a dummy
40// test here to make references to symbols in the gtest library and avoid
41// 'undefined symbol' linker errors in gmock_main:
42//
43// TEST(DummyTest, Dummy) {}
zhanyong.wan2e7c4752009-11-06 23:43:30 +000044
vladlosev201ac162009-11-18 00:12:05 +000045namespace testing {
46namespace internal {
47// Needed to avoid name collisions in gmock_all_test.cc.
48namespace gmock_port_test {
49
50class Base {
51 public:
52 // Copy constructor and assignment operator do exactly what we need, so we
53 // use them.
54 Base() : member_(0) {}
55 explicit Base(int n) : member_(n) {}
56 virtual ~Base() {}
57 int member() { return member_; }
58
59 private:
60 int member_;
61};
62
63class Derived : public Base {
64 public:
65 explicit Derived(int n) : Base(n) {}
66};
67
68TEST(ImplicitCastTest, ConvertsPointers) {
69 Derived derived(0);
70 EXPECT_TRUE(&derived == ::testing::internal::implicit_cast<Base*>(&derived));
71}
72
73TEST(ImplicitCastTest, CanUseInheritance) {
74 Derived derived(1);
75 Base base = ::testing::internal::implicit_cast<Base>(derived);
76 EXPECT_EQ(derived.member(), base.member());
77}
78
vladlosev201ac162009-11-18 00:12:05 +000079class Castable {
80 public:
81 Castable(bool* converted) : converted_(converted) {}
82 operator Base() {
83 *converted_ = true;
84 return Base();
85 }
86
87 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +000088 bool* converted_;
vladlosev201ac162009-11-18 00:12:05 +000089};
90
91TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
92 bool converted = false;
93 Castable castable(&converted);
94 Base base = ::testing::internal::implicit_cast<Base>(castable);
95 EXPECT_TRUE(converted);
96}
vladlosev201ac162009-11-18 00:12:05 +000097
98class ConstCastable {
99 public:
100 ConstCastable(bool* converted) : converted_(converted) {}
101 operator Base() const {
102 *converted_ = true;
103 return Base();
104 }
105
106 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000107 bool* converted_;
vladlosev201ac162009-11-18 00:12:05 +0000108};
109
110TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
111 bool converted = false;
112 const ConstCastable const_castable(&converted);
113 Base base = ::testing::internal::implicit_cast<Base>(const_castable);
114 EXPECT_TRUE(converted);
115}
116
vladlosev201ac162009-11-18 00:12:05 +0000117class ConstAndNonConstCastable {
118 public:
119 ConstAndNonConstCastable(bool* converted, bool* const_converted)
120 : converted_(converted), const_converted_(const_converted) {}
121 operator Base() {
122 *converted_ = true;
123 return Base();
124 }
125 operator Base() const {
126 *const_converted_ = true;
127 return Base();
128 }
129
130 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000131 bool* converted_;
132 bool* const_converted_;
vladlosev201ac162009-11-18 00:12:05 +0000133};
134
135TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) {
136 bool converted = false;
137 bool const_converted = false;
138 ConstAndNonConstCastable castable(&converted, &const_converted);
139 Base base = ::testing::internal::implicit_cast<Base>(castable);
140 EXPECT_TRUE(converted);
141 EXPECT_FALSE(const_converted);
142
143 converted = false;
144 const_converted = false;
145 const ConstAndNonConstCastable const_castable(&converted, &const_converted);
146 base = ::testing::internal::implicit_cast<Base>(const_castable);
147 EXPECT_FALSE(converted);
148 EXPECT_TRUE(const_converted);
149}
vladlosev201ac162009-11-18 00:12:05 +0000150
151class To {
152 public:
153 To(bool* converted) { *converted = true; } // NOLINT
154};
155
156TEST(ImplicitCastTest, CanUseImplicitConstructor) {
157 bool converted = false;
158 To to = ::testing::internal::implicit_cast<To>(&converted);
159 EXPECT_TRUE(converted);
160}
161
162} // namespace gmock_port_test
163} // namespace internal
164} // namespace testing