Esempio n. 1
0
void test_erase_map() {
    IntMap map;
    for (int i = 0; i < 100; ++i) {
        map[i] = i + 10000;
    }

    std::cout << "map_size: " << map.size() << std::endl;

    for (IntMap::iterator it = map.begin(); it != map.end(); ++it) {
        std::cout << it->first << ": "  << it->second << std::endl;

        if (it->first % 2 == 0) {
            map.erase(it->first);
        }
    }

    std::cout << "clean finish" << std::endl;
    std::cout << map.size() << std::endl;

    for (IntMap::iterator it = map.begin(); it != map.end(); ++it) {
        std::cout << it->first << ": "  << it->second << std::endl;
    }

    std::cout << map.size() << std::endl;
}
TEST(ThreadLocalStorage, ThreadLocalPtrCount)
{
	// Create multiple objects in this thread
	// Only 1 ID should be added to the list
	TLSObject* o1 = new TLSObject();
	TLSObject* o2 = new TLSObject();
	TLSObject* o3 = new TLSObject();

	// Spawn a thread to create a new TLS int
	// This should add another ID to the list
	Thread thread1(JustAThread);
	thread1.join();

	// Spawn another thread
	// This should add another ID to the list
	Thread thread2(JustAThread);
	thread2.join();

	// Now, only 3 IDs should be added to the list

	typedef map<string, int> IntMap;
	IntMap map = ObjectManager::sIntMap;
	for(auto&& t : map)
	{
		std::cout << t.first << " : " << t.second << "\n";
	}

	ASSERT_EQ(3, map.size());
}
Esempio n. 3
0
void HashMapTest::testErase()
{
	const int N = 1000;

	typedef HashMap<int, int> IntMap;
	IntMap hm;

	for (int i = 0; i < N; ++i)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	assert (hm.size() == N);
	
	for (int i = 0; i < N; i += 2)
	{
		hm.erase(i);
		IntMap::Iterator it = hm.find(i);
		assert (it == hm.end());
	}
	assert (hm.size() == N/2);
	
	for (int i = 0; i < N; i += 2)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it == hm.end());
	}
	
	for (int i = 1; i < N; i += 2)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (*it == i);
	}

	for (int i = 0; i < N; i += 2)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	
	for (int i = 0; i < N; ++i)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (it->first == i);
		assert (it->second == i*2);		
	}
}
Esempio n. 4
0
void HashMapTest::testInsert()
{
	const int N = 1000;

	typedef HashMap<int, int> IntMap;
	IntMap hm;
	
	assert (hm.empty());
	
	for (int i = 0; i < N; ++i)
	{
		std::pair<IntMap::Iterator, bool> res = hm.insert(IntMap::ValueType(i, i*2));
		assert (res.first->first == i);
		assert (res.first->second == i*2);
		assert (res.second);
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (it->first == i);
		assert (it->second == i*2);
		assert (hm.count(i) == 1);
		assert (hm.size() == i + 1);
	}		
	
	assert (!hm.empty());
	
	for (int i = 0; i < N; ++i)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (it->first == i);
		assert (it->second == i*2);
	}
	
	for (int i = 0; i < N; ++i)
	{
		std::pair<IntMap::Iterator, bool> res = hm.insert(IntMap::ValueType(i, 0));
		assert (res.first->first == i);
		assert (res.first->second == i*2);
		assert (!res.second);
	}		
}
Esempio n. 5
0
void ListMapTest::testInsert()
{
    const int N = 1000;

    typedef ListMap<int, int> IntMap;
    IntMap hm;

    assert (hm.empty());

    for (int i = 0; i < N; ++i)
    {
        IntMap::Iterator res = hm.insert(IntMap::ValueType(i, i*2));
        assert (res->first == i);
        assert (res->second == i*2);
        IntMap::Iterator it = hm.find(i);
        assert (it != hm.end());
        assert (it->first == i);
        assert (it->second == i*2);
        assert (hm.size() == i + 1);
    }

    assert (!hm.empty());

    for (int i = 0; i < N; ++i)
    {
        IntMap::Iterator it = hm.find(i);
        assert (it != hm.end());
        assert (it->first == i);
        assert (it->second == i*2);
    }

    hm.clear();
    for (int i = 0; i < N; ++i)
    {
        IntMap::Iterator res = hm.insert(IntMap::ValueType(i, 0));
        assert (res->first == i);
        assert (res->second == 0);
    }
}
Esempio n. 6
0
void HashMapTest::testIndex()
{
	typedef HashMap<int, int> IntMap;
	IntMap hm;

	hm[1] = 2;
	hm[2] = 4;
	hm[3] = 6;
	
	assert (hm.size() == 3);
	assert (hm[1] == 2);
	assert (hm[2] == 4);
	assert (hm[3] == 6);
	
	try
	{
		const IntMap& im = hm;
		int x = im[4];
		fail("no such key - must throw");
	}
	catch (Poco::NotFoundException&)
	{
	}
}
static void
reduce_using_and(IntMap &map, std::vector<std::unique_ptr<Step>> &steps)
{
    IntPairs map_entries(begin(map), end(map));
    IntMap tmp;
    tmp.reserve(map_entries.size());
    std::bitset<32> mask;
    for (int bit = 31; bit >= 0; bit--) {
        auto candidate_mask = mask;
        candidate_mask.set(bit);
        auto bitmask = ~static_cast<uint32_t>(candidate_mask.to_ulong());
        auto hash = [=](uint32_t x) {
            return x & bitmask;
        };
        if (apply_hash_to_map(map_entries, hash, tmp) &&
                tmp.size() < map_entries.size()) {
            std::swap(map, tmp);
            mask = candidate_mask;
        }
    }
    if (mask.any()) {
        steps.push_back(std::unique_ptr<Step>(new AndNotStep(mask.to_ulong())));
    }
}