/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: bsl_test_hashmap.h,v 1.6 2009/06/26 05:23:25 xiaowei Exp $ * * hashmap phashmap的测试代码 * test_hashmap 测试hashmap phashmap的基本功能 * test_hashmap_iterator 测试hashmap的迭代器功能 * test_hashmap_serialization 测试容器的序列化功能 * test_hashmap_assign 测试assign功能 * test_hashmap_clear clear功能的测试 * * 注意:phashmap的线程安全性问题,在另外的测试文件里面单独测试 **************************************************************************/ /** * @file hashtest.cpp * @author xiaowei(com@baidu.com) * @date 2008/08/14 11:10:06 * @version $Revision: 1.6 $ * @brief * **/ #ifndef __BSL_TEST_HASHMAP_H #define __BSL_TEST_HASHMAP_H #include #include #include #include #include #include #include #include #include #include #include #include #include #define __XASSERT(flag, fmt, arg...) \ {\ bool ___bsl_flag = flag; \ if (!(___bsl_flag)) { \ fprintf(stdout, "\n[error][%s:%d][%s]"fmt"\n", __FILE__, __LINE__, #flag, ##arg); \ return false; \ }\ } #define __XASSERT2(flag) __XASSERT(flag, "") char s[][32]={"yingxiang", "yufan", "wangjiping", "xiaowei", "yujianjia", "maran", "baonenghui", "gonglei", "wangyue", "changxinglong", "chenxiaoming", "guoxingrong", "kuangyuheng"}; const size_t N = sizeof(s) / sizeof(s[0]); std::string randstr() { char buf[130] = {0}; int len = rand()%64 + 1; for (int i=0; i bool test_hashmap() { { int loop = 1<<10; for (int i=0; i vec; for (int i=0; i bool test_hashmap_iterator() { THMp test; std::map vec; __XASSERT2 (test.create(1<<15) == 0); int insertsize = 1<<10; for (int i=0; ifirst)->second == iter->second); } for (typename THMp::iterator iter = test.begin(); iter != test.end(); iter++) { __XASSERT2 (vec.find(iter->first)->second == iter->second); } for (typename THMp::const_iterator iter = test.begin(); iter != test.end(); ++iter) { __XASSERT2 (vec.find(iter->first)->second == iter->second); } for (typename THMp::const_iterator iter = test.begin(); iter != test.end(); iter++) { __XASSERT2 (vec.find(iter->first)->second == iter->second); } const THMp & testp = test; for (typename THMp::const_iterator iter = testp.begin(); iter != testp.end(); ++iter) { __XASSERT2 (vec.find(iter->first)->second == iter->second); } for (typename THMp::const_iterator iter = testp.begin(); iter != testp.end(); iter++) { __XASSERT2 (vec.find(iter->first)->second == iter->second); } testp.get(randstr()); size_t vsize = vec.size(); //根据序列化参数对value进行附值 for (typename THMp::iterator iter = test.begin(); iter != test.end(); ++iter) { iter->second = randstr(); vec[iter->first] = iter->second; } __XASSERT(vsize == vec.size(), "%lu != %lu", (unsigned long)vsize, (unsigned long)vec.size()); for (typename THMp::iterator iter = test.begin(); iter != test.end(); ++iter) { __XASSERT2 (vec.find(iter->first)->second == iter->second); } //不调用destroy,自己调用析构函数析构 //需要valgrind检查是否内存泄露 return true; } template bool test_hashmap_serialization() { THMp test; std::map vec; __XASSERT2 (test.create(1<<15) == 0); int loop = 1<<12; for (int i=0; i::iterator iter = vec.begin(); iter != vec.end(); ++iter) { std::string val = ""; int ret = test.get(iter->first, &val); __XASSERT2 (ret == bsl::HASH_EXIST); __XASSERT2 (val == iter->second); } } test.clear(); //读硬盘到新结构体 { THMp test_0; __XASSERT2 (test_0.create(100) == 0); bsl::filestream fs; __XASSERT2 (fs.open(htdat, "r") == 0); bsl::binarchive ar(fs); __XASSERT2 (ar.read(&test_0) == 0); fs.close(); //check __XASSERT (vec.size() == test_0.size(), "%lu != %lu", (unsigned long)vec.size(), (unsigned long)test_0.size()); for (std::map::iterator iter = vec.begin(); iter != vec.end(); ++iter) { std::string val = ""; int ret = test_0.get(iter->first, &val); __XASSERT2 (ret == bsl::HASH_EXIST); __XASSERT2 (val == iter->second); } } //读硬盘到新结构体, 新结构体不初始化 { THMp test_0; bsl::filestream fs; __XASSERT2 (fs.open(htdat, "r") == 0); bsl::binarchive ar(fs); __XASSERT2 (ar.read(&test_0) == 0); fs.close(); //check __XASSERT (vec.size() == test_0.size(), "%lu != %lu", (unsigned long)vec.size(), (unsigned long)test_0.size()); for (std::map::iterator iter = vec.begin(); iter != vec.end(); ++iter) { std::string val = ""; int ret = test_0.get(iter->first, &val); __XASSERT2 (ret == bsl::HASH_EXIST); __XASSERT2 (val == iter->second); } } return true; } //assign测试 template bool test_hashmap_assign() { THMp test; std::map vec; __XASSERT2 (test.create(102400) == 0); __BSL_DEBUG("create hashmap"); int loop = 1<<15; for (int i=0; i::iterator iter = vec.begin(); iter != vec.end(); ++iter) { std::string val = ""; int ret = test.get(iter->first, &val); __XASSERT2 (ret == bsl::HASH_EXIST); __XASSERT (val == iter->second, "%s == %s", val.c_str(), iter->second.c_str()); } for (int i=0; i<16; ++i) { __XASSERT2 (test.assign(vec.begin(), vec.end()) == 0); } return true; } template bool test_hashmap_clear() { //没有create的情况下clear { THMp test0; __XASSERT2(test0.size() == 0); for (int i=0; i<10000; ++i) { __XASSERT2(test0.clear() == 0); __XASSERT2(test0.size() == 0); } } //create的情况下clear { THMp test0; __XASSERT2(test0.size() == 0); __XASSERT2(test0.create(100000) == 0); __XASSERT2(test0.size() == 0); __XASSERT2(test0.clear() == 0); __XASSERT2(test0.size() == 0); } //有数据的情况下clear { THMp test0; typedef std::map map_t; map_t vmap; for (int i=0; i<10000; ++i) { vmap.insert(std::make_pair(randstr(), randstr())); } __XASSERT2(test0.create(vmap.size() * 2) == 0); for (int i=0; i<10; ++i) { __XASSERT2(test0.assign(vmap.begin(), vmap.end()) == 0); } for (int i=0; i<10; ++i) { __XASSERT2(test0.clear() == 0); __XASSERT2(test0.size() == 0); } for (int i=0; i<100; ++i) { __XASSERT2(test0.destroy() == 0); } } return true; } class bsl_test_hashmap : public CxxTest::TestSuite { public: typedef std::string key; typedef std::string value; typedef bsl::hashmap THMp1; void test_operator() { { THMp1 ht; std::map mp; ht.assign(mp.begin(),mp.end()); THMp1 ht2; ht2 = ht; THMp1 ht3(ht); } { std::map mp; for (size_t i = 0; i < N; i ++) { mp[key(s[i])] = int(i); } THMp1 ht; ht.assign(mp.begin(),mp.end()); THMp1 ht2; ht2 = ht; THMp1 ht3(ht); for (size_t i = 0; i < N; i ++) { value val; value val2; value val3; int ret = ht.get(key(s[i]),&val); TS_ASSERT( ret == bsl::HASH_EXIST ); ret = ht2.get(key(s[i]),&val2); TS_ASSERT( ret == bsl::HASH_EXIST ); ret = ht3.get(key(s[i]),&val3); TS_ASSERT( ret == bsl::HASH_EXIST ); TS_ASSERT( val2 == val3 ); TS_ASSERT( val == val2 ); } } } void test_create() { THMp1 ht; for (int i = 10; i < 100; i ++) { ht.create(i); } THMp1 ht2; for (int i = 0; i < 100; i ++) { ht2 = ht; } } /** * @brief 测试hash表的正常功能 * * @return void * @retval * @author xiaowei **/ void test_hashmap_(void) { __BSL_DEBUG("open debug mode"); TSM_ASSERT ("test_hashmap error", test_hashmap()); } /** * @brief 测试hash表迭代器功能 * * @return void * @retval * @author xiaowei **/ void test_hashmap_iterator_(void) { TSM_ASSERT ("", test_hashmap_iterator()); } /** * @brief 测试hash表序列化功能 * * @return void * @retval * @author xiaowei **/ void test_hashmap_serialization_(void) { TSM_ASSERT ("", test_hashmap_serialization()); } /** * @brief 测试hash表附值功能 * * @return void * @retval * @author xiaowei **/ void test_hashmap_assign_() { TSM_ASSERT ("", test_hashmap_assign()); } void test_hashmap_clear_() { TSM_ASSERT ("", test_hashmap_clear()); } //bsl_alloc分配器 typedef bsl::hashmap, bsl::bsl_alloc > THMp2; void test_hashmap2_() { TSM_ASSERT ("", test_hashmap()); } void test_hashmap2_iterator_() { TSM_ASSERT ("", test_hashmap_iterator()); } void test_hashmap2_serialization_() { TSM_ASSERT ("", test_hashmap_serialization()); } void test_hashmap2_assign_() { TSM_ASSERT("", test_hashmap_assign()); } void test_hashmap2_clear_() { TSM_ASSERT ("", test_hashmap_clear()); } //默认的phashmap typedef bsl::phashmap THMp3; void test_phashmap_() { TSM_ASSERT ("", test_hashmap()); } void test_phashmap_iterator_() { TSM_ASSERT ("", test_hashmap_iterator()); } void test_phashmap_serialization_() { TSM_ASSERT ("", test_hashmap_serialization()); } void test_phashmap_assign_() { TSM_ASSERT ("", test_hashmap_assign()); } void test_phashmap_clear_() { TSM_ASSERT ("", test_hashmap_clear()); } //bsl_alloc的phashmap typedef bsl::phashmap, bsl::bsl_alloc > THMp4; void test_phashmap2_() { TSM_ASSERT ("", test_hashmap()); } void test_phashmap2_iterator_() { TSM_ASSERT ("", test_hashmap_iterator()); } void test_phashmap2_serialization_() { TSM_ASSERT ("", test_hashmap_serialization()); } void test_phashmap2_assign_() { TSM_ASSERT ("", test_hashmap_assign()); } void test_phashmap2_clear_() { TSM_ASSERT ("", test_hashmap_clear()); } //bsl_cpsalloc分配器 typedef bsl::phashmap, bsl::bsl_cpsalloc > > THMp5; void test_phashmap3_() { TSM_ASSERT ("", test_hashmap()); } void test_phashmp3_iterator_() { TSM_ASSERT ("", test_hashmap_iterator()); } void test_phashmap3_serialization_() { TSM_ASSERT ("", test_hashmap_serialization()); } void test_phashmap3_assign_() { TSM_ASSERT ("", test_hashmap_assign()); } void test_phashmap3_clear_() { bsl::hashmap v1; bsl::hashmap v2; v1.create(100); v2.create(1000); v1.set(1, "hello"); v1.get(1); v2.set(2, "hello"); v2.get(2); TSM_ASSERT ("", test_hashmap_clear()); } }; #endif /* vim: set ts=4 sw=4 sts=4 tw=100 noet: */