OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_TESTING_GMOCK_SUPPORT_H_ | 5 #ifndef V8_TESTING_GMOCK_SUPPORT_H_ |
6 #define V8_TESTING_GMOCK_SUPPORT_H_ | 6 #define V8_TESTING_GMOCK_SUPPORT_H_ |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 #include <cstring> | 9 #include <cstring> |
| 10 #include <string> |
10 | 11 |
11 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
12 | 13 |
13 namespace testing { | 14 namespace testing { |
14 | 15 |
15 template <typename T> | 16 template <typename T> |
16 class Capture { | 17 class Capture { |
17 public: | 18 public: |
18 Capture() : value_(), has_value_(false) {} | 19 Capture() : value_(), has_value_(false) {} |
19 | 20 |
20 const T& value() const { return value_; } | 21 const T& value() const { return value_; } |
21 bool has_value() const { return has_value_; } | 22 bool has_value() const { return has_value_; } |
22 | 23 |
23 void SetValue(const T& value) { | 24 void SetValue(const T& value) { |
24 DCHECK(!has_value()); | 25 DCHECK(!has_value()); |
25 value_ = value; | 26 value_ = value; |
26 has_value_ = true; | 27 has_value_ = true; |
27 } | 28 } |
28 | 29 |
29 private: | 30 private: |
30 T value_; | 31 T value_; |
31 bool has_value_; | 32 bool has_value_; |
32 }; | 33 }; |
33 | 34 |
34 | 35 |
35 namespace internal { | 36 namespace internal { |
36 | 37 |
37 struct AnyBitEq { | |
38 template <typename A, typename B> | |
39 bool operator()(A const& a, B const& b) const { | |
40 if (sizeof(A) != sizeof(B)) return false; | |
41 return std::memcmp(&a, &b, sizeof(A)) == 0; | |
42 } | |
43 }; | |
44 | |
45 | |
46 template <typename Rhs> | |
47 class BitEqMatcher : public ComparisonBase<BitEqMatcher<Rhs>, Rhs, AnyBitEq> { | |
48 public: | |
49 explicit BitEqMatcher(Rhs const& rhs) | |
50 : ComparisonBase<BitEqMatcher<Rhs>, Rhs, AnyBitEq>(rhs) {} | |
51 static const char* Desc() { return "is bitwise equal to"; } | |
52 static const char* NegatedDesc() { return "isn't bitwise equal to"; } | |
53 }; | |
54 | |
55 | |
56 template <typename T> | 38 template <typename T> |
57 class CaptureEqMatcher : public MatcherInterface<T> { | 39 class CaptureEqMatcher : public MatcherInterface<T> { |
58 public: | 40 public: |
59 explicit CaptureEqMatcher(Capture<T>* capture) : capture_(capture) {} | 41 explicit CaptureEqMatcher(Capture<T>* capture) : capture_(capture) {} |
60 | 42 |
61 virtual void DescribeTo(std::ostream* os) const { | 43 virtual void DescribeTo(std::ostream* os) const { |
62 *os << "captured by " << static_cast<const void*>(capture_); | 44 *os << "captured by " << static_cast<const void*>(capture_); |
63 if (capture_->has_value()) *os << " which has value " << capture_->value(); | 45 if (capture_->has_value()) *os << " which has value " << capture_->value(); |
64 } | 46 } |
65 | 47 |
(...skipping 10 matching lines...) Expand all Loading... |
76 } | 58 } |
77 | 59 |
78 private: | 60 private: |
79 Capture<T>* capture_; | 61 Capture<T>* capture_; |
80 }; | 62 }; |
81 | 63 |
82 } // namespace internal | 64 } // namespace internal |
83 | 65 |
84 | 66 |
85 // Creates a polymorphic matcher that matches anything whose bit representation | 67 // Creates a polymorphic matcher that matches anything whose bit representation |
86 // is equal to that of x. | 68 // is equal to that of {x}. |
87 template <typename T> | 69 MATCHER_P(BitEq, x, std::string(negation ? "isn't" : "is") + |
88 inline internal::BitEqMatcher<T> BitEq(T const& x) { | 70 " bitwise equal to " + PrintToString(x)) { |
89 return internal::BitEqMatcher<T>(x); | 71 static_assert(sizeof(x) == sizeof(arg), "Size mismatch"); |
| 72 return std::memcmp(&arg, &x, sizeof(x)) == 0; |
90 } | 73 } |
91 | 74 |
92 | 75 |
93 // CaptureEq(capture) captures the value passed in during matching as long as it | 76 // CaptureEq(capture) captures the value passed in during matching as long as it |
94 // is unset, and once set, compares the value for equality with the argument. | 77 // is unset, and once set, compares the value for equality with the argument. |
95 template <typename T> | 78 template <typename T> |
96 inline Matcher<T> CaptureEq(Capture<T>* capture) { | 79 inline Matcher<T> CaptureEq(Capture<T>* capture) { |
97 return MakeMatcher(new internal::CaptureEqMatcher<T>(capture)); | 80 return MakeMatcher(new internal::CaptureEqMatcher<T>(capture)); |
98 } | 81 } |
99 | 82 |
100 | 83 |
101 // Creates a polymorphic matcher that matches any floating point NaN value. | 84 // Creates a polymorphic matcher that matches any floating point NaN value. |
102 MATCHER(IsNaN, std::string(negation ? "isn't" : "is") + " not a number") { | 85 MATCHER(IsNaN, std::string(negation ? "isn't" : "is") + " not a number") { |
103 return std::isnan(arg); | 86 return std::isnan(arg); |
104 } | 87 } |
105 | 88 |
106 } // namespace testing | 89 } // namespace testing |
107 | 90 |
108 #endif // V8_TESTING_GMOCK_SUPPORT_H_ | 91 #endif // V8_TESTING_GMOCK_SUPPORT_H_ |
OLD | NEW |