//---------------------------------------------------------------------- // MTNew <(size_t numObjects)> [-Array (size_t arraySize)] //---------------------------------------------------------------------- CmdExecStatus MTNewCmd::exec(const string& option) { // TODO vector<string> tokens; int num, arrs; if(option == ""){ return errorOption(CMD_OPT_MISSING, ""); } if(!CmdExec::lexOptions(option, tokens)) return CMD_EXEC_ERROR; if(tokens.size() == 1){ if(myStr2Int(tokens[0], num)){ if(num > 0){ mtest.newObjs(num); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else if(tokens.size() == 3){ if(myStr2Int(tokens[0], num)){ if(num > 0){ if(myStrNCmp("-Array", tokens[1], 2) == 0){ if(myStr2Int(tokens[2], arrs)){ if(arrs > 0){ mtest.newArrs(num, arrs); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else if(myStrNCmp("-Array", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], arrs)){ if(arrs > 0){ if(myStr2Int(tokens[2], num)){ if(num > 0){ mtest.newArrs(num, arrs); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else return CmdExec::errorOption(CMD_OPT_EXTRA, tokens[tokens.size() - 1]); return CMD_EXEC_DONE; }
//---------------------------------------------------------------------- // MTPrint //---------------------------------------------------------------------- CmdExecStatus MTPrintCmd::exec(const string& option) { // check option if (option.size()) return CmdExec::errorOption(CMD_OPT_EXTRA, option); mtest.print(); return CMD_EXEC_DONE; }
//---------------------------------------------------------------------- // MTNew <(size_t numObjects)> [-Array (size_t arraySize)] //---------------------------------------------------------------------- CmdExecStatus MTNewCmd::exec(const string& option) { // TODO // check option vector<string> options; if (!CmdExec::lexOptions(option, options)) return CMD_EXEC_ERROR; bool doArray = false; int nObjs = -1, aSize; for (size_t i = 0, n = options.size(); i < n; ++i) { if (myStrNCmp("-Array", options[i], 2) == 0) { if (doArray) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); if (++i == n) return CmdExec::errorOption(CMD_OPT_MISSING, options[i - 1]); if (!myStr2Int(options[i], aSize) || (aSize <= 0)) return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); doArray = true; } else { if (nObjs > 0) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); if (!myStr2Int(options[i], nObjs) || (nObjs <= 0)) return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); } } if (nObjs <= 0) return CmdExec::errorOption(CMD_OPT_MISSING, ""); try { if (doArray) mtest.newArrs(nObjs, aSize); else mtest.newObjs(nObjs); } catch (bad_alloc) { return CMD_EXEC_ERROR; } return CMD_EXEC_DONE; }
//---------------------------------------------------------------------- // MTReset [(size_t blockSize)] //---------------------------------------------------------------------- CmdExecStatus MTResetCmd::exec(const string& option) { // check option string token; if (!CmdExec::lexSingleOption(option, token)) return CMD_EXEC_ERROR; if (token.size()) { int b; if (!myStr2Int(token, b) || b < toSizeT(sizeof(MemTestObj))) { cerr << "Illegal block size (" << token << ")!!" << endl; return CmdExec::errorOption(CMD_OPT_ILLEGAL, token); } #ifdef MEM_MGR_H mtest.reset(toSizeT(b)); #else mtest.reset(); #endif // MEM_MGR_H } else mtest.reset(); return CMD_EXEC_DONE; }
//---------------------------------------------------------------------- // MTDelete <-Index (size_t objId) | -Random (size_t numRandId)> [-Array] //---------------------------------------------------------------------- CmdExecStatus MTDeleteCmd::exec(const string& option) { // TODO // check option vector<string> options; if (!CmdExec::lexOptions(option, options)) return CMD_EXEC_ERROR; bool doIdx = false, doRand = false, doArray = false; vector<int> objIds(1); int nRand; size_t objOpt = 0, randOpt = 0; for (size_t i = 0, n = options.size(); i < n; ++i) { if (myStrNCmp("-Array", options[i], 2) == 0) { if (doArray) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); doArray = true; } else if (myStrNCmp("-Index", options[i], 2) == 0) { if (doIdx || doRand) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); if (++i == n) return CmdExec::errorOption(CMD_OPT_MISSING, options[i - 1]); if (!myStr2Int(options[i], objIds[0]) || (objIds[0] < 0)) return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); objOpt = i; doIdx = true; } else if (myStrNCmp("-Random", options[i], 2) == 0) { if (doIdx || doRand) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); randOpt = i; if (++i == n) return CmdExec::errorOption(CMD_OPT_MISSING, options[i - 1]); if (!myStr2Int(options[i], nRand) || (nRand <= 0)) return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); doRand = true; } else return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); } int s = (doArray)? mtest.getArrListSize(): mtest.getObjListSize(); if (doIdx) { if (objIds[0] >= s) { cerr << "Size of " << (doArray?"array":"object") << " list (" << s << ") is <= " << objIds[0] << "!!\n"; return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[objOpt]); } } else if (doRand) { objIds.clear(); if (s > 0) // has something to delete for (int i = 0; i < nRand; ++i) objIds.push_back(rnGen(s)); else { cerr << "Size of " << (doArray?"array":"object") << " list is 0!!" << endl; return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[randOpt]); } } else return CmdExec::errorOption(CMD_OPT_MISSING, ""); if (doArray) for (size_t i = 0, n = objIds.size(); i < n; ++i) mtest.deleteArr(objIds[i]); else for (size_t i = 0, n = objIds.size(); i < n; ++i) mtest.deleteObj(objIds[i]); return CMD_EXEC_DONE; }
//---------------------------------------------------------------------- // MTDelete <-Index (size_t objId) | -Random (size_t numRandId)> [-Array] //---------------------------------------------------------------------- CmdExecStatus MTDeleteCmd::exec(const string& option) { // TODO vector<string> tokens; int objId, numRandId; if(option == ""){ return errorOption(CMD_OPT_MISSING, ""); } if(!CmdExec::lexOptions(option, tokens)){ return CMD_EXEC_ERROR; } if(tokens.size() == 2){ if(myStrNCmp("-Index", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], objId)){ if(objId >= 0){ if(!objErr(objId, mtest.getObjListSize())){ mtest.deleteObj(objId); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else if(myStrNCmp("-Random", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], numRandId)){ if(numRandId >= 1){ if(mtest.getObjListSize() != 0){ for(int i = 0; i < numRandId; i++){ mtest.deleteObj(rnGen(mtest.getObjListSize())); } } else{ cout << "Size of object list is 0 !!" << endl; return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else if(tokens.size() == 3){ if(myStrNCmp("-Array", tokens[0], 2) == 0){ if(myStrNCmp("-Index", tokens[1], 2) == 0){ if(myStr2Int(tokens[2], objId)){ if(objId >= 0){ if(!arrErr(objId, mtest.getArrListSize())){ mtest.deleteArr(objId); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else if(myStrNCmp("-Random", tokens[1], 2) == 0){ if(myStr2Int(tokens[2], numRandId)){ if(numRandId >= 1){ if(mtest.getArrListSize() != 0){ for(int i = 0; i < numRandId; i++){ mtest.deleteArr(rnGen(mtest.getArrListSize())); } } else{ cout << "Size of array list is 0 !!" << endl; return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else if(myStrNCmp("-Array", tokens[2], 2) == 0){ if(myStrNCmp("-Index", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], objId)){ if(objId >= 0){ if(!arrErr(objId, mtest.getArrListSize())){ mtest.deleteArr(objId); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else if(myStrNCmp("-Random", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], numRandId)){ if(numRandId >= 1){ if(mtest.getArrListSize() != 0){ for(int i = 0; i < numRandId; i++){ mtest.deleteArr(rnGen(mtest.getArrListSize())); } } else{ cout << "Size of array list is 0 !!" << endl; return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else return errorOption(CMD_OPT_MISSING, ""); return CMD_EXEC_DONE; }