📄 mscl_02.cc
字号:
return Error::handle(name(), L"sinh", Error::TEST, __FILE__, __LINE__); } } // test sqrt // val0.sqrt(81); if (!val0.almostEqual(9)) { val0.debug(L"sqrt(81)"); return Error::handle(name(), L"sqrt", Error::TEST, __FILE__, __LINE__); } // test square // val0.square(9); val1.assign(9); TIntegral sq_temp = val1.square(); if (!val0.almostEqual(81) || (sq_temp != (TIntegral)81)) { val0.debug(L"square(9)"); return Error::handle(name(), L"square", Error::TEST, __FILE__, __LINE__); } // test tan // if ((typeid(TIntegral) == typeid(float)) || (typeid(TIntegral) == typeid(double))) { val0.assign(Integral::QUARTER_PI); val1.tan(val0); if (!val1.almostEqual((TIntegral)1.0)) { return Error::handle(name(), L"tan", Error::TEST, __FILE__, __LINE__); } } // test tanh // if ((typeid(TIntegral) == typeid(float)) || (typeid(TIntegral) == typeid(double))) { val0.assign((TIntegral)1); val1.tanh(val0); if (!val1.almostEqual((TIntegral)0.7615)) { return Error::handle(name(), L"tanh", Error::TEST, __FILE__, __LINE__); } } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // class-specific public methods: // random number generation methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: random number generation methods...\n"); Console::increaseIndention(); } // declare local variables // long N = 1000; double min, max, mean; double expected_min, expected_max, expected_mean; double comp_mean; TScalar rand; //-------------------------------------------------------------------- // // test rand // //-------------------------------------------------------------------- // determine the expected values based on the specific type // if (typeid(TIntegral) == typeid(byte)) { expected_min = 0; expected_max = TScalar::RAND_BYTE_MAX - 1.0; expected_mean = TScalar::RAND_BYTE_MAX / 2.0; comp_mean = 126.76; } else if (typeid(TIntegral) == typeid(ushort)) { expected_min = 0; expected_max = TScalar::RAND_USHORT_MAX - 1.0; expected_mean = TScalar::RAND_USHORT_MAX / 2.0; comp_mean = 32578.44; } else if (typeid(TIntegral) == typeid(ulong)) { expected_min = 0; expected_mean = TScalar::RAND_ULONG_MAX / 2.0; expected_max = TScalar::RAND_ULONG_MAX - 1.0; comp_mean = 2135094016.00; } else if (typeid(TIntegral) == typeid(ullong)) { expected_min = 0; expected_max = TScalar::RAND_ULLONG_MAX - 1.0; expected_mean = TScalar::RAND_ULLONG_MAX / 2.0; comp_mean = 9170158972605300736.00; } else if (typeid(TIntegral) == typeid(short)) { expected_min = -TScalar::RAND_SHORT_MAX; expected_max = TScalar::RAND_SHORT_MAX - 1.0; expected_mean = 0; comp_mean = -189.05; } else if (typeid(TIntegral) == typeid(long)) { expected_min = -TScalar::RAND_LONG_MAX; expected_max = TScalar::RAND_LONG_MAX - 1.0; expected_mean = 0; comp_mean = -12389639.00; } else if (typeid(TIntegral) == typeid(llong)) { expected_min = -TScalar::RAND_LLONG_MAX; expected_max = TScalar::RAND_LLONG_MAX - 1.0; expected_mean = 0; comp_mean = -53213094314246144.00; } else if ((typeid(TIntegral) == typeid(float)) || (typeid(TIntegral) == typeid(double))) { expected_min = 0; expected_max = 1.0; expected_mean = 0.5; comp_mean = 0.500823; } else { return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } // set the seed // Random::GLOBAL_UNIFORM.seed(27); // generate N random numbers: use rand() // mean = (TIntegral)0; min = expected_max; max = expected_min; for (long i = 0; i < N; i++) { // take a random sample // TIntegral tmp = rand.rand(); // update the mean // mean += tmp; // update the min/max // min = Integral::min(tmp, min); max = Integral::max(tmp, max); } mean /= N; // check if the computed values are in the proper range // if (!Integral::almostEqual(mean, comp_mean, 1.0)) { SysString out(L"mean = "); out.concat(mean); out.concat(L"; comp_mean = "); out.concat(comp_mean); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (min < expected_min) { SysString out(L"min = "); out.concat(min); out.concat(L"expected_min= "); out.concat(expected_min); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (max > expected_max) { SysString out(L"max = "); out.concat(max); out.concat(L"expected_max = "); out.concat(expected_max); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } //-------------------------------------------------------------------- // // test rand(expected_min, expected_max) // //-------------------------------------------------------------------- // determine the expected values based on the specific type // expected_min = 10.0; expected_max = 90.0; expected_mean = (expected_min + expected_max) / (double)2; if ((typeid(TIntegral) == typeid(byte)) || (typeid(TIntegral) == typeid(ushort)) || (typeid(TIntegral) == typeid(ulong)) || (typeid(TIntegral) == typeid(ullong)) || (typeid(TIntegral) == typeid(short)) || (typeid(TIntegral) == typeid(long)) || (typeid(TIntegral) == typeid(llong))) { comp_mean = 49.267; } else if ((typeid(TIntegral) == typeid(float)) || (typeid(TIntegral) == typeid(double))) { comp_mean = 49.769; } else { return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } // set the seed // Random::GLOBAL_UNIFORM.seed(27); // generate N random numbers: use rand() // mean = 0.0; min = expected_max; max = expected_min; for (long i = 0; i < N; i++) { // take a random sample // TIntegral tmp = rand.rand((TIntegral)expected_min, (TIntegral)expected_max); // update the mean // mean += tmp; // update the min/max // min = Integral::min(tmp, min); max = Integral::max(tmp, max); } mean /= (double)N; // check if the computed values are in the proper range // if (!Integral::almostEqual(mean, comp_mean, 1.0)) { SysString out(L"mean = "); out.concat(mean); out.concat(L"; comp_mean = "); out.concat(comp_mean); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (min < expected_min) { SysString out(L"min = "); out.concat(min); out.concat(L"expected_min = "); out.concat(expected_min); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (max > expected_max) { SysString out(L"max = "); out.concat(max); out.concat(L"expected_max = "); out.concat(expected_max); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } //-------------------------------------------------------------------- // // test grand(expected_mean, expected_stddev) // //-------------------------------------------------------------------- // determine the expected values based on the specific type: // choose a mean and stdev that works for all datatypes // expected_mean = 128.0; double expected_stddev = 10.0; double comp_stddev; double stddev = 0; // determine the expected values based on the specific type // if ((typeid(TIntegral) == typeid(float)) || (typeid(TIntegral) == typeid(double))) { comp_mean = 128; comp_stddev = 3.3166; } else { comp_mean = 127; comp_stddev = 11.7047; } // set the seed // Random::GLOBAL_GAUSSIAN.seed(27); mean = 0.0; // generate N random numbers: use grand() // for (long i = 0; i < N; i++) { // update the mean / stddev // TIntegral tmp = rand.grand((TIntegral)expected_mean, (TIntegral)expected_stddev); mean += tmp; // for real numbers simple do simple multiplication // stddev += tmp*tmp; } // normalize the mean and stddev: // note stddev = sqrt(E(x^2) - (Ex)^2) // mean = Integral::round((double)mean / double(N)); stddev = Integral::round((double)stddev / double(N)); stddev = Integral::sqrt(stddev - mean * mean); // check if the computed values are in the proper range // if (!Integral::almostEqual(mean, comp_mean)) { SysString out(L"mean = "); out.concat(mean); out.concat(L"; computed_mean = "); out.concat(comp_mean); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (!Integral::almostEqual(stddev, comp_stddev)) { SysString out(L"stddev = "); out.concat(stddev); out.concat(L"; computed_stddev = "); out.concat(comp_stddev); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // private methods // //--------------------------------------------------------------------------- // testing conversions // // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing private methods: conversions...\n"); Console::increaseIndention(); } // test conversion with Byte // MScalar<byte, byte8> scalar_byte; scalar_byte.assign((byte)43); val0.assign(scalar_byte); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_byte; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(byte)", Error::TEST, __FILE__, __LINE__); } // test conversion with Long // MScalar<long, int32> scalar_long; scalar_long.assign((long)43); val0.assign(scalar_long); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_long; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(long)", Error::TEST, __FILE__, __LINE__); } // test conversion with Llong // MScalar<llong, int64> scalar_llong; scalar_llong.assign((llong)43); val0.assign(scalar_llong); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_llong; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(llong)", Error::TEST, __FILE__, __LINE__); } // test conversion with Short // MScalar<short, int16> scalar_short; scalar_short.assign((short)43); val0.assign(scalar_short); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_short; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(short)", Error::TEST, __FILE__, __LINE__); } // test conversion with Ulong // MScalar<ulong, uint32> scalar_ulong; scalar_ulong.assign((ulong)43); val0.assign(scalar_ulong); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_ulong; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(ulong)", Error::TEST, __FILE__, __LINE__); } // test conversion with Ullong // MScalar<ullong, uint64> scalar_ullong; scalar_ullong.assign((ullong)43); val0.assign(scalar_ullong); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_ullong; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(ullong)", Error::TEST, __FILE__, __LINE__); } // test conversion with Ushort // MScalar<ushort, uint16> scalar_ushort; scalar_ushort.assign((ushort)43); val0.assign(scalar_ushort); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_ushort; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(ushort)", Error::TEST, __FILE__, __LINE__); } // test conversion with Double // MScalar<double, float64> scalar_double; scalar_double.assign((double)43); val0.assign(scalar_double); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_double; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(double)", Error::TEST, __FILE__, __LINE__); } // test conversion with Float // MScalar<float, float32> scalar_float; scalar_float.assign((float)43); val0.assign(scalar_float); val1.assign((TIntegral)43); val2.value_d = (TIntegral)scalar_float; if ((val0 != val1) || (val0 != val2)) { return Error::handle(name(), L"assign(float)", Error::TEST, __FILE__, __LINE__); } // test conversion with String // String str0; str0.assign((TIntegral)val0); TIntegral scalar_val; str0.get(scalar_val); val1.assign(str0); if (!val0.almostEqual(val1)) { return Error::handle(name(), L"<String::get>", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // exit gracefully // return true;} // declare classes that need to inherit MScalar//template booleanMScalarMethods::diagnose<MScalar<ISIP_TEMPLATE_TARGET>, ISIP_TEMPLATE_T0>(Integral::DEBUG level);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -