bsl_test_hashtable.h 4.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

#ifndef __BSL_TEST_HASHTABLE_H
#define __BSL_TEST_HASHTABLE_H

#include <cxxtest/TestSuite.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <bsl/containers/hash/bsl_hashmap.h>
#include <bsl/containers/string/bsl_string.h>
#include <bsl/containers/hash/bsl_phashmap.h>
#include <bsl/containers/hash/bsl_hashtable.h>
#include <bsl/containers/hash/bsl_phashtable.h>

#include <bsl/archive/bsl_filestream.h>
#include <bsl/archive/bsl_serialization.h>
#include <bsl/archive/bsl_binarchive.h>
#include <bsl/alloc/allocator.h>
#include <vector>
#include <map>
#include <set>

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<len; ++i) {
		buf[i] = rand()%26 + 'a';
	}
	return buf;
};

/**
 *key hashº
 */
struct hash_func
{
	size_t operator () (const std::string &__key) const {
		size_t key = 0;
		for (size_t i=0; i<__key.size(); ++i) {
			key = (key<<8)+__key[i];
		}
		return key;
	}
};

class bsl_test_hashtable : public CxxTest::TestSuite
{
public:

	typedef std::string key;
	typedef int value;
	typedef bsl::bsl_hashtable<key,key,hash_func,std::equal_to<key>,
				bsl::param_select<key>,bsl::bsl_sample_alloc<bsl::bsl_alloc<key>, 256> > hash_type;
	 
	void test_hashtable() {
		
		{
			hash_type ht;
			TS_ASSERT( 0 == ht.create(100));
			TS_ASSERT( true == ht.is_created() );
			
			hash_type::const_iterator cit = ht.begin();		
			++cit;
			cit++;
			
			hash_type::iterator it = ht.begin();
			++it;
			it++;
			
			const hash_type cht;
			TS_ASSERT(false == cht.is_created());
			hash_type::const_iterator cht_cit= cht.begin();
			++cht_cit;
			cht_cit++;
		}
		{
			try {
				hash_type ht;
				hash_type ht1;
				ht = ht1;
			} catch (bsl::Exception& e) {
				printf("\n==\n%s\n==\n",e.all());
			}
		}
		{
			try {
				hash_type ht;
				hash_type ht1(ht);
			} catch (bsl::Exception& e) {
				printf("\n==\n%s\n==\n",e.all());
			}
		}

                //added 2011/11/15
                {
                    try{
                        hash_type ht;
                        hash_type ht1(ht);
                        std::string key1("what");
                        ht1.set(ht1._hashfun(key1),key1,key1);
                    } catch (bsl::Exception& e) {
                        printf("-------------------new\n\n==\n%s\n==\n",e.all());
                    }
                }

                //added 2011/11/15
                {
                    try{
                        hash_type ht;
                        ht.create(10);
                        hash_type ht1(ht);
                        
                        std::string key1("what");
                        ht1.set(ht1._hashfun(key1),key1,key1);
                    } catch (bsl::Exception& e) {
                        printf("-------------------new\n\n==\n%s\n==\n",e.all());
                    }
                }

                //added 2011/11/15
                {
                    try{
                        hash_type ht;
                        ht.create(10);
                        hash_type ht1;
                        ht1.create(5);
                        ht1 = ht;
                        
                        std::string key1("what");
                        ht1.set(ht1._hashfun(key1),key1,key1);
                    } catch (bsl::Exception& e) {
                        printf("-------------------new\n\n==\n%s\n==\n",e.all());
                    }
                }
                
		{
			try {
				printf("hello\n");
				hash_type ht(0);
			} catch (bsl::Exception& e) {
				printf("\n==\n%s\n==\n",e.all());
				printf("world\n");
			}
		}
		{
			try {
				hash_type ht(1000);
				hash_type ht1(ht);
				hash_type ht2 = ht;
			} catch (bsl::Exception& e) {
				printf("\n==\n%s\n==\n",e.all());
			}
		}
		{
			hash_type ht(100);
			std::set<key> st;
			ht.assign(st.begin(),st.end());
		}
		{
			std::set<key> st;
			for (size_t i = 0; i < N; i ++) {
				st.insert(s[i]);
			}
			hash_type ht(100);
			ht.assign(st.begin(),st.end());
			TS_ASSERT( ht.size() == N );

			hash_type ht2 = ht;
			TS_ASSERT( ht2.size() == N );
			hash_type ht3;
			ht3 = ht;
			TS_ASSERT( ht3.size() == N );

			for (size_t i = 0; i < N; i ++) {
				TS_ASSERT( ht.find( ht._hashfun( s[i] ), s[i] ) != NULL );
				TS_ASSERT( ht2.find( ht2._hashfun( s[i] ), s[i] ) != NULL );
				TS_ASSERT( ht3.find( ht3._hashfun( s[i] ), s[i] ) != NULL );
			}
		}
		
	}
	
	void test_create() {
		hash_type ht;
		for (int i = 10; i < 100; i ++) {
			ht.create(i);
		}
		hash_type ht2;
		for (int i = 0; i < 100; i ++) {
			ht2 = ht;
		}
	}	
};

#endif
/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */