⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 ICE-3.2 一个开源的中间件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// **********************************************************************//// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.//// This copy of Ice is licensed to you under the terms described in the// ICE_LICENSE file included in this distribution.//// **********************************************************************#include <IceUtil/IceUtil.h>#include <Freeze/Freeze.h>#include <TestCommon.h>#include <ByteIntMap.h>#include <IntIdentityMap.h>#include <IntIdentityMapWithIndex.h>#include <SortedMap.h>#include <WstringWstringMap.h>#include <Freeze/TransactionHolder.h>#include <algorithm>using namespace std;using namespace Ice;using namespace Freeze;using namespace Test;// #define SHOW_EXCEPTIONS 1#ifdef __SUNPRO_CCextern#elsestatic #endifByte alphabetChars[] = "abcdefghijklmnopqrstuvwxyz";vector<Byte> alphabet;// The extern in the following function is due to a Sun C++ 5.4 template bug//extern voidForEachTest(const pair<const Byte, const Int>&){}extern boolFindIfTest(const pair<const Byte, const Int>& p){    return p.first == 'b';}extern boolFindFirstOfTest(const pair<const Byte, const Int>& p, Byte q){    return p.first == q;}voidpopulateDB(const Freeze::ConnectionPtr& connection, ByteIntMap& m){    alphabet.assign(alphabetChars, alphabetChars + sizeof(alphabetChars) - 1);    size_t length = alphabet.size();        for(;;)    {        try        {            TransactionHolder txHolder(connection);            for(size_t j = 0; j < length; ++j)            {                m.put(ByteIntMap::value_type(alphabet[j], static_cast<Int>(j)));            }            txHolder.commit();            break;        }        catch(const DeadlockException&)        {#ifdef SHOW_EXCEPTIONS            cerr << "t" << flush;#endif            length = length / 2;            //            // Try again            //        }    }}class ReadThread : public IceUtil::Thread{public:    ReadThread(const CommunicatorPtr& communicator, const string& envName, const string& dbName) :        _connection(createConnection(communicator, envName)),        _map(_connection, dbName)    {    }    virtual void    run()    {        for(int i = 0; i < 10; ++i)        {            for(;;)            {                try                {                    for(ByteIntMap::iterator p = _map.begin(); p != _map.end(); ++p)                    {                        test(p->first == p->second + 'a');                        IceUtil::ThreadControl::yield();                    }                    break; // for(;;)                }                catch(const DeadlockException&)                {#ifdef SHOW_EXCEPTIONS                    cerr << "r" << flush;#endif                    //                    // Try again                    //                }                catch(const InvalidPositionException&)                {#ifdef SHOW_EXCEPTIONS                    cerr << "i" << flush;#endif                    break;                }            }        }    }private:    Freeze::ConnectionPtr _connection;    ByteIntMap  _map;};class WriteThread : public IceUtil::Thread{public:    WriteThread(const CommunicatorPtr& communicator, const string& envName, const string& dbName) :        _connection(createConnection(communicator, envName)),        _map(_connection, dbName)    {    }    virtual void    run()    {        //        // Delete an recreate each object        //        for(int i = 0; i < 4; ++i)        {            for(;;)            {                try                {                    TransactionHolder txHolder(_connection);                    for(ByteIntMap::iterator p = _map.begin(); p != _map.end(); ++p)                    {                        p.set(p->second + 1);                        _map.erase(p);                    }                    break; // for(;;)                    txHolder.commit();                }                catch(const DeadlockException&)                {#ifdef SHOW_EXCEPTIONS                    cerr << "w" << flush;#endif                    //                    // Try again                    //                }                catch(const InvalidPositionException&)                {#ifdef SHOW_EXCEPTIONS                    cerr << "I" << flush;#endif                    break;                }            }            populateDB(_connection, _map);        }    }private:    Freeze::ConnectionPtr _connection;    ByteIntMap  _map;};intrun(const CommunicatorPtr& communicator, const string& envName){    const string dbName = "binary";    Freeze::ConnectionPtr connection = createConnection(communicator, envName);    ByteIntMap m(connection, dbName);        //    // Populate the database with the alphabet    //    populateDB(connection, m);    vector<Byte>::const_iterator j;    ByteIntMap::iterator p;    ByteIntMap::const_iterator cp;    cout << "testing populate... " << flush;    //    // First try non-const iterator    //    for(j = alphabet.begin(); j != alphabet.end(); ++j)    {        p = m.find(*j);        test(p != m.end());        test(p->first == *j && p->second == j - alphabet.begin());    }    //    // Next try const iterator    //    for(j = alphabet.begin(); j != alphabet.end(); ++j)    {        cp = m.find(*j);        test(cp != m.end());        test(cp->first == *j && cp->second == j - alphabet.begin());    }       test(!m.empty());    test(m.size() == alphabet.size());    cout << "ok" << endl;    cout << "testing map::find... " << flush;    j = find(alphabet.begin(), alphabet.end(), 'n');        cp = m.find(*j);    test(cp != m.end());    test(cp->first == 'n' && cp->second == j - alphabet.begin());    cout << "ok" << endl;    cout << "testing erase... " << flush;    //    // erase first offset characters (first offset characters is    // important for later verification of the correct second value in    // the map).    //    int offset = 3;    vector<Byte> bytes;    bytes.push_back('a');    bytes.push_back('b');    bytes.push_back('c');    for(j = bytes.begin(); j != bytes.end(); ++j)    {        p = m.find(*j);        test(p != m.end());        m.erase(p);                //        // Release locks to avoid self deadlock        //        p = m.end();                p = m.find(*j);        test(p == m.end());        vector<Byte>::iterator r = find(alphabet.begin(), alphabet.end(), *j);        test(r != alphabet.end());        alphabet.erase(r);    }    for(j = alphabet.begin(); j != alphabet.end(); ++j)    {        cp = m.find(*j);        test(cp != m.end());        test(cp->first == *j && cp->second == (j - alphabet.begin()) + offset);    }    cout << "ok" << endl;    //    // Get an iterator for the deleted element - this should fail.    //    cout << "testing map::find (again)... " << flush;    cp = m.find('a');    test(cp == m.end());    cout << "ok" << endl;    cout << "testing iterators... " << flush;    p = m.begin();    ByteIntMap::iterator p2 = p;    //    // Verify both iterators point at the same element, and that    // element is in the map.    //    test(p == p2);    test(p->first == p2->first && p->second == p2->second);    test(find(alphabet.begin(), alphabet.end(), p->first) != alphabet.end());    //    // Create iterator that points at 'n'    //    p = m.find('n');    p2 = p;    //    // Verify both iterators point at 'n'    //    test(p == p2);    test(p->first == 'n' && p->second == 13);    test(p2->first == 'n' && p2->second == 13);    //    // Create cursor that points at 'n'    //    p = m.find('n');    test(p->first == 'n' && p->second == 13);    ++p;    p2 = p;    //    // Verify cloned cursors are independent    //    test(p->first != 'n' && p->second != 13);    pair<const Byte, const Int> data = *p;    ++p;    test(p->first != data.first && p->second != data.second);    ++p;    test(p2->first == data.first && p2->second == data.second);      p = m.find('n');    p2 = ++p;    test(p2->first == p->first);    char c = p2->first;    p2 = p++;    test(c == p2->first); // p2 should still be the same    test(p2->first != p->first && (++p2)->first == p->first);        cout << "ok" << endl;    //    // Test writing into an iterator.    //    cout << "testing iterator.set... " << flush;    p = m.find('d');    test(p != m.end() && p->second == 3);    test(m.find('a') == m.end());    ByteIntMap::value_type i1('a', 1);    m.put(i1);    //    // Note: VC++ won't accept this    //    //m.put(ByteIntMap::value_type('a', 1));    p = m.find('a');    test(p != m.end() && p->second == 1);    ByteIntMap::value_type i2('a', 0);    m.put(i2);    //    // Note: VC++ won't accept this    //    //m.put(ByteIntMap::value_type('a', 0));        p = m.find('a');    test(p != m.end() && p->second == 0);    //    // Test inserts    //         ByteIntMap::value_type i3('a', 7);    pair<ByteIntMap::iterator, bool> insertResult = m.insert(i3);    test(insertResult.first == m.find('a'));    test(insertResult.first->second == 0);    test(insertResult.second == false);    insertResult.first = m.end();        p = m.insert(m.end(), i3);    test(p == m.find('a'));    test(p->second == 0);    ByteIntMap::value_type i4('b', 7);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -