ProteoWizard
cumsumtest.cpp
Go to the documentation of this file.
1 //
2 // $Id: cumsumtest.cpp 5313 2013-12-17 18:06:54Z chambm $
3 //
4 //
5 // Original author: Witold Wolski <wewolski@gmail.com>
6 //
7 // Copyright : ETH Zurich
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 
23 
26 
27 #include <vector>
28 #include <algorithm>
29 
31 
32 
33 namespace {
34 
35  using namespace pwiz::util;
36  void testCumSum()
37  {
38  unsigned int resC[]={ 1 , 3 , 6 ,10 ,15 ,21 ,28 ,36 ,45 ,55};
39 
40  std::vector<unsigned int> res;
41  ralab::base::base::seq(static_cast<unsigned int>(1),static_cast<unsigned int>(10),res);
42  std::vector<unsigned int> res2;
43  ralab::base::cumsum(res.begin(),res.end() , res2);
44  std::equal(res2.begin(),res2.end(),resC);
45  //test the in place version.
46  ralab::base::cumsum(res.begin(),res.end());
47  unit_assert(std::equal(res.begin(),res.end(),resC));
48  }
49 
50  void testCumProd()
51  {
52  unsigned int resC[]={1,2,6,24,120,720,5040,40320,362880,3628800};
53 
54  std::vector<unsigned int> res;
55  ralab::base::base::seq(static_cast<unsigned int>(1),static_cast<unsigned int>(10),res);
56  std::vector<unsigned int> res2;
57  ralab::base::cumprod(res,res2);
58  unit_assert(std::equal(res2.begin(),res2.end(),resC));
59  }
60 
61  void testCumMin()
62  {
63  unsigned int data[] ={ 3 ,2 ,1 ,2 ,1 ,0 ,4 ,3 ,2};
64  unsigned int resC[]={3 , 2 , 1 , 1 , 1 , 0 , 0 , 0 , 0};
65 
66  std::vector<unsigned int> res;
67  res.assign(data, data + 9);
68  std::vector<unsigned int> res2;
69  ralab::base::cummin(res,res2);
70  unit_assert(std::equal(res2.begin(),res2.end(),resC));
71  }
72 
73  void testCumMax()
74  {
75  unsigned int data[] ={ 3 ,2 ,1 ,2 ,1 ,0 ,4 ,3 ,2};
76  unsigned int resC[]={ 3, 3, 3, 3, 3, 3, 4, 4, 4};
77  std::vector<unsigned int> res;
78  res.assign(data, data + 9);
79  std::vector<unsigned int> res2;
80  ralab::base::cummax(res,res2);
81  unit_assert(std::equal(res2.begin(),res2.end(),resC));
82  }
83 }//end namespace UNITTEST
84 
85 int main(int argc, char **argv) {
86  testCumSum();
87 testCumProd();
88 testCumMin();
89 testCumMax();
90 }
91 
92 
93 
94 
95 
void cumprod(std::vector< T > &x, std::vector< T > &res)
Returns a vector whose elements are the cumulative products of the elements of the argument...
Definition: cumsum.hpp:80
void cummax(std::vector< T > &x, std::vector< T > &res)
Returns a vector whose elements are the cumulative maximum of the elements of the argument...
Definition: cumsum.hpp:97
void cumsum(TIterator beg, TIterator end, std::vector< T > &res)
Definition: cumsum.hpp:40
void seq(TReal from, TReal to, std::vector< TReal > &result)
generates the sequence from, from+/-1, ..., to (identical to from:to).
Definition: base.hpp:49
#define unit_assert(x)
Definition: unit.hpp:85
int main(int argc, char **argv)
Definition: cumsumtest.cpp:85
void cummin(std::vector< T > &x, std::vector< T > &res)
Returns a vector whose elements are the cumulative sums, products, minima or maxima of the elements o...
Definition: cumsum.hpp:115