1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| #include <iomanip>
#include <iostream>
namespace ns_test_palindrome_with_spaces {
typedef bool (*t_func) (const std::string&);
/**
* 1) Copy into new_str input without spaces
* 2) Test palindrome using new_str
*/
bool process_copy(const std::string& input) {
size_t size = input.size();
bool ret;
if (size > 1) {
std::string new_str;
size_t input_index, new_index;
char c;
new_str.reserve(size);
for(input_index=0; input_index < size; ++input_index) {
c = input[input_index];
if (c != ' ') { new_str.append(1, c); }
}
size = new_str.size();
if (size > 1) {
// std::cout << "input without spaces: " << new_str << " (" << size << ")" << std::endl;
ret = true;
for(input_index=0, new_index=(size - 1); (ret && (input_index < new_index)); ++input_index, --new_index) {
// std::cout << "test: " << new_str[input_index] << " (" << input_index << ") and " << new_str[new_index] << " (" << new_index << ")" << std::endl;
if (new_str[input_index] != new_str[new_index]) { ret = false; }
}
} else {
ret = false;
}
} else {
ret = false;
}
return ret;
}
bool process_in_place(const std::string& input) {
size_t input_size = input.size();
bool ret;
if (input_size > 1) {
size_t left_index, right_index, count;
char left_c, right_c;
ret = true;
for(left_index=0, right_index=(input_size - 1), count=0; (ret && (left_index < right_index));) {
left_c = input[left_index];
if (left_c != ' ') {
right_c = input[right_index];
if (right_c != ' ') {
// std::cout << "test: " << left_c << " (" << left_index << ") and " << right_c << " (" << right_index << ")" << std::endl;
if (left_c == right_c) {
++left_index;
--right_index;
++count;
} else {
ret = false;
}
} else {
--right_index;
}
} else {
++left_index;
}
}
// std::cout << "Nb comparaisons : " << count << std::endl;
ret = (ret && (count > 0));
} else {
ret = false;
}
return ret;
}
class c_unit_test {
public:
c_unit_test() { reset(); }
public:
void is_palindrome(t_func func, const std::string& input, const std::string& test_msg) {
++count;
if ( func(input) ) {
std::cout << std::setw(3) << count << " - " << std::left << std::setw(62) << test_msg << std::right << " : ok" << std::endl;
} else {
std::cout << std::setw(3) << count << " - " << std::left << std::setw(62) << test_msg << std::right << " : not ok" << std::endl;
}
}
void is_not_palindrome(t_func func, const std::string& input, const std::string& test_msg) {
++count;
if (!func(input)) {
std::cout << std::setw(3) << count << " - " << std::left << std::setw(62) << test_msg << std::right << " : ok" << std::endl;
} else {
std::cout << std::setw(3) << count << " - " << std::left << std::setw(62) << test_msg << std::right << " : not ok" << std::endl;
}
}
void reset() {
count = 0;
}
private:
size_t count;
};
}
int main (int argc, char** argv)
{
std::string palindrome01 = "caser vite ce palindrome ne mord ni lape cet ivre sac";
std::string palindrome02 = "et si l arome des bottes revele ma deviante et naive dame le verset t obsede moraliste";
std::string palindrome03 = "acubaannaabuca"; // size is even
std::string palindrome04 = "a v i v a"; // size is odd
std::string empty_str, one_char = "a", space_str = " ";
std::string word01 = "align data in columns when writing to an output stream";
std::string word02 = "aligndataincolumnswhenwritingtoanoutputstream";
ns_test_palindrome_with_spaces::c_unit_test test;
std::cout << "test copy :" << std::endl;
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_copy, empty_str, "test empty");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_copy, one_char, "one char");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_copy, space_str, "str full of spaces");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_copy, word01, "not palindrome");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_copy, word02, "not palindrome (without spaces)");
test.is_palindrome(ns_test_palindrome_with_spaces::process_copy, palindrome01, "test 1 palindrome");
test.is_palindrome(ns_test_palindrome_with_spaces::process_copy, palindrome02, "test 2 palindrome");
test.is_palindrome(ns_test_palindrome_with_spaces::process_copy, palindrome03, "test 3 palindrome (without spaces)");
test.is_palindrome(ns_test_palindrome_with_spaces::process_copy, palindrome04, "test 4 palindrome (a lof of spaces)");
test.reset();
std::cout << std::endl << std::endl << "test in place :" << std::endl;
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_in_place, empty_str, "test empty");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_in_place, one_char, "one char");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_in_place, space_str, "str full of spaces");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_in_place, word01, "not palindrome");
test.is_not_palindrome(ns_test_palindrome_with_spaces::process_in_place, word02, "not palindrome (without spaces)");
test.is_palindrome(ns_test_palindrome_with_spaces::process_in_place, palindrome01, "test 1 palindrome");
test.is_palindrome(ns_test_palindrome_with_spaces::process_in_place, palindrome02, "test 2 palindrome");
test.is_palindrome(ns_test_palindrome_with_spaces::process_in_place, palindrome03, "test 3 palindrome (without spaces)");
test.is_palindrome(ns_test_palindrome_with_spaces::process_in_place, palindrome04, "test 4 palindrome (a lof of spaces)");
return 0;
} |
Partager