| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 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, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 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 | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 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 | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "config.h" | 26 #include "config.h" |
| 27 #include "wtf/Functional.h" |
| 27 | 28 |
| 28 #include "wtf/Functional.h" | |
| 29 #include "wtf/OwnPtr.h" | 29 #include "wtf/OwnPtr.h" |
| 30 #include "wtf/RefCounted.h" | 30 #include "wtf/RefCounted.h" |
| 31 #include <gtest/gtest.h> | 31 #include <gtest/gtest.h> |
| 32 | 32 |
| 33 namespace { | 33 namespace WTF { |
| 34 | 34 |
| 35 class UnwrappedClass { | 35 class UnwrappedClass { |
| 36 public: | 36 public: |
| 37 explicit UnwrappedClass(int value) | 37 explicit UnwrappedClass(int value) |
| 38 : m_value(value) | 38 : m_value(value) |
| 39 { | 39 { |
| 40 } | 40 } |
| 41 | 41 |
| 42 int value() const { return m_value; } | 42 int value() const { return m_value; } |
| 43 | 43 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 66 : m_value(value) | 66 : m_value(value) |
| 67 { | 67 { |
| 68 } | 68 } |
| 69 | 69 |
| 70 WrappedClass wrap() const { return WrappedClass(m_value); } | 70 WrappedClass wrap() const { return WrappedClass(m_value); } |
| 71 | 71 |
| 72 private: | 72 private: |
| 73 int m_value; | 73 int m_value; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 } // namespace | |
| 77 | |
| 78 namespace WTF { | |
| 79 | |
| 80 template<> struct ParamStorageTraits<ClassToBeWrapped> { | 76 template<> struct ParamStorageTraits<ClassToBeWrapped> { |
| 81 using StorageType = WrappedClass; | 77 using StorageType = WrappedClass; |
| 82 static StorageType wrap(const ClassToBeWrapped& value) { return value.wrap()
; } | 78 static StorageType wrap(const ClassToBeWrapped& value) { return value.wrap()
; } |
| 83 static UnwrappedClass unwrap(const StorageType& value) { return value.unwrap
(); } | 79 static UnwrappedClass unwrap(const StorageType& value) { return value.unwrap
(); } |
| 84 }; | 80 }; |
| 85 | 81 |
| 86 } // namespace WTF | |
| 87 | |
| 88 namespace { | 82 namespace { |
| 89 | 83 |
| 90 static int returnFortyTwo() | 84 int returnFortyTwo() |
| 91 { | 85 { |
| 92 return 42; | 86 return 42; |
| 93 } | 87 } |
| 94 | 88 |
| 95 TEST(FunctionalTest, Basic) | 89 TEST(FunctionalTest, Basic) |
| 96 { | 90 { |
| 97 OwnPtr<Function<int()>> returnFortyTwoFunction = bind(returnFortyTwo); | 91 OwnPtr<Function<int()>> returnFortyTwoFunction = bind(returnFortyTwo); |
| 98 EXPECT_EQ(42, (*returnFortyTwoFunction)()); | 92 EXPECT_EQ(42, (*returnFortyTwoFunction)()); |
| 99 } | 93 } |
| 100 | 94 |
| 101 static int multiplyByTwo(int n) | 95 int multiplyByTwo(int n) |
| 102 { | 96 { |
| 103 return n * 2; | 97 return n * 2; |
| 104 } | 98 } |
| 105 | 99 |
| 106 static double multiplyByOneAndAHalf(double d) | 100 double multiplyByOneAndAHalf(double d) |
| 107 { | 101 { |
| 108 return d * 1.5; | 102 return d * 1.5; |
| 109 } | 103 } |
| 110 | 104 |
| 111 TEST(FunctionalTest, UnaryBind) | 105 TEST(FunctionalTest, UnaryBind) |
| 112 { | 106 { |
| 113 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyByTwo, 4); | 107 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyByTwo, 4); |
| 114 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); | 108 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); |
| 115 | 109 |
| 116 OwnPtr<Function<double()>> multiplyByOneAndAHalfFunction = bind(multiplyByOn
eAndAHalf, 3); | 110 OwnPtr<Function<double()>> multiplyByOneAndAHalfFunction = bind(multiplyByOn
eAndAHalf, 3); |
| 117 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)()); | 111 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)()); |
| 118 } | 112 } |
| 119 | 113 |
| 120 TEST(FunctionalTest, UnaryPartBind) | 114 TEST(FunctionalTest, UnaryPartBind) |
| 121 { | 115 { |
| 122 OwnPtr<Function<int(int)>> multiplyByTwoFunction = bind<int>(multiplyByTwo); | 116 OwnPtr<Function<int(int)>> multiplyByTwoFunction = bind<int>(multiplyByTwo); |
| 123 EXPECT_EQ(8, (*multiplyByTwoFunction)(4)); | 117 EXPECT_EQ(8, (*multiplyByTwoFunction)(4)); |
| 124 | 118 |
| 125 OwnPtr<Function<double(double)>> multiplyByOneAndAHalfFunction = bind<double
>(multiplyByOneAndAHalf); | 119 OwnPtr<Function<double(double)>> multiplyByOneAndAHalfFunction = bind<double
>(multiplyByOneAndAHalf); |
| 126 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)(3)); | 120 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)(3)); |
| 127 } | 121 } |
| 128 | 122 |
| 129 static int multiply(int x, int y) | 123 int multiply(int x, int y) |
| 130 { | 124 { |
| 131 return x * y; | 125 return x * y; |
| 132 } | 126 } |
| 133 | 127 |
| 134 static int subtract(int x, int y) | 128 int subtract(int x, int y) |
| 135 { | 129 { |
| 136 return x - y; | 130 return x - y; |
| 137 } | 131 } |
| 138 | 132 |
| 139 TEST(FunctionalTest, BinaryBind) | 133 TEST(FunctionalTest, BinaryBind) |
| 140 { | 134 { |
| 141 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiply, 4, 2); | 135 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiply, 4, 2); |
| 142 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); | 136 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); |
| 143 | 137 |
| 144 OwnPtr<Function<int()>> subtractTwoFromFourFunction = bind(subtract, 4, 2); | 138 OwnPtr<Function<int()>> subtractTwoFromFourFunction = bind(subtract, 4, 2); |
| 145 EXPECT_EQ(2, (*subtractTwoFromFourFunction)()); | 139 EXPECT_EQ(2, (*subtractTwoFromFourFunction)()); |
| 146 } | 140 } |
| 147 | 141 |
| 148 TEST(FunctionalTest, BinaryPartBind) | 142 TEST(FunctionalTest, BinaryPartBind) |
| 149 { | 143 { |
| 150 OwnPtr<Function<int(int)>> multiplyFourFunction = bind<int>(multiply, 4); | 144 OwnPtr<Function<int(int)>> multiplyFourFunction = bind<int>(multiply, 4); |
| 151 EXPECT_EQ(8, (*multiplyFourFunction)(2)); | 145 EXPECT_EQ(8, (*multiplyFourFunction)(2)); |
| 152 OwnPtr<Function<int(int, int)>> multiplyFunction = bind<int, int>(multiply); | 146 OwnPtr<Function<int(int, int)>> multiplyFunction = bind<int, int>(multiply); |
| 153 EXPECT_EQ(8, (*multiplyFunction)(4, 2)); | 147 EXPECT_EQ(8, (*multiplyFunction)(4, 2)); |
| 154 | 148 |
| 155 OwnPtr<Function<int(int)>> subtractFromFourFunction = bind<int>(subtract, 4)
; | 149 OwnPtr<Function<int(int)>> subtractFromFourFunction = bind<int>(subtract, 4)
; |
| 156 EXPECT_EQ(2, (*subtractFromFourFunction)(2)); | 150 EXPECT_EQ(2, (*subtractFromFourFunction)(2)); |
| 157 OwnPtr<Function<int(int, int)>> subtractFunction = bind<int, int>(subtract); | 151 OwnPtr<Function<int(int, int)>> subtractFunction = bind<int, int>(subtract); |
| 158 EXPECT_EQ(2, (*subtractFunction)(4, 2)); | 152 EXPECT_EQ(2, (*subtractFunction)(4, 2)); |
| 159 } | 153 } |
| 160 | 154 |
| 161 static void sixArgFunc(int a, double b, char c, int* d, double* e, char* f) | 155 void sixArgFunc(int a, double b, char c, int* d, double* e, char* f) |
| 162 { | 156 { |
| 163 *d = a; | 157 *d = a; |
| 164 *e = b; | 158 *e = b; |
| 165 *f = c; | 159 *f = c; |
| 166 } | 160 } |
| 167 | 161 |
| 168 static void assertArgs(int actualInt, double actualDouble, char actualChar, int
expectedInt, double expectedDouble, char expectedChar) | 162 void assertArgs(int actualInt, double actualDouble, char actualChar, int expecte
dInt, double expectedDouble, char expectedChar) |
| 169 { | 163 { |
| 170 EXPECT_EQ(expectedInt, actualInt); | 164 EXPECT_EQ(expectedInt, actualInt); |
| 171 EXPECT_EQ(expectedDouble, actualDouble); | 165 EXPECT_EQ(expectedDouble, actualDouble); |
| 172 EXPECT_EQ(expectedChar, actualChar); | 166 EXPECT_EQ(expectedChar, actualChar); |
| 173 } | 167 } |
| 174 | 168 |
| 175 TEST(FunctionalTest, MultiPartBind) | 169 TEST(FunctionalTest, MultiPartBind) |
| 176 { | 170 { |
| 177 int a = 0; | 171 int a = 0; |
| 178 double b = 0.5; | 172 double b = 0.5; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 262 |
| 269 private: | 263 private: |
| 270 explicit Number(int value) | 264 explicit Number(int value) |
| 271 : m_value(value) | 265 : m_value(value) |
| 272 { | 266 { |
| 273 } | 267 } |
| 274 | 268 |
| 275 int m_value; | 269 int m_value; |
| 276 }; | 270 }; |
| 277 | 271 |
| 278 static int multiplyNumberByTwo(Number* number) | 272 int multiplyNumberByTwo(Number* number) |
| 279 { | 273 { |
| 280 return number->value() * 2; | 274 return number->value() * 2; |
| 281 } | 275 } |
| 282 | 276 |
| 283 TEST(FunctionalTest, RefCountedStorage) | 277 TEST(FunctionalTest, RefCountedStorage) |
| 284 { | 278 { |
| 285 RefPtr<Number> five = Number::create(5); | 279 RefPtr<Number> five = Number::create(5); |
| 286 OwnPtr<Function<int()>> multiplyFiveByTwoFunction = bind(multiplyNumberByTwo
, five); | 280 OwnPtr<Function<int()>> multiplyFiveByTwoFunction = bind(multiplyNumberByTwo
, five); |
| 287 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)()); | 281 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)()); |
| 288 | 282 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 311 OwnPtr<Function<int()>> function = bind(processUnwrappedClass, ClassToBeWrap
ped(3), 7); | 305 OwnPtr<Function<int()>> function = bind(processUnwrappedClass, ClassToBeWrap
ped(3), 7); |
| 312 EXPECT_EQ(21, (*function)()); | 306 EXPECT_EQ(21, (*function)()); |
| 313 } | 307 } |
| 314 | 308 |
| 315 TEST(FunctionalTest, WrapUnwrapInPartialBind) | 309 TEST(FunctionalTest, WrapUnwrapInPartialBind) |
| 316 { | 310 { |
| 317 OwnPtr<Function<int(int)>> partiallyBoundFunction = bind<int>(processUnwrapp
edClass, ClassToBeWrapped(3)); | 311 OwnPtr<Function<int(int)>> partiallyBoundFunction = bind<int>(processUnwrapp
edClass, ClassToBeWrapped(3)); |
| 318 EXPECT_EQ(21, (*partiallyBoundFunction)(7)); | 312 EXPECT_EQ(21, (*partiallyBoundFunction)(7)); |
| 319 } | 313 } |
| 320 | 314 |
| 321 } // namespace | 315 } // anonymous namespace |
| 316 |
| 317 } // namespace WTF |
| OLD | NEW |