/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: bsl_test_hashset.h,v 1.5 2008/12/15 09:57:00 xiaowei Exp $ * * hashset phashset的测试代码 * test_hashset 测试hashset phashset的基本功能 * test_hashset_iterator 测试hashset的迭代器功能 * test_hashset_serialization 测试容器的序列化功能 * * 注意:phashset的线程安全性问题,在另外的测试文件里面单独测试 **************************************************************************/ /** * @file settest.cpp * @author xiaowei(com@baidu.com) * @date 2008/08/14 11:10:06 * @version $Revision: 1.5 $ * @brief * **/ #ifndef __BSL_TEST_HASHSET_H #define __BSL_TEST_HASHSET_H #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_hashset() { int ret = 0; { int loop = 1<<10; for (int i=0; i vec; for (int i=0; i bool test_hashset_iterator() { int ret = 0; THMp test; std::set vec; __XASSERT2 (test.create(1<<15) == 0); int insertsize = 1<<10; for (int i=0; i bool test_hashset_serialization() { THMp test; int ret = 0; std::set vec; __XASSERT2 (test.create(1<<15) == 0); int loop = 1<<12; for (int i=0; i::iterator iter = vec.begin(); iter != vec.end(); ++iter) { ret = test.get(*iter); __XASSERT2 (ret == bsl::HASH_EXIST); } } //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::set::iterator iter = vec.begin(); iter != vec.end(); ++iter) { ret = test_0.get(*iter); __XASSERT2 (ret == bsl::HASH_EXIST); } } //读硬盘到新结构体, 新结构体不初始化 { 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::set::iterator iter = vec.begin(); iter != vec.end(); ++iter) { std::string val = ""; ret = test_0.get(*iter); __XASSERT2 (ret == bsl::HASH_EXIST); } } return true; } //assign测试 template bool test_hashset_assign() { THMp test; std::set vec; __XASSERT2(test.create(1<<15) == 0); int loop = 1<<15; for (int i=0; i::iterator iter = vec.begin(); iter != vec.end(); ++iter) { int ret = test.get(*iter); __XASSERT2(ret == bsl::HASH_EXIST); } return true; } template bool test_hashset_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::set map_t; map_t vmap; for (int i=0; i<10000; ++i) { vmap.insert(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 bsl::hashset THMp1; void test_operator() { { THMp1 ht; std::set st; ht.assign(st.begin(),st.end()); THMp1 ht2; ht2 = ht; THMp1 ht3(ht); } { std::set st; for (size_t i = 0; i < N; i ++) { st.insert( key(s[i] ) ); } THMp1 ht; ht.assign(st.begin(),st.end()); THMp1 ht2; ht2 = ht; THMp1 ht3(ht); for (size_t i = 0; i < N; i ++) { int ret = ht.get(key(s[i])); TS_ASSERT( ret == bsl::HASH_EXIST ); ret = ht2.get(key(s[i])); TS_ASSERT( ret == bsl::HASH_EXIST ); ret = ht3.get(key(s[i])); TS_ASSERT( ret == bsl::HASH_EXIST ); } } } 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; } } void test_test_hashset_(void) { __BSL_DEBUG("open debug mode"); TSM_ASSERT ("", test_hashset()); } void test_hashset_iterator_() { TSM_ASSERT ("", test_hashset_iterator()); } void test_hashset_serialization_() { TSM_ASSERT ("", test_hashset_serialization()); } void test_hashset_assign_() { TSM_ASSERT ("", test_hashset_assign()); } void test_hashset_clear_() { TSM_ASSERT ("", test_hashset_clear()); } typedef bsl::hashset, bsl::bsl_alloc > THMp2; void test_hashset2_() { TSM_ASSERT ("", test_hashset()); } void test_hashset2_iterator_() { TSM_ASSERT ("", test_hashset_iterator()); } void test_hashset2_serialization_() { TSM_ASSERT ("", test_hashset_serialization()); } void test_hashset2_assign_() { TSM_ASSERT ("", test_hashset_assign()); } void test_hashset2_clear_() { TSM_ASSERT ("", test_hashset_clear()); } typedef bsl::phashset THMp3; void test_phashset_() { TSM_ASSERT ("", test_hashset()); } void test_phashset_iterator_() { TSM_ASSERT ("", test_hashset_iterator()); } void test_phashset_serialization_() { TSM_ASSERT ("", test_hashset_serialization()); } void test_phashset_assign_() { TSM_ASSERT ("", test_hashset_assign()); } void test_phashset_clear_() { TSM_ASSERT ("", test_hashset_clear()); } typedef bsl::phashset, bsl::bsl_alloc > THMp4; void test_phashset2_() { TSM_ASSERT ("", test_hashset()); } void test_phashset2_iterator_() { TSM_ASSERT ("", test_hashset_iterator()); } void test_phashset2_serialization() { TSM_ASSERT ("", test_hashset_serialization()); } void test_phashset2_assign_() { TSM_ASSERT ("", test_hashset_assign()); } void test_phashset2_clear_() { TSM_ASSERT ("", test_hashset_clear()); } typedef bsl::bsl_rwhashset, bsl::bsl_alloc > THMp5; void test_rwhashset_() { TSM_ASSERT ("", test_hashset()); } void test_rwhashset_clear_() { TSM_ASSERT ("", test_hashset_clear()); } typedef bsl::phashset, bsl::bsl_cpsalloc > > THMp6; void test_phashset3_() { TSM_ASSERT ("", test_hashset()); } void test_phashset3_iterator_() { TSM_ASSERT ("", test_hashset_iterator()); } void test_phashset3_serialization() { TSM_ASSERT ("", test_hashset_serialization()); } void test_phashset3_assign_() { TSM_ASSERT ("", test_hashset_assign()); } void test_phashset3_clear_() { TSM_ASSERT ("", test_hashset_clear()); } }; #endif /* vim: set ts=4 sw=4 sts=4 tw=100 noet: */