📄 circularbufferdiagnose.h
字号:
cbuf2.prepend(f); if ((cbuf2.getNumElements() != 3) || (cbuf2.getNumForward() != 1) || (cbuf2.getNumBackward() != 1) || (!cbuf2(1).almostEqual(0.2)) || (!cbuf2(0).almostEqual(0.1)) || (!cbuf2(-1).almostEqual(0.4))) { cbuf2.debug(L"cbuf2"); return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } f = 0.3; cbuf2.append(f); if ((cbuf2.getNumElements() != 4) || (cbuf2.getNumForward() != 2) || (cbuf2.getNumBackward() != 1) || (!cbuf2(2).almostEqual(0.3)) || (!cbuf2(1).almostEqual(0.2)) || (!cbuf2(0).almostEqual(0.1)) || (!cbuf2(-1).almostEqual(0.4))) { cbuf2.debug(L"cbuf2"); return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } f = 0.7; cbuf2.prepend(f); if ((cbuf2.getNumElements() != 5) || (cbuf2.getNumForward() != 2) || (cbuf2.getNumBackward() != 2) || (!cbuf2(2).almostEqual(0.3)) || (!cbuf2(1).almostEqual(0.2)) || (!cbuf2(0).almostEqual(0.1)) || (!cbuf2(-1).almostEqual(0.4)) || (!cbuf2(-2).almostEqual(0.7))) { cbuf2.debug(L"cbuf2"); return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // -------------------------------------------------------------------- // // 3. testing class-specific public methods // get methods // // -------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: get methods...\n"); Console::increaseIndention(); } if (cbuf1.getNumElements() != 1) { return Error::handle(name(), L"append", Error::TEST, __FILE__, __LINE__); } // prepare data // Vector<Float> floats(200); for (long i = 0; i < 200; i++) { floats(i) = (float)i; } // append single element at a time // [0 - 49] were appended // for (long i = 0; i < 50; i++) { cbuf.append(floats(i)); } if (cbuf.getNumElements() != 50) { return Error::handle(name(), L"append", Error::TEST, __FILE__, __LINE__); } // append a vector of numbers at a time // [50 - 99] were appended // cbuf.append(floats, 50, 50); // test some basic operation // CircularBuffer<Float> cbuf3(100); // prepend single element at a time // [50 - 99] were prepended // for (long i = 0; i < 50; i++) { cbuf3.prepend(floats(i)); } if (cbuf3.getNumElements() != 50) { return Error::handle(name(), L"prepend", Error::TEST, __FILE__, __LINE__); } // append a vector of numbers at a time // [0 - 49] were appended // cbuf3.prepend(floats, 49, 50); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // -------------------------------------------------------------------- // // 4. testing class-specific public methods // class property methods // // -------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: class property methods...\n"); Console::increaseIndention(); } // the buffer should be full now // if (!cbuf.isFull()) { return Error::handle(name(), L"isFull", Error::TEST, __FILE__, __LINE__); } if (cbuf.getNumElements() != 100) { return Error::handle(name(), L"append", Error::TEST, __FILE__, __LINE__); } for (long i = 0; i < 100; i++) { if (cbuf(i) != (float)i) { return Error::handle(name(), L"append", Error::TEST, __FILE__, __LINE__); } } // move the curr and read index // release [0 - 49] // cbuf.setRead((long)50); cbuf.resetCurr(); // the valid number of elements should be reduced now // if (cbuf.getNumElements() != 50) { return Error::handle(name(), L"setRead", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // -------------------------------------------------------------------- // // 5. testing class-specific public methods // data manipulation methods // // -------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: data manipulation methods...\n"); Console::increaseIndention(); } // add more data into the CircularBuffer // [100 - 149] overwrote the [0 - 49] // cbuf.append(floats, 50, 100); // get the number // for (long i = 0; i < 100; i++) { if (cbuf(i) != (float)(50 + i)) { return Error::handle(name(), L"append", Error::TEST, __FILE__, __LINE__); } } // prepare data // Vector<Float> vfloats(200); for (long i = 0; i < 200; i++) { vfloats(i) = (float)i; } CircularBuffer<Float> cbuf7; CircularBuffer<Float> cbuf8; // append single element at a time // buffer contains [0 - 199] // for (long i = 0; i < 200; i++) { cbuf7.append(vfloats(i)); } // append the vector // buffer contains [0 - 199] // cbuf8.append(vfloats); if (!cbuf7.eq(cbuf8)) { return Error::handle(name(), L"append", Error::TEST, __FILE__, __LINE__); } // append a portion of the vector // cbuf7.append(vfloats, 50, 50); cbuf8.append(vfloats, 50, 50); if (!cbuf7.eq(cbuf8)) { return Error::handle(name(), L"append", Error::TEST, __FILE__, __LINE__); } CircularBuffer<Float> cbuf5; CircularBuffer<Float> cbuf6; // prepend single element at a time // buffer contains [199 - 0] // for (long i = 0; i < 200; i++) { cbuf5.prepend(vfloats(i)); } // prepend the vector // buffer contains [199 - 0] // cbuf6.prepend(vfloats); if (!cbuf5.eq(cbuf6)) { return Error::handle(name(), L"prepend", Error::TEST, __FILE__, __LINE__); } // prepend a portion of the vector // cbuf5.prepend(vfloats, 50, 50); cbuf6.prepend(vfloats, 50, 50); if (!cbuf5.eq(cbuf6)) { return Error::handle(name(), L"prepend", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // -------------------------------------------------------------------- // // 6. testing class-specific public methods // operator overload methods // // -------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: positioning methods...\n"); Console::increaseIndention(); } // move the curr_d // cbuf.seekCurr((long)50); if (cbuf.getNumForward() != 49 ) { return Error::handle(name(), L"getNumForward", Error::TEST, __FILE__, __LINE__); } if (cbuf.getNumBackward() != 50 ) { return Error::handle(name(), L"getNumBackward", Error::TEST, __FILE__, __LINE__); } // get a vector of elements // Vector<Float> tmp_vec; // get [100 - 119] // cbuf.getElement(tmp_vec, 20, 0); for (long i = 0; i < 20; i++) { if (tmp_vec(i) != (float)(100 + i)) { if (level_a >= Integral::ALL) { tmp_vec.debug(L"tmp_vec"); } return Error::handle(name(), L"getElement", Error::TEST, __FILE__, __LINE__); } } // print out the debug information // only the last 10 numbers [140 - 149] are still valid // cbuf.setRead((long)90); cbuf.setDebug(Integral::NONE); if (level_a >= Integral::ALL) { cbuf.debug(L"cbuf"); } // test seekCurr and get the element with negative offset // cbuf.seekCurr(9); if (cbuf(0) != (float)149 || cbuf(-2) != (float)147) { return Error::handle(name(), L"seekCurr", Error::TEST, __FILE__, __LINE__); } cbuf.seekCurr(-5); if (cbuf(0) != (float)144 || cbuf(-4) != (float)140 || cbuf(4) != (float)148) { return Error::handle(name(), L"seekCurr", Error::TEST, __FILE__, __LINE__); } // test a smaller buffer // CircularBuffer<Long> buf; cbuf.setCapacity(20); for (long i = 0; i < 100; i++) { if (i > 20) { buf.setRead(1); } Long l = (long)i; buf.append(l); if (i > 1) { buf.seekCurr(1); } } // test clear // buf.clear(); if (buf.getNumElements() != 0) { return Error::handle(name(), L"clear", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------- // // 3. 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;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -