|
| 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