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 175 176 177 178 179 180 181 182 183 184 185 186
|
int main() {
// Create UserInputHandler Object and test variables
UserInputHandler uih;
char char_var;
int int_var;
double double_var;
bool bool_var;
string string_var;
// Testing Accessors
cout << "Testing Accessors\n" << "----------------------------\n";
cout.setf(std::ios::left);
cout << setw(22) << "invalid_char_input_:" << uih.invalid_char_input()
<< endl;
cout << setw(22) << "char_not_allowed_:" << uih.char_not_allowed() << endl;
cout << setw(22) << "invalid_int_:" << uih.invalid_int() << endl;
cout << setw(22) << "low_int_:" << uih.low_int() << endl;
cout << setw(22) << "high_int_:" << uih.high_int() << endl;
cout << setw(22) << "invalid_double_:" << uih.invalid_double() << endl;
cout << setw(22) << "invalid_bool_:" << uih.invalid_bool() << endl;
cout << setw(22) << "too_long_string_:" << uih.too_long_string() << endl;
cout << setw(22) << "empty_string_:" << uih.empty_string() << endl << endl;
// Testing Mutators
cout << "Testing Mutators\n" << "----------------------------\n";
cout.setf(std::ios::left);
uih.set_invalid_char_input("invalid_char_input_");
uih.set_char_not_allowed("char_not_allowed_");
uih.set_invalid_int("invalid_int_");
uih.set_low_int("low_int_");
uih.set_high_int("high_int_");
uih.set_invalid_double("invalid_double_");
uih.set_invalid_bool("invalid_bool_");
uih.set_too_long_string("too_long_string_");
uih.set_empty_string("empty_string_");
cout << setw(22) << "invalid_char_input_:" << uih.invalid_char_input()
<< endl;
cout << setw(22) << "char_not_allowed_:" << uih.char_not_allowed() << endl;
cout << setw(22) << "invalid_int_:" << uih.invalid_int() << endl;
cout << setw(22) << "low_int_:" << uih.low_int() << endl;
cout << setw(22) << "high_int_:" << uih.high_int() << endl;
cout << setw(22) << "invalid_double_:" << uih.invalid_double() << endl;
cout << setw(22) << "invalid_bool_:" << uih.invalid_bool() << endl;
cout << setw(22) << "too_long_string_:" << uih.too_long_string() << endl;
cout << setw(22) << "empty_string_:" << uih.empty_string() << endl << endl;
// Tests GetSingleCharacter()
cout << "Testing GetSingleCharacter()\n" << "----------------------------\n"
<< "Correct Input: A, b, %, 8\n"
<< "Incorrect Input (invalid_char_input_): (empty string), hello, 12345"
<< "\nTry a character: ";
char_var = uih.GetSingleCharacter();
cout << char_var << " is indeed a single character!\n\n" << endl;
// Tests GetSingleCharacter(allowed_characters)
cout << "Testing GetSingleCharacter(\"ABC123!$\")\n"
<< "----------------------------\n" << "Correct Input: A, B, 1, 2, $\n"
<< "Incorrect Input (invalid_char_input_): (empty string), hello, 12345"
<< "\nIncorrect Input (char_not_allowed_): %, 8, a, D"
<< "\nTry a character: ";
char_var = uih.GetSingleCharacter("ABC123$");
cout << char_var << " is indeed a single character!\n\n" << endl;
// Tests GetInteger()
cout
<< "Testing GetInteger()\n"
<< "----------------------------\n"
<< "Correct Input: -1000, 0, 1000\n"
<< "Incorrect Input (invalid_int_): (empty string), hello, abc123, 123abc, 1.0, 5.5, -"
<< "\nTry an integer: ";
int_var = uih.GetInteger();
cout << int_var << " is indeed an integer!\n\n" << endl;
// Tests GetInteger(0)
cout
<< "Testing GetInteger(0)\n"
<< "----------------------------\n"
<< "Correct Input: 0, 1000\n"
<< "Incorrect Input (invalid_int_): (empty string), hello, abc123, 123abc, 1.0, 5.5, -\n"
<< "Incorrect Input (low_int_): -1, -1000\n" << "Try an integer: ";
int_var = uih.GetInteger(0);
cout << int_var << " is indeed an integer!\n\n" << endl;
// Tests GetInteger(0, 10)
cout
<< "Testing GetInteger(0, 10)\n"
<< "----------------------------\n"
<< "Correct Input: 0, 5, 10\n"
<< "Incorrect Input (invalid_int_): (empty string), hello, abc123, 123abc, 1.0, 5.5, -\n"
<< "Incorrect Input (low_int_): -1, -1000\n"
<< "Incorrect Input (high_int_): 11, 1000\n" << "Try an integer: ";
int_var = uih.GetInteger(0, 10);
cout << int_var << " is indeed an integer!\n\n" << endl;
// Tests GetDouble()
cout
<< "Testing GetDouble()\n"
<< "----------------------------\n"
<< "Correct Input: -10, 0, 10, -10.5, 10.0, 0.5, .5\n"
<< "Incorrect Input (invalid_double_): (empty string), hello, abc123, 123abc, 1.0., .5-, -, .\n"
<< "Try a double: ";
double_var = uih.GetDouble();
cout << double_var << " is indeed a double!\n\n" << endl;
// Tests GetBoolean()
cout
<< "Testing GetBoolean()\n"
<< "----------------------------\n"
<< "Correct Input: T, F, t, f, TRUE, false, TrUe, fAlSe\n"
<< "Incorrect Input (invalid_bool_): (empty string), hello, abc123, 123abc, 1.0, 1, truea, zfalse\n"
<< "Try a boolean: ";
bool_var = uih.GetBoolean();
cout << std::boolalpha << bool_var << " is indeed a boolean!\n\n" << endl;
// Tests GetString()
cout
<< "Testing GetString()\n"
<< "----------------------------\n"
<< "Correct Input: ABCDEFGHIJKLMNOPQRSTUVWXYZ, 123, &^%, (empty string), Hello World!\n"
<< "Try a string: ";
string_var = uih.GetString();
cout << ((string_var.empty()) ? "(empty string)" : string_var)
<< " is indeed a string!\n\n" << endl;
// Tests GetString(0)
cout
<< "Testing GetString(0)\n"
<< "----------------------------\n"
<< "Correct Input: (empty string)\n"
<< "Incorrect Input (too_long_string_): a, %, ABCDEFGHIJKLMNOPQRSTUVWXYZ, 123, &^%, Hello World!\n"
<< "Try a string: ";
string_var = uih.GetString(0);
cout << ((string_var.empty()) ? "(empty string)" : string_var)
<< " is indeed a string!\n\n" << endl;
// Tests GetString(5)
cout
<< "Testing GetString(5)\n"
<< "----------------------------\n"
<< "Correct Input: (empty string), a, abc, 12345, 12 45\n"
<< "Incorrect Input (too_long_string_): ABCDEFGHIJKLMNOPQRSTUVWXYZ, Hello World!\n"
<< "Try a string: ";
string_var = uih.GetString(5);
cout << ((string_var.empty()) ? "(empty string)" : string_var)
<< " is indeed a string!\n\n" << endl;
// Tests GetString(-1, false)
cout << "Testing GetString(-1, false)\n" << "----------------------------\n"
<< "Correct Input: ABCDEFGHIJKLMNOPQRSTUVWXYZ, 123, &^%, Hello World!\n"
<< "Incorrect Input (empty_string_): (empty string)\n"
<< "Try a string: ";
string_var = uih.GetString(-1, false);
cout << ((string_var.empty()) ? "(empty string)" : string_var)
<< " is indeed a string!\n\n" << endl;
// Tests GetString(1, false)
cout
<< "Testing GetString(1, false)\n"
<< "----------------------------\n"
<< "Correct Input: 1, A, %\n"
<< "Incorrect Input (too_long_string_): ABCDEFGHIJKLMNOPQRSTUVWXYZ, 123, &^%, Hello World!\n"
<< "Incorrect Input (empty_string_): (empty string)\n"
<< "Try a string: ";
string_var = uih.GetString(1, false);
cout << ((string_var.empty()) ? "(empty string)" : string_var)
<< " is indeed a string!\n\n" << endl;
// Tests GetString(5, false)
cout
<< "Testing GetString(5, false)\n"
<< "----------------------------\n"
<< "Correct Input: a, abc, 12345, 12 45\n"
<< "Incorrect Input (too_long_string_): ABCDEFGHIJKLMNOPQRSTUVWXYZ, Hello World!\n"
<< "Incorrect Input (empty_string_): (empty string)\n"
<< "Try a string: ";
string_var = uih.GetString(5, false);
cout << ((string_var.empty()) ? "(empty string)" : string_var)
<< " is indeed a string!\n\n" << endl;
}
|