Skip to content

Commit 82b5d36

Browse files
authored
Merge pull request #768 from hongriTianqi/develop
UT for container (vector & map) operators (+,-,*)
2 parents a4c6f5a + 3684c9d commit 82b5d36

File tree

3 files changed

+139
-8
lines changed

3 files changed

+139
-8
lines changed

source/module_base/container_operator.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <vector>
66
#include <map>
77

8-
9-
108
template< typename T>
119
std::vector<T> operator + ( const std::vector<T> & x1, const std::vector<T> & x2 )
1210
{
@@ -30,23 +28,23 @@ std::vector<T> operator - ( const std::vector<T> & x1, const std::vector<T> & x2
3028
template< typename T1, typename T2 >
3129
std::map<T1,T2> operator + ( const std::map<T1,T2> & x1, const std::map<T1,T2> & x2 )
3230
{
31+
assert(x1.size()==x2.size());
3332
std::map<T1,T2> x;
3433
for( const auto &x1i : x1 )
35-
x.insert(make_pair( x1i.first, x1i.second + x2.at(x1i.first) ));
34+
x.insert(std::make_pair( x1i.first, x1i.second + x2.at(x1i.first) ));
3635
return x;
3736
}
3837

3938
template< typename T1, typename T2 >
4039
std::map<T1,T2> operator - ( const std::map<T1,T2> & x1, const std::map<T1,T2> & x2 )
4140
{
41+
assert(x1.size()==x2.size());
4242
std::map<T1,T2> x;
4343
for( const auto &x1i : x1 )
44-
x.insert(make_pair( x1i.first, x1i.second - x2.at(x1i.first) ));
44+
x.insert(std::make_pair( x1i.first, x1i.second - x2.at(x1i.first) ));
4545
return x;
4646
}
4747

48-
49-
5048
template< typename T1, typename T2 >
5149
std::vector<T2> operator * ( const T1 & x1, const std::vector<T2> & x2 )
5250
{
@@ -61,8 +59,8 @@ std::map<T21,T22> operator * ( const T1 & x1, const std::map<T21,T22> & x2 )
6159
{
6260
std::map<T21,T22> x;
6361
for( const auto & x2i : x2 )
64-
x.insert(make_pair( x2i.first, x1*x2i.second ));
62+
x.insert(std::make_pair( x2i.first, x1*x2i.second ));
6563
return x;
6664
}
6765

68-
#endif // CONTAINER_OPERATOR_H
66+
#endif // CONTAINER_OPERATOR_H

source/module_base/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,7 @@ AddTest(
9595
TARGET base_mymath
9696
SOURCES mymath_test.cpp ../mymath3.cpp ../timer.cpp
9797
)
98+
AddTest(
99+
TARGET base_container
100+
SOURCES container_operator_test.cpp ../container_operator.h
101+
)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include "../container_operator.h"
2+
#include "gtest/gtest.h"
3+
#include <iostream>
4+
5+
/************************************************
6+
* unit test of container operator
7+
***********************************************/
8+
9+
/**
10+
* - Tested Functions:
11+
* - VectorPlus
12+
* - "+" operator for vectors
13+
* - VectorMinus
14+
* - "-" operator for vectors
15+
* - VectorMultiply
16+
* - "*" operator for scalar*vector
17+
* - MapPlus
18+
* - "+" operator for maps
19+
* - MapMinus
20+
* - "-" operator for maps
21+
* - MapMultiply
22+
* - "*" operator for scalar*map
23+
*/
24+
25+
TEST(ContainerOperator,VectorPlus)
26+
{
27+
std::vector<double> a(20,10.0);
28+
std::vector<double> b(20,1.0);
29+
std::vector<double> c(20);
30+
c = a+b;
31+
for (int i=0;i<c.size();i++)
32+
EXPECT_EQ(c[i],11.0);
33+
}
34+
35+
TEST(ContainerOperator,VectorMinus)
36+
{
37+
std::vector<double> a(20,10.0);
38+
std::vector<double> b(20,1.0);
39+
std::vector<double> c(20);
40+
c = a-b;
41+
for (int i=0;i<c.size();i++)
42+
EXPECT_EQ(c[i],9.0);
43+
}
44+
45+
TEST(ContainerOperator,VectorMultiply)
46+
{
47+
std::vector<double> a(20,10.0);
48+
double b = 2.0;
49+
std::vector<double> c(20);
50+
c = b*a;
51+
for (int i=0;i<c.size();i++)
52+
EXPECT_EQ(c[i],20.0);
53+
}
54+
55+
TEST(ContainerOperator,VectorLengthCheck)
56+
{
57+
std::vector<double> a(20,10.0);
58+
std::vector<double> b(19,1.0);
59+
std::vector<double> c(20);
60+
EXPECT_DEATH(c=a+b,"");
61+
EXPECT_DEATH(c=a-b,"");
62+
}
63+
64+
TEST(ContainerOperator,MapLengthCheck)
65+
{
66+
std::map<int,double> a;
67+
std::map<int,double> b;
68+
std::map<int,double> c;
69+
for (int i=0;i<10;i++)
70+
{
71+
a.insert(std::pair<int,double> (i, i*2.0));
72+
if (i<9) b.insert(std::pair<int,double> (i, i*3.0));
73+
}
74+
EXPECT_DEATH(c=a+b,"");
75+
EXPECT_DEATH(c=a-b,"");
76+
}
77+
78+
TEST(ContainerOperator,MapPlus)
79+
{
80+
std::map<int,double> a;
81+
std::map<int,double> b;
82+
std::map<int,double> c;
83+
for (int i=0;i<10;i++)
84+
{
85+
a.insert(std::pair<int,double> (i, i*2.0));
86+
b.insert(std::pair<int,double> (i, i*3.0));
87+
}
88+
c = a+b;
89+
for (int i=0;i<10;i++)
90+
{
91+
//std::cout << c[i] << std::endl;
92+
EXPECT_EQ(c[i],i*5.0);
93+
}
94+
}
95+
96+
TEST(ContainerOperator,MapMinus)
97+
{
98+
std::map<int,double> a;
99+
std::map<int,double> b;
100+
std::map<int,double> c;
101+
for (int i=0;i<10;i++)
102+
{
103+
a.insert(std::pair<int,double> (i, i*4.0));
104+
b.insert(std::pair<int,double> (i, i*2.0));
105+
}
106+
c = a-b;
107+
for (int i=0;i<10;i++)
108+
{
109+
//std::cout << c[i] << std::endl;
110+
EXPECT_EQ(c[i],i*2.0);
111+
}
112+
}
113+
114+
TEST(ContainerOperator,MapMultiply)
115+
{
116+
std::map<int,double> a;
117+
double b = 3.0;
118+
std::map<int,double> c;
119+
for (int i=0;i<10;i++)
120+
{
121+
a.insert(std::pair<int,double> (i, i*4.0));
122+
}
123+
c = b*a;
124+
for (int i=0;i<10;i++)
125+
{
126+
//std::cout << c[i] << std::endl;
127+
EXPECT_EQ(c[i],i*4.0*3.0);
128+
}
129+
}

0 commit comments

Comments
 (0)