| 27 | #include "dlib/hash.h" |
| 28 | |
| 29 | TEST(dmStringPool, Test01) |
| 30 | { |
| 31 | dmStringPool::HPool pool = dmStringPool::New(); |
| 32 | |
| 33 | std::map<const char*, std::string > strings; |
| 34 | |
| 35 | uint32_t n_allocated = 0; |
| 36 | for (uint32_t i = 0; i < 20000; ++i) |
| 37 | { |
| 38 | std::string tmp; |
| 39 | uint32_t n = rand() % 16; |
| 40 | for (uint32_t j = 0; j < n; ++j) |
| 41 | { |
| 42 | char c = 'a' + (rand() % 26); |
| 43 | tmp += c; |
| 44 | } |
| 45 | n_allocated += n; |
| 46 | |
| 47 | const char* s = dmStringPool::Add(pool, tmp.c_str(), tmp.length(), dmHashBufferNoReverse32(tmp.c_str(), tmp.length())); |
| 48 | strings[s] = std::string(s); |
| 49 | } |
| 50 | |
| 51 | std::map<const char*, std::string>::iterator iter; |
| 52 | for (iter = strings.begin(); iter != strings.end(); ++iter) |
| 53 | { |
| 54 | const char*key = iter->first; |
| 55 | const std::string& value = iter->second; |
| 56 | |
| 57 | ASSERT_STREQ(value.c_str(), key); |
| 58 | |
| 59 | // Assert that we get the same pointer back |
| 60 | ASSERT_EQ((uintptr_t) key, (uintptr_t) dmStringPool::Add(pool, key, strlen(key), dmHashBufferNoReverse32(key, strlen(key)))); |
| 61 | ASSERT_EQ((uintptr_t) key, (uintptr_t) dmStringPool::Add(pool, value.c_str(), value.length(), dmHashBufferNoReverse32(value.c_str(), value.length()))); |
| 62 | } |
| 63 | |
| 64 | // Assert that at least 5 pages are allocated in string-pool |
| 65 | ASSERT_GE(n_allocated, 5U * 4096U); |
| 66 | |
| 67 | dmStringPool::Delete(pool); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | int main(int argc, char **argv) |