Skip to content

Commit

Permalink
TEST: removed fixed hashes in test/common/hash-str.h
Browse files Browse the repository at this point in the history
The hash function does not necessarily have to conform to one specific algorithm as long as equals/differs is respected.
  • Loading branch information
tobiatesan committed Jul 10, 2013
1 parent 6245a68 commit e9406fa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 62 deletions.
59 changes: 20 additions & 39 deletions test/common/hash-str.h
Expand Up @@ -26,8 +26,8 @@ class HashStrTestSuite : public CxxTest::TestSuite {
const Common::String spaced("test ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");
const Common::String plus128("t\xE5est");
// 'e'+128 = 0xE5
const Common::String plus128("t\345est");
// 'e'+128 = 0xE5 = 0o345

Common::CaseSensitiveString_EqualTo css_et;
TS_ASSERT_EQUALS(css_et(lower, mixed), false);
Expand Down Expand Up @@ -56,7 +56,7 @@ class HashStrTestSuite : public CxxTest::TestSuite {
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");
const Common::String plus128("t\xE5est");
const Common::String plus128("t\345est");

Common::IgnoreCase_EqualTo ic_et;
TS_ASSERT_EQUALS(ic_et(lower, mixed), true);
Expand All @@ -78,105 +78,86 @@ class HashStrTestSuite : public CxxTest::TestSuite {
// Here we compute string hashes for different
// strings and see that the functor is case sensitive
// and does not ignore spaces.
// The hashes come from Python's hash() function.

const Common::String lower("test");
const uint lower_hash = 1308370872;
const Common::String lower1("test");
const Common::String mixed("tESt");
const uint mixed_hash = -1217273608;
const Common::String spaced("test ");
const uint spaced_hash = -1086267887;
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");
const uint tabbed_hash = -1086267848;

Common::CaseSensitiveString_Hash css_h;
TS_ASSERT_EQUALS(css_h(lower), lower_hash);
TS_ASSERT_EQUALS(css_h(mixed), mixed_hash);
TS_ASSERT_EQUALS(css_h(spaced), spaced_hash);
TS_ASSERT_EQUALS(css_h(tabbed), tabbed_hash);
TS_ASSERT_EQUALS(css_h(lower), css_h(lower1));
TS_ASSERT_DIFFERS(css_h(mixed), css_h(lower));
TS_ASSERT_DIFFERS(css_h(spaced), css_h(lower));
TS_ASSERT_DIFFERS(css_h(tabbed), css_h(spaced));
TS_ASSERT_DIFFERS(css_h(spaced), css_h(doublespaced));
}

void test_ignore_case_hash() {
// Same as test_case_sensitive_string_hash, but case insensitive.
const Common::String lower("test");
const uint lower_hash = 1308370872;
const Common::String lower1("test");
const Common::String mixed("tESt");
const Common::String spaced("test ");
const uint spaced_hash = -1086267887;
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");
const uint tabbed_hash = -1086267848;

Common::IgnoreCase_Hash ic_h;
TS_ASSERT_EQUALS(ic_h(lower), lower_hash);
TS_ASSERT_EQUALS(ic_h(mixed), lower_hash);
TS_ASSERT_EQUALS(ic_h(spaced), spaced_hash);
TS_ASSERT_EQUALS(ic_h(tabbed), tabbed_hash);
TS_ASSERT_EQUALS(ic_h(mixedspaced), spaced_hash);
TS_ASSERT_EQUALS(ic_h(lower), ic_h(lower1));
TS_ASSERT_EQUALS(ic_h(mixed), ic_h(lower));
TS_ASSERT_EQUALS(ic_h(spaced), ic_h(mixedspaced));
TS_ASSERT_DIFFERS(ic_h(tabbed), ic_h(lower));
TS_ASSERT_DIFFERS(ic_h(spaced), ic_h(doublespaced));
}

void test_cpp_string_hash ()
void test_cpp_string_hash()
{
// We run the same tests with Hash<String>,
// a template specialization of Hash, also a functor.
// It is supposed to be case sensitive.
// Again, hashes come from Python's hash().

const Common::String lower("test");
const uint lower_hash = 1308370872;
const Common::String lower1("test");
const Common::String mixed("tESt");
const uint mixed_hash = -1217273608;
const Common::String spaced("test ");
const uint spaced_hash = -1086267887;
const Common::String mixedspaced("tESt ");
const Common::String doublespaced("test ");
const Common::String tabbed("test\t");
const uint tabbed_hash = -1086267848;

Common::Hash<Common::String> h;
TS_ASSERT_EQUALS(h(lower), lower_hash);
TS_ASSERT_EQUALS(h(lower), h(lower1));
TS_ASSERT_EQUALS(h(mixed), mixed_hash);
TS_ASSERT_EQUALS(h(spaced), spaced_hash);
TS_ASSERT_EQUALS(h(tabbed), tabbed_hash);
TS_ASSERT_DIFFERS(h(mixed), h(lower));
TS_ASSERT_DIFFERS(h(spaced), h(lower));
TS_ASSERT_DIFFERS(h(tabbed), h(spaced));
TS_ASSERT_DIFFERS(h(spaced), h(doublespaced));
}

void test_c_style_string_hash ()
void test_c_style_string_hash()
{
// Same as test_cpp_string_hash but with Hash<const char*>,
// a template specialization of Hash, also a functor,
// that works with C-Style strings.
// It is supposed to be case sensitive.

char lower[] = "test";
const uint lower_hash = 1308370872; // CPython told me so
char lower1[] = "test";
char mixed[] = "tESt";
const uint mixed_hash = -1217273608;
char spaced[] = "test ";
const uint spaced_hash = -1086267887;
char mixedspaced[] = "tESt ";
char doublespaced[] = "test ";
char tabbed[] = "test\t";
const uint tabbed_hash = -1086267848;

Common::Hash<const char*> h;
TS_ASSERT_EQUALS(h(lower), lower_hash);
TS_ASSERT_EQUALS(h(lower), h(lower1));
TS_ASSERT_EQUALS(h(mixed), mixed_hash);
TS_ASSERT_EQUALS(h(spaced), spaced_hash);
TS_ASSERT_DIFFERS(h(mixed), h(lower));
TS_ASSERT_DIFFERS(h(spaced), h(lower));
TS_ASSERT_DIFFERS(h(spaced), h(mixedspaced));
TS_ASSERT_EQUALS(h(tabbed), tabbed_hash);
TS_ASSERT_DIFFERS(h(tabbed), h(spaced));
TS_ASSERT_DIFFERS(h(spaced), h(doublespaced));

}


Expand Down
46 changes: 23 additions & 23 deletions test/common/util.h
Expand Up @@ -14,13 +14,13 @@ class UtilTestSuite : public CxxTest::TestSuite {
bool valasbool;
bool success;

Common::String string_1 ("Yes");
success = Common::parseBool (string_1, valasbool);
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);
Common::String string_2("nO");
success = Common::parseBool(string_2, valasbool);
TS_ASSERT_EQUALS(success, 1);
TS_ASSERT_EQUALS(valasbool, 0);
}
Expand All @@ -33,13 +33,13 @@ class UtilTestSuite : public CxxTest::TestSuite {
bool valasbool;
bool success;

Common::String string_3 ("tRuE");
success = Common::parseBool (string_3, valasbool);
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);
Common::String string_4("fAlSe");
success = Common::parseBool(string_4, valasbool);
TS_ASSERT_EQUALS(success, 1);
TS_ASSERT_EQUALS(valasbool, 0);
}
Expand All @@ -56,12 +56,12 @@ class UtilTestSuite : public CxxTest::TestSuite {
bool success;

Common::String string_5 ("1");
success = Common::parseBool (string_5, valasbool);
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);
Common::String string_6("0");
success = Common::parseBool(string_6, valasbool);
TS_ASSERT_EQUALS(success, 1);
TS_ASSERT_EQUALS(valasbool, 0);

Expand All @@ -74,33 +74,33 @@ class UtilTestSuite : public CxxTest::TestSuite {

// 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);
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);
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);
Common::String string_3("yes ");
success = Common::parseBool(string_3, valasbool);
TS_ASSERT_EQUALS(success, 0);

// While 'a-z'+0x20 must work just fine,
// '0-1'+0x20 should NOT. '2' is not good either.

Common::String string_4 ("\x50");
success = Common::parseBool (string_4, valasbool);
Common::String string_4("\x50");
success = Common::parseBool(string_4, valasbool);
TS_ASSERT_EQUALS(success, 0);

Common::String string_5 ("\x51");
success = Common::parseBool (string_5, valasbool);
Common::String string_5("\x51");
success = Common::parseBool(string_5, valasbool);
TS_ASSERT_EQUALS(success, 0);

Common::String string_6 ("2");
success = Common::parseBool (string_6, valasbool);
Common::String string_6("2");
success = Common::parseBool(string_6, valasbool);
TS_ASSERT_EQUALS(success, 0);
}

Expand Down

0 comments on commit e9406fa

Please sign in to comment.