aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/test/firebase/firestore/immutable/testing.h
blob: 9e839c63a3d56e7f02e7fabecb168a1bbf1c9b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright 2018 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_IMMUTABLE_TESTING_H_
#define FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_IMMUTABLE_TESTING_H_

#include <algorithm>
#include <utility>
#include <vector>

#include "Firestore/core/src/firebase/firestore/util/secure_random.h"
#include "gtest/gtest.h"

namespace firebase {
namespace firestore {
namespace immutable {

template <typename Container, typename K>
testing::AssertionResult NotFound(const Container& map, const K& key) {
  if (map.contains(key)) {
    return testing::AssertionFailure()
           << "Should not have found " << key << " using contains()";
  }

  auto found = map.find(key);
  if (found == map.end()) {
    return testing::AssertionSuccess();
  } else {
    return testing::AssertionFailure()
           << "Should not have found (" << found->first << ", " << found->second
           << ")";
  }
}

template <typename Container, typename K, typename V>
testing::AssertionResult Found(const Container& map,
                               const K& key,
                               const V& expected) {
  if (!map.contains(key)) {
    return testing::AssertionFailure()
           << "Did not find key " << key << " using contains()";
  }

  auto found = map.find(key);
  if (found == map.end()) {
    return testing::AssertionFailure()
           << "Did not find key " << key << " using find()";
  }
  if (found->second == expected) {
    return testing::AssertionSuccess();
  } else {
    return testing::AssertionFailure() << "Found entry was (" << found->first
                                       << ", " << found->second << ")";
  }
}

/** Creates an empty vector (for readability). */
inline std::vector<int> Empty() {
  return {};
}

/**
 * Creates a vector containing a sequence of integers from the given starting
 * element up to, but not including, the given end element, with values
 * incremented by the given step.
 *
 * If step is negative the sequence is in descending order (but still starting
 * at start and ending before end).
 */
inline std::vector<int> Sequence(int start, int end, int step = 1) {
  std::vector<int> result;
  if (step > 0) {
    for (int i = start; i < end; i += step) {
      result.push_back(i);
    }
  } else {
    for (int i = start; i > end; i += step) {
      result.push_back(i);
    }
  }
  return result;
}

/**
 * Creates a vector containing a sequence of integers with the given number of
 * elements, from zero up to, but not including the given value.
 */
inline std::vector<int> Sequence(int num_elements) {
  return Sequence(0, num_elements);
}

/**
 * Creates a copy of the given vector with contents shuffled randomly.
 */
inline std::vector<int> Shuffled(const std::vector<int>& values) {
  std::vector<int> result{values};
  util::SecureRandom rng;
  std::shuffle(result.begin(), result.end(), rng);
  return result;
}

/**
 * Creates a copy of the given vector with contents sorted.
 */
inline std::vector<int> Sorted(const std::vector<int>& values) {
  std::vector<int> result{values};
  std::sort(result.begin(), result.end());
  return result;
}

/**
 * Creates a copy of the given vector with contents reversed.
 */
inline std::vector<int> Reversed(const std::vector<int>& values) {
  std::vector<int> result{values};
  std::reverse(result.begin(), result.end());
  return result;
}

/**
 * Creates a vector of pairs where each pair has the same first and second
 * corresponding to an element in the given vector.
 */
inline std::vector<std::pair<int, int>> Pairs(const std::vector<int>& values) {
  std::vector<std::pair<int, int>> result;
  for (auto&& value : values) {
    result.emplace_back(value, value);
  }
  return result;
}

/**
 * Creates a SortedMap by inserting a pair for each value in the vector.
 * Each pair will have the same key and value.
 */
template <typename Container>
Container ToMap(const std::vector<int>& values) {
  Container result;
  for (auto&& value : values) {
    result = result.insert(value, value);
  }
  return result;
}

template <typename Container>
std::vector<int> Keys(const Container& container) {
  std::vector<int> keys;
  for (const auto& element : container) {
    keys.push_back(element.first);
  }
  return keys;
}

/**
 * Appends the contents of the given container to a new vector.
 */
template <typename Container>
std::vector<typename Container::value_type> Collect(
    const Container& container) {
  return {container.begin(), container.end()};
}

#define ASSERT_SEQ_EQ(x, y) ASSERT_EQ((x), Collect(y));
#define EXPECT_SEQ_EQ(x, y) EXPECT_EQ((x), Collect(y));

}  // namespace immutable
}  // namespace firestore
}  // namespace firebase

#endif  // FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_IMMUTABLE_TESTING_H_