Example #1
0
static bool testLockMap()
{
    CLockMap<int, std::string> map;
    typedef CLockMap<int, std::string>::guard_type guard_type;
    std::string s;
    for(int i = 0;i < 100;++i){
        s.push_back('a' + i % 26);
        if(!map.insertL(make_pair(i, s))){
            cerr<<"map.insertL(value_type={"<<i<<", '"<<s<<"'}) failed\n";
            return false;
        }
    }
    if(map.Size() != 100){
        cerr<<"1: map.Size()="<<map.Size()<<" is not 100\n";
        return false;
    }
    for(int i = 100;i < 200;++i){
        s.push_back('a' + i % 26);
        if(!map.insertL(i, s)){
            cerr<<"map.insertL("<<i<<", '"<<s<<"') failed\n";
            return false;
        }
    }
    if(map.Size() != 200){
        cerr<<"map.Size()="<<map.Size()<<" is not 200\n";
        return false;
    }
    for(int i = 200;i > 100;--i){
        int j = i - 1;
        {
            guard_type g(map.GetLock());
            if(s != map[j]){
                cerr<<"map["<<j<<"]='"<<map[j]<<"' is not '"<<s<<"'\n";
                return false;
            }
        }
        map.eraseL(j);
        if(!s.empty())
            s.resize(s.size() - 1);
    }
    if(map.Size() != 100){
        cerr<<"2: map.Size()="<<map.Size()<<" is not 100\n";
        return false;
    }
    for(int i = 100;i > 0;--i){
        int j = i - 1;
        std::string v;
        if(!map.pickL(j, v)){
            cerr<<"map.pickL("<<j<<") return false\n";
            return false;
        }
        if(s != v){
            cerr<<"map["<<j<<"]='"<<v<<"' is not '"<<s<<"'\n";
            return false;
        }
        if(!s.empty())
            s.resize(s.size() - 1);
    }
    if(map.Size() != 0){
        cerr<<"map.Size()="<<map.Size()<<" is not 0\n";
        return false;
    }
    return true;
}