QtBase  v6.3.1
testhelper_functions.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 #ifndef FUNCTIONS_H
29 #define FUNCTIONS_H
30 
31 #include <QList>
32 
33 #include <vector>
34 
35 bool keepEvenIntegers(const int &x)
36 {
37  return (x & 1) == 0;
38 }
39 
41 {
42 public:
43  bool operator()(const int &x)
44  {
45  return (x & 1) == 0;
46  }
47 };
48 
50 {
51 public:
55 
58 
59  bool operator()(int x) { return (x & 1) == 0; }
60 };
61 
62 class Number
63 {
64  int n;
65 
66 public:
68  : n(0)
69  { }
70 
71  Number(int n)
72  : n(n)
73  { }
74 
75  void multiplyBy2()
76  {
77  n *= 2;
78  }
79 
81  {
82  return n * 2;
83  }
84 
85  bool isEven() const
86  {
87  return (n & 1) == 0;
88  }
89 
90  int toInt() const
91  {
92  return n;
93  }
94 
95  QString toString() const
96  {
97  return QString::number(n);
98  }
99 
100  Number squared() const
101  {
102  return Number(n * n);
103  }
104 
105  bool operator==(const Number &other) const
106  {
107  return n == other.n;
108  }
109 };
110 
112 {
113  return (x.toInt() & 1) == 0;
114 }
115 
117 {
118 public:
119  bool operator()(const Number &x)
120  {
121  return (x.toInt() & 1) == 0;
122  }
123 };
124 
125 void intSumReduce(int &sum, int x)
126 {
127  sum += x;
128 }
129 
131 {
132 public:
133  void operator()(int &sum, int x)
134  {
135  sum += x;
136  }
137 };
138 
140 {
141 public:
142  IntSumReduceMoveOnly() = default;
145 
148 
149  void operator()(int &sum, int x) { sum += x; }
150 };
151 
152 void numberSumReduce(int &sum, const Number &x)
153 {
154  sum += x.toInt();
155 }
156 
158 {
159 public:
160  void operator()(int &sum, const Number &x)
161  {
162  sum += x.toInt();
163  }
164 };
165 
166 template<typename T>
168 {
169 public:
170  using value_type = T;
171 
172  // rule of six
173  MoveOnlyVector() = default;
174  ~MoveOnlyVector() = default;
177 
180 
181  // convenience for creation
182  explicit MoveOnlyVector(const std::vector<T> &v) : data(v) { }
183  void push_back(T &&el) { data.push_back(el); }
184  void push_back(const T &el) { data.push_back(el); }
185 
186  // minimal interface to be usable as a Sequence in QtConcurrent
189  const_iterator cbegin() const { return data.cbegin(); }
190  const_iterator cend() const { return data.cend(); }
191  iterator begin() { return data.begin(); }
192  iterator end() { return data.end(); }
193  const_iterator begin() const { return data.cbegin(); }
194  const_iterator end() const { return data.cend(); }
195  bool operator==(const MoveOnlyVector<T> &other) const { return data == other.data; }
196 
197 private:
198  std::vector<T> data;
199 };
200 
201 struct NonTemplateSequence : public QList<int>
202 {
203  NonTemplateSequence() = default;
204 
205  NonTemplateSequence(std::initializer_list<int> args) : QList(args) { }
206 };
207 
208 #endif
void operator()(int &sum, int x)
IntSumReduceMoveOnly(IntSumReduceMoveOnly &&)=default
IntSumReduceMoveOnly()=default
IntSumReduceMoveOnly & operator=(IntSumReduceMoveOnly &&other)=default
IntSumReduceMoveOnly & operator=(const IntSumReduceMoveOnly &)=delete
IntSumReduceMoveOnly(const IntSumReduceMoveOnly &)=delete
void operator()(int &sum, int x)
bool operator()(const int &x)
KeepEvenIntegersMoveOnly & operator=(KeepEvenIntegersMoveOnly &&other)=default
KeepEvenIntegersMoveOnly(KeepEvenIntegersMoveOnly &&)=default
KeepEvenIntegersMoveOnly & operator=(const KeepEvenIntegersMoveOnly &)=delete
KeepEvenIntegersMoveOnly()=default
KeepEvenIntegersMoveOnly(const KeepEvenIntegersMoveOnly &)=delete
bool operator()(const Number &x)
const_iterator end() const
void push_back(T &&el)
MoveOnlyVector(const MoveOnlyVector< T > &)=delete
void push_back(const T &el)
MoveOnlyVector(MoveOnlyVector< T > &&other)=default
bool operator==(const MoveOnlyVector< T > &other) const
MoveOnlyVector()=default
~MoveOnlyVector()=default
const_iterator begin() const
std::vector< T >::const_iterator const_iterator
MoveOnlyVector(const std::vector< T > &v)
std::vector< T >::iterator iterator
const_iterator cend() const
MoveOnlyVector & operator=(const MoveOnlyVector< T > &)=delete
const_iterator cbegin() const
MoveOnlyVector & operator=(MoveOnlyVector< T > &&other)=default
Number multipliedBy2() const
void multiplyBy2()
QString toString() const
int toInt() const
bool operator==(const Number &other) const
bool isEven() const
Number squared() const
void operator()(int &sum, const Number &x)
Definition: qlist.h:108
The QString class provides a Unicode character string.
Definition: qstring.h:388
static QString number(int, int base=10)
Definition: qstring.cpp:7538
#define T(x)
Definition: main.cpp:42
typename C::const_iterator const_iterator
typename C::iterator iterator
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat n
QStringView el
QFuture< int > sum
QSharedPointer< T > other(t)
[5]
NonTemplateSequence()=default
NonTemplateSequence(std::initializer_list< int > args)
Definition: main.cpp:38
bool keepEvenIntegers(const int &x)
bool keepEvenNumbers(const Number &x)
void numberSumReduce(int &sum, const Number &x)
void intSumReduce(int &sum, int x)