Igor Murashkin | fb326cf | 2015-07-23 16:53:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "out.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | struct OutTest : public testing::Test { |
| 25 | // Multiplies values less than 10 by two, stores the result and returns 0. |
| 26 | // Returns -1 if the original value was not multiplied by two. |
| 27 | static int multiply_small_values_by_two(size_t args, out<int> result) { |
| 28 | if (args < 10) { |
| 29 | *result = args * 2; |
| 30 | return 0; |
| 31 | } else { |
| 32 | return -1; |
| 33 | } |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | extern "C" int multiply_small_values_by_two_legacy(size_t args, int* result) { |
| 38 | if (args < 10) { |
| 39 | *result = args * 2; |
| 40 | return 0; |
| 41 | } else { |
| 42 | return -1; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | TEST_F(OutTest, TraditionalCall) { |
| 47 | // For calling traditional C++ functions. |
| 48 | int res; |
| 49 | EXPECT_EQ(multiply_small_values_by_two(1, outof(res)), 0); |
| 50 | EXPECT_EQ(2, res); |
| 51 | } |
| 52 | |
| 53 | TEST_F(OutTest, LegacyCall) { |
| 54 | // For calling legacy, e.g. C-style functions. |
| 55 | int res2; |
| 56 | EXPECT_EQ(0, multiply_small_values_by_two_legacy(1, outof(res2))); |
| 57 | EXPECT_EQ(2, res2); |
| 58 | } |
| 59 | |
| 60 | TEST_F(OutTest, CallFromIterator) { |
| 61 | // For calling a function with a parameter originating as an iterator. |
| 62 | std::vector<int> list = {1, 2, 3}; // NOLINT [whitespace/labels] [4] |
| 63 | std::vector<int>::iterator it = list.begin(); |
| 64 | |
| 65 | EXPECT_EQ(0, multiply_small_values_by_two(2, outof_iterator(it))); |
| 66 | EXPECT_EQ(4, list[0]); |
| 67 | } |
| 68 | |
| 69 | TEST_F(OutTest, CallFromPointer) { |
| 70 | // For calling a function with a parameter originating as a C-pointer. |
| 71 | std::vector<int> list = {1, 2, 3}; // NOLINT [whitespace/labels] [4] |
| 72 | |
| 73 | int* list_ptr = &list[2]; // 3 |
| 74 | |
| 75 | EXPECT_EQ(0, multiply_small_values_by_two(2, outof_ptr(list_ptr))); |
| 76 | EXPECT_EQ(4, list[2]); |
| 77 | } |
| 78 | |
| 79 | TEST_F(OutTest, OutAsIterator) { |
| 80 | // For using the out<T> parameter as an iterator inside of the callee. |
| 81 | std::vector<int> list; |
| 82 | int x = 100; |
| 83 | out<int> out_from_x = outof(x); |
| 84 | |
| 85 | for (const int& val : out_from_x) { |
| 86 | list.push_back(val); |
| 87 | } |
| 88 | |
| 89 | ASSERT_EQ(1u, list.size()); |
| 90 | EXPECT_EQ(100, list[0]); |
| 91 | |
| 92 | // A more typical use-case would be to use std algorithms |
| 93 | EXPECT_NE(out_from_x.end(), |
| 94 | std::find(out_from_x.begin(), |
| 95 | out_from_x.end(), |
| 96 | 100)); // Search for '100' in out. |
| 97 | } |
| 98 | |
| 99 | } // namespace art |