aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test/common/util.h200
1 files changed, 200 insertions, 0 deletions
diff --git a/test/common/util.h b/test/common/util.h
new file mode 100644
index 0000000000..c11ab740de
--- /dev/null
+++ b/test/common/util.h
@@ -0,0 +1,200 @@
+#include <cxxtest/TestSuite.h>
+#include "common/util.h"
+
+class UtilTestSuite : public CxxTest::TestSuite {
+public:
+
+ void test_parsebool_good() {
+
+ // Test the parseBool function
+
+ bool valasbool;
+ bool success;
+
+ // 'Regular' cases that must work
+ // (note that the function must be case insensitive):
+
+ Common::String string_1 ("Yes");
+ success = Common::parseBool (string_1, valasbool);
+ TS_ASSERT_EQUALS(success, 1);
+ TS_ASSERT_EQUALS(valasbool, 1);
+
+ Common::String string_2 ("nO");
+ success = Common::parseBool (string_2, valasbool);
+ TS_ASSERT_EQUALS(success, 1);
+ TS_ASSERT_EQUALS(valasbool, 0);
+
+ Common::String string_3 ("tRuE");
+ success = Common::parseBool (string_3, valasbool);
+ TS_ASSERT_EQUALS(success, 1);
+ TS_ASSERT_EQUALS(valasbool, 1);
+
+ Common::String string_4 ("fAlSe");
+ success = Common::parseBool (string_4, valasbool);
+ TS_ASSERT_EQUALS(success, 1);
+ TS_ASSERT_EQUALS(valasbool, 0);
+
+ Common::String string_5 ("1");
+ success = Common::parseBool (string_5, valasbool);
+ TS_ASSERT_EQUALS(success, 1);
+ TS_ASSERT_EQUALS(valasbool, 1);
+
+ Common::String string_6 ("0");
+ success = Common::parseBool (string_6, valasbool);
+ TS_ASSERT_EQUALS(success, 1);
+ TS_ASSERT_EQUALS(valasbool, 0);
+
+ }
+
+ void test_parsebool_bad() {
+
+ bool valasbool;
+ bool success;
+
+ // Bad cases that should not return success #1:
+ // Random string
+ Common::String string_1 ("u_f1ght_l1k3_a_c0w");
+ success = Common::parseBool (string_1, valasbool);
+ TS_ASSERT_EQUALS(success, 0);
+
+ // Bad cases that should not return success #2, #3:
+ // The function should NOT accept trailing whitespaces:
+ Common::String string_2 (" yes");
+ success = Common::parseBool (string_2, valasbool);
+ TS_ASSERT_EQUALS(success, 0);
+
+ Common::String string_3 ("yes ");
+ success = Common::parseBool (string_3, valasbool);
+ TS_ASSERT_EQUALS(success, 0);
+
+ }
+
+ void test_is_al_num() {
+
+ // Test the isAlnum function
+ // Should return true if and only if the input is an alphanumeric char
+
+ TS_ASSERT_EQUALS(Common::isAlnum('a'), 1);
+ TS_ASSERT_EQUALS(Common::isAlnum('A'), 1);
+ TS_ASSERT_EQUALS(Common::isAlnum('z'), 1);
+ TS_ASSERT_EQUALS(Common::isAlnum('Z'), 1);
+ TS_ASSERT_EQUALS(Common::isAlnum('1'), 1);
+ TS_ASSERT_EQUALS(Common::isAlnum('0'), 1);
+ TS_ASSERT_EQUALS(Common::isAlnum('§'), 0);
+ TS_ASSERT_EQUALS(Common::isAlnum('$'), 0);
+ TS_ASSERT_EQUALS(Common::isAlnum(' '), 0);
+ TS_ASSERT_EQUALS(Common::isAlnum('\n'), 0);
+ TS_ASSERT_EQUALS(Common::isAlnum('\b'), 0);
+ TS_ASSERT_EQUALS(Common::isAlnum(0), 0);
+ TS_ASSERT_EQUALS(Common::isAlnum(255), 0);
+
+ }
+
+ void test_is_alpha() {
+
+ // Test the isAlpha function
+ // Should return true if and only if the input is an alphanumeric char
+
+ TS_ASSERT_EQUALS(Common::isAlpha('a'), 1);
+ TS_ASSERT_EQUALS(Common::isAlpha('A'), 1);
+ TS_ASSERT_EQUALS(Common::isAlpha('z'), 1);
+ TS_ASSERT_EQUALS(Common::isAlpha('Z'), 1);
+ TS_ASSERT_EQUALS(Common::isAlpha('1'), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha('0'), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha('§'), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha('$'), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha(' '), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha('\n'), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha('\b'), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha(0), 0);
+ TS_ASSERT_EQUALS(Common::isAlpha(255), 0);
+
+ }
+
+ void test_is_digit() {
+
+ // Test the isDigit function
+ // Should return true if and only if the input is a single digit
+
+ TS_ASSERT_EQUALS(Common::isDigit('a'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit('A'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit('z'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit('Z'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit('1'), 1);
+ TS_ASSERT_EQUALS(Common::isDigit('0'), 1);
+ TS_ASSERT_EQUALS(Common::isDigit('§'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit('$'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit(' '), 0);
+ TS_ASSERT_EQUALS(Common::isDigit('\n'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit('\b'), 0);
+ TS_ASSERT_EQUALS(Common::isDigit(0), 0);
+ TS_ASSERT_EQUALS(Common::isDigit(255), 0);
+
+ }
+
+ void test_is_lower() {
+
+ // Test the isLower function
+ // Should return true if and only if the input is a lowercase char
+
+ TS_ASSERT_EQUALS(Common::isLower('a'), 1);
+ TS_ASSERT_EQUALS(Common::isLower('A'), 0);
+ TS_ASSERT_EQUALS(Common::isLower('z'), 1);
+ TS_ASSERT_EQUALS(Common::isLower('Z'), 0);
+ TS_ASSERT_EQUALS(Common::isLower('1'), 0);
+ TS_ASSERT_EQUALS(Common::isLower('0'), 0);
+ TS_ASSERT_EQUALS(Common::isLower('§'), 0);
+ TS_ASSERT_EQUALS(Common::isLower('$'), 0);
+ TS_ASSERT_EQUALS(Common::isLower(' '), 0);
+ TS_ASSERT_EQUALS(Common::isLower('\n'), 0);
+ TS_ASSERT_EQUALS(Common::isLower('\b'), 0);
+ TS_ASSERT_EQUALS(Common::isLower(0), 0);
+ TS_ASSERT_EQUALS(Common::isLower(255), 0);
+
+ }
+
+
+ void test_is_upper() {
+
+ // Test the isUpper function
+ // Should return true if and only if the input is an uppercase char
+
+ TS_ASSERT_EQUALS(Common::isUpper('a'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper('A'), 1);
+ TS_ASSERT_EQUALS(Common::isUpper('z'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper('Z'), 1);
+ TS_ASSERT_EQUALS(Common::isUpper('1'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper('0'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper('§'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper('$'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper(' '), 0);
+ TS_ASSERT_EQUALS(Common::isUpper('\n'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper('\b'), 0);
+ TS_ASSERT_EQUALS(Common::isUpper(0), 0);
+ TS_ASSERT_EQUALS(Common::isUpper(255), 0);
+
+ }
+ void test_is_space() {
+ // isSpace should return true iff the character is some kind of whitespace
+ // or tab character
+ for (int c=0; c<255; c++) {
+ if (c==' ' || c=='\t' || c=='\r' || c=='\n' || c=='\v' || c=='\f') {
+ TS_ASSERT_EQUALS(Common::isSpace(c), 1);
+ } else {
+ TS_ASSERT_EQUALS(Common::isSpace(c), 0);
+ }
+ }
+ }
+
+ void test_is_print() {
+ // isPrint should return true iff the input is a printable ascii char.
+ // That is to say, 0x20 to 0x7E.
+ for (int c=0; c<255; c++) {
+ if (c>=0x20 && c<=0x7E) {
+ TS_ASSERT_EQUALS(Common::isPrint(c), 1);
+ } else {
+ TS_ASSERT_EQUALS(Common::isPrint(c), 0);
+ }
+ }
+ }
+};