00001 #include <vector>
00002 #include <iostream>
00003
00004 #include <gtest/gtest.h>
00005
00006 using std::vector;
00007
00008
00009
00010 inline size_t offsetFromRowCol(const size_t row, const size_t col, const size_t ncol)
00011 {
00012 return (row * ncol) + col;
00013 }
00014
00015
00016
00017
00018
00019
00020
00021 inline int offsetFromRowColNoRangeCheck(const int row, const int col, const size_t ncol)
00022 {
00023 return (row * ncol) + col;
00024 }
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 template <typename T>
00041 void moveAndCopyImage(const int fill, const int dx, const int dy, const unsigned int ncol, vector<T>& map)
00042 {
00043 if (dx == 0 && dy == 0)
00044 {
00045 return;
00046 }
00047
00048 const unsigned int nrow = map.size() / ncol;
00049 int row_start = 0;
00050 int row_end = nrow;
00051 int row_increment = 1;
00052 if (dy < 0)
00053 {
00054 row_start = nrow - 1;
00055 row_end = -1;
00056 row_increment = -1;
00057 }
00058 int col_start = 0;
00059 int col_steps = ncol;
00060 int col_increment = 1;
00061 if (dx < 0)
00062 {
00063 col_start = ncol - 1;
00064 col_steps = -ncol;
00065 col_increment = -1;
00066 }
00067 for (int new_row = row_start; new_row != row_end; new_row += row_increment)
00068 {
00069 const size_t new_idx_start = offsetFromRowColNoRangeCheck(new_row, col_start, ncol);
00070 const int row = new_row + dy;
00071 int idx = offsetFromRowColNoRangeCheck(row, col_start + dx, ncol);
00072 const int min_idx = std::max(0, offsetFromRowColNoRangeCheck(row, 0, ncol));
00073 const int max_idx = std::min((int) map.size() - 1, offsetFromRowColNoRangeCheck(row, ncol - 1, ncol));
00074 const size_t new_idx_end = new_idx_start + col_steps;
00075 for (int new_idx = new_idx_start; new_idx != new_idx_end; )
00076 {
00077 if (min_idx <= idx && idx <= max_idx)
00078 {
00079 map[new_idx] = map[idx];
00080 }
00081 else
00082 {
00083 map[new_idx] = fill;
00084 }
00085 new_idx += col_increment;
00086 idx += col_increment;
00087 }
00088 }
00089 }
00090
00091 void printMap(std::vector<int8_t>& map, const size_t ncol)
00092 {
00093 const size_t nrow = map.size() / ncol;
00094 std::cout << "map:" << std::endl;
00095 for (int row = nrow - 1; row >= 0; --row)
00096 {
00097 for (size_t col = 0; col < ncol; ++col)
00098 {
00099 if (col % ncol)
00100 {
00101 std::cout << ", ";
00102 }
00103 std::cout << (int)map[offsetFromRowCol((size_t) row, col, ncol)];
00104 }
00105 std::cout << std::endl;
00106 }
00107 }
00108
00109 TEST(TestSuite, testMoveAndCopyMap)
00110 {
00111
00112
00113 static const int8_t arr1[] = {
00114 0, 0, 0, 0, 0,
00115 0, 3, 0, 0, 0,
00116 1, 2, 3, 0, 0,
00117 0, 1, 0, 0, 0};
00118
00119 std::vector<int8_t> old_map(arr1, arr1 + sizeof(arr1) / sizeof(arr1[0]));
00120 std::vector<int8_t> map(old_map);
00121
00122 static const int8_t arr2[] = {
00123 -1, -1, 0, 0, 0,
00124 -1, -1, 0, 3, 0,
00125 -1, -1, 1, 2, 3,
00126 -1, -1, 0, 1, 0};
00127 std::vector<int8_t> new_map_m2x(arr2, arr2 + sizeof(arr2) / sizeof(arr2[0]));
00128 std::vector<int8_t> new_map(new_map_m2x);
00129
00130 moveAndCopyImage(-1, -2, 0, 5, map);
00131
00132 for (size_t i = 0; i < map.size(); ++i)
00133 {
00134 EXPECT_EQ(map[i], new_map[i]) << "Maps differ at index " << i <<
00135 ", map = " << (int) map[i] << ", new_map = " << (int) new_map[i];
00136 }
00137
00138 static const int8_t arr3[] = {
00139 0, 0, -1, -1, -1,
00140 0, 0, -1, -1, -1,
00141 0, 0, -1, -1, -1,
00142 0, 0, -1, -1, -1};
00143 std::vector<int8_t> new_map_p3x(arr3, arr3 + sizeof(arr3) / sizeof(arr3[0]));
00144
00145 map = old_map;
00146 new_map = new_map_p3x;
00147 moveAndCopyImage(-1, 3, 0, 5, map);
00148
00149 for (size_t i = 0; i < map.size(); ++i)
00150 {
00151 EXPECT_EQ(map[i], new_map[i]) << "Maps differ at index " << i <<
00152 ", map = " << (int) map[i] << ", new_map = " << (int) new_map[i];
00153 }
00154
00155 static const int8_t arr4[] = {
00156 0, 3, 0, 0, 0,
00157 1, 2, 3, 0, 0,
00158 0, 1, 0, 0, 0,
00159 -1, -1, -1, -1, -1};
00160 std::vector<int8_t> new_map_p1y(arr4, arr4 + sizeof(arr4) / sizeof(arr4[0]));
00161
00162 map = old_map;
00163 new_map = new_map_p1y;
00164 moveAndCopyImage(-1, 0, 1, 5, map);
00165
00166 for (size_t i = 0; i < map.size(); ++i)
00167 {
00168 EXPECT_EQ(map[i], new_map[i]) << "Maps differ at index " << i <<
00169 ", map = " << (int) map[i] << ", new_map = " << (int) new_map[i];
00170 }
00171
00172 static const int8_t arr5[] = {
00173 -1, -1, -1, -1, -1,
00174 0, 0, 0, 0, 0,
00175 0, 3, 0, 0, 0,
00176 1, 2, 3, 0, 0};
00177 std::vector<int8_t> new_map_m1y(arr5, arr5 + sizeof(arr5) / sizeof(arr5[0]));
00178
00179 map = old_map;
00180 new_map = new_map_m1y;
00181 moveAndCopyImage(-1, 0, -1, 5, map);
00182
00183 for (size_t i = 0; i < map.size(); ++i)
00184 {
00185 EXPECT_EQ(map[i], new_map[i]) << "Maps differ at index " << i <<
00186 ", map = " << (int) map[i] << ", new_map = " << (int) new_map[i];
00187 }
00188
00189 static const int8_t arr6[] = {
00190 -1, -1, -1, -1, -1,
00191 -1, -1, -1, -1, -1,
00192 -1, -1, -1, -1, -1,
00193 -1, -1, -1, -1, -1};
00194 std::vector<int8_t> new_map_m5y(arr6, arr6 + sizeof(arr6) / sizeof(arr6[0]));
00195
00196 map = old_map;
00197 new_map = new_map_m5y;
00198 moveAndCopyImage(-1, 0, -5, 5, map);
00199
00200 for (size_t i = 0; i < map.size(); ++i)
00201 {
00202 EXPECT_EQ(map[i], new_map[i]) << "Maps differ at index " << i <<
00203 ", map = " << (int) map[i] << ", new_map = " << (int) new_map[i];
00204 }
00205
00206 static const int8_t arr7[] = {
00207 -1, -1, -1, -1, -1,
00208 -1, 0, 0, 0, 0,
00209 -1, 0, 3, 0, 0,
00210 -1, 1, 2, 3, 0};
00211 std::vector<int8_t> new_map_m1x_m1y(arr7, arr7 + sizeof(arr7) / sizeof(arr7[0]));
00212
00213 map = old_map;
00214 new_map = new_map_m1x_m1y;
00215 moveAndCopyImage(-1, -1, -1, 5, map);
00216
00217 for (size_t i = 0; i < map.size(); ++i)
00218 {
00219 EXPECT_EQ(map[i], new_map[i]) << "Maps differ at index " << i <<
00220 ", map = " << (int) map[i] << ", new_map = " << (int) new_map[i];
00221 }
00222
00223 }
00224
00225 int main(int argc, char** argv)
00226 {
00227 testing::InitGoogleTest(&argc, argv);
00228 return RUN_ALL_TESTS();
00229 }
00230
00231