📄 mvec_02.cc
字号:
// file: $isip/class/math/vector/MVector/mvec_02.cc// version: $Id: mvec_02.cc,v 1.33 2002/03/05 15:12:46 gao Exp $//// isip include files//#include "MVector.h"#include <Console.h>#include <SofParser.h>#include <typeinfo>// method: diagnose//// arguments:// Integral::DEBUG level: (input) debug level//// return: a boolean value indicating status//template<class TVector, class TScalar, class TIntegral>boolean MVectorMethods::diagnose(Integral::DEBUG level_a) { //--------------------------------------------------------------------------- // // 0. preliminaries // //--------------------------------------------------------------------------- // output the class name // if (level_a > Integral::NONE) { SysString output(L"diagnosing class "); output.concat(name()); output.concat(L": "); Console::put(output); Console::increaseIndention(); } //--------------------------------------------------------------------------- // // 1. required public methods and extensions to required public methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing required public methods...\n"); Console::increaseIndention(); } diagnose1<TVector, TScalar, TIntegral>(level_a); diagnose2<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 2. class-specific public methods: // equality and comparison methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: comparison methods...\n"); Console::increaseIndention(); } diagnose2<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 3. class-specific public methods: // get and set methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: get/set methods...\n"); Console::increaseIndention(); } diagnose3<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 4. class-specific public methods: // bitwise methods - these are only tested for non-complex // integer types. // //--------------------------------------------------------------------------- if ((typeid(TIntegral) != typeid(float)) && (typeid(TIntegral) != typeid(double)) && (typeid(TIntegral) != typeid(complexlong)) && (typeid(TIntegral) != typeid(complexfloat)) && (typeid(TIntegral) != typeid(complexdouble))) { // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: bitwise methods...\n"); Console::increaseIndention(); } // call the diagnose method // diagnose4<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } } //--------------------------------------------------------------------------- // // 5. class-specific public methods: // basic mathematical methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: basic mathematical methods...\n"); Console::increaseIndention(); } diagnose5<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 6. class-specific public methods: // other mathematical methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: other mathematical methods...\n"); Console::increaseIndention(); } diagnose6<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 7. class-specific public methods: // sign and dsp related methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: dsp methods...\n"); Console::increaseIndention(); } diagnose7<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 8. class-specific public methods: // position operation and search methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: position and search methods...\n"); Console::increaseIndention(); } diagnose8<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 9. class-specific public methods: // complex type methods which is best to test separately // //--------------------------------------------------------------------------#ifdef ISIP_TEMPLATE_complex // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: complex type methods...\n"); Console::increaseIndention(); } // test complex functions // diagnose9<TVector, TScalar, TIntegral>(level_a); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); }#endif //--------------------------------------------------------------------------- // // 10. print completion message // //--------------------------------------------------------------------------- // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } if (level_a > Integral::NONE) { SysString output(L"diagnostics passed for class "); output.concat(name()); output.concat(L"\n"); Console::put(output); } // exit gracefully // return true;}// method: diagnose1//// arguments:// Integral::DEBUG level: (input) debug level for diagnostics//// return: a boolean value indicating status//// this method tests required public methods//template<class TVector, class TScalar, class TIntegral>boolean MVectorMethods::diagnose1(Integral::DEBUG level_a) { // declare local variables // String tmp_filename0; String tmp_filename1; // create some temporary files for I/O tests // Integral::makeTemp(tmp_filename0); Integral::makeTemp(tmp_filename1); // test constructors // TVector val0; val0.assign(L"1, 3, 5, 7, 9"); long num_elem = 5; TVector val1(num_elem); val1.assign(L"0, 2, 4, 6, 8"); TVector val2; TVector val3; long test_size = 1024; TVector val4(test_size); TVector val5(test_size); // assign elements to a vector // for (long i = 0; i < test_size; i++) {#ifdef ISIP_TEMPLATE_complex val4(i) = (TIntegral)(complexlong(i, i*3));#endif val4(i) = (TIntegral)(i * 3); } // test scalar assignment // TVector val6(50); for (long i = 0; i < 50; i++) {#ifdef ISIP_TEMPLATE_complex val6(i) = (TIntegral)(complexlong(i, i + 1));#endif val6(i) = (TIntegral)i; } if (level_a > Integral::BRIEF) { val6.debug(L"val6"); } // test assign methods // val2.assign(val4); val4.swap(val5); // test eq methods // if (val5.ne(val2)) { return Error::handle(TVector::name(), L"swap", Error::TEST, __FILE__, __LINE__); } if (val4.ne((TIntegral)0)) { return Error::handle(TVector::name(), L"swap", Error::TEST, __FILE__, __LINE__); } //--------------------------------------------------------------------------- // // test get methods for real numbers // //---------------------------------------------------------------------------#ifndef ISIP_TEMPLATE_complex // real types // String val_str; val1.get(val_str); if (val_str.ne(L"0, 2, 4, 6, 8")) { val_str.debug(L"val_str"); return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } val1.get(val_str, L"xyz"); if (val_str.ne(L"0xyz2xyz4xyz6xyz8")) { val_str.debug(L"val_str"); return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } TVector val_zero; val_zero.get(val_str); if (val_str.ne(L"")) { val_str.debug(L"val_str"); return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } //--------------------------------------------------------------------------- // // test get methods for complex numbers // //---------------------------------------------------------------------------#else // complex types // String val_str; val1.get(val_str); if (val_str.ne(L"0+0j, 2+0j, 4+0j, 6+0j, 8+0j")) { val_str.debug(L"val_str"); return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } val1.get(val_str, L"xyz"); if (val_str.ne(L"0+0jxyz2+0jxyz4+0jxyz6+0jxyz8+0j")) { val_str.debug(L"val_str"); return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); } TVector val_zero; val_zero.get(val_str); if (val_str.ne(L"")) { val_str.debug(L"val_str"); return Error::handle(name(), L"get", Error::TEST, __FILE__, __LINE__); }#endif //--------------------------------------------------------------------------- // // continue diagnosing using the same code for all types // //--------------------------------------------------------------------------- // swap them back and reinitialize val2 // val5.swap(val4); if (val4.ne(val2)) { return Error::handle(TVector::name(), L"swap", Error::TEST, __FILE__, __LINE__); } val2.setLength((long)0); if (val5.length() == 0) { return Error::handle(TVector::name(), L"swap", Error::TEST, __FILE__, __LINE__); } // make sure deletion works // val5.setLength((long)0); if (val5.length() != 0) { return Error::handle(TVector::name(), L"setCapacity", Error::TEST, __FILE__, __LINE__); } // make sure deletion works // val5.setCapacity((long)1); if (val5.getCapacity() != 1) { return Error::handle(TVector::name(), L"getCapacity", Error::TEST, __FILE__, __LINE__); } // test io methods // if (level_a > Integral::BRIEF) { Console::put(L"testing io methods:\n"); } // create two special vectors of length 0 and 1 // TVector val_0; TVector val_1(1); TVector big_val(80000); big_val.rand(); val_1(0) = 42; // open files in write mode // Sof text_file; text_file.open(tmp_filename0, File::WRITE_ONLY, File::TEXT); Sof bin_file; bin_file.open(tmp_filename1, File::WRITE_ONLY, File::BINARY); // write the values // val_0.write(text_file, (long)10); // this is a 0 length vector val_0.write(bin_file, (long)10); val_1.write(text_file, (long)11); // this is a single length vector val_1.write(bin_file, (long)11); val0.write(text_file, (long)0); val0.write(bin_file, (long)0); val1.write(text_file, (long)1); val1.write(bin_file, (long)1); val4.write(text_file, (long)7); val4.write(bin_file, (long)7); big_val.write(text_file, (long)9); big_val.write(bin_file, (long)9); // close the files // text_file.close(); bin_file.close(); // print some vectors out // if (level_a > Integral::DETAILED) { val_1.setDebug(Integral::NONE); val_1.debug(L"val_1"); val0.debug(L"val0"); } // open the files in read mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -