📄 stackdiagnose.h
字号:
// populate the first stack with all of the letters from 'a' - 'n' in // reverse order, marking the 'i' // for (long i = 0; i < 9; i++) { pos_stack_1.push(items[i]); } pos_stack_1.setMark(); for (long i = 9; i < 14; i++) { pos_stack_1.push(items[i]); } // make sure that the top element is the 'n' // if (pos_stack_1.peek() != items[13]) { return Error::handle(name(), L"push", Error::TEST, __FILE__, __LINE__); } // make sure that the mark is set // if (!pos_stack_1.markIsSet()) { return Error::handle(name(), L"setMark/markIsSet", Error::TEST, __FILE__, __LINE__); } // populate the second stack with the entire alphabet // pos_stack_2.push(items, num_elem); // populate the third stack with all alphabets as well, but copied from // the second stack // pos_stack_3.push(pos_stack_2); // make sure that the second and third stack are the same // if (!pos_stack_2.eq(pos_stack_3)) { return Error::handle(name(), L"push", Error::TEST, __FILE__, __LINE__); } // pop the first stack to the mark - it should now have 'i' - 'a' on it // pos_stack_1.popToMark(temp_clear_stack); // make sure the top item is the marked item // if (!(pos_stack_1.markIsSet() && pos_stack_1.isMarkedElement())) { return Error::handle(name(), L"isMarkedElement", Error::TEST, __FILE__, __LINE__); } // make sure the top item is the 'i' // if (pos_stack_1.peek() != items[8]) { return Error::handle(name(), L"popToMark", Error::TEST, __FILE__, __LINE__); } // make sure the clearMark method works // pos_stack_1.clearMark(); if (pos_stack_1.markIsSet() || pos_stack_1.isMarkedElement()) { return Error::handle(name(), L"clearMark", Error::TEST, __FILE__, __LINE__); } // mark the item again // pos_stack_1.setMark(); if (!(pos_stack_1.markIsSet() && pos_stack_1.isMarkedElement())) { return Error::handle(name(), L"setMark", Error::TEST, __FILE__, __LINE__); } // pop'ing the item off of the first stack should clear the mark // Char* tmp_char_ptr3; tmp_char_ptr3 = pos_stack_1.pop(); if (pos_stack_1.markIsSet() || pos_stack_1.isMarkedElement()) { return Error::handle(name(), L"isMarkedElement", Error::TEST, __FILE__, __LINE__); } // put the 'i' back onto the stack so that the first stack holds 'i' - 'a' // pos_stack_1.push(tmp_char_ptr3); // the second stack currently holds 'a' - 'z'. pop until we reach the 'p' // push the items pop'd onto the fourth stack // while ((pos_stack_2.peek())->ne(*items[18])) { Char* tmp_char_ptr2; tmp_char_ptr2 = pos_stack_2.pop(); pos_stack_4.push(tmp_char_ptr2); } // the second stack now holds 's' - 'z' and the fourth stack now holds // 'r' - 'a'. pop the fourth stack so that it holds 'i' - 'a' // pos_stack_4.pop(temp_clear_stack, 9); // now the first stack holds 'i' - 'a', the second stack holds 's' - 'z', // the fourth stack holds 'i' - 'a' and the third holds 'a' - 'z'. // // call popAll to put all items from the third stack onto the temp stack // pos_stack_3.popAll(temp_clear_stack); // make sure the third stack is empty // if (!pos_stack_3.isEmpty()) { return Error::handle(name(), L"popAll", Error::TEST, __FILE__, __LINE__); } // now pop elements off of the temp stack until we reach the 'p' // while ((temp_clear_stack.peek())->ne(*items[15])) { temp_clear_stack.pop(); } // pop the 'p' from the temp stack to the third stack // temp_clear_stack.pop(pos_stack_3, 1); // now the first stack should have an 'i' at the top, the second stack // should have a 's' at the top, the third stack should have a 'p' at the // top, and the fourth stack should have a 'i' at the top. put all of these // together to get "isip" // String isip_string_i1, isip_string_s, isip_string_i2, isip_string_p; isip_string_i1.assign(*(pos_stack_1.peek())); isip_string_s.assign(*(pos_stack_2.peek())); isip_string_i2.assign(*(pos_stack_4.peek())); isip_string_p.assign(*(pos_stack_3.peek())); String isip_string; isip_string.concat(isip_string_i1); isip_string.concat(isip_string_s); isip_string.concat(isip_string_i2); isip_string.concat(isip_string_p); // compare the generated string to the set string // String comp_string(L"isip"); if (comp_string.ne(isip_string)) { return Error::handle(name(), L"push/pop/mark methods failed", Error::TEST, __FILE__, __LINE__); } // test the clear, isEmpty and length methods // if (!temp_clear_stack.clear() || !temp_clear_stack.isEmpty() || (temp_clear_stack.length() != (long)0)) { return Error::handle(name(), L"clear, isEmpty, length", Error::TEST, __FILE__, __LINE__); } // check that the length on one of the occupied stacks is valid // if (pos_stack_2.isEmpty() || (pos_stack_2.length() != 8)) { return Error::handle(name(), L"length", Error::TEST, __FILE__, __LINE__); } } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 6. class-specific public methods: // computation methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: computation methods...\n"); Console::increaseIndention(); } // testing the sort/apply/reverse methods for reference mode // Char** low_chars = new Char*[num_elem]; for (long i = 0; i < num_elem; i++) { low_chars[i] = new Char(*items[i]); } Char** up_chars = new Char*[num_elem]; for (long i = 0; i < num_elem; i++) { up_chars[i] = new Char(*items[i]); } // initialize stacks and Char objects to use for testing // Stack<Char> char_stack_1; Stack<Char> char_stack_2; Stack<Char> char_stack_3; Stack<Char> char_stack_4; Stack<Char> temp_stack; // add all of the lower case characters to the first stack 'a' - 'z' // char_stack_1.push(low_chars, num_elem); // create an upper case version of these in the second stack 'A' - 'Z' // char_stack_2.push(up_chars, num_elem); char_stack_2.apply(&Char::toUpper); // add all of the lower case elements to the third stack and upcase them // all, then reverse them 'Z' - 'A' // char_stack_3.apply(&Char::toUpper, char_stack_1); char_stack_3.reverse(); // create the fourth stack with all lower case elements // char_stack_4.push(items, num_elem); // copy the fourth stack onto the third stack 'a' - 'z', 'Z' - 'A' // char_stack_3.push(char_stack_4); // move the lower case letters off of the third stack and onto the temp // stack 'a' - 'z', the third stack now has 'Z' - 'A' // char_stack_3.pop(temp_stack, 26); // reverse the letters in the second stack 'Z' - 'A' // char_stack_2.reverse(); // the third stack should be the same as the second stack now and the // temp stack should be the same as the fourth stack // if (char_stack_2.ne(char_stack_3) || temp_stack.ne(char_stack_4)) { return Error::handle(name(), L"sort/apply", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 7. class-specific public methods: // element ordering methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: element ordering methods...\n"); Console::increaseIndention(); } // sort the third stack in ascending order, 'A' - 'Z', 'a' - 'z' // char_stack_2.sort(); char_stack_3.sort(Integral::ASCENDING, Stack<Char>::INSERTION); char_stack_4.apply(&Char::toUpper); // all these three list should be 'A' - 'Z' // if (char_stack_2.ne(char_stack_3) || char_stack_2.ne(char_stack_4)) { return Error::handle(name(), L"sort", Error::TEST, __FILE__, __LINE__); } if (level_a >= Integral::ALL) { char_stack_2.debug(L"sorted list"); } // lower case the forth stack // char_stack_4.apply(&Char::toLower); // see if the fourth stack contains the letter 'm' // if (!char_stack_4.contains(items[12])) { return Error::handle(name(), L"contains", Error::TEST, __FILE__, __LINE__); } // make sure that 'a' is still the current item // if (char_stack_4.peek()->ne(*items[0])) { return Error::handle(name(), L"contains", Error::TEST, __FILE__, __LINE__); } // test the find method // if (!char_stack_4.find(items[12])) { return Error::handle(name(), L"find", Error::TEST, __FILE__, __LINE__); } // make sure that items[12] is marked // if (char_stack_4.getMark()->ne(*items[12])) { return Error::handle(name(), L"find", Error::TEST, __FILE__, __LINE__); } { // testing the sort/apply/reverse methods for reference mode // Stack<Char> char_stack_1(USER); Stack<Char> char_stack_2(USER); Stack<Char> char_stack_3(USER); Stack<Char> char_stack_4(USER); Stack<Char> temp_stack(USER); // add all of the lower case characters to the first stack 'a' - 'z' // char_stack_1.push(low_chars, num_elem); // create an upper case version of these in the second stack 'A' - 'Z' // char_stack_2.push(up_chars, num_elem); char_stack_2.apply(&Char::toUpper); // add all of the lower case elements to the third stack and upcase them // all, then reverse them 'Z' - 'A' // char_stack_3.apply(&Char::toUpper, char_stack_1); char_stack_3.reverse(); // create the fourth stack with all lower case elements // char_stack_4.push(items, num_elem); // copy the fourth stack onto the third stack 'a' - 'z', 'Z' - 'A' // char_stack_3.push(char_stack_4); // move the lower case letters off of the third stack and onto the temp // stack 'a' - 'z', the third stack now has 'Z' - 'A' // char_stack_3.pop(temp_stack, 26); // reverse the letters in the second stack 'Z' - 'A' // char_stack_2.reverse(); // the third stack should be the same as the second stack now and the // temp stack should be the same as the fourth stack // if (char_stack_2.ne(char_stack_3) || temp_stack.ne(char_stack_4)) { return Error::handle(name(), L"sort/apply", Error::TEST, __FILE__, __LINE__); } // sort the third stack in ascending order, 'A' - 'Z', 'a' - 'z' // char_stack_2.sort(); char_stack_3.sort(Integral::ASCENDING, Stack<Char>::INSERTION); char_stack_4.apply(&Char::toUpper); // all these three list should be 'A' - 'Z' // if (char_stack_2.ne(char_stack_3) || char_stack_2.ne(char_stack_4)) { return Error::handle(name(), L"sort", Error::TEST, __FILE__, __LINE__); } if (level_a >= Integral::ALL) { char_stack_2.debug(L"sorted list"); } // see if the fourth stack contains the letter 'm' // if (!char_stack_4.contains(items[12])) { return Error::handle(name(), L"contains", Error::TEST, __FILE__, __LINE__); } // make sure that 'a' is still the current item // if (char_stack_4.peek()->ne(*items[0])) { return Error::handle(name(), L"contains", Error::TEST, __FILE__, __LINE__); } } // clean up memory // for (long i = 0; i < num_elem; i++) { delete items[i]; delete up_chars[i]; delete low_chars[i]; } delete [] items; delete [] up_chars; delete [] low_chars; delete tmp_char_ptr; // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 8. print completion message // //--------------------------------------------------------------------------- // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // possibly print completion message // if (level_a > Integral::NONE) { String output(L"diagnostics completed successfully for class "); output.concat(name()); output.concat(L"\n"); Console::put(output); } // exit gracefully // return true;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -