TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / testing / gtest_utils.h
1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #include "anbox/common/type_traits.h"
18
19 #include <gtest/gtest.h>
20 #include <iterator>
21
22 // Miscellenaous helper declarations for unit-tests using the GoogleTest
23 // framework.
24
25 namespace anbox {
26 namespace testing {
27 // RangesMatch is a useful template used to compare the content of two
28 // ranges at runtime. Usage is simply:
29 //
30 //   EXPECT_TRUE(RangesMatch(range1, range2);
31 //
32 // Where |range1| and |range2| must have the same item type, and size.
33 template <typename Range1,
34           typename Range2,
35           typename = common::enable_if_c<common::is_range<Range1>::value &&
36                                          common::is_range<Range2>::value>>
37 inline ::testing::AssertionResult RangesMatch(const Range1& expected,
38                                               const Range2& actual) {
39   const auto expectedSize =
40       std::distance(std::begin(expected), std::end(expected));
41   const auto actualSize = std::distance(std::begin(actual), std::end(actual));
42   if (actualSize != expectedSize) {
43     return ::testing::AssertionFailure()
44            << "actual range size " << actualSize << " != expected size "
45            << expectedSize;
46   }
47
48   auto itExp = std::begin(expected);
49   for (const auto& act : actual) {
50     if (*itExp != act) {
51       const auto index = std::distance(std::begin(expected), itExp);
52       return ::testing::AssertionFailure()
53              << "range[" << index << "] (" << act << ") != expected["
54              << index << "] (" << *itExp << ")";
55     }
56     ++itExp;
57   }
58
59   return ::testing::AssertionSuccess();
60 }
61 }  // namespace testing
62 }  // namespace anbox