📄 vectordiagnose.h
字号:
// file: $isip/class/dstr/Vector/VectorDiagnose.h// version: $Id: VectorDiagnose.h,v 1.15 2002/08/18 02:07:48 gao Exp $//// make sure definitions are made only once//#ifndef ISIP_VECTOR_DIAGNOSE#define ISIP_VECTOR_DIAGNOSE// isip include files//#ifndef ISIP_VECTOR#include <Vector.h>#endif#ifndef ISIP_FILENAME#include "Filename.h"#endif// VectorDiagnose: a class that contains the diagnose method of Vector class.//template<class TObject>class VectorDiagnose : public Vector<TObject> { //--------------------------------------------------------------------------- // // public constants // //---------------------------------------------------------------------------public: // define the class name // //---------------------------------------- // // i/o related constants // //---------------------------------------- //---------------------------------------- // // default values and arguments // //---------------------------------------- // default values // // default arguments to methods // //---------------------------------------- // // error codes // //---------------------------------------- //--------------------------------------------------------------------------- // // protected data // //---------------------------------------------------------------------------protected: //--------------------------------------------------------------------------- // // required public methods // //---------------------------------------------------------------------------public: // method: name // static const String& name() { return Vector<TObject>::name(); } // other static methods // static boolean diagnose(Integral::DEBUG debug_level); // debug methods // these methods are omitted since this class does not have data // members and operations // // destructor/constructor(s): // these methods are omitted since this class does not have data // members and operations // // assign methods: // these methods are omitted since this class does not have data // members and operations // // operator= methods: // these methods are omitted since this class does not have data // members and operations // // i/o methods: // these methods are omitted since this class does not have data // members and operations // // equality methods: // these methods are omitted since this class does not have data // members and operations // // memory-management methods: // these methods are omitted since this class does not have data // members and operations // //--------------------------------------------------------------------------- // // class-specific public methods // //--------------------------------------------------------------------------- // these methods are omitted since this class does not have data // members and operations // //--------------------------------------------------------------------------- // // private methods // //---------------------------------------------------------------------------private:}; // below are all the methods for the VectorDiagnose template class////-----------------------------------------------------------------------//// required static methods////----------------------------------------------------------------------// method: diagnose//// arguments:// Integral::DEBUG level: (input) debug level for diagnostics//// return: a boolean value indicating status//template<class TObject>boolean VectorDiagnose<TObject>::diagnose(Integral::DEBUG level_a) { //--------------------------------------------------------------------------- // // 0. preliminaries // //--------------------------------------------------------------------------- // output the class name // if (level_a > Integral::NONE) { SysString output(L"diagnosing class "); output.concat(CLASS_NAME); output.concat(L": "); Console::put(output); Console::increaseIndention(); } //-------------------------------------------------------------------------- // // 1. required public methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing required public methods...\n"); Console::increaseIndention(); } // prepare items for all the vectors used in this diagnose // long num_elem = 10; Char** items = new Char*[num_elem]; for (long i = 0; i < num_elem; i++) { items[i] = new Char(); } // test setDebug // setDebug(debug_level_d); // test constructors // // check the constructors for allocating on the stack // Vector<Char> def_vector((long)3); // default constructor def_vector(0).assign(*items[0]); def_vector(1).assign(*items[1]); def_vector(2).assign(*items[2]); Vector<Char> copy_vector(def_vector); // copy constructor Vector<Char> opeq_vector; opeq_vector = def_vector; // the two constructed vectors should have the same items now // if (def_vector.ne(copy_vector)) { return Error::handle(name(), L"copy constructor", Error::TEST, __FILE__, __LINE__); } // the two constructed vectors should have the same items now // if (def_vector.ne(opeq_vector)) { return Error::handle(name(), L"copy constructor", Error::TEST, __FILE__, __LINE__); } // check the constructors and destructors for allocating on the dynamic // memory heap // Vector<Char>* def_dyn_vector = new Vector<Char>((long)3); (*def_dyn_vector)(0).assign(*items[0]); (*def_dyn_vector)(1).assign(*items[1]); (*def_dyn_vector)(2).assign(*items[2]); Vector<Char>* copy_dyn_vector = new Vector<Char>(*def_dyn_vector); // the two constructed vectors should have the same items now // if (def_dyn_vector->ne(copy_vector)) { return Error::handle(name(), L"copy constructor", Error::TEST, __FILE__, __LINE__); } // see if we can dynamically delete // delete def_dyn_vector; delete copy_dyn_vector; // test memory management // // set the memory to a strange block size so we can hopefully catch any // frame overrun errors // Vector<Char>::setGrowSize((long)731); // loop for a large number of times creating and deleting a large number // of vectors at each loop // for (long j = 1; j <= 10; j++) { Vector<Char>** vectors = new Vector<Char>*[j * 100]; // create the items // for (long i = 0; i < j * 100; i++) { vectors[i] = new Vector<Char>(); } // delete vectors // for (long i = (j * 100) - 1; i >= 0; i--) { delete vectors[i]; } // clean up memory // delete [] vectors; } // clean up // for (long i = 0; i < num_elem; i++) { delete items[i]; } delete [] items; // test assign methods, including operator = // // create some temporary vectors // Vector<Char> tmp_vector(def_vector); Vector<Char>* tmp_dyn_vector = new Vector<Char>(); // try the vector assign method // tmp_dyn_vector->assign(tmp_vector); if (tmp_dyn_vector->ne(tmp_vector)) { return Error::handle(name(), L"vector assign", Error::TEST, __FILE__, __LINE__); } // test operator = // Vector<Char> tmp_vector_2; tmp_vector_2 = tmp_vector; if (tmp_vector_2.ne(tmp_vector)) { return Error::handle(name(), L"operator =", Error::TEST, __FILE__, __LINE__); } // clean up // delete tmp_dyn_vector; // testing equality methods // // initialize Vectors and Char objects to use for testing // Vector<Char> char_vector(1000); unichar tmp_char = L'a'; // initialize the characters and store them in the vectors // for (long i = 0; i < 500; i++) { // add the character to the vector // char_vector(i).assign(tmp_char); // increment the character value // tmp_char++; } // we also want a really big vector for i/o testing // long num_floats = 3050; // BUGBUG: change to 3050 Vector<Float> float_vec(num_floats); for (long i = 0; i < num_floats; i++) { float_vec(i) = i * 2.0; } // create another vector the same as this // Vector<Char> char_copy_vector(char_vector); if (!char_vector.eq(char_copy_vector)) { return Error::handle(name(), L"eq", Error::TEST, __FILE__, __LINE__); } // test i/o methods // // we need binary and text sof files // Filename text_filename; Filename bin_filename; Integral::makeTemp(text_filename); Integral::makeTemp(bin_filename); File::registerTemp(text_filename); File::registerTemp(bin_filename); // open files in write mode // Sof text_file; text_file.open(text_filename, File::WRITE_ONLY, File::TEXT); Sof bin_file; bin_file.open(bin_filename, File::WRITE_ONLY, File::BINARY); // prepare items for the vectors // String write_strings[5]; write_strings[0].assign(L"this "); write_strings[1].assign(L"is"); write_strings[2].assign(L"a"); write_strings[3].assign(L"Vector"); write_strings[4].assign(L"String"); DoubleLinkedList<Char> write_llist[5]; Char** chars = new Char*[5]; for (long i = 0; i < 5; i++) { write_llist[i].setAllocationMode(USER); chars[i] = new Char((unichar)((int)'a' + i)); for (long j = 0; j < i + 1; j++) { write_llist[i].insert(chars[j]); } } // create vectors to write // Vector<Char> write_null_vector; // vector of length 0 Vector<Char> write_char_vector(1); // vector of length 1 Vector<String> write_str_vector(5); Vector< DoubleLinkedList<Char> > write_llist_vector(5); Vector< Vector<Char> > write_char_vec_vector(3); write_char_vector(0).assign(*chars[0]); for ( long i = 0; i < 5; i++) { write_str_vector(i).assign(write_strings[i]); write_llist_vector(i).assign(write_llist[i]); } write_char_vec_vector.assign(write_char_vector); // write the values // write_null_vector.write(text_file, (long)10); write_null_vector.write(bin_file, (long)10); write_char_vector.write(text_file, (long)0); write_char_vector.write(bin_file, (long)0); write_str_vector.write(text_file, (long)0); write_str_vector.write(bin_file, (long)0); write_llist_vector.write(text_file, (long)0); write_llist_vector.write(bin_file, (long)0); write_char_vec_vector.write(text_file, (long)0); write_char_vec_vector.write(bin_file, (long)0); float_vec.write(text_file, (long)0); float_vec.write(bin_file, (long)0); // write a compound object with both an empty and non-empty Vector in it. // String object(L"Object"); String pname0(L"values0"); String pname1(L"values1"); String pname2(L"values2"); long obj_size = write_char_vector.sofSize() +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -